From 12b84a7ab96532ce64261791647b48b8edff7b6a Mon Sep 17 00:00:00 2001 From: Alberto Xamin Date: Sun, 22 Nov 2020 18:08:40 +0100 Subject: [PATCH] victory conditions --- backend/game.py | 47 +++++++++++++----------------- backend/roles.py | 38 +++++++++++++++--------- frontend/src/components/Player.vue | 3 ++ 3 files changed, 49 insertions(+), 39 deletions(-) diff --git a/backend/game.py b/backend/game.py index 2f78057..31e4f9e 100644 --- a/backend/game.py +++ b/backend/game.py @@ -21,27 +21,6 @@ class Game: self.readyCount = 0 self.waiting_for = 0 - def handle_disconnect(self, player: players.Player): - print(f'player {player.name} left the game {self.name}') - index = self.players.index(player) - for c in player.hand: - self.deck.scrap(c) - for c in player.equipment: - self.deck.scrap(c) - died_in_his_turn = self.started and index == self.turn - if self.started and index < self.turn: - self.turn -= 1 - self.players.pop(index) - if len(self.players) == 0: - print(f'no players left in game {self.name}') - return True - self.sio.emit('room', room=self.name, data={'name': self.name, 'started': self.started, 'players': [p.name for p in self.players]}) - self.sio.emit('chat_message', room=self.name, data=f'{player.name} si è disconnesso.') - self.players_map = {c.name: i for i, c in enumerate(self.players)} - if died_in_his_turn: - self.next_turn() - return False - def add_player(self, player: players.Player): if player in self.players: return @@ -70,6 +49,7 @@ class Game: self.sio.emit('start', room=self.name) self.started = True self.deck = Deck(self) + self.initial_players = len(self.players) self.choose_characters() def distribute_roles(self): @@ -185,6 +165,14 @@ class Game: print('scrap') self.sio.emit('scrap', room=self.name, data=self.deck.peek_scrap_pile().__dict__) + def handle_disconnect(self, player: players.Player): + print(f'player {player.name} left the game {self.name}') + self.player_death(player=player) + if len(self.players) == 0: + print(f'no players left in game {self.name}') + return True + else: return False + def player_death(self, player: players.Player): print(f'player {player.name} died') for c in player.hand: @@ -192,18 +180,25 @@ class Game: for c in player.equipment: self.deck.scrap(c) index = self.players.index(player) - died_in_his_turn = index == self.turn - if index <= self.turn: + died_in_his_turn = self.started and index == self.turn + if self.started and index <= self.turn: self.turn -= 1 self.players.pop(index) - if len(self.players) == 0: - print(f'no players left in game {self.name}') - return True self.sio.emit('room', room=self.name, data={'name': self.name, 'started': self.started, 'players': [p.name for p in self.players]}) self.sio.emit('chat_message', room=self.name, data=f'{player.name} è morto.') for p in self.players: p.notify_self() self.players_map = {c.name: i for i, c in enumerate(self.players)} + if self.started: + print('Check win status') + winners = [p for p in self.players if p.role != None and p.role.on_player_death(self.players, initial_players=self.initial_players)] + if len(winners) > 0: + print('WE HAVE A WINNER') + for p in self.players: + p.win_status = p in winners + p.notify_self() + return + if died_in_his_turn: self.next_turn() diff --git a/backend/roles.py b/backend/roles.py index 846937a..4f44ea0 100644 --- a/backend/roles.py +++ b/backend/roles.py @@ -8,7 +8,7 @@ class Role(ABC): self.health_mod = health_mod @abstractmethod - def on_player_death(self, alive_players: list): + def on_player_death(self, alive_players: list, initial_players: int): pass class Sheriff(Role): @@ -17,10 +17,13 @@ class Sheriff(Role): self.max_players = 1 self.icon = '⭐️' - def on_player_death(self, alive_players: list): - if not any([isinstance(p.role) == Outlaw or isinstance(p.role) == Renegade for p in alive_players]): + def on_player_death(self, alive_players: list, initial_players: int): + if initial_players == 3 and len(alive_players) == 1: + return True + elif initial_players != 3 and not any([isinstance(p.role, Outlaw) or isinstance(p.role, Renegade) for p in alive_players]): print("The Sheriff won!") - pass + return True + return False class Vice(Role): @@ -29,10 +32,13 @@ class Vice(Role): self.max_players = 2 self.icon = '🎖' - def on_player_death(self, alive_players: list): - if not any([isinstance(p.role) == Outlaw or isinstance(p.role) == Renegade for p in alive_players]): + def on_player_death(self, alive_players: list, initial_players: int): + if initial_players == 3 and len(alive_players) == 1: + return True + elif initial_players != 3 and not any([isinstance(p.role, Outlaw) or isinstance(p.role, Renegade) for p in alive_players]): print("The Vice won!") - pass + return True + return False class Outlaw(Role): def __init__(self): @@ -40,10 +46,13 @@ class Outlaw(Role): self.max_players = 3 self.icon = '🐺' - def on_player_death(self, alive_players: list): - if not any([isinstance(p.role) == Sheriff for p in alive_players]): + def on_player_death(self, alive_players: list, initial_players: int): + if initial_players == 3 and len(alive_players) == 1: + return True + elif initial_players != 3 and not any([isinstance(p.role, Sheriff) for p in alive_players]): print("The Outlaw won!") - pass + return True + return False class Renegade(Role): def __init__(self): @@ -51,7 +60,10 @@ class Renegade(Role): self.max_players = 1 self.icon = '🦅' - def on_player_death(self, alive_players: list): - if len(alive_players) == 1 and isinstance(alive_players[0]) == Renegade: + def on_player_death(self, alive_players: list, initial_players: int): + if initial_players == 3 and len(alive_players) == 1: + return True + elif initial_players != 3 and len(alive_players) == 1 and isinstance(alive_players[0], Renegade): print("The Renegade won!") - pass + return True + return False diff --git a/frontend/src/components/Player.vue b/frontend/src/components/Player.vue index 183fe9c..30d959e 100644 --- a/frontend/src/components/Player.vue +++ b/frontend/src/components/Player.vue @@ -26,6 +26,7 @@ + @@ -60,6 +61,7 @@ export default { expected_response: null, shouldChooseCard: false, available_cards: [], + win_status: undefined, }), sockets: { role(role) { @@ -78,6 +80,7 @@ export default { this.is_my_turn = self.is_my_turn this.expected_response = self.expected_response this.available_cards = self.available_cards + this.win_status = self.win_status if (this.pending_action == 5 && self.target_p) { this.chooseCardFromPlayer(self.target_p) } else if (this.pending_action == 5) {