From 379499a3fa5c9a76b3a383cfcaeaac9d059a7506 Mon Sep 17 00:00:00 2001 From: gbtami Date: Mon, 13 Jan 2025 01:21:17 +0100 Subject: [PATCH] Move more info to ServerVariants enum --- server/compress.py | 18 ------------------ server/game.py | 5 ++--- server/game_api.py | 11 ++++++----- server/utils.py | 14 +++++++------- server/variants.py | 2 ++ 5 files changed, 17 insertions(+), 33 deletions(-) diff --git a/server/compress.py b/server/compress.py index 1f352c206..69922c2fd 100644 --- a/server/compress.py +++ b/server/compress.py @@ -72,15 +72,6 @@ def encode_move_standard(move): return chr(M2C[move[0:2]]) + chr(M2C[move[2:4]]) + (move[4] if len(move) == 5 else "") -def get_encode_method(variant): - if variant in ("kyotoshogi", "chennis"): - return encode_move_flipping - elif variant == "duck": - return encode_move_duck - else: - return encode_move_standard - - def decode_move_flipping(move): return ( C2M[ord(move[0])] + "@" + C2M[ord(move[1])] @@ -102,12 +93,3 @@ def decode_move_duck(move): def decode_move_standard(move): return C2M[ord(move[0])] + C2M[ord(move[1])] + (move[2] if len(move) == 3 else "") - - -def get_decode_method(variant): - if variant in ("kyotoshogi", "chennis"): - return decode_move_flipping - elif variant == "duck": - return decode_move_duck - else: - return decode_move_standard diff --git a/server/game.py b/server/game.py index 9f436b4ba..3d9c48c29 100644 --- a/server/game.py +++ b/server/game.py @@ -7,7 +7,7 @@ from broadcast import round_broadcast from clock import Clock, CorrClock -from compress import get_encode_method, R2C +from compress import R2C from const import ( CREATED, DARK_FEN, @@ -99,6 +99,7 @@ def __init__( self.imported_by = "" self.server_variant = get_server_variant(variant, chess960) + self.encode_method = self.server_variant.move_encoding self.berserk_time = self.base * 1000 * 30 @@ -153,8 +154,6 @@ def __init__( self.id = gameId - self.encode_method = get_encode_method(variant) - self.fow = variant == "fogofwar" self.n_fold_is_draw = self.variant in ( diff --git a/server/game_api.py b/server/game_api.py index 8c9c21409..1da6ea1bc 100644 --- a/server/game_api.py +++ b/server/game_api.py @@ -10,7 +10,7 @@ from aiohttp_sse import sse_response import pymongo -from compress import get_decode_method, C2R, decode_move_standard +from compress import C2R, decode_move_standard from const import DARK_FEN, STARTED, MATE, INVALIDMOVE, VARIANTEND, CLAIM from convert import zero2grand from settings import ADMINS @@ -157,8 +157,9 @@ async def get_tournament_games(request): cursor = app_state.db.game.find({"tid": tournamentId}) game_doc_list = [] - variant = app_state.tournaments[tournamentId].variant - decode_method = get_decode_method(variant) + tournament = app_state.tournaments[tournamentId] + variant = tournament.variant + decode_method = tournament.server_variant.move_decoding async for doc in cursor: doc["v"] = C2V[doc["v"]] @@ -317,8 +318,8 @@ async def get_user_games(request): 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: - decode_method = get_decode_method(variant) - + 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/utils.py b/server/utils.py index f6b06921a..74ed87b76 100644 --- a/server/utils.py +++ b/server/utils.py @@ -27,7 +27,7 @@ MANCHU_FEN, T_STARTED, ) -from compress import get_decode_method, get_encode_method, R2C, C2R +from compress import R2C, C2R from convert import mirror5, mirror9, grand2zero, zero2grand from fairy import ( BLACK, @@ -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 +from variants import C2V, GRANDS, get_server_variant async def tv_game(app_state: PychessGlobalAppState): @@ -144,7 +144,7 @@ async def load_game(app_state: PychessGlobalAppState, game_id): game.usi_format = usi_format - decode_method = get_decode_method(variant) + decode_method = game.server_variant.move_decoding mlist = [*map(decode_method, doc["m"])] if (mlist or game.tournamentId is not None) and doc["s"] > STARTED: @@ -294,7 +294,7 @@ async def import_game(request): base, inc = 0, 0 move_stack = data.get("moves", "").split(" ") - encode_method = get_encode_method(variant) + encode_method = get_server_variant(variant, chess960).move_encoding moves = [*map(encode_method, map(grand2zero, move_stack) if variant in GRANDS else move_stack)] game_id = await new_id(None if app_state.db is None else app_state.db.game) @@ -663,13 +663,13 @@ async def play_move(app_state: PychessGlobalAppState, user, game, move, clocks=N def pgn(doc): variant = C2V[doc["v"]] - decode_method = get_decode_method(variant) + chess960 = bool(int(doc.get("z"))) if "z" in doc else False + + decode_method = get_server_variant(variant, chess960).move_decoding mlist = [*map(decode_method, doc["m"])] if len(mlist) == 0: return None - chess960 = bool(int(doc.get("z"))) if "z" in doc else False - initial_fen = doc.get("if") usi_format = variant.endswith("shogi") and doc.get("uci") is None diff --git a/server/variants.py b/server/variants.py index 4f4ae5a12..c67b58493 100644 --- a/server/variants.py +++ b/server/variants.py @@ -42,6 +42,8 @@ def __init__(self, variant): self.chess960 = variant.chess960 self.grand = variant.grand self.byo = variant.byo + self.move_encoding = variant.move_encoding + self.move_decoding = variant.move_decoding CHESS = Variant("n", "chess", _("Chess"), "M") CHESS960 = Variant("n", "chess", _("Chess960"), "V", chess960=True)