From bb04e4df8b3b744292a4f1f6f62998c2ab16b70c Mon Sep 17 00:00:00 2001 From: gbtami Date: Mon, 13 Jan 2025 01:58:26 +0100 Subject: [PATCH] Move more info to ServerVariants enum --- server/auto_pair.py | 2 +- server/clock.py | 2 +- server/game_api.py | 5 +++-- server/generate_crosstable.py | 5 ++--- server/index.py | 2 +- server/utils.py | 8 ++++---- server/variants.py | 7 +++---- server/wsl.py | 4 +++- server/wsr.py | 8 ++++---- 9 files changed, 22 insertions(+), 21 deletions(-) diff --git a/server/auto_pair.py b/server/auto_pair.py index 40580a02a..c91a9d518 100644 --- a/server/auto_pair.py +++ b/server/auto_pair.py @@ -40,7 +40,7 @@ def add_to_auto_pairings(app_state, user, data): if ( (byoyomi_period > 0 and variant not in BYOS) or (byoyomi_period == 0 and variant in BYOS) - or variant.startswith("bughouse") + or variant.startswith("bug") ): continue diff --git a/server/clock.py b/server/clock.py index 0e0715655..a71c40c31 100644 --- a/server/clock.py +++ b/server/clock.py @@ -35,7 +35,7 @@ def restart(self, secs=None): self.secs = secs else: # give some time to make first move - if self.ply < 2 and self.game.variant != "bughouse": + if self.ply < 2 and not self.game.server_variant.bug: if self.game.tournamentId is None: # Non tournament games are not timed for the first moves of either # player. We stop the clock to prevent unnecessary clock diff --git a/server/game_api.py b/server/game_api.py index 1da6ea1bc..54313c8fc 100644 --- a/server/game_api.py +++ b/server/game_api.py @@ -312,15 +312,16 @@ async def get_user_games(request): app_state.users[doc["us"][3]].title if doc["us"][3] in app_state.users else "" ) - if variant.startswith("bughouse"): + server_variant = get_server_variant(variant, bool(doc.get("z", 0))) + if server_variant.bug: mA = [m for idx, m in enumerate(doc["m"]) if "o" in doc and doc["o"][idx] == 0] mB = [m for idx, m in enumerate(doc["m"]) if "o" in doc and doc["o"][idx] == 1] doc["lm"] = decode_move_standard(mA[-1]) if len(mA) > 0 else "" doc["lmB"] = decode_move_standard(mB[-1]) if len(mB) > 0 else "" else: - server_variant = get_server_variant(variant, bool(doc.get("z", 0))) decode_method = server_variant.move_decoding doc["lm"] = decode_method(doc["m"][-1]) if len(doc["m"]) > 0 else "" + if variant in GRANDS and doc["lm"] != "": doc["lm"] = zero2grand(doc["lm"]) diff --git a/server/generate_crosstable.py b/server/generate_crosstable.py index 3a640a8d8..ed31bfba2 100644 --- a/server/generate_crosstable.py +++ b/server/generate_crosstable.py @@ -1,6 +1,6 @@ from __future__ import annotations -from variants import BUG_VARIANTS +from variants import BUG_VARIANT_CODES async def generate_crosstable(app_state, username=None): @@ -13,9 +13,8 @@ async def generate_crosstable(app_state, username=None): cursor = db.game.find({"us": username}).sort("d") print("START generate_crosstable", username) - bug_variant_codes = [variant.code for variant in BUG_VARIANTS] async for doc in cursor: - if doc["v"] in bug_variant_codes: + if doc["v"] in BUG_VARIANT_CODES: continue # todo:bughouse has no crosstable implemented at the moment game_id = doc["_id"] diff --git a/server/index.py b/server/index.py index 0e5207afc..82e4122c2 100644 --- a/server/index.py +++ b/server/index.py @@ -602,7 +602,7 @@ def video_target(target): render["tournamentname"] = tournament_name render["wberserk"] = game.wberserk render["bberserk"] = game.bberserk - if game.variant == "bughouse": + if game.server_variant.bug: render["wplayerB"] = game.wplayerB.username render["wtitleB"] = game.wplayerB.title render["wratingB"] = game.wrating_b diff --git a/server/utils.py b/server/utils.py index 74ed87b76..98972c28f 100644 --- a/server/utils.py +++ b/server/utils.py @@ -49,7 +49,7 @@ from pychess_global_app_state import PychessGlobalAppState from pychess_global_app_state_utils import get_app_state from logger import log -from variants import C2V, GRANDS, get_server_variant +from variants import BUG_VARIANT_CODES, C2V, GRANDS, get_server_variant async def tv_game(app_state: PychessGlobalAppState): @@ -90,7 +90,7 @@ async def load_game(app_state: PychessGlobalAppState, game_id): variant = C2V[doc["v"]] - if variant == "bughouse": + if doc["v"] in BUG_VARIANT_CODES: from bug.utils_bug import load_game_bug return await load_game_bug(app_state, game_id) @@ -740,8 +740,8 @@ def pgn(doc): def sanitize_fen(variant, initial_fen, chess960): - - if variant == "bughouse": + server_variant = get_server_variant(variant, chess960) + if server_variant.bug: fens = initial_fen.split(" | ") fen_a = fens[0] fen_b = fens[1] diff --git a/server/variants.py b/server/variants.py index c67b58493..9690f4820 100644 --- a/server/variants.py +++ b/server/variants.py @@ -42,6 +42,7 @@ def __init__(self, variant): self.chess960 = variant.chess960 self.grand = variant.grand self.byo = variant.byo + self.bug = variant.bug self.move_encoding = variant.move_encoding self.move_decoding = variant.move_decoding @@ -138,10 +139,8 @@ def get_server_variant(uci_variant, chess960): ServerVariants.SHINOBI, ) -BUG_VARIANTS = ( - ServerVariants.BUGHOUSE, - ServerVariants.BUGHOUSE960, -) +BUG_VARIANTS = tuple(variant for variant in ServerVariants if variant.bug) +BUG_VARIANT_CODES = [variant.code for variant in BUG_VARIANTS] ALL_VARIANTS = {variant.server_name: variant for variant in ServerVariants} diff --git a/server/wsl.py b/server/wsl.py index 5ed4e58a0..baff8695c 100644 --- a/server/wsl.py +++ b/server/wsl.py @@ -35,6 +35,7 @@ from utils import join_seek, load_game, remove_seek from websocket_utils import get_user, process_ws, ws_send_json from logger import log +from variants import get_server_variant async def lobby_socket_handler(request): @@ -221,7 +222,8 @@ async def handle_accept_seek(app_state: PychessGlobalAppState, ws, user, data): return # print("accept_seek", seek.seek_json) - if seek.variant == "bughouse": + server_variant = get_server_variant(seek.variant, seek.chess960) + if server_variant.bug: await handle_accept_seek_bughouse(app_state, user, data, seek) else: response = await join_seek(app_state, user, seek) diff --git a/server/wsr.py b/server/wsr.py index afcaa7ecc..b10cb3b50 100644 --- a/server/wsr.py +++ b/server/wsr.py @@ -97,7 +97,7 @@ async def process_message(app_state, user, ws, data, game): elif data["type"] == "takeback": await handle_takeback(ws, game) elif data["type"] in ("abort", "resign", "abandon", "flag"): - if game.variant == "bughouse": + if game.server_variant.bug: await handle_resign_bughouse(data, game, user) else: await handle_abort_resign_abandon_flag(ws, app_state.users, user, data, game) @@ -146,7 +146,7 @@ async def finally_logic(app_state: PychessGlobalAppState, ws, user, game): async def handle_move(app_state: PychessGlobalAppState, user, data, game): log.debug("Got USER move %s %s %s" % (user.username, data["gameId"], data["move"])) async with game.move_lock: - if game.variant == "bughouse": + if game.server_variant.bug: try: await play_move_bug( app_state, @@ -352,7 +352,7 @@ async def handle_analysis(app_state: PychessGlobalAppState, ws, data, game): async def handle_rematch(app_state: PychessGlobalAppState, ws, user, data, game): - if game.variant == "bughouse": + if game.server_variant.bug: await handle_rematch_bughouse(app_state, game, user, app_state.users) return @@ -558,7 +558,7 @@ async def handle_game_user_connected(app_state: PychessGlobalAppState, ws, user, game.spectators.add(user) await round_broadcast(game, game.spectator_list, full=True) - stopwatch_secs = game.stopwatch.secs if (not game.corr and game.variant != "bughouse") else 0 + stopwatch_secs = game.stopwatch.secs if (not game.corr and not game.server_variant.bug) else 0 response = { "type": "game_user_connected", "username": user.username,