hanabi-league/src/utils.py

84 lines
2.5 KiB
Python
Raw Normal View History

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
2023-12-04 23:49:42 +01:00
def describe_rating_type(rating_type: int):
if rating_type == 0:
2023-12-05 00:03:11 +01:00
return "No Variant"
2023-12-04 23:49:42 +01:00
elif rating_type == 1:
return "Clue Starved"