Implement restrictions for time of games

This commit is contained in:
Maximilian Keßler 2023-11-24 18:34:55 +01:00
parent d8ef86dcfe
commit 7289dce5d9
Signed by: max
GPG Key ID: BCC5A619923C0BA5
4 changed files with 46 additions and 7 deletions

View File

@ -39,10 +39,12 @@ min_suits: 4
max_suits: 6 max_suits: 6
# Corresponds to game IDs from hanab.live # Corresponds to game IDs from hanab.live
# Only games matching these criteria will be considered
starting_game_id: 1000000 starting_game_id: 1000000
ending_game_id: 9999999 ending_game_id: 9999999
# EST = Eastern Standard Time, so USA/Eastern # EST = Eastern Standard Time, so USA/Eastern
# Only games started within this time period will be considered.
starting_time: "2023-10-10 00:00:00 EST" starting_time: "2023-10-10 00:00:00 EST"
ending_time: "2023-12-10 00:00:00 EST" ending_time: "2023-12-10 00:00:00 EST"

View File

@ -128,7 +128,7 @@ class Config:
@check_config_attr @check_config_attr
def starting_time(self) -> datetime.datetime: def starting_time(self) -> datetime.datetime:
time = self._config["starting_time"] time = self._config["starting_time"]
return dateutil.parser(time, tzinfos={'EST': 'US/Eastern'}) return dateutil.parser.parse(time, tzinfos={'EST': 'US/Eastern'})
@property @property
@check_config_attr @check_config_attr

View File

@ -66,6 +66,7 @@ def process_game_entry(game_json: Dict, username_dict: Dict, variant_ids: List[i
seed = game_json["seed"] seed = game_json["seed"]
score = game_json["score"] score = game_json["score"]
num_turns = game_json["numTurns"] num_turns = game_json["numTurns"]
start_time = game_json["datetimeStarted"]
game_options = game_json["options"] game_options = game_json["options"]
var_id = game_options["variantID"] var_id = game_options["variantID"]
@ -74,10 +75,12 @@ def process_game_entry(game_json: Dict, username_dict: Dict, variant_ids: List[i
# Now, check if the game is one that we accept for league # Now, check if the game is one that we accept for league
if not utils.are_game_options_allowed(game_id, game_options): if not all([
return utils.are_game_options_allowed(game_id, game_options),
utils.is_player_count_allowed(game_id, num_players),
if not utils.is_player_count_allowed(game_id, num_players): utils.is_game_id_allowed(game_id),
utils.is_time_allowed(game_id, start_time)
]):
return return
if var_id not in variant_ids: if var_id not in variant_ids:
@ -217,6 +220,9 @@ def detailed_fetch_game(game_id: int) -> bool:
if not utils.is_player_count_allowed(game_id, num_players): if not utils.is_player_count_allowed(game_id, num_players):
return False return False
if not utils.is_game_id_allowed(game_id):
return False
var_id = database.get_variant_id(var_name) var_id = database.get_variant_id(var_name)
if var_id is None: if var_id is None:
logger.debug("Rejected game {} due to invalid variant id {}".format(game_id, var_id)) logger.debug("Rejected game {} due to invalid variant id {}".format(game_id, var_id))

View File

@ -4,6 +4,8 @@ import unidecode
import constants import constants
from config import config_manager from config import config_manager
import dateutil.parser
import datetime
from log_setup import logger from log_setup import logger
@ -19,7 +21,7 @@ def are_game_options_allowed(game_id: int, game_options: Dict) -> bool:
""" """
for forbidden_option in constants.FORBIDDEN_GAME_OPTIONS: for forbidden_option in constants.FORBIDDEN_GAME_OPTIONS:
if game_options.get(forbidden_option, False): if game_options.get(forbidden_option, False):
logger.debug("Rejected game {} due to option {} set".format(game_id, forbidden_option)) logger.debug("Rejected game {} due to option {} set.".format(game_id, forbidden_option))
return False return False
# All options ok # All options ok
return True return True
@ -31,7 +33,36 @@ def is_player_count_allowed(game_id: int, num_players: int) -> bool:
""" """
config = config_manager.get_config() config = config_manager.get_config()
if not (config.min_player_count <= num_players <= config.max_player_count): if not (config.min_player_count <= num_players <= config.max_player_count):
logger.debug("Rejected game {} due to invalid number of players ({})".format(game_id, num_players)) logger.debug("Rejected game {} due to invalid number of players ({}): Must be in range [{},{}]".format(
game_id, num_players, config.min_player_count, config.max_player_count
))
return False
return True
def is_game_id_allowed(game_id: int) -> bool:
"""
Check if the game is is allowed for league.
"""
config = config_manager.get_config()
if not (config.starting_game_id <= game_id <= config.ending_game_id):
logger.debug("Rejected game {} due to invalid game id: Must be in range [{},{}]".format(
game_id, config.starting_game_id, config.ending_game_id
))
return False
return True
def is_time_allowed(game_id: int, start_time: str) -> bool:
"""
Check if game was played during the league period and is thus allowed
"""
time = dateutil.parser.parse(start_time)
config = config_manager.get_config()
if not config.starting_time <= time <= config.ending_time:
logger.debug("Rejected game {} due to invalid start time ({}): Must be in range [{},{}]".format(
game_id, time, config.starting_time, config.ending_time
))
return False return False
return True return True