def test_give_cards_to_player(pasur_with_two_players: Pasur): player = pasur_with_two_players.player_in_turn for _ in range(52): pasur_with_two_players.give_card_from_deck(to_card_holder=player) assert player.card_count == 52 assert pasur_with_two_players.deck.card_count == 0 with pytest.raises(Exception): pasur_with_two_players.give_card_from_deck(to_card_holder=player)
def test_play_card_no_collect(pasur_with_two_players: Pasur): player = pasur_with_two_players.players[0] with patch_card_pop_sequence(pasur=pasur_with_two_players, sequence=[ 7, 1, 9, 2, # player 1 4, 5, 6, 8, # player 2 3, 14, 27, 40, # board ]): pasur_with_two_players.deal_cards() pasur_with_two_players.play_card( player=player, card=player.list_all_cards()[0], )
def test_play_card_collect_3_8(pasur_with_two_players: Pasur): player = pasur_with_two_players.players[0] with patch_card_pop_sequence(pasur=pasur_with_two_players, sequence=[ 7, 1, 9, 3, # player 1 4, 5, 6, 8, # player 2 2, 14, 27, 40, # board ]): pasur_with_two_players.deal_cards() pasur_with_two_players.play_card( player=player, card=player.list_all_cards()[0], collect_cards=[pasur_with_two_players.board.list_all_cards()[0]] ) assert player.card_count_collected() == 2 assert Card(7) in [c for c in player.list_all_cards() if c.collected] assert Card(2) in [c for c in player.list_all_cards() if c.collected]
def to_python(self, value): value = super(GameField, self).to_python(value) if isinstance(value, Deck): return value if value is None: return value return Pasur.load_json(value)
def test_play_card_collect_3_6_2(pasur_with_two_players: Pasur): player = pasur_with_two_players.players[0] with patch_card_pop_sequence(pasur=pasur_with_two_players, sequence=[ 2, 7, 9, 3, # player 1 4, 27, 6, 8, # player 2 5, 1, 25, 40, # board ]): pasur_with_two_players.deal_cards() pasur_with_two_players.play_card( player=player, card=player.list_all_cards()[0], collect_cards=pasur_with_two_players.board.list_all_cards()[0:2] ) assert player.card_count_collected() == 3 assert Card(1) in [c for c in player.list_all_cards() if c.collected] assert Card(2) in [c for c in player.list_all_cards() if c.collected] assert Card(5) in [c for c in player.list_all_cards() if c.collected]
def __init__(self, *args, **kwargs): super(Game, self).__init__(*args, **kwargs) if self.id is None and self.pasur is None: self.pasur: Pasur = Pasur.create_new_game() for player in self.match.game_players.order_by( 'created'): # type: MatchPlayer self.pasur.add_player(player=player.player.get_pasur_player()) self.pasur.status = self.status
def game_status(self, event): message = event['message'] pasur = Pasur.load_json(event['pasur']) player_points = event['player_points'] # Send message to WebSocket self.send(text_data=json.dumps({ 'game_status': self.get_game_status(pasur=pasur, player_points=player_points), 'message': escape(message), }))
def test_count_points(pasur_with_two_players_all_cards_played: Pasur): pasur_with_two_players_all_cards_played.surs.append(pasur_with_two_players_all_cards_played.players[0]) pasur_with_two_players_all_cards_played.surs.append(pasur_with_two_players_all_cards_played.players[0]) pasur_with_two_players_all_cards_played.surs.append(pasur_with_two_players_all_cards_played.players[0]) pasur_with_two_players_all_cards_played.surs.append(pasur_with_two_players_all_cards_played.players[1]) pasur_with_two_players_all_cards_played.surs.append(pasur_with_two_players_all_cards_played.players[1]) player_points = pasur_with_two_players_all_cards_played.count_points() assert player_points[pasur_with_two_players_all_cards_played.players[0].identifier] == ( CLUBS_WIN_POINT + DIAMONDS_TEN_POINT + 2*ACE_POINT + 2*JACK_POINT + SUR_POINT ) assert player_points[pasur_with_two_players_all_cards_played.players[1].identifier] == ( 2 * ACE_POINT + 2 * JACK_POINT + CLUBS_TWO_POINT )
def test_deal_cards_to_player(pasur_with_two_players: Pasur): pasur_with_two_players.deal_cards() assert len(pasur_with_two_players.board.list_all_cards()) == 4 assert pasur_with_two_players.deck.card_count == 52-4-4*len(pasur_with_two_players.players) for player in pasur_with_two_players.players: assert player.card_count == 4
def pasur_with_two_players(pasur: Pasur): pasur.add_player(Player('1')) pasur.add_player(Player('2')) return pasur
def pasur(): return Pasur.create_new_game()
def get_prep_value(self, value: Pasur): return super(GameField, self).get_prep_value(value.dump_json())
def receive(self, text_data): text_data_json = json.loads(text_data) # message = text_data_json['message'] player_action_name = text_data_json['player_action'] with transaction.atomic(): game = self.match.get_latest_game(select_for_update=True) try: if player_action_name == PlayerActions.deal_cards: game.pasur.deal_cards() game.save() message = 'Cards dealed' elif player_action_name == PlayerActions.play_card: player_ch: PlayerCardHolder = game.pasur.card_holders.get( self.player.name) played_card = player_ch.get( card_id=text_data_json['played_card']) collected_cards = [ game.pasur.board.get(card_id) for card_id in text_data_json['collect_cards'] ] game.pasur.play_card(player=player_ch, card=played_card, collect_cards=collected_cards) game.save() message = ('Player played card {played_card}'.format( played_card=played_card) + (' and picked up {collected_cards}'.format( collected_cards=', '.join( [f"{card}" for card in collected_cards])) if collected_cards else '')) elif player_action_name == PlayerActions.count_points: player_points = game.pasur.count_points() game.save() for player_identifier, score in player_points.items(): models.GameMatchPlayerScore.objects.get_or_create( game=game, match_player=models.MatchPlayer.objects.get( match=self.match, player__name=player_identifier), score=score) message = 'Counted points: {}'.format(player_points) elif player_action_name == PlayerActions.next_game: if game.status not in [STATUS.finished, STATUS.cancelled]: raise PasurIllegalAction( 'Cannot go to next game before this game is finished or cancelled' ) new_pasur = Pasur.create_new_game() new_pasur.starter = game.pasur.players_in_play_order[1] game = models.Game(match=self.match, pasur=new_pasur) game.save() message = 'New game started' else: message = "UNKNOWN ACTION" # Send message to room group self.push_to_group( message=message, game=game, ) except PasurIllegalAction as e: message = 'ERROR ({}): {}'.format(self.player.name, e) self.send(text_data=json.dumps({'message': escape(message)}))