-
Notifications
You must be signed in to change notification settings - Fork 2
/
leaderboard.py
36 lines (30 loc) · 1.39 KB
/
leaderboard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from pymongo import MongoClient
from datetime import datetime
client = MongoClient(
f"")
db = client.CTCrypt
leaderboard = db["leaderboard"]
users = db['users']
def leaderboard_sort():
results = leaderboard.find()
leader_list = []
not_started = []
for result in results:
if result['level'] == '-':
not_started.append((result['points'], result["level"],
datetime.strptime(result["last_solved"][:len(result["last_solved"]) - 10],
'%d/%m/%Y %H:%M:%S'), result["username"], result['_id']))
else:
leader_list.append((result['points'], result["level"],
datetime.strptime(result["last_solved"][:len(result["last_solved"]) - 10],
'%d/%m/%Y %H:%M:%S'), result["username"], result['_id']))
leader_list.sort()
leader_list.sort(key=lambda x: x[0], reverse=True) # reverse=True
# leader_list = reversed(leader_list)
leader_list = tuple(leader_list)
leader_main = []
for element in leader_list:
leader_main.append({'points': element[0], 'username': element[3], '_id': element[4], 'level': element[1]})
for element in not_started:
leader_main.append({'points': element[0], 'username': element[3], '_id': element[4], 'level': element[1]})
return leader_main