fix reversed A/C chair mapping and update hot/cold on round end

CHAIRS mapping was {1:A, 2:B, 3:C} but the API's country field has
1=C and 3=A. Fixed the mapping in backend and both frontends, added a
startup migration to swap A↔C columns in existing DB data, corrected
multiIf SQL queries that hardcoded the wrong winner→column mapping,
and moved _save_round() to the ENDED status block so hot/cold stats
reflect the latest round immediately.
This commit is contained in:
2026-02-25 20:49:16 +05:00
parent 3016f33783
commit e65b6b2cfb
6 changed files with 39 additions and 6 deletions

View File

@@ -13,6 +13,7 @@ log = logging.getLogger(__name__)
_client = None
_lock = threading.Lock()
_migrations_applied = False
def get_client():
@@ -34,6 +35,34 @@ def get_client():
return _client
def run_migrations():
"""Run one-time data migrations on startup."""
global _migrations_applied
if _migrations_applied:
return
client = get_client()
client.command(
"CREATE TABLE IF NOT EXISTS _migrations ("
" name String, applied_at DateTime DEFAULT now()"
") ENGINE = MergeTree() ORDER BY name"
)
result = client.query(
"SELECT count() FROM _migrations WHERE name = 'swap_ac_chairs'"
)
if result.result_rows[0][0] == 0:
log.info("Running migration: swap_ac_chairs")
client.command(
"ALTER TABLE games UPDATE "
"hand_a = hand_c, hand_c = hand_a, "
"bet_a = bet_c, bet_c = bet_a, "
"hand_type_a = hand_type_c, hand_type_c = hand_type_a "
"WHERE 1=1"
)
client.insert("_migrations", [["swap_ac_chairs"]], column_names=["name"])
log.info("Migration swap_ac_chairs applied")
_migrations_applied = True
def _with_lock(fn):
"""Decorator to serialize all ClickHouse operations."""
def wrapper(*args, **kwargs):
@@ -213,7 +242,7 @@ def get_win_distribution() -> dict:
FROM (
SELECT
bet_a, bet_b, bet_c,
multiIf(winner = 1, bet_a, winner = 2, bet_b, bet_c) AS winner_bet
multiIf(winner = 3, bet_a, winner = 2, bet_b, bet_c) AS winner_bet
FROM games
WHERE bet_a + bet_b + bet_c > 0
)
@@ -414,7 +443,7 @@ def get_analytics(period: str = "all") -> dict:
FROM (
SELECT
bet_a, bet_b, bet_c,
multiIf(winner = 1, bet_a, winner = 2, bet_b, bet_c) AS winner_bet
multiIf(winner = 3, bet_a, winner = 2, bet_b, bet_c) AS winner_bet
FROM games
{game_where + ' AND' if game_where else 'WHERE'} bet_a + bet_b + bet_c > 0
)
@@ -431,7 +460,7 @@ def get_analytics(period: str = "all") -> dict:
hand_type_result = client.query(
f"""
SELECT hand_type, count() AS cnt FROM (
SELECT multiIf(winner = 1, hand_type_a, winner = 2, hand_type_b, hand_type_c) AS hand_type
SELECT multiIf(winner = 3, hand_type_a, winner = 2, hand_type_b, hand_type_c) AS hand_type
FROM games
{game_where}
)