-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaderboard.py
187 lines (157 loc) · 6.22 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env python3
import argparse
import json
import os
import sqlite3
import sys
import tempfile
import time
parser = argparse.ArgumentParser(description="Generate a leaderboard for Werewolf.")
required = parser.add_argument_group("required arguments")
required.add_argument("--channel", required=True,
help="the channel where the game is played")
required.add_argument("--db-path", required=True,
help="path to the bot's SQL database")
required.add_argument("--max-players", type=int, required=True,
help="the maximum number of players to display on the leaderboard")
required.add_argument("--min-games", type=int, required=True,
help="the minimum number of games required to appear on the leaderboard")
required.add_argument("--activity-cutoff", type=int, required=True,
help="the number of days in which a user must have recently played to appear on the leaderboard")
#parser.add_argument("--json", action="store_true",
# help="output as JSON instead of a human-readable form")
args = parser.parse_args()
generated = time.gmtime()
data = {
"info": {
"channel": args.channel,
"generated": time.strftime("%FT%TZ", generated),
"max_players": args.max_players,
"min_games": args.min_games,
"cutoff": args.activity_cutoff
},
"leaderboards": {
"01_win_ratio": {
"display_header": "win ratio",
},
"02_games": {
"display_header": "games played",
},
"03_wins": {
"display_header": "wins",
},
},
}
def generate_board(cursor, suffix="", floats=False):
rows = cursor.fetchall()
def find_min_precision(precision=0):
nums = {}
for (_, num) in rows:
rnd = round(num, precision)
if rnd in nums and nums[rnd] != num:
return find_min_precision(precision + 1)
else:
nums[rnd] = num
return precision
if floats:
precision = find_min_precision()
else:
precision = 0
tie_length = 0
last_num = None
last_cnt = None
last_tie = None
board = {
"display_precision": precision,
"display_suffix": suffix,
"entries": [],
}
for (num, row) in enumerate(rows):
num = num + max(tie_length - 1, 1)
(pl, cnt) = row
if cnt == last_cnt:
num = last_num
tie_length += 1
else:
tie_length = 0
last_tie = pl
last_num = num
last_cnt = cnt
board["entries"].append({
"position": num,
"player": pl,
"value": cnt,
"tie": (tie_length > 0)
})
if tie_length:
for entry in board["entries"]:
if entry["player"] == last_tie:
entry["tie"] = True
return board
tmp = tempfile.NamedTemporaryFile()
with open(args.db_path, "rb") as fd:
tmp.write(fd.read())
with sqlite3.connect(tmp.name) as conn:
c = conn.cursor()
data["info"]["total_games"] = c.execute("""
SELECT COUNT(*) FROM game
WHERE gamemode NOT IN ('random', 'maelstrom', 'drunkfire', 'rapidfire', 'valentines', 'roles', 'roles_mud')
""").fetchone()[0]
min_games = data["info"]["min_games"]
max_players = data["info"]["max_players"]
cutoff = "-{0} days".format(data["info"]["cutoff"])
c.execute("""SELECT
pp.account_display,
(CAST(SUM(team_win OR indiv_win) AS FLOAT) / COUNT(*)) * 100 as winratio
FROM person pe
JOIN player pl
ON pl.person = pe.id
JOIN player pp
ON pp.id = pe.primary_player
JOIN game_player gp
ON gp.player = pl.id
JOIN game g
ON g.id = gp.game
WHERE g.gamemode NOT IN ('random', 'maelstrom', 'drunkfire', 'rapidfire', 'valentines', 'roles', 'roles_mud')
GROUP BY pe.id
HAVING COUNT(*) >= :min_games AND MAX(g.started) >= date('now', :cutoff)
ORDER BY winratio DESC
LIMIT :max_players""", {"min_games": min_games, "max_players": max_players, "cutoff": cutoff})
data["leaderboards"]["01_win_ratio"].update(generate_board(c, suffix="%", floats=True))
c.execute("""SELECT
pp.account_display,
COUNT(*) as cnt
FROM person pe
JOIN player pl
ON pl.person = pe.id
JOIN player pp
ON pp.id = pe.primary_player
JOIN game_player gp
ON gp.player = pl.id
JOIN game g
ON g.id = gp.game
WHERE g.gamemode NOT IN ('random', 'maelstrom', 'drunkfire', 'rapidfire', 'valentines', 'roles', 'roles_mud')
GROUP BY pe.id
HAVING cnt >= :min_games AND MAX(g.started) >= date('now', :cutoff)
ORDER BY cnt DESC
LIMIT :max_players""", {"min_games": min_games, "max_players": max_players, "cutoff": cutoff})
data["leaderboards"]["02_games"].update(generate_board(c, suffix=" games"))
c.execute("""SELECT
pp.account_display,
SUM(gp.team_win OR gp.indiv_win) AS wins
FROM person pe
JOIN player pl
ON pl.person = pe.id
JOIN player pp
ON pp.id = pe.primary_player
JOIN game_player gp
ON gp.player = pl.id
JOIN game g
ON g.id = gp.game
WHERE g.gamemode NOT IN ('random', 'maelstrom', 'drunkfire', 'rapidfire', 'valentines', 'roles', 'roles_mud')
GROUP BY pe.id
HAVING COUNT(*) > :min_games AND MAX(g.started) >= date('now', :cutoff)
ORDER BY wins DESC
LIMIT :max_players""", {"min_games": min_games, "max_players": max_players, "cutoff": cutoff})
data["leaderboards"]["03_wins"].update(generate_board(c, suffix=" wins"))
print(json.dumps(data, indent=2, sort_keys=True))