-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtournament_repository.py
More file actions
428 lines (385 loc) · 15 KB
/
tournament_repository.py
File metadata and controls
428 lines (385 loc) · 15 KB
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
import json
import sqlite3
from typing import Any
from tournament_core import ITournamentRepository, Match, MatchResult, Player
class SQLiteTournamentRepository(ITournamentRepository):
"""SQLite implementation of tournament repository."""
def __init__(self, db_path: str = "tournament.db"):
self.db_path = db_path
self._init_database()
def _get_connection(self):
"""Get database connection with row factory."""
conn = sqlite3.connect(self.db_path)
conn.row_factory = sqlite3.Row
return conn
def _init_database(self):
"""Initialize database schema."""
with self._get_connection() as conn:
cur = conn.cursor()
cur.execute("""
CREATE TABLE IF NOT EXISTS players (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
created_at TEXT,
date_of_birth TEXT,
category TEXT
)
""")
try:
cur.execute("ALTER TABLE players ADD COLUMN date_of_birth TEXT")
except sqlite3.OperationalError as e:
if "duplicate column" not in str(e).lower():
raise
try:
cur.execute("ALTER TABLE players ADD COLUMN category TEXT")
except sqlite3.OperationalError as e:
if "duplicate column" not in str(e).lower():
raise
cur.execute("""
CREATE TABLE IF NOT EXISTS tournaments (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
created_at TEXT,
default_calculator TEXT DEFAULT 'standard'
)
""")
cur.execute("""
CREATE TABLE IF NOT EXISTS tournament_players (
tournament_id TEXT NOT NULL,
player_id TEXT NOT NULL,
added_at TEXT,
able_to_play INTEGER DEFAULT 1,
PRIMARY KEY (tournament_id, player_id)
)
""")
cur.execute("""
CREATE TABLE IF NOT EXISTS rounds (
id TEXT PRIMARY KEY,
tournament_id TEXT,
round_type TEXT,
ordinal INTEGER,
created_at TEXT
)
""")
cur.execute("""
CREATE TABLE IF NOT EXISTS matches (
id TEXT PRIMARY KEY,
round_id TEXT,
tournament_id TEXT,
player_ids TEXT, -- JSON array of player IDs
scheduled_at TEXT,
result TEXT,
winner_ids TEXT, -- JSON array of winner IDs
rankings TEXT, -- JSON object of player_id -> rank
auto_bye INTEGER DEFAULT 0,
players_per_match INTEGER DEFAULT 2,
board_no INTEGER DEFAULT NULL,
colors TEXT DEFAULT NULL -- JSON array e.g. ["white","black"]
)
""")
cur.execute("""
CREATE TABLE IF NOT EXISTS stats (
player_id TEXT,
tournament_id TEXT,
wins REAL DEFAULT 0,
draws REAL DEFAULT 0,
losses REAL DEFAULT 0,
matches_played INTEGER DEFAULT 0,
points REAL DEFAULT 0,
PRIMARY KEY(player_id, tournament_id)
)
""")
cur.execute("""
CREATE TABLE IF NOT EXISTS waiting_list (
id TEXT PRIMARY KEY,
tournament_id TEXT,
player_id TEXT,
added_at TEXT
)
""")
conn.commit()
def save_player(self, player: Player) -> None:
"""Save a player to the database."""
with self._get_connection() as conn:
conn.execute(
"""
INSERT OR REPLACE INTO players
(id, name, created_at, date_of_birth, category)
VALUES (?, ?, ?, ?, ?)
""",
(
player.id,
player.name,
player.created_at,
player.date_of_birth,
player.category,
),
)
conn.commit()
def get_player(self, player_id: str) -> Player | None:
"""Get a player by ID."""
with self._get_connection() as conn:
row = conn.execute(
"SELECT * FROM players WHERE id = ?", (player_id,)
).fetchone()
if row:
return Player(
id=row["id"],
name=row["name"],
created_at=row["created_at"],
date_of_birth=row["date_of_birth"],
category=row["category"],
)
return None
def get_player_by_name(self, name: str) -> Player | None:
"""Get a player by name (case-insensitive)."""
with self._get_connection() as conn:
row = conn.execute(
"SELECT * FROM players WHERE lower(name) = ?", (name.lower(),)
).fetchone()
if row:
return Player(
id=row["id"],
name=row["name"],
created_at=row["created_at"],
date_of_birth=row["date_of_birth"],
category=row["category"],
)
return None
def list_players(self) -> list[Player]:
"""list all players."""
with self._get_connection() as conn:
rows = conn.execute("SELECT * FROM players ORDER BY name").fetchall()
return [
Player(
id=r["id"],
name=r["name"],
created_at=r["created_at"],
date_of_birth=r["date_of_birth"],
category=r["category"],
)
for r in rows
]
def delete_player(self, player_id: str) -> None:
with self._get_connection() as conn:
conn.execute(
"DELETE FROM tournament_players WHERE player_id = ?", (player_id,)
)
conn.execute("DELETE FROM stats WHERE player_id = ?", (player_id,))
conn.execute("DELETE FROM players WHERE id = ?", (player_id,))
conn.commit()
def save_match(self, match: Match) -> None:
"""Save a match to the database."""
with self._get_connection() as conn:
try:
conn.execute(
"ALTER TABLE matches ADD COLUMN board_no INTEGER DEFAULT NULL"
)
conn.execute("ALTER TABLE matches ADD COLUMN colors TEXT DEFAULT NULL")
conn.commit()
except Exception:
pass
conn.execute(
"""
INSERT OR REPLACE INTO matches
(id, round_id, tournament_id, player_ids, scheduled_at, result,
winner_ids, rankings, auto_bye, players_per_match, board_no, colors)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
match.id,
match.round_id,
match.tournament_id,
json.dumps(match.player_ids),
match.scheduled_at,
match.result,
json.dumps(match.winner_ids) if match.winner_ids else None,
json.dumps(match.rankings) if match.rankings else None,
1 if match.auto_bye else 0,
match.players_per_match,
match.board_no,
json.dumps(match.colors) if match.colors else None,
),
)
conn.commit()
def get_match(self, match_id: str) -> Match | None:
"""Get a match by ID."""
with self._get_connection() as conn:
row = conn.execute(
"SELECT * FROM matches WHERE id = ?", (match_id,)
).fetchone()
if row:
return self._row_to_match(row)
return None
def list_matches_for_round(self, round_id: str) -> list[Match]:
"""list all matches for a round."""
with self._get_connection() as conn:
rows = conn.execute(
"SELECT * FROM matches WHERE round_id = ?", (round_id,)
).fetchall()
return [self._row_to_match(r) for r in rows]
def update_match_result(self, match_id: str, result: MatchResult) -> None:
"""Update match result."""
with self._get_connection() as conn:
conn.execute(
"""
UPDATE matches
SET result = ?, winner_ids = ?, rankings = ?
WHERE id = ?
""",
(
"draw" if result.is_draw else "complete",
json.dumps(result.winner_ids),
json.dumps(result.rankings),
match_id,
),
)
conn.commit()
def eliminate_player(self, tournament_id: str, player_id: str) -> None:
"""Mark a player as eliminated (unable to play)."""
with self._get_connection() as conn:
conn.execute(
"""
UPDATE tournament_players
SET able_to_play = 0
WHERE tournament_id = ? AND player_id = ?
""",
(tournament_id, player_id),
)
conn.commit()
def activate_player(self, tournament_id: str, player_id: str) -> None:
"""Mark a player as active (able to play)."""
with self._get_connection() as conn:
conn.execute(
"""
UPDATE tournament_players
SET able_to_play = 1
WHERE tournament_id = ? AND player_id = ?
""",
(tournament_id, player_id),
)
conn.commit()
def get_round_type(self, round_id: str) -> str:
"""Get the type of a round."""
with self._get_connection() as conn:
row = conn.execute(
"SELECT round_type FROM rounds WHERE id = ?", (round_id,)
).fetchone()
return row["round_type"] if row else None
def save_tournament(self, tournament_id: str, name: str, created_at: str) -> None:
"""Save a tournament."""
with self._get_connection() as conn:
conn.execute(
"INSERT INTO tournaments (id, name, created_at) VALUES (?, ?, ?)",
(tournament_id, name, created_at),
)
conn.commit()
def get_tournament_players(self, tournament_id: str) -> list[dict[str, Any]]:
"""Get all players in a tournament."""
with self._get_connection() as conn:
rows = conn.execute(
"""
SELECT p.id as player_id, p.name, tp.able_to_play
FROM players p
JOIN tournament_players tp ON tp.player_id = p.id
WHERE tp.tournament_id = ?
ORDER BY p.name
""",
(tournament_id,),
).fetchall()
return [dict(r) for r in rows]
def add_player_to_tournament(self, tournament_id: str, player_id: str) -> None:
"""Add a player to a tournament."""
with self._get_connection() as conn:
conn.execute(
"""
INSERT OR IGNORE INTO tournament_players
(tournament_id, player_id, added_at, able_to_play)
VALUES (?, ?, datetime('now'), 1)
""",
(tournament_id, player_id),
)
conn.execute(
"""
INSERT OR IGNORE INTO stats (player_id, tournament_id)
VALUES (?, ?)
""",
(player_id, tournament_id),
)
conn.commit()
def save_round(
self,
round_id: str,
tournament_id: str,
round_type: str,
ordinal: int,
created_at: str,
) -> None:
"""Save a round."""
with self._get_connection() as conn:
conn.execute(
"""
INSERT INTO rounds (id, tournament_id, round_type, ordinal, created_at)
VALUES (?, ?, ?, ?, ?)
""",
(round_id, tournament_id, round_type, ordinal, created_at),
)
conn.commit()
def get_stats(self, tournament_id: str) -> list[dict[str, Any]]:
"""Get tournament statistics."""
with self._get_connection() as conn:
rows = conn.execute(
"""
SELECT s.player_id, p.name, s.wins, s.draws, s.losses,
s.matches_played, s.points
FROM stats s
JOIN players p ON p.id = s.player_id
WHERE s.tournament_id = ?
ORDER BY s.points DESC, s.wins DESC
""",
(tournament_id,),
).fetchall()
return [dict(r) for r in rows]
def update_player_stats(
self, tournament_id: str, player_id: str, stats: dict[str, float]
) -> None:
"""Update player statistics."""
with self._get_connection() as conn:
conn.execute(
"""
INSERT OR REPLACE INTO stats
(player_id, tournament_id, wins, draws, losses, matches_played, points)
VALUES (?, ?, ?, ?, ?, ?, ?)
""",
(
player_id,
tournament_id,
stats.get("wins", 0),
stats.get("draws", 0),
stats.get("losses", 0),
stats.get("matches_played", 0),
stats.get("points", 0),
),
)
conn.commit()
def _row_to_match(self, row) -> Match:
"""Convert database row to Match object."""
keys = row.keys() if hasattr(row, "keys") else []
return Match(
id=row["id"],
round_id=row["round_id"],
tournament_id=row["tournament_id"],
player_ids=json.loads(row["player_ids"]),
scheduled_at=row["scheduled_at"],
result=row["result"],
winner_ids=json.loads(row["winner_ids"]) if row["winner_ids"] else None,
rankings=json.loads(row["rankings"]) if row["rankings"] else None,
auto_bye=bool(row["auto_bye"]),
players_per_match=row["players_per_match"],
board_no=row["board_no"] if "board_no" in keys else None,
colors=(
json.loads(row["colors"])
if ("colors" in keys and row["colors"])
else None
),
)