Skip to content

Commit

Permalink
Merge pull request #57 from TukiLaCosa:feature/LC-213/new-finalizar-p…
Browse files Browse the repository at this point in the history
…artida

Feature/LC-213/new-finalizar-partida

# Cambios en finalizar partida.

* Se agregó el endpoint para que la cosa pueda declarar la finalización de la partida. "/{game_name}/the-thing-end-game". El endpoint finaliza la partida y envía el evento GAME_ENDED por websocket.

* Se modifico el endpoint " /{game_name}/result" para agregarle el caso en el que la cosa se declara ganador erroneamente.

* Se modificó la finalización automática para que esta se realice al eliminar a la cosa o que la cosa sea el único jugador no eliminado.


Falta agregar una cosa más que está en las reglas para cuando sucede que se infectan todos los jugadores vivos restantes y se tiene que agregar a la lista de perdedores al último jugador infectado. 

Co-authored-by: anelioalvarez <anelio272014@gmail.com>
  • Loading branch information
ezeluduena and anelioalvarez authored Nov 8, 2023
2 parents 7e0a0de + fbfe143 commit acbb683
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 38 deletions.
6 changes: 3 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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()
13 changes: 13 additions & 0 deletions app/routers/games/games.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
26 changes: 21 additions & 5 deletions app/routers/games/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down Expand Up @@ -415,20 +423,28 @@ 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 == PlayerRol.HUMAN)[:]
losers = game.players.select(
lambda p: p.rol != PlayerRol.THE_THING)[:]
lambda p: p.rol != PlayerRol.HUMAN)[:]

return GameResult(
reason=reason,
Expand Down
25 changes: 23 additions & 2 deletions app/routers/games/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down
56 changes: 28 additions & 28 deletions app/utils.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit acbb683

Please sign in to comment.