Skip to content

Commit

Permalink
Add some enums
Browse files Browse the repository at this point in the history
  • Loading branch information
gbtami committed Jan 11, 2025
1 parent bded639 commit f25f2b4
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 40 deletions.
27 changes: 25 additions & 2 deletions server/const.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations
from datetime import timedelta
from enum import IntEnum, StrEnum
import re

from settings import static_url, PROD
Expand Down Expand Up @@ -39,11 +40,33 @@
# Show the number of spectators only after this limit
MAX_NAMED_SPECTATORS = 20


# tournament status
T_CREATED, T_STARTED, T_ABORTED, T_FINISHED, T_ARCHIVED = range(5)
class TStatus(IntEnum):
CREATED = 0
STARTED = 1
ABORTED = 2
FINISHED = 3
ARCHIVED = 4


T_CREATED = TStatus.CREATED
T_STARTED = TStatus.STARTED
T_ABORTED = TStatus.ABORTED
T_FINISHED = TStatus.FINISHED
T_ARCHIVED = TStatus.ARCHIVED


# tournament frequency
HOURLY, DAILY, WEEKLY, MONTHLY, YEARLY, MARATHON, SHIELD = "h", "d", "w", "m", "y", "a", "s"
class TFreq(StrEnum):
HOURLY = "h"
DAILY = "d"
WEEKLY = "w"
MONTHLY = "m"
YEARLY = "y"
MARATHON = "a"
SHIELD = "s"


# tournament pairing
ARENA, RR, SWISS = range(3)
Expand Down
4 changes: 2 additions & 2 deletions server/generate_shield.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations
from compress import V2C
from const import SHIELD, VARIANTS, T_STARTED, TYPE_CHECKING
from const import TFreq, VARIANTS, T_STARTED, TYPE_CHECKING

if TYPE_CHECKING:
from pychess_global_app_state import PychessGlobalAppState
Expand All @@ -17,7 +17,7 @@ async def generate_shield(app_state: PychessGlobalAppState):
app_state.shield[variant] = []

cursor = app_state.db.tournament.find(
{"v": v, "z": z, "fr": SHIELD}, sort=[("startsAt", -1)], limit=5
{"v": v, "z": z, "fr": TFreq.SHIELD}, sort=[("startsAt", -1)], limit=5
)
async for doc in cursor:
if doc["status"] > T_STARTED:
Expand Down
16 changes: 7 additions & 9 deletions server/pychess_global_app_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@
VARIANTS,
LANGUAGES,
MAX_CHAT_LINES,
MONTHLY,
ARENA,
WEEKLY,
SHIELD,
T_CREATED,
T_STARTED,
SCHEDULE_MAX_DAYS,
ABORTED,
TFreq,
)
from broadcast import round_broadcast
from discord_bot import DiscordBot, FakeDiscordBot
Expand Down Expand Up @@ -385,14 +383,14 @@ def __init_translations(self):
or variant in SEATURDAY
or variant in PAUSED_MONTHLY_VARIANTS
):
tname = translated_tournament_name(variant, MONTHLY, ARENA, translation)
self.tourneynames[lang][(variant, MONTHLY, ARENA)] = tname
tname = translated_tournament_name(variant, TFreq.MONTHLY, ARENA, translation)
self.tourneynames[lang][(variant, TFreq.MONTHLY, ARENA)] = tname
if variant in SEATURDAY or variant in WEEKLY_VARIANTS:
tname = translated_tournament_name(variant, WEEKLY, ARENA, translation)
self.tourneynames[lang][(variant, WEEKLY, ARENA)] = tname
tname = translated_tournament_name(variant, TFreq.WEEKLY, ARENA, translation)
self.tourneynames[lang][(variant, TFreq.WEEKLY, ARENA)] = tname
if variant in SHIELDS:
tname = translated_tournament_name(variant, SHIELD, ARENA, translation)
self.tourneynames[lang][(variant, SHIELD, ARENA)] = tname
tname = translated_tournament_name(variant, TFreq.SHIELD, ARENA, translation)
self.tourneynames[lang][(variant, TFreq.SHIELD, ARENA)] = tname

def __start_bots(self):
rm = self.users["Random-Mover"]
Expand Down
49 changes: 22 additions & 27 deletions server/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@
ARENA,
CATEGORIES,
GRANDS,
DAILY,
WEEKLY,
MONTHLY,
SHIELD,
variant_display_name,
SCHEDULE_MAX_DAYS,
TYPE_CHECKING,
TFreq,
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -182,7 +179,7 @@ def schedule_plan(self):
except ValueError:
log.error("schedule_plan() ValueError")
break
plans.append(Plan(MONTHLY, date, 14, v.rstrip("960"), is_960, base, inc, byo, 90))
plans.append(Plan(TFreq.MONTHLY, date, 14, v.rstrip("960"), is_960, base, inc, byo, 90))

for i, v in enumerate(MONTHLY_VARIANTS):
if i + 1 > number_of_days:
Expand All @@ -194,27 +191,25 @@ def schedule_plan(self):
except ValueError:
log.error("schedule_plan() ValueError")
break
plans.append(Plan(MONTHLY, date, 16, v.rstrip("960"), is_960, base, inc, byo, 90))
plans.append(Plan(TFreq.MONTHLY, date, 16, v.rstrip("960"), is_960, base, inc, byo, 90))

