hanabi-league/src/utils.py

84 lines
No EOL
2.5 KiB
Python

from typing import Dict
import unidecode
import constants
from config import config_manager
import dateutil.parser
import datetime
from log_setup import logger
def normalize_username(username: str) -> str:
decoded = unidecode.unidecode(username)
return decoded.lower()
def are_game_options_allowed(game_id: int, game_options: Dict) -> bool:
"""
Check if the game options are allowed for league.
"""
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))
return False
# All options ok
return True
def is_player_count_allowed(game_id: int, num_players: int) -> bool:
"""
Check if the player count is allowed for league.
"""
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 ({}): 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
def get_rating_type(clue_starved: bool):
"""
Convenience function to get the magic values that we use to identify the different rating types of a player
"""
if clue_starved:
return 1
else:
return 0
def describe_rating_type(rating_type: int):
if rating_type == 0:
return "No Variant"
elif rating_type == 1:
return "Clue Starved"