sending rankings and highscores
This commit is contained in:
parent
c3669077b5
commit
44dde0cf6d
@ -270,7 +270,10 @@ response:
|
||||
{
|
||||
"type": "elo_update",
|
||||
"data": {
|
||||
"elo": <elo_value>
|
||||
"elo": <elo_value>,
|
||||
"rank": <rank>,
|
||||
"top_names": <list of top 100 names>,
|
||||
"top_elos": <list of top 100 elos>
|
||||
}
|
||||
}
|
||||
```
|
||||
|
@ -620,10 +620,16 @@ class ConnectionHandler(object):
|
||||
}))
|
||||
|
||||
async def send_elo(self, conn):
|
||||
|
||||
top_names, top_elos = self.user_manager.get_highscores(100,0)
|
||||
|
||||
await conn.send(json.dumps({
|
||||
'type': 'elo_update',
|
||||
'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
|
||||
}
|
||||
}))
|
||||
|
||||
|
@ -73,6 +73,22 @@ class UserManager(object):
|
||||
|
||||
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):
|
||||
if len(self.get_user(friend_name)) > 0:
|
||||
query = "INSERT INTO friends (user, friend) VALUES ( %s, %s)"
|
||||
|
Loading…
Reference in New Issue
Block a user