-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreport_generator.py
319 lines (286 loc) · 10.6 KB
/
report_generator.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import sqlite3
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from pathlib import Path
import os
import datetime
class TournamentReportGenerator:
def __init__(self, db_path="results/results.sqlite"):
self.db_path = db_path
self.report_dir = Path("report")
self.report_dir.mkdir(exist_ok=True)
self.plots_dir = self.report_dir / "plots"
self.plots_dir.mkdir(exist_ok=True)
def _execute_query(self, query, conn):
return pd.read_sql_query(query, conn)
def generate_win_rates_chart(self, conn):
query = """
SELECT
m.player_a,
COUNT(CASE WHEN g.winner = 'player_a' THEN 1 END) as wins,
COUNT(*) as total_games,
ROUND(CAST(COUNT(CASE WHEN g.winner = 'player_a' THEN 1 END) AS FLOAT) / COUNT(*) * 100, 2) as win_percentage
FROM matchups m
JOIN games g ON m.id = g.matchup_id
GROUP BY m.player_a
ORDER BY win_percentage DESC
"""
df = self._execute_query(query, conn)
plt.figure(figsize=(12, 6))
sns.barplot(data=df, x='player_a', y='win_percentage')
plt.xticks(rotation=45)
plt.title('Win Rates by Player')
plt.tight_layout()
plt.savefig(self.plots_dir / 'win_rates.png')
plt.close()
return df
def generate_game_length_chart(self, conn):
query = """
SELECT
m.player_a,
m.player_b,
ROUND(AVG(max_turn + 1), 2) as avg_game_length
FROM matchups m
JOIN games g ON m.id = g.matchup_id
JOIN (
SELECT game_id, MAX(turn_number) as max_turn
FROM turns
GROUP BY game_id
) t ON g.id = t.game_id
GROUP BY m.player_a, m.player_b
ORDER BY avg_game_length DESC
"""
df = self._execute_query(query, conn)
plt.figure(figsize=(12, 6))
sns.barplot(data=df, x='avg_game_length', y=df.apply(lambda x: f"{x['player_a']} vs {x['player_b']}", axis=1))
plt.title('Average Game Length by Matchup')
plt.tight_layout()
plt.savefig(self.plots_dir / 'game_lengths.png')
plt.close()
return df
def generate_elo_ratings(self, conn):
query = """
WITH all_players AS (
SELECT player_name, 1500 as base_elo
FROM (
SELECT player_a as player_name FROM matchups
UNION
SELECT player_b FROM matchups
)
)
SELECT
p.player_name,
p.base_elo + (
COALESCE(SUM(
CASE
WHEN g.winner = 'player_a' AND m.player_a = p.player_name THEN 32
WHEN g.winner = 'player_b' AND m.player_b = p.player_name THEN 32
WHEN g.winner != 'tie' AND
(m.player_a = p.player_name OR m.player_b = p.player_name) THEN -32
ELSE 0
END
), 0)
) as final_elo
FROM all_players p
LEFT JOIN matchups m ON p.player_name IN (m.player_a, m.player_b)
LEFT JOIN games g ON m.id = g.matchup_id
GROUP BY p.player_name
ORDER BY final_elo DESC
"""
df = self._execute_query(query, conn)
plt.figure(figsize=(10, 6))
sns.barplot(data=df, x='player_name', y='final_elo')
plt.xticks(rotation=45)
plt.title('Player ELO Ratings')
plt.tight_layout()
plt.savefig(self.plots_dir / 'elo_ratings.png')
plt.close()
return df
def generate_head_to_head_stats(self, conn):
query = """
SELECT
m.player_a,
m.player_b,
COUNT(CASE WHEN g.winner = 'player_a' THEN 1 END) as player_a_wins,
COUNT(CASE WHEN g.winner = 'player_b' THEN 1 END) as player_b_wins,
COUNT(CASE WHEN g.winner = 'tie' THEN 1 END) as ties,
COUNT(*) as total_games
FROM matchups m
JOIN games g ON m.id = g.matchup_id
GROUP BY m.player_a, m.player_b
ORDER BY total_games DESC
"""
return self._execute_query(query, conn)
def generate_winning_combinations(self, conn):
query = """
SELECT
CASE
WHEN g.winner = 'player_a' THEN m.player_a
WHEN g.winner = 'player_b' THEN m.player_b
END as winner,
CASE
WHEN g.winner = 'player_a' THEN m.player_b
WHEN g.winner = 'player_b' THEN m.player_a
END as loser,
COUNT(*) as wins
FROM matchups m
JOIN games g ON m.id = g.matchup_id
WHERE g.winner != 'tie'
GROUP BY winner, loser
ORDER BY wins DESC
"""
df = self._execute_query(query, conn)
plt.figure(figsize=(12, 6))
sns.barplot(data=df.head(10), x='wins', y=df.apply(lambda x: f"{x['winner']} vs {x['loser']}", axis=1))
plt.title('Most Common Winning Combinations')
plt.tight_layout()
plt.savefig(self.plots_dir / 'winning_combinations.png')
plt.close()
return df
def generate_tie_statistics(self, conn):
query = """
SELECT
m.player_a,
m.player_b,
COUNT(*) as tie_count,
ROUND(CAST(COUNT(*) AS FLOAT) / COUNT(*) OVER (PARTITION BY m.id) * 100, 2) as tie_percentage
FROM matchups m
JOIN games g ON m.id = g.matchup_id
WHERE g.winner = 'tie'
GROUP BY m.player_a, m.player_b
ORDER BY tie_count DESC
"""
return self._execute_query(query, conn)
def generate_game_length_extremes(self, conn):
query = """
SELECT
m.player_a,
m.player_b,
g.id as game_id,
MAX(t.turn_number) + 1 as game_length,
g.winner
FROM matchups m
JOIN games g ON m.id = g.matchup_id
JOIN turns t ON g.id = t.game_id
GROUP BY g.id, m.player_a, m.player_b, g.winner
ORDER BY game_length DESC
LIMIT 10
"""
return self._execute_query(query, conn)
def generate_win_streaks(self, conn):
query = """
WITH game_results AS (
SELECT
m.player_a,
m.player_b,
g.id,
g.winner,
ROW_NUMBER() OVER (ORDER BY g.id) as game_number
FROM matchups m
JOIN games g ON m.id = g.matchup_id
),
win_streaks AS (
SELECT
CASE
WHEN winner = 'player_a' THEN player_a
WHEN winner = 'player_b' THEN player_b
END as player,
game_number,
game_number - ROW_NUMBER() OVER (
PARTITION BY
CASE
WHEN winner = 'player_a' THEN player_a
WHEN winner = 'player_b' THEN player_b
END
ORDER BY game_number
) as streak_group
FROM game_results
WHERE winner != 'tie'
)
SELECT
player,
COUNT(*) as streak_length
FROM win_streaks
GROUP BY player, streak_group
HAVING COUNT(*) > 1
ORDER BY streak_length DESC
LIMIT 10
"""
df = self._execute_query(query, conn)
plt.figure(figsize=(10, 6))
sns.barplot(data=df, x='player', y='streak_length')
plt.xticks(rotation=45)
plt.title('Longest Win Streaks by Player')
plt.tight_layout()
plt.savefig(self.plots_dir / 'win_streaks.png')
plt.close()
return df
def generate_html_report(self):
with sqlite3.connect(self.db_path) as conn:
# Get all statistics
win_rates_df = self.generate_win_rates_chart(conn)
game_length_df = self.generate_game_length_chart(conn)
elo_df = self.generate_elo_ratings(conn)
h2h_df = self.generate_head_to_head_stats(conn)
winning_combinations_df = self.generate_winning_combinations(conn)
tie_stats_df = self.generate_tie_statistics(conn)
game_extremes_df = self.generate_game_length_extremes(conn)
win_streaks_df = self.generate_win_streaks(conn)
html_content = f"""
<html>
<head>
<title>Tournament Report - {datetime.datetime.now().strftime('%Y-%m-%d')}</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@1.0.2/css/bulma.min.css">
</head>
<body>
<h1>Tournament Report</h1>
<div class="section">
<h2>Win Rates</h2>
<img src="plots/win_rates.png">
{win_rates_df.to_html()}
</div>
<div class="section">
<h2>Head-to-Head Statistics</h2>
{h2h_df.to_html()}
</div>
<div class="section">
<h2>Most Common Winning Combinations</h2>
<img src="plots/winning_combinations.png">
{winning_combinations_df.to_html()}
</div>
<div class="section">
<h2>Game Lengths</h2>
<img src="plots/game_lengths.png">
{game_length_df.to_html()}
</div>
<div class="section">
<h2>Longest Games</h2>
{game_extremes_df.to_html()}
</div>
<div class="section">
<h2>Tie Statistics</h2>
{tie_stats_df.to_html()}
</div>
<div class="section">
<h2>Win Streaks</h2>
<img src="plots/win_streaks.png">
{win_streaks_df.to_html()}
</div>
<div class="section">
<h2>ELO Ratings</h2>
<img src="plots/elo_ratings.png">
{elo_df.to_html()}
</div>
</body>
</html>
"""
with open(self.report_dir / "report.html", "w") as f:
f.write(html_content)
def main():
generator = TournamentReportGenerator()
generator.generate_html_report()
if __name__ == "__main__":
main()