From f65f46528e1ddd034829339930bded52dc684da7 Mon Sep 17 00:00:00 2001 From: Alberto Xamin Date: Sun, 22 Nov 2020 15:03:34 +0100 Subject: [PATCH] implemented duel --- backend/game.py | 10 ++++-- backend/players.py | 53 ++++++++++++++++++++++-------- frontend/src/components/Player.vue | 4 ++- 3 files changed, 50 insertions(+), 17 deletions(-) diff --git a/backend/game.py b/backend/game.py index ab3c16d..48f2038 100644 --- a/backend/game.py +++ b/backend/game.py @@ -1,6 +1,8 @@ +import cards from typing import List, Set, Dict, Tuple, Optional import random import socketio +from cards import Bang import players from characters import all_characters from deck import Deck @@ -95,15 +97,19 @@ class Game: } for j in range(len(self.players)) if i != j] def attack(self, attacker:Player, target_username:str): - self.sio.emit('chat_message', room=self.name, data=f'{attacker.name} ha fatto Bang contro {target_username}.') if self.players[self.players_map[target_username]].get_banged(): attacker.pending_action = players.PendingAction.WAIT attacker.notify_self() + def duel(self, attacker:Player, target_username:str): + if self.players[self.players_map[target_username]].get_dueled(attacker=attacker): + attacker.pending_action = players.PendingAction.WAIT + attacker.notify_self() + def get_player_named(self, name:str): return self.players[self.players_map[name]] - def responders_did_respond(self): + def responders_did_respond_resume_turn(self): self.players[self.turn].pending_action = players.PendingAction.PLAY self.players[self.turn].notify_self() diff --git a/backend/players.py b/backend/players.py index 5ac51b3..e43d508 100644 --- a/backend/players.py +++ b/backend/players.py @@ -35,8 +35,10 @@ class Player: self.available_characters = [] self.was_shot = False self.on_pick_cb = None - self.on_response_cb = None + self.on_failed_response_cb = None + self.event_type: str = None self.expected_response = None + self.attacker = None self.target_p: str = None def join_game(self, game): @@ -78,8 +80,9 @@ class Player: ser.pop('sio') ser.pop('sid') ser.pop('on_pick_cb') - ser.pop('on_response_cb') - ser.pop('expected_response') + ser.pop('on_failed_response_cb') + # ser.pop('expected_response') + ser.pop('attacker') self.sio.emit('self', room=self.sid, data=json.dumps(ser, default=lambda o: o.__dict__)) self.sio.emit('self_vis', room=self.sid, data=json.dumps(self.game.get_visible_players(self), default=lambda o: o.__dict__)) self.game.notify_all() @@ -211,7 +214,7 @@ class Player: self.hand.append(self.game.deck.draw()) if isinstance(card, cards.Duello): self.sio.emit('chat_message', room=self.game.name, data=f'{self.name} ha giocato {card.name} contro {againts}.') - pass + self.game.duel(self, againts) if isinstance(card, cards.Emporio): self.sio.emit('chat_message', room=self.game.name, data=f'{self.name} ha giocato {card.name}.') pass @@ -272,17 +275,18 @@ class Player: if picked.suit == cards.Suit.HEARTS: self.notify_self() self.game.responders_did_respond() + self.attacker = None return if len([c for c in self.hand if isinstance(c, cards.Mancato)]) == 0: self.take_damage_response() self.game.responders_did_respond() else: self.pending_action = PendingAction.RESPOND - self.expected_response = cards.Mancato - self.on_response_cb = self.take_damage_response + self.expected_response = cards.Mancato(0,0).name + self.on_failed_response_cb = self.take_damage_response self.notify_self() - def get_banged(self): + def get_banged(self, attacker = None): if len([c for c in self.hand if isinstance(c, cards.Mancato)]) == 0 and len([c for c in self.equipment if isinstance(c, cards.Barile)]) == 0: print('Cant defend') self.take_damage_response() @@ -295,24 +299,45 @@ class Player: else: print('has mancato') self.pending_action = PendingAction.RESPOND - self.expected_response = cards.Mancato - self.on_response_cb = self.take_damage_response + self.expected_response = cards.Mancato(0,0).name + self.on_failed_response_cb = self.take_damage_response self.notify_self() return True - + + def get_dueled(self, attacker): + if len([c for c in self.hand if isinstance(c, cards.Bang)]) == 0: + print('Cant defend') + self.take_damage_response() + self.game.responders_did_respond_resume_turn() + return False + else: + self.attacker = attacker + self.pending_action = PendingAction.RESPOND + self.expected_response = cards.Bang(0,0).name + self.event_type = 'duel' + self.on_failed_response_cb = self.take_damage_response + self.notify_self() + return True + def take_damage_response(self): self.lives -= 1 + self.attacker = None self.notify_self() def respond(self, hand_index): self.pending_action = PendingAction.WAIT - if hand_index != -1 and isinstance(self.hand[hand_index], self.expected_response): + if hand_index != -1 and self.hand[hand_index].name == self.expected_response: self.game.deck.scrap(self.hand.pop(hand_index)) self.notify_self() - self.game.responders_did_respond() + if self.event_type == 'duel': + self.game.duel(self, self.attacker.name) + else: + self.game.responders_did_respond_resume_turn() + self.event_type = '' else: - self.on_response_cb() - self.game.responders_did_respond() + self.on_failed_response_cb() + self.game.responders_did_respond_resume_turn() + self.attacker = None def get_sight(self): aim = 0 diff --git a/frontend/src/components/Player.vue b/frontend/src/components/Player.vue index 626768f..b8dee47 100644 --- a/frontend/src/components/Player.vue +++ b/frontend/src/components/Player.vue @@ -55,6 +55,7 @@ export default { has_played_bang: false, visiblePlayers: [], is_my_turn: false, + expected_response: null, }), sockets: { role(role) { @@ -71,6 +72,7 @@ export default { this.max_lives = self.max_lives this.has_played_bang = self.has_played_bang this.is_my_turn = self.is_my_turn + this.expected_response = self.expected_response if (this.pending_action == 5) { this.chooseCardFromPlayer(self.target_p) } @@ -103,7 +105,7 @@ export default { icon: '❌', is_equipment: true, }] - this.hand.filter(x => x.name == 'Mancato!').forEach(x=>{ + this.hand.filter(x => x.name == this.expected_response).forEach(x=>{ cc.push(x) }) return cc