Save finished games and add history views

This commit is contained in:
2026-03-03 19:05:54 +01:00
parent 7887986a5a
commit d1b432f8aa
11 changed files with 919 additions and 33 deletions
+36 -3
View File
@@ -3,10 +3,15 @@ from flask import g
DATABASE = 'sqlite.db'
def connect_db():
db = sqlite3.connect(DATABASE)
db.row_factory = sqlite3.Row
db.execute("PRAGMA foreign_keys=ON;")
return db
def get_db():
if 'db' not in g:
g.db = sqlite3.connect(DATABASE)
g.db.row_factory = sqlite3.Row
g.db = connect_db()
return g.db
def close_db(e=None):
@@ -33,7 +38,13 @@ def init_db(app):
id INTEGER PRIMARY KEY AUTOINCREMENT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
final_fen TEXT,
termination TEXT CHECK(termination IN ('checkmate', 'resignation', 'timeout', 'draw', 'other'))
termination TEXT CHECK(termination IN ('checkmate', 'resignation', 'timeout', 'draw', 'other')),
termination_detail TEXT,
winner_color TEXT CHECK(winner_color IN ('white', 'black', 'draw')),
move_history TEXT NOT NULL DEFAULT '[]',
time_mode TEXT,
started_at TIMESTAMP,
ended_at TIMESTAMP
);
CREATE TABLE IF NOT EXISTS game_players (
@@ -75,4 +86,26 @@ def init_db(app):
if "last_seen_at" not in user_columns:
db.execute("ALTER TABLE users ADD COLUMN last_seen_at TIMESTAMP")
game_columns = {
row["name"]
for row in db.execute("PRAGMA table_info(games)").fetchall()
}
if "termination_detail" not in game_columns:
db.execute("ALTER TABLE games ADD COLUMN termination_detail TEXT")
if "winner_color" not in game_columns:
db.execute(
"ALTER TABLE games ADD COLUMN winner_color TEXT "
"CHECK(winner_color IN ('white', 'black', 'draw'))"
)
if "move_history" not in game_columns:
db.execute(
"ALTER TABLE games ADD COLUMN move_history TEXT NOT NULL DEFAULT '[]'"
)
if "time_mode" not in game_columns:
db.execute("ALTER TABLE games ADD COLUMN time_mode TEXT")
if "started_at" not in game_columns:
db.execute("ALTER TABLE games ADD COLUMN started_at TIMESTAMP")
if "ended_at" not in game_columns:
db.execute("ALTER TABLE games ADD COLUMN ended_at TIMESTAMP")
db.commit()