Jump to content
Sign in to follow this  
Dev

Adding a simple leaderboard system in Houdini

Recommended Posts

Hi people,

in this tutorial we are going to add a leaderboard system where we will display the total scores in a table which you earned by playing some games which give you score.

First of all, we need to make some service-side changes and then move onto the web-side changes.

This is a simple system, so first open your database and click the table 'penguins' and create a column named 'score', make sure it's INT(Integer) and also has a default value, '0'.

Next, we would want to update that 'score' column whenever they play a mini-game so what we will do is go to the following folder:

/Houdini/Houdini/Handlers/Games/

and open the __init__.py file.

Well, I had to use the MYSQL module because I couldn't really understand sqlalchemy  and MYSQLdb  module seemed easy, so yeah.

If you don't have the module, install it using the following command:

pip install MySQLdb

After that in the file, import this

import MySQLdb

after

import math, time

Now, we need to use the module so paste this code after the maxScorePerMinute = { array

db = MySQLdb.connect(host="localhost",   
                     user="root",       
                     passwd="pass",     
                     db="houdini",
					 autocommit=True)   

cur = db.cursor()

Find the

def handleSendGameOver(self, data): function

and inside

if self.server.stampGroups.isStampRoom(self.room.Id):

after coinsEarned = determineCoinsEarned(self.room.Id, data.Score)

add this:

ID1 = self.user.ID
cur.execute("UPDATE penguins SET score = score + %s WHERE ID = %s " % (data.Score, ID1))

Do the same for the next else statement:

http://prntscr.com/ibb3b4

Restart Houdini, now onto the web-server part.

<html>
<head>
<title>Leaderboard</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<center><h1>TOP 10 Score Leaderboard</h1></center>
<center>
<?php
$db = new mysqli('localhost','root','pass','houdini');

if ($db->connect_error) {
die('Error : ('. $db->connect_errno .') '. $db->connect_error);
}

$results = $db->query("SELECT username, score FROM penguins ORDER BY score DESC LIMIT 10");
echo "<table border='1'
       <tr>
	    <th>Username</th>
	   	<th>Score</th>
		</tr>";
		
while($row = $results->fetch_array()) {
    echo '<tr>';
    echo '<td>'.$row["username"].'</td>';
    echo '<td>'.$row["score"].'</td>';
    echo '</tr>';
}   
echo '</table>';
$db->close();
?> 
</center>
</body>
</html>

You can change the LIMIT 10 to whatever number you like. So, whenever someone plays a game their score will get updated and it will display on the leaderboard.

Thanks,

Dev.

  • Like 1

Share this post


Link to post
Share on other sites
Sign in to follow this  

×