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 352861d commit bb04e4d
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 21 deletions.
2 changes: 1 addition & 1 deletion server/auto_pair.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion server/clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions server/game_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])

Expand Down
5 changes: 2 additions & 3 deletions server/generate_crosstable.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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"]
Expand Down
2 changes: 1 addition & 1 deletion server/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions server/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand Down
7 changes: 3 additions & 4 deletions server/variants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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}

Expand Down
4 changes: 3 additions & 1 deletion server/wsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions server/wsr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit bb04e4d

Please sign in to comment.