From 7289dce5d967266882d81a19462b0acb6f1b12fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Fri, 24 Nov 2023 18:34:55 +0100 Subject: [PATCH] Implement restrictions for time of games --- install/default_config.yaml | 2 ++ src/config.py | 2 +- src/fetch_games.py | 14 ++++++++++---- src/utils.py | 35 +++++++++++++++++++++++++++++++++-- 4 files changed, 46 insertions(+), 7 deletions(-) diff --git a/install/default_config.yaml b/install/default_config.yaml index 5c4f85b..5469c23 100644 --- a/install/default_config.yaml +++ b/install/default_config.yaml @@ -39,10 +39,12 @@ min_suits: 4 max_suits: 6 # Corresponds to game IDs from hanab.live +# Only games matching these criteria will be considered starting_game_id: 1000000 ending_game_id: 9999999 # 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" ending_time: "2023-12-10 00:00:00 EST" diff --git a/src/config.py b/src/config.py index 98facd6..34e4b94 100644 --- a/src/config.py +++ b/src/config.py @@ -128,7 +128,7 @@ class Config: @check_config_attr def starting_time(self) -> datetime.datetime: time = self._config["starting_time"] - return dateutil.parser(time, tzinfos={'EST': 'US/Eastern'}) + return dateutil.parser.parse(time, tzinfos={'EST': 'US/Eastern'}) @property @check_config_attr diff --git a/src/fetch_games.py b/src/fetch_games.py index 5323ef0..ecd9ac2 100644 --- a/src/fetch_games.py +++ b/src/fetch_games.py @@ -66,6 +66,7 @@ def process_game_entry(game_json: Dict, username_dict: Dict, variant_ids: List[i seed = game_json["seed"] score = game_json["score"] num_turns = game_json["numTurns"] + start_time = game_json["datetimeStarted"] game_options = game_json["options"] 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 - if not utils.are_game_options_allowed(game_id, game_options): - return - - if not utils.is_player_count_allowed(game_id, num_players): + if not all([ + utils.are_game_options_allowed(game_id, game_options), + 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 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): return False + if not utils.is_game_id_allowed(game_id): + return False + var_id = database.get_variant_id(var_name) if var_id is None: logger.debug("Rejected game {} due to invalid variant id {}".format(game_id, var_id)) diff --git a/src/utils.py b/src/utils.py index 124f7fc..825407e 100644 --- a/src/utils.py +++ b/src/utils.py @@ -4,6 +4,8 @@ import unidecode import constants from config import config_manager +import dateutil.parser +import datetime 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: 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 # All options ok return True @@ -31,7 +33,36 @@ def is_player_count_allowed(game_id: int, num_players: int) -> bool: """ config = config_manager.get_config() 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 True