sending rankings and highscores

This commit is contained in:
Jonas Weinz 2019-03-29 14:50:38 +01:00
parent c3669077b5
commit 44dde0cf6d
3 changed files with 27 additions and 2 deletions

View File

@ -270,7 +270,10 @@ response:
{ {
"type": "elo_update", "type": "elo_update",
"data": { "data": {
"elo": <elo_value> "elo": <elo_value>,
"rank": <rank>,
"top_names": <list of top 100 names>,
"top_elos": <list of top 100 elos>
} }
} }
``` ```

View File

@ -620,10 +620,16 @@ class ConnectionHandler(object):
})) }))
async def send_elo(self, conn): async def send_elo(self, conn):
top_names, top_elos = self.user_manager.get_highscores(100,0)
await conn.send(json.dumps({ await conn.send(json.dumps({
'type': 'elo_update', 'type': 'elo_update',
'data': { 'data': {
'elo': self.user_manager.get_elo(conn.user_name) 'elo': self.user_manager.get_elo(conn.user_name),
'rank': self.user_manager.get_rank_for_user(conn.user_name),
'top_names': top_names,
'top_elos': top_elos
} }
})) }))

View File

@ -73,6 +73,22 @@ class UserManager(object):
return None return None
def get_highscores(self, n, list_offset):
query = "SELECT name, elo FROM users ORDER BY elo DESC LIMIT %s OFFSET %s"
q = DatabaseConnection.global_single_query(query, (n, list_offset))
names = []
elos = []
for entry in q:
names.append(entry['name'])
elos.append(int(entry['elo']))
return names, elos
def get_rank_for_user(self, user_name):
query = "SELECT Count(*)+1 as rank from users where elo >(SELECT elo from users WHERE name=%s)"
q = DatabaseConnection.global_single_query(query, (user_name))
return int(q[0]['rank'])
def add_friend_to_user(self, user_name, friend_name): def add_friend_to_user(self, user_name, friend_name):
if len(self.get_user(friend_name)) > 0: if len(self.get_user(friend_name)) > 0:
query = "INSERT INTO friends (user, friend) VALUES ( %s, %s)" query = "INSERT INTO friends (user, friend) VALUES ( %s, %s)"