async def join_lobby(request: Request, join_lobby_request: JoinLobbyRequest): if not request.user.is_authenticated: raise HTTPException(status_code=401) if not can_join_lobby(User(username=request.user.username)): raise HTTPException(status_code=400, detail="Already in a lobby") lobby = lobbies.get(join_lobby_request.lobby_id) if not lobby: raise HTTPException(status_code=400, detail="Lobby does not exist") lobby.players.append(User(username=request.user.username)) lobby.occupy += 1 return JoinLobbyResponse(lobby=lobby, success=True)
async def create_lobby(request: Request): if not request.user.is_authenticated: raise HTTPException(status_code=401) user = User(username=request.user.username) if not can_join_lobby(User(username=request.user.username)): raise HTTPException(status_code=400, detail="Already in a lobby") lobby_id = uuid.uuid4().hex[:6].upper() lobby = Lobby( id=lobby_id, size=9, occupy=1, host=user, players=[user], ) lobbies[lobby_id] = lobby return CreateLobbyResponse(lobby=lobby)
def game_success_full_attack(game: Game): game.last_action = Action( action_type=Action.ActionType.CALL_FOR_AN_ATTACK, action_data=CaptainCallForAttackData( which_captain=User(username=game.get_jr_caption()), state=State.Success, from_other_ship=False)) return game
async def leave_lobby(request: Request, leave_lobby_request: LeaveLobbyRequest): if not request.user.is_authenticated: raise HTTPException(status_code=401) lobby = lobbies.get(leave_lobby_request.lobby_id) if not lobby: raise HTTPException(status_code=400, detail="Lobby does not exist") user = User(username=request.user.username) if user not in lobby.players: raise HTTPException(status_code=400) lobby_service.leave_current_lobby(lobby.id, user)
async def start_game(request: Request, start_game_request: StartGameRequest): if not request.user.is_authenticated: raise HTTPException(status_code=401) lobby = lobbies.get(start_game_request.lobby_id) if not lobby: raise HTTPException(status_code=400, detail="Lobby does not exist") user = User(username=request.user.username) if lobby.host != user: raise HTTPException(status_code=403, detail="You cannot start this game") if len(lobby.players) < 2: raise HTTPException(status_code=400, detail="Not enough players") lobby.game_started = True create_new_game(lobby.id, [user.username for user in lobby.players], lobby.host.username)
async def my_lobby(request: Request): if not request.user.is_authenticated: raise HTTPException(status_code=401) found_lobby = None can_start = False has_lobby = False user = User(username=request.user.username) for lobby in lobbies.values(): if user in lobby.players: found_lobby = lobby can_start = ((user == lobby.host) and (len(lobby.players) >= 2)) if lobby.game_started is False: has_lobby = True break return MyLobbyResponse(lobby=found_lobby, can_start=can_start, has_lobby=has_lobby)
def generate_game_schema_from_game(username: str): game = get_player_game(username) player_info = get_player_info_in_game(game, username) game_status = game_schema.GameStatus( players_position=game.players_position, chests_position=game_schema.Chests( tr_en=game.chests_position.tr_en, tr_fr=game.chests_position.tr_fr, fd_fr=game.chests_position.fd_fr, fd_en=game.chests_position.fd_en, jr_fr=game.chests_position.jr_fr, jr_en=game.chests_position.jr_en, sg_nt=game.chests_position.sg_nt, ), player_game_info=game_schema.PlayerGameInfo( team=player_info.team, vote_cards=player_info.vote_cards, event_cards=get_kept_event_cards(player_info, game), seen_event_cards=player_info.seen_event_cards, role=None, available_actions=_get_available_actions(player_info, game), chests=player_info.chests ), event_cards_deck=game_schema.EventCardDeck( count=game.get_event_cards_deck_count(), selectable_cards=game.get_event_card_deck_selectable_cards() ), last_action=game.last_action, activities=game.activities, is_over=game.is_over, turn=User(username=game.turn), ) if game.winner: game_status.winner = game_schema.WinState( winner_team=game.winner, players_teams={ player: info.team for player, info in game.players_info.items() } ) return game_status