Skip to content

Commit

Permalink
Use code property of Variant instead of V2C
Browse files Browse the repository at this point in the history
  • Loading branch information
gbtami committed Jan 12, 2025
1 parent 95cfc49 commit 7e4264b
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 50 deletions.
3 changes: 3 additions & 0 deletions server/bug/game_bug.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
)
from fairy import FairyBoard, BLACK, WHITE
from spectators import spectators
from variants import get_server_variant

MAX_HIGH_SCORE = 10
MAX_PLY = 2 * 600
Expand Down Expand Up @@ -79,6 +80,8 @@ def __init__(
self.create = create
self.imported_by = ""

self.server_variant = get_server_variant(variant, chess960)

self.berserk_time = self.base * 1000 * 30

self.browser_title = "%s • %s+%s vs %s+%s" % (
Expand Down
13 changes: 8 additions & 5 deletions server/bug/import_bugh_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
IMPORTED,
)
from datetime import datetime, timezone
from compress import R2C, V2C, encode_move_standard
from compress import R2C, encode_move_standard
from newid import new_id
from aiohttp import web
from bugchess.pgn import read_game, Game
from logger import log
from variants import get_server_variant


def get_main_variation(game: Game, base_tc_ms: int) -> [list, list]:
Expand Down Expand Up @@ -93,8 +94,10 @@ async def import_game_bpgn(request):
wplayer_a, bplayer_a, wplayer_b, bplayer_b = init_players(app_state, wp_a, bp_a, wp_b, bp_b)

variant = "bughouse"
chess960 = False # variant.endswith("960")
# variant = variant.removesuffix("960")
chess960 = variant.endswith("960")
variant = variant.removesuffix("960")

server_variant = get_server_variant(variant, chess960)

# todo: replace with valid initial fen for now - maybe fix the problematic fen that ends with / instead of [] eventually - that is how chess.com fen looks like and doesn't parse well here
initial_fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR[] w KQkq - 0 1 | rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR[] w KQkq - 0 1" # first_game.headers.get("FEN", "")
Expand Down Expand Up @@ -159,7 +162,7 @@ async def import_game_bpgn(request):
document = {
"_id": game_id,
"us": [wplayer_a.username, bplayer_a.username, wplayer_b.username, bplayer_b.username],
"v": V2C[variant],
"v": server_variant.code,
"b": base,
"i": inc,
"bp": 0,
Expand All @@ -175,7 +178,7 @@ async def import_game_bpgn(request):
"r": R2C[result],
"x": 0,
"y": IMPORTED,
"z": int(False), # int(new_game.chess960),
"z": int(chess960),
"by": first_game.headers.get("username"),
}

Expand Down
5 changes: 3 additions & 2 deletions server/bug/utils_bug.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from pychess_global_app_state import PychessGlobalAppState
from user import User
from compress import R2C, C2R, V2C, C2V, decode_move_standard
from compress import R2C, C2R, decode_move_standard
from bug.game_bug import GameBug
from const import (
STARTED,
Expand All @@ -18,6 +18,7 @@
from utils import remove_seek, round_broadcast, sanitize_fen
from websocket_utils import ws_send_json
from logger import log
from variants import C2V


def init_players(app_state: PychessGlobalAppState, wp_a, bp_a, wp_b, bp_b):
Expand Down Expand Up @@ -347,7 +348,7 @@ async def insert_game_to_db_bughouse(game: GameBug, app_state: PychessGlobalAppS
"p1": {"e": game.brating_a},
"p2": {"e": game.wrating_b},
"p3": {"e": game.brating_b},
"v": V2C[game.variant],
"v": game.server_variant.code,
"b": game.base,
"i": game.inc,
# "bp": game.byoyomi_period,
Expand Down
3 changes: 1 addition & 2 deletions server/compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"""

# Create mappings to compress variant, result and uci/usi move lists a little
V2C = {
V2C_ORIG = {
"ataxx": "Z",
"chess": "n",
"capablanca": "c",
Expand Down Expand Up @@ -66,7 +66,6 @@
"horde": "š",
"shatranj": "†",
}
C2V = {v: k for k, v in V2C.items()}

R2C = {"1-0": "a", "0-1": "b", "1/2-1/2": "c", "*": "d"}
C2R = {v: k for k, v in R2C.items()}
Expand Down
6 changes: 2 additions & 4 deletions server/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
from settings import URI
from spectators import spectators
from logger import log
from variants import VARIANTS
from variants import get_server_variant

if TYPE_CHECKING:
from pychess_global_app_state import PychessGlobalAppState
Expand Down Expand Up @@ -100,9 +100,7 @@ def __init__(
self.create = create
self.imported_by = ""

self.server_variant = VARIANTS[
("_" if variant[0].isdigit() else "") + variant + ("960" if chess960 else "")
]
self.server_variant = get_server_variant(variant, chess960)

self.berserk_time = self.base * 1000 * 30

Expand Down
20 changes: 10 additions & 10 deletions server/game_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
from aiohttp_sse import sse_response
import pymongo

from compress import get_decode_method, C2V, V2C, C2R, decode_move_standard
from compress import get_decode_method, C2R, decode_move_standard
from const import DARK_FEN, GRANDS, STARTED, MATE, INVALIDMOVE, VARIANTEND, CLAIM
from convert import zero2grand
from settings import ADMINS
from tournaments import get_tournament_name
from utils import pgn
from pychess_global_app_state_utils import get_app_state
from logger import log
from variants import VARIANTS
from variants import C2V, get_server_variant, VARIANTS

GAME_PAGE_SIZE = 12

Expand Down Expand Up @@ -239,15 +239,15 @@ async def get_user_games(request):
filter_cond["by"] = profileId
filter_cond["y"] = 2
elif ("/perf" in request.path or uci_moves) and variant in VARIANTS:
if variant.endswith("960"):
v = V2C[variant[:-3]]
z = 1
else:
v = V2C[variant]
z = 0
variant960 = variant.endswith("960")
uci_variant = variant[:-3] if variant960 else variant

v = get_server_variant(uci_variant, variant960)
z = 1 if variant960 else 0

filter_cond["$or"] = [
{"v": v, "z": z, "us.1": profileId},
{"v": v, "z": z, "us.0": profileId},
{"v": v.code, "z": z, "us.1": profileId},
{"v": v.code, "z": z, "us.0": profileId},
]
elif "/me" in request.path:
session = await aiohttp_session.get_session(request)
Expand Down
5 changes: 3 additions & 2 deletions server/generate_crosstable.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from compress import V2C
from variants import BUG_VARIANTS


async def generate_crosstable(app_state, username=None):
Expand All @@ -13,8 +13,9 @@ 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"] == V2C["bughouse"]:
if doc["v"] in bug_variant_codes:
continue # todo:bughouse has no crosstable implemented at the moment

game_id = doc["_id"]
Expand Down
9 changes: 4 additions & 5 deletions server/generate_shield.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations
from compress import V2C
from const import SHIELD, T_STARTED, TYPE_CHECKING
from variants import VARIANTS
from variants import get_server_variant, VARIANTS

if TYPE_CHECKING:
from pychess_global_app_state import PychessGlobalAppState
Expand All @@ -10,15 +9,15 @@
async def generate_shield(app_state: PychessGlobalAppState):
for variant in VARIANTS:
variant960 = variant.endswith("960")
variant_name = variant[:-3] if variant960 else variant
uci_variant = variant[:-3] if variant960 else variant

v = V2C[variant_name]
v = get_server_variant(uci_variant, variant960)
z = 1 if variant960 else 0

app_state.shield[variant] = []

cursor = app_state.db.tournament.find(
{"v": v, "z": z, "fr": SHIELD}, sort=[("startsAt", -1)], limit=5
{"v": v.code, "z": z, "fr": SHIELD}, sort=[("startsAt", -1)], limit=5
)
async for doc in cursor:
if doc["status"] > T_STARTED:
Expand Down
3 changes: 3 additions & 0 deletions server/tournament.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
from user import User
from utils import insert_game_to_db
from logger import log
from variants import get_server_variant


SCORE, STREAK, DOUBLE = range(1, 4)
Expand Down Expand Up @@ -233,6 +234,8 @@ def __init__(
self.rounds = rounds
self.frequency = frequency

self.server_variant = get_server_variant(variant, chess960)

self.created_by = created_by
self.created_at = datetime.now(timezone.utc) if created_at is None else created_at
if starts_at == "" or starts_at is None:
Expand Down
19 changes: 9 additions & 10 deletions server/tournaments.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import aiohttp_session

from arena_new import ArenaTournament
from compress import C2V, V2C, C2R
from compress import C2R
from const import (
CASUAL,
RATED,
Expand Down Expand Up @@ -34,7 +34,7 @@
from swiss import SwissTournament
from tournament import GameData, PlayerData, SCORE_SHIFT, Tournament
from logger import log
from variants import VARIANTS
from variants import C2V, get_server_variant, VARIANTS


async def create_or_update_tournament(
Expand Down Expand Up @@ -155,7 +155,7 @@ async def upsert_tournament_to_db(tournament, app_state: PychessGlobalAppState):
"d": tournament.description,
"fr": tournament.frequency,
"minutes": tournament.minutes,
"v": V2C[tournament.variant],
"v": tournament.server_variant.code,
"b": tournament.base,
"i": tournament.inc,
"bp": tournament.byoyomi_period,
Expand Down Expand Up @@ -192,14 +192,13 @@ async def get_winners(app_state: PychessGlobalAppState, shield, variant: str = N
limit = 50

for variant in variants:
if variant.endswith("960"):
v = variant[:-3]
z = 1
else:
v = variant
z = 0
variant960 = variant.endswith("960")
uci_variant = variant[:-3] if variant960 else variant

v = get_server_variant(uci_variant, variant960)
z = 1 if variant960 else 0

filter_cond = {"v": V2C[v], "z": z, "status": {"$in": [T_FINISHED, T_ARCHIVED]}}
filter_cond = {"v": v.code, "z": z, "status": {"$in": [T_FINISHED, T_ARCHIVED]}}
if shield:
filter_cond["fr"] = SHIELD

Expand Down
7 changes: 4 additions & 3 deletions server/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
MANCHU_FEN,
T_STARTED,
)
from compress import get_decode_method, get_encode_method, R2C, C2R, V2C, C2V
from compress import get_decode_method, get_encode_method, R2C, C2R
from convert import mirror5, mirror9, grand2zero, zero2grand
from fairy import (
BLACK,
Expand All @@ -50,6 +50,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


async def tv_game(app_state: PychessGlobalAppState):
Expand Down Expand Up @@ -325,7 +326,7 @@ async def import_game(request):
document = {
"_id": game_id,
"us": [wplayer.username, bplayer.username],
"v": V2C[variant],
"v": new_game.server_variant.code,
"b": base,
"i": inc,
"bp": new_game.byoyomi_period,
Expand Down Expand Up @@ -483,7 +484,7 @@ async def insert_game_to_db(game, app_state: PychessGlobalAppState):
"us": [game.wplayer.username, game.bplayer.username],
"p0": {"e": game.wrating},
"p1": {"e": game.brating},
"v": V2C[game.variant],
"v": game.server_variant.code,
"b": game.base,
"i": game.inc,
"bp": game.byoyomi_period,
Expand Down
24 changes: 18 additions & 6 deletions server/variants.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ class Variant:


class ServerVariants(Enum):
def __init__(self, variant):
self.code = variant.code
self.uci_variant = variant.uci_variant
self.display_name = variant.display_name
self.icon = variant.icon

CHESS = Variant("n", "chess", "CHESS", "M")
CHESS960 = Variant("n", "chess", "CHESS960", "V", chess960=True)
BUGHOUSE = Variant("F", "bughouse", "BUGHOUSE", "¢", bug=True)
Expand Down Expand Up @@ -105,14 +111,18 @@ def server_name(self):
return self.value.uci_variant + ("960" if self.value.chess960 else "")


def get_server_variant(uci_variant, chess960):
return VARIANTS[uci_variant + ("960" if chess960 else "")]


OLD_VARIANTS = (
ServerVariants.EMBASSY,
ServerVariants.GOTHIC,
ServerVariants.GOTHHOUSE,
ServerVariants.SHINOBI,
)

DEV_VARIANTS = (
BUG_VARIANTS = (
ServerVariants.BUGHOUSE,
ServerVariants.BUGHOUSE960,
)
Expand All @@ -128,16 +138,18 @@ def server_name(self):

# Remove new variants on prod site until they stabilize
if PROD:
for variant in DEV_VARIANTS:
for variant in BUG_VARIANTS:
del VARIANTS[variant.server_name]

C2V = {variant.code: variant.uci_variant for variant in ServerVariants}


if __name__ == "__main__":

from const import VARIANT_ICONS_ORIG
from compress import V2C
from compress import V2C_ORIG

for sn, variant in VARIANTS.items():
print(variant.value.code, variant.value.icon, sn)
assert variant.value.code == V2C[variant.value.uci_variant]
assert variant.value.icon == VARIANT_ICONS_ORIG[variant.server_name]
print(variant.code, variant.icon, sn)
assert variant.code == V2C_ORIG[variant.uci_variant]
assert variant.icon == VARIANT_ICONS_ORIG[variant.server_name]
2 changes: 1 addition & 1 deletion tests/test_compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from compress import get_encode_method, get_decode_method
from fairy import FairyBoard
from const import VARIANTS
from variants import VARIANTS


class EncodeDecodeTestCase(unittest.TestCase):
Expand Down

0 comments on commit 7e4264b

Please sign in to comment.