diff --git a/app/main.py b/app/main.py index d410539..ee6cbb8 100644 --- a/app/main.py +++ b/app/main.py @@ -5,8 +5,8 @@ from app.routers.games import games from app.routers.cards import cards from app.routers.websockets import websockets -from .utils import show_initial_image -import threading +# from .utils import show_initial_image +# import threading app = FastAPI() @@ -26,5 +26,5 @@ app.include_router(websockets.router) # This displays the initial image with the sound -t = threading.Thread(target=show_initial_image) +# t = threading.Thread(target=show_initial_image) # t.start() diff --git a/app/routers/games/games.py b/app/routers/games/games.py index 6d0b9a7..7200d76 100644 --- a/app/routers/games/games.py +++ b/app/routers/games/games.py @@ -347,3 +347,16 @@ async def play_defense_card(game_name: str, defense_info: PlayDefenseInformation process_intention_in_game(game_name) clean_intention_in_game(game_name) + + +@router.patch("/{game_name}/the-thing-end-game") +async def the_thing_end_game(game_name: str, player_id: int): + utils.verify_player_is_the_thing(player_id, game_name) + services.finish_game_by_the_thing(game_name) + + json_msg = { + "event": utils.Events.GAME_ENDED + } + await player_connections.send_event_to_all_players_in_game(game_name, json_msg) + + return {"message": "The thing ended the game"} diff --git a/app/routers/games/services.py b/app/routers/games/services.py index 9d58d3e..8fdd920 100644 --- a/app/routers/games/services.py +++ b/app/routers/games/services.py @@ -253,6 +253,14 @@ async def finish_game(name: str) -> Game: return game +@db_session +async def finish_game_by_the_thing(name: str) -> Game: + game: Game = find_game_by_name(name) + game.status = GameStatus.ENDED + + return game + + @db_session def play_action_card(game_name: str, play_info: PlayInformation): result = {"message": "Action card played"} @@ -415,6 +423,14 @@ def get_game_result(name: str) -> GameResult: losers = game.players.select( lambda p: p.rol in [PlayerRol.INFECTED, PlayerRol.ELIMINATED])[:] + elif the_thing_infected_everyone(game): + reason = '''La Cosa ha logrado infectar a todos los demás jugadores + sin que haya sido eliminado ningún Humano de la partida.''' + winners = game.players.select( + lambda p: p.rol == PlayerRol.THE_THING)[:] + losers = game.players.select( + lambda p: p.rol != PlayerRol.THE_THING)[:] + elif no_human_remains(game): reason = "No queda ningún Humano en la partida." winners = game.players.select( @@ -422,13 +438,13 @@ def get_game_result(name: str) -> GameResult: losers = game.players.select( lambda p: p.rol == PlayerRol.ELIMINATED)[:] - elif the_thing_infected_everyone(game): - reason = '''La Cosa ha logrado infectar a todos los demás jugadores - sin que haya sido eliminado ningún Humano de la partida.''' + # elif the_thing_declared_a_wrong_victory(game): + else: + reason = '''La Cosa ha declarado una victoria equivocada. Todavia queda algún humano vivo.''' winners = game.players.select( - lambda p: p.rol == PlayerRol.THE_THING)[:] + lambda p: p.rol == PlayerRol.HUMAN)[:] losers = game.players.select( - lambda p: p.rol != PlayerRol.THE_THING)[:] + lambda p: p.rol != PlayerRol.HUMAN)[:] return GameResult( reason=reason, diff --git a/app/routers/games/utils.py b/app/routers/games/utils.py index d9ad64a..71aa06d 100644 --- a/app/routers/games/utils.py +++ b/app/routers/games/utils.py @@ -142,10 +142,21 @@ def verify_game_can_be_abandon(game_name: str, player_id: int): ) +@db_session +def verify_player_is_the_thing(player_id, game_name): + verify_player_in_game(player_id, game_name) + player: Player = find_player_by_id(player_id) + if player.rol != PlayerRol.THE_THING: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail=f"The player {player.name}, with id= {player.id} is not The Thing" + ) + + @db_session def verify_game_can_be_finished(game: Game): - if not (no_human_remains(game) or the_thing_is_eliminated(game)): - raise Exception('There are living Humans and The Thing') + if not (the_thing_is_eliminated(game) or all_humans_were_eliminated(game)): + raise Exception('The Thing is alive, cannot auto-finish the game') @db_session @@ -167,6 +178,16 @@ def no_human_remains(game: Game) -> bool: return the_thing_exists and number_of_humans == 0 +def all_humans_were_eliminated(game: Game) -> bool: + the_thing_exists = game.players.select( + lambda p: p.rol == PlayerRol.THE_THING).exists() + + number_of_eliminated_players = game.players.select( + lambda p: p.rol == PlayerRol.ELIMINATED).count() + + return the_thing_exists and number_of_eliminated_players == game.players.count() - 1 + + @db_session def the_thing_infected_everyone(game: Game) -> bool: the_thing_exists = game.players.select( diff --git a/app/utils.py b/app/utils.py index 91bb096..88eeed0 100644 --- a/app/utils.py +++ b/app/utils.py @@ -1,39 +1,39 @@ -import os -import pygame -import time +# import os +# import pygame +# import time -def show_initial_image(): - # Path of the image I want to display - image_path = "./app/resources/stay_away.png" +# def show_initial_image(): +# # Path of the image I want to display +# image_path = "./app/resources/stay_away.png" - # Path of the audio file - audio_path = "./app/resources/stay_away.mp3" +# # Path of the audio file +# audio_path = "./app/resources/stay_away.mp3" - # pygame configuration - pygame.init() - window = pygame.display.set_mode((400, 300), pygame.NOFRAME) +# # pygame configuration +# pygame.init() +# window = pygame.display.set_mode((400, 300), pygame.NOFRAME) - # Load the image - image = pygame.image.load(image_path) +# # Load the image +# image = pygame.image.load(image_path) - # Get the width and height of the window - window_width, window_height = window.get_size() +# # Get the width and height of the window +# window_width, window_height = window.get_size() - # Scale the image to the size of the window - image = pygame.transform.scale(image, (window_width, window_height)) +# # Scale the image to the size of the window +# image = pygame.transform.scale(image, (window_width, window_height)) - # Show the image in the window - window.blit(image, (0, 0)) - pygame.display.flip() +# # Show the image in the window +# window.blit(image, (0, 0)) +# pygame.display.flip() - # Play audio file - pygame.mixer.init() - pygame.mixer.music.load(audio_path) - pygame.mixer.music.play() +# # Play audio file +# pygame.mixer.init() +# pygame.mixer.music.load(audio_path) +# pygame.mixer.music.play() - # Wait 6 seconds - time.sleep(6) +# # Wait 6 seconds +# time.sleep(6) - # Close the window - pygame.quit() +# # Close the window +# pygame.quit()