async def get_user_profile_by_email(email: EmailStr): try: user = db_user.get_user_by_email(email) return UserProfile(email=user.email, username=user.username, last_access_date=user.last_access_date, is_validated=user.is_validated) except: raise user_not_found_exception
def is_player_in_game_by_email(user_email: EmailStr, game_id: int): ''' Check if the player is in the game with its email ''' user_joining = db_user.get_user_by_email(email=user_email) game = db_game.get_game_by_id(game_id=game_id) for player in game.players: if player.user == user_joining: return 1 return 0
def check_create_conditions(user_email: EmailStr, name: str, min_players: int, max_players: int): ''' Check the conditions to create a game ''' user = db_user.get_user_by_email(user_email) if not user: raise user_not_found_exception if not len(name.strip()): raise all_spaces_exception if min_players < 5: raise less_than_five_players_exception if max_players < min_players: raise incoherent_amount_of_players_exception if max_players > 10: raise more_than_ten_players_exception
def put_new_player_in_game(user: EmailStr, game_id: int): ''' Create a new player and join a game ''' game = db_game.get_game_by_id(game_id=game_id) creator = db_user.get_user_by_email(email=user) new_player = Player(user=creator, is_alive=True, game_in=game, chat_enabled=True, is_investigated=False, turn=game.players.count() + 1) commit() creator.playing_in.add(Player[new_player.id]) game.players.add(Player[new_player.id]) return new_player.id
def check_join_conditions(game_id: int, user_email: EmailStr): ''' Check the conditions to join a game ''' game = get_game_by_id(game_id=game_id) if not game: raise game_not_found_exception user = db_user.get_user_by_email(email=user_email) if not user: raise user_not_found_exception if db_player.is_player_in_game_by_email(user_email, game_id): raise player_already_in_game_exception if game.players.count() >= game.max_players: raise max_players_reach_exception if game.state == 1: raise game_has_started_exception if game.state == 2: raise game_has_finished_exception
def save_new_game(owner: EmailStr, name: str, min_players: int, max_players: int): ''' Create a new game with its board ''' owner = db_user.get_user_by_email(email=owner) game = Game(owner=owner, name=name, creation_date=datetime.datetime.today(), state=0, chaos=False, min_players=min_players, max_players=max_players, end_game_notified=[]) board = Board(game=game, fenix_promulgation=0, death_eater_promulgation=0, election_counter=0, spell_available=False) commit() return game.id
async def get_user_icon_by_email(email: EmailStr): try: user = db_user.get_user_by_email(email) return user.icon except: raise user_not_found_exception