plans += [
Plan(
SHIELD, self.first_monthly(MONDAY), 18, "kingofthehill", True, 3, 2, 0, 180
), # 960
Plan(SHIELD, self.second_monthly(MONDAY), 18, "crazyhouse", True, 3, 2, 0, 180), # 960
Plan(SHIELD, self.third_monthly(MONDAY), 18, "3check", True, 3, 2, 0, 180), # 960
# Plan(SHIELD, self.second_monthly(THURSDAY), 18, "shinobi", False, 3, 4, 0, 180),
Plan(SHIELD, self.second_monthly(SATURDAY), 12, "makruk", False, 5, 3, 0, 180),
Plan(SHIELD, self.third_monthly(SUNDAY), 12, "atomic", True, 3, 2, 0, 180), # 960
Plan(MONTHLY, self.first_monthly(SATURDAY), 12, "asean", False, 3, 2, 0, 90),
Plan(TFreq.SHIELD, self.first_monthly(MONDAY), 18, "kingofthehill", True, 3, 2, 0, 180), # 960
Plan(TFreq.SHIELD, self.second_monthly(MONDAY), 18, "crazyhouse", True, 3, 2, 0, 180), # 960
Plan(TFreq.SHIELD, self.third_monthly(MONDAY), 18, "3check", True, 3, 2, 0, 180), # 960
# Plan(TFreq.SHIELD, self.second_monthly(THURSDAY), 18, "shinobi", False, 3, 4, 0, 180),
Plan(TFreq.SHIELD, self.second_monthly(SATURDAY), 12, "makruk", False, 5, 3, 0, 180),
Plan(TFreq.SHIELD, self.third_monthly(SUNDAY), 12, "atomic", True, 3, 2, 0, 180), # 960
Plan(TFreq.MONTHLY, self.first_monthly(SATURDAY), 12, "asean", False, 3, 2, 0, 90),
# The second Saturday is Makruk Shield
Plan(MONTHLY, self.third_monthly(SATURDAY), 12, SEA, False, 3, 2, 0, 90),
Plan(MONTHLY, self.fourth_monthly(SATURDAY), 12, "makpong", False, 3, 2, 0, 90),
# Plan(WEEKLY, self.next_day_of_week(FRIDAY), 18, "crazyhouse", True, 3, 0, 0, 60), # 960
# Plan(WEEKLY, self.next_day_of_week(TUESDAY), 18, "atomic", True, 3, 0, 0, 60), # 960
Plan(WEEKLY, self.next_day_of_week(THURSDAY), 12, "makruk", False, 3, 2, 0, 90),
Plan(WEEKLY, self.next_day_of_week(SUNDAY), 18, "duck", False, 3, 5, 0, 90),
Plan(WEEKLY, self.next_day_of_week(FRIDAY), 12, "xiangqi", False, 5, 3, 0, 90),
Plan(WEEKLY, self.next_day_of_week(WEDNESDAY), 12, "janggi", False, 5, 15, 1, 90),
Plan(TFreq.MONTHLY, self.third_monthly(SATURDAY), 12, SEA, False, 3, 2, 0, 90),
Plan(TFreq.MONTHLY, self.fourth_monthly(SATURDAY), 12, "makpong", False, 3, 2, 0, 90),
# Plan(TFreq.WEEKLY, self.next_day_of_week(FRIDAY), 18, "crazyhouse", True, 3, 0, 0, 60), # 960
# Plan(TFreq.WEEKLY, self.next_day_of_week(TUESDAY), 18, "atomic", True, 3, 0, 0, 60), # 960
Plan(TFreq.WEEKLY, self.next_day_of_week(THURSDAY), 12, "makruk", False, 3, 2, 0, 90),
Plan(TFreq.WEEKLY, self.next_day_of_week(SUNDAY), 18, "duck", False, 3, 5, 0, 90),
Plan(TFreq.WEEKLY, self.next_day_of_week(FRIDAY), 12, "xiangqi", False, 5, 3, 0, 90),
Plan(TFreq.WEEKLY, self.next_day_of_week(WEDNESDAY), 12, "janggi", False, 5, 15, 1, 90),
]

return plans
Expand Down Expand Up @@ -267,16 +262,16 @@ def new_scheduled_tournaments(already_scheduled, now=None):
plan.variant + ("960" if plan.is960 else "")
).title()

if plan.freq == SHIELD:
if plan.freq == TFreq.SHIELD:
name = "%s Shield Arena" % variant_name
elif plan.freq == MONTHLY:
elif plan.freq == TFreq.MONTHLY:
if plan.variant in CATEGORIES["makruk"]:
name = "SEAturday %s Arena" % variant_name
else:
name = "Monthly %s Arena" % variant_name
elif plan.freq == WEEKLY:
elif plan.freq == TFreq.WEEKLY:
name = "Weekly %s Arena" % variant_name
elif plan.freq == DAILY:
elif plan.freq == TFreq.DAILY:
name = "Daily %s Arena" % variant_name
else:
name = "%s Arena" % variant_name
Expand Down

0 comments on commit f25f2b4

Please sign in to comment.