From 3d8732a7174f7ffa9fbf3c4775dedc83007d7e21 Mon Sep 17 00:00:00 2001 From: ezeluduena Date: Tue, 7 Nov 2023 12:35:25 -0300 Subject: [PATCH 1/5] Finalizado de partida actualizado para que la cosa declare la victoria --- app/routers/games/games.py | 13 +++++++++++++ app/routers/games/services.py | 30 ++++++++++++++++++++++++------ app/routers/games/utils.py | 26 ++++++++++++++++++++++++-- 3 files changed, 61 insertions(+), 8 deletions(-) diff --git a/app/routers/games/games.py b/app/routers/games/games.py index e2b0a7c..717f177 100644 --- a/app/routers/games/games.py +++ b/app/routers/games/games.py @@ -346,3 +346,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.post("/{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_BY_THE_THING + } + 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 f646270..8df612e 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,20 +423,30 @@ 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( lambda p: p.rol in [PlayerRol.THE_THING, PlayerRol.INFECTED])[:] 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 in [PlayerRol.HUMAN, PlayerRol.INFECTED])[:] 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 ffb429f..bd69d36 100644 --- a/app/routers/games/utils.py +++ b/app/routers/games/utils.py @@ -20,6 +20,7 @@ class Events(str, Enum): GAME_STARTED = 'game_started' GAME_CANCELED = 'game_canceled' GAME_ENDED = 'game_ended' + GAME_ENDED_BY_THE_THING = 'game_ended_by_the_thing' PLAYER_JOINED = 'player_joined' PLAYER_LEFT = 'player_left' PLAYER_INIT_HAND = 'player_init_hand' @@ -141,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 @@ -166,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( From e17951209a6c76dc95d9c74c951b650962192fb8 Mon Sep 17 00:00:00 2001 From: ezeluduena Date: Tue, 7 Nov 2023 12:53:15 -0300 Subject: [PATCH 2/5] hotfix --- app/routers/games/games.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/routers/games/games.py b/app/routers/games/games.py index 717f177..62825bf 100644 --- a/app/routers/games/games.py +++ b/app/routers/games/games.py @@ -348,7 +348,7 @@ async def play_defense_card(game_name: str, defense_info: PlayDefenseInformation clean_intention_in_game(game_name) -@router.post("/{game_name}/the-thing-end-game") +@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) From 2a7f7c499aafc13cdc0592a739a5616017bf95d1 Mon Sep 17 00:00:00 2001 From: ezeluduena Date: Tue, 7 Nov 2023 19:48:33 -0300 Subject: [PATCH 3/5] =?UTF-8?q?actualizaci=C3=B3n=20sobre=20los=20eventos.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/routers/games/games.py | 2 +- app/routers/games/utils.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/app/routers/games/games.py b/app/routers/games/games.py index 62825bf..2c90703 100644 --- a/app/routers/games/games.py +++ b/app/routers/games/games.py @@ -354,7 +354,7 @@ async def the_thing_end_game(game_name: str, player_id: int): services.finish_game_by_the_thing(game_name) json_msg = { - "event": utils.Events.GAME_ENDED_BY_THE_THING + "event": utils.Events.GAME_ENDED } await player_connections.send_event_to_all_players_in_game(game_name, json_msg) diff --git a/app/routers/games/utils.py b/app/routers/games/utils.py index bd69d36..af55d02 100644 --- a/app/routers/games/utils.py +++ b/app/routers/games/utils.py @@ -20,7 +20,6 @@ class Events(str, Enum): GAME_STARTED = 'game_started' GAME_CANCELED = 'game_canceled' GAME_ENDED = 'game_ended' - GAME_ENDED_BY_THE_THING = 'game_ended_by_the_thing' PLAYER_JOINED = 'player_joined' PLAYER_LEFT = 'player_left' PLAYER_INIT_HAND = 'player_init_hand' From 8b0fa1d093377ce5afa9b08b8bd0047c9aa851ef Mon Sep 17 00:00:00 2001 From: ezeluduena Date: Tue, 7 Nov 2023 20:02:22 -0300 Subject: [PATCH 4/5] autopep8 --- app/routers/games/games.py | 2 +- app/routers/games/services.py | 10 ++++------ app/routers/games/utils.py | 2 +- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/app/routers/games/games.py b/app/routers/games/games.py index 2c90703..0967215 100644 --- a/app/routers/games/games.py +++ b/app/routers/games/games.py @@ -352,7 +352,7 @@ async def play_defense_card(game_name: str, defense_info: PlayDefenseInformation 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 } diff --git a/app/routers/games/services.py b/app/routers/games/services.py index 8df612e..8cd8d49 100644 --- a/app/routers/games/services.py +++ b/app/routers/games/services.py @@ -430,21 +430,19 @@ def get_game_result(name: str) -> GameResult: 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( lambda p: p.rol in [PlayerRol.THE_THING, PlayerRol.INFECTED])[:] losers = game.players.select( lambda p: p.rol == PlayerRol.ELIMINATED)[:] - - - - #elif the_thing_declared_a_wrong_victory(game): + + # 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 in [PlayerRol.HUMAN, PlayerRol.INFECTED])[:] + lambda p: p.rol == PlayerRol.HUMAN)[:] losers = game.players.select( lambda p: p.rol != PlayerRol.HUMAN)[:] diff --git a/app/routers/games/utils.py b/app/routers/games/utils.py index af55d02..58a626d 100644 --- a/app/routers/games/utils.py +++ b/app/routers/games/utils.py @@ -150,7 +150,7 @@ def verify_player_is_the_thing(player_id, game_name): 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): From fbfe143c0fc26c40cbfd28066915fb01f97caee2 Mon Sep 17 00:00:00 2001 From: ezeluduena Date: Wed, 8 Nov 2023 15:54:24 -0300 Subject: [PATCH 5/5] =?UTF-8?q?se=20coment=C3=B3=20pygame=20para=20evitar?= =?UTF-8?q?=20warnings=20en=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/main.py | 6 +++--- app/utils.py | 56 ++++++++++++++++++++++++++-------------------------- 2 files changed, 31 insertions(+), 31 deletions(-) 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/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()