Skip to content

Commit

Permalink
Move more info to ServerVariants enum
Browse files Browse the repository at this point in the history
  • Loading branch information
gbtami committed Jan 13, 2025
1 parent c0e81a6 commit 379499a
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 33 deletions.
18 changes: 0 additions & 18 deletions server/compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])]
Expand All @@ -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
5 changes: 2 additions & 3 deletions server/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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 (
Expand Down
11 changes: 6 additions & 5 deletions server/game_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"]]
Expand Down Expand Up @@ -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"])
Expand Down
14 changes: 7 additions & 7 deletions server/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions server/variants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 379499a

Please sign in to comment.