2023-11-23 12:52:56 +01:00
|
|
|
from typing import Dict
|
|
|
|
|
|
|
|
import unidecode
|
|
|
|
|
|
|
|
import constants
|
|
|
|
from config import config_manager
|
2023-11-24 18:34:55 +01:00
|
|
|
import dateutil.parser
|
|
|
|
import datetime
|
2023-11-23 12:52:56 +01:00
|
|
|
|
|
|
|
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):
|
2023-11-24 18:34:55 +01:00
|
|
|
logger.debug("Rejected game {} due to option {} set.".format(game_id, forbidden_option))
|
2023-11-23 12:52:56 +01:00
|
|
|
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):
|
2023-11-24 18:34:55 +01:00
|
|
|
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
|
|
|
|
))
|
2023-11-23 12:52:56 +01:00
|
|
|
return False
|
|
|
|
return True
|
2023-11-23 15:24:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
return "Standard"
|
|
|
|
elif rating_type == 1:
|
|
|
|
return "Clue Starved"
|