forked from Hanabi/hanabi-league
Implement restrictions for time of games
This commit is contained in:
parent
d8ef86dcfe
commit
7289dce5d9
4 changed files with 46 additions and 7 deletions
|
@ -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"
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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))
|
||||
|
|
35
src/utils.py
35
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
|
||||
|
||||
|
|
Loading…
Reference in a new issue