implemented duel

This commit is contained in:
Alberto Xamin 2020-11-22 15:03:34 +01:00
parent 89a3a4e4d6
commit f65f46528e
No known key found for this signature in database
GPG Key ID: 4F026F48309500A2
3 changed files with 50 additions and 17 deletions

View File

@ -1,6 +1,8 @@
import cards
from typing import List, Set, Dict, Tuple, Optional from typing import List, Set, Dict, Tuple, Optional
import random import random
import socketio import socketio
from cards import Bang
import players import players
from characters import all_characters from characters import all_characters
from deck import Deck from deck import Deck
@ -95,15 +97,19 @@ class Game:
} for j in range(len(self.players)) if i != j] } for j in range(len(self.players)) if i != j]
def attack(self, attacker:Player, target_username:str): 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(): if self.players[self.players_map[target_username]].get_banged():
attacker.pending_action = players.PendingAction.WAIT attacker.pending_action = players.PendingAction.WAIT
attacker.notify_self() 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): def get_player_named(self, name:str):
return self.players[self.players_map[name]] 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].pending_action = players.PendingAction.PLAY
self.players[self.turn].notify_self() self.players[self.turn].notify_self()

View File

@ -35,8 +35,10 @@ class Player:
self.available_characters = [] self.available_characters = []
self.was_shot = False self.was_shot = False
self.on_pick_cb = None 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.expected_response = None
self.attacker = None
self.target_p: str = None self.target_p: str = None
def join_game(self, game): def join_game(self, game):
@ -78,8 +80,9 @@ class Player:
ser.pop('sio') ser.pop('sio')
ser.pop('sid') ser.pop('sid')
ser.pop('on_pick_cb') ser.pop('on_pick_cb')
ser.pop('on_response_cb') ser.pop('on_failed_response_cb')
ser.pop('expected_response') # 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', 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.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() self.game.notify_all()
@ -211,7 +214,7 @@ class Player:
self.hand.append(self.game.deck.draw()) self.hand.append(self.game.deck.draw())
if isinstance(card, cards.Duello): if isinstance(card, cards.Duello):
self.sio.emit('chat_message', room=self.game.name, data=f'{self.name} ha giocato {card.name} contro {againts}.') 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): if isinstance(card, cards.Emporio):
self.sio.emit('chat_message', room=self.game.name, data=f'{self.name} ha giocato {card.name}.') self.sio.emit('chat_message', room=self.game.name, data=f'{self.name} ha giocato {card.name}.')
pass pass
@ -272,17 +275,18 @@ class Player:
if picked.suit == cards.Suit.HEARTS: if picked.suit == cards.Suit.HEARTS:
self.notify_self() self.notify_self()
self.game.responders_did_respond() self.game.responders_did_respond()
self.attacker = None
return return
if len([c for c in self.hand if isinstance(c, cards.Mancato)]) == 0: if len([c for c in self.hand if isinstance(c, cards.Mancato)]) == 0:
self.take_damage_response() self.take_damage_response()
self.game.responders_did_respond() self.game.responders_did_respond()
else: else:
self.pending_action = PendingAction.RESPOND self.pending_action = PendingAction.RESPOND
self.expected_response = cards.Mancato self.expected_response = cards.Mancato(0,0).name
self.on_response_cb = self.take_damage_response self.on_failed_response_cb = self.take_damage_response
self.notify_self() 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: 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') print('Cant defend')
self.take_damage_response() self.take_damage_response()
@ -295,24 +299,45 @@ class Player:
else: else:
print('has mancato') print('has mancato')
self.pending_action = PendingAction.RESPOND self.pending_action = PendingAction.RESPOND
self.expected_response = cards.Mancato self.expected_response = cards.Mancato(0,0).name
self.on_response_cb = self.take_damage_response self.on_failed_response_cb = self.take_damage_response
self.notify_self() self.notify_self()
return True 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): def take_damage_response(self):
self.lives -= 1 self.lives -= 1
self.attacker = None
self.notify_self() self.notify_self()
def respond(self, hand_index): def respond(self, hand_index):
self.pending_action = PendingAction.WAIT 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.game.deck.scrap(self.hand.pop(hand_index))
self.notify_self() 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: else:
self.on_response_cb() self.on_failed_response_cb()
self.game.responders_did_respond() self.game.responders_did_respond_resume_turn()
self.attacker = None
def get_sight(self): def get_sight(self):
aim = 0 aim = 0

View File

@ -55,6 +55,7 @@ export default {
has_played_bang: false, has_played_bang: false,
visiblePlayers: [], visiblePlayers: [],
is_my_turn: false, is_my_turn: false,
expected_response: null,
}), }),
sockets: { sockets: {
role(role) { role(role) {
@ -71,6 +72,7 @@ export default {
this.max_lives = self.max_lives this.max_lives = self.max_lives
this.has_played_bang = self.has_played_bang this.has_played_bang = self.has_played_bang
this.is_my_turn = self.is_my_turn this.is_my_turn = self.is_my_turn
this.expected_response = self.expected_response
if (this.pending_action == 5) { if (this.pending_action == 5) {
this.chooseCardFromPlayer(self.target_p) this.chooseCardFromPlayer(self.target_p)
} }
@ -103,7 +105,7 @@ export default {
icon: '❌', icon: '❌',
is_equipment: true, 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) cc.push(x)
}) })
return cc return cc