def test_not_your_turn(self): self._get_all_events() uno_round = UnoRound() player1 = uno_round.add_player('1') player2 = uno_round.add_player('2') uno_round.start_round() card_copy = uno_round.deck.active_card player1.cards = [card_copy] player2.cards = [card_copy] uno_round.current_player_index = 0 with self.assertRaises(NotYourTurnException): uno_round.process_input( player2, uno_service.UnoService.execute_command.request( Command.PUT, Card(color=Color.RED, number=1) ) ) uno_round.process_input( player1, uno_service.UnoService.execute_command.request( Command.PUT, card_copy ) )
def test_skip_action(self): self._get_all_events() cards = list() for item in range(40): cards.append(Card(color=Color.RED, number=1)) deck = Deck(cards=cards) uno_round = UnoRound(deck=deck) uno_round.add_player('1') uno_round.add_player('2') uno_round.start_round() uno_round.process_input( uno_round.current_player, uno_service.UnoService.execute_command.request(Command.TAKE)) old_current_player_index = uno_round.current_player_index player_cards_len = len(uno_round.current_player.cards) deck_cards_len = len(uno_round.deck._cards) player = uno_round.current_player events = self._get_all_events() uno_round.process_input( uno_round.current_player, uno_service.UnoService.execute_command.request(Command.SKIP)) # current player changed self.assertNotEqual(uno_round.current_player_index, old_current_player_index) # card not removed from player self.assertEqual(player_cards_len, len(player.cards)) # card not taken from deck self.assertEqual(deck_cards_len, len(uno_round.deck._cards)) # proper actions setted self.assertEqual(len(uno_round.actions_list), 8) for item in uno_round.actions_list: self.assertTrue(item in allowed_actions_for_card( Card(color=Color.RED, number=1))) # player put card events events = self._get_all_events() self.assertEqual(len(events), 3) # your turn event self.assertTrue( self._find_event(NotificationType.PLAYER_SKIPPED, events)) self.assertTrue(self._find_event(NotificationType.YOUR_TURN, events))
def test_start_round(self): self._get_all_events() cards = list() for item in range(40): cards.append(Card(color=Color.RED, number=1)) deck = Deck(cards=cards) uno_round = UnoRound(deck=deck) uno_round.add_player('1') uno_round.add_player('2') uno_round.start_round() self.assertNotEqual(uno_round.current_player_index, None) for i in range(2): self.assertEqual(len(uno_round.player_list[1].cards), 7) self.assertTrue(uno_round.deck.active_card) self.assertEqual(len(uno_round.actions_list), 9) for item in uno_round.actions_list: self.assertTrue( item in allowed_actions_for_card(Card(color=Color.RED, number=1)) ) events = self._get_all_events() logger.debug(events) self.assertEqual(len(events), 19)
def start_game(self): logger.debug('starting new game') uno_round = UnoRound() event_manager.subscribe('game_command', uno_round) for data in self.players_list: player_uid, time = data uno_round.add_player(player_uid, 'human') if len(self.players_list) < 2: for item in range(1): ai_user = AIUser() online_players.add_player(ai_user, ai_user.protocol) uno_round.add_player(ai_user.uid) uno_round.start_round() self.players_list = list() logger.debug('game started')
def test_take_action(self): self._get_all_events() cards = list() for item in range(40): cards.append(Card(color=Color.RED, number=1)) deck = Deck(cards=cards) uno_round = UnoRound(deck=deck) uno_round.add_player('1') uno_round.add_player('2') uno_round.start_round() events = self._get_all_events() old_current_player_index = uno_round.current_player_index player_cards_len = len(uno_round.current_player.cards) deck_cards_len = len(uno_round.deck._cards) player = uno_round.current_player events = self._get_all_events() uno_round.process_input( player, uno_service.UnoService.execute_command.request(Command.TAKE)) # current player equal self.assertEqual(uno_round.current_player_index, old_current_player_index) # card added to player cards self.assertEqual(player_cards_len + 1, len(player.cards)) # card removed from deck self.assertEqual(deck_cards_len - 1, len(uno_round.deck._cards)) # proper actions setted self.assertEqual(len(uno_round.actions_list), 3) for item in uno_round.actions_list: self.assertTrue( item in [PutTakenCardAction, SkipAction, ListCardsAction]) events = self._get_all_events() self.assertEqual(len(events), 2) self.assertTrue( self._find_event(NotificationType.YOU_TOOK_CARD, events)) self.assertTrue( self._find_event(NotificationType.PLAYER_TOOK_CARD, events))
def test_take_two_and_skip_action(self): self._get_all_events() cards = list() for item in range(40): cards.append(Card(color=Color.RED, number=1)) deck = Deck(cards=cards) uno_round = UnoRound(deck=deck) uno_round.add_player('1') uno_round.add_player('2') uno_round.start_round() events = self._get_all_events() uno_round.current_player.cards.append( Card(color=Color.RED, action=CardAction.TAKE_TWO)) uno_round.process_input( uno_round.current_player, uno_service.UnoService.execute_command.request( Command.PUT, Card(color=Color.RED, action=CardAction.TAKE_TWO))) with self.assertRaises(ActionNotAllowedError): uno_round.process_input( uno_round.current_player, uno_service.UnoService.execute_command.request( Command.PUT, Card(color=Color.RED, number=1))) old_current_player_index = uno_round.current_player_index player_cards_len = len(uno_round.current_player.cards) deck_cards_len = len(uno_round.deck._cards) player = uno_round.current_player events = self._get_all_events() uno_round.process_input( uno_round.current_player, uno_service.UnoService.execute_command.request( Command.TAKE_TWO_AND_SKIP)) # current player changed self.assertNotEqual(uno_round.current_player_index, old_current_player_index) # cards added to player self.assertEqual(player_cards_len + 2, len(player.cards)) # card removed from deck self.assertEqual(deck_cards_len - 2, len(uno_round.deck._cards)) # proper actions setted self.assertEqual(len(uno_round.actions_list), 8) for item in uno_round.actions_list: self.assertTrue(item in allowed_actions_for_card( Card(color=Color.RED, number=1))) # player put card events events = self._get_all_events() self.assertEqual(len(events), 4) # your turn event self.assertTrue(self._find_event(NotificationType.YOUR_TURN, events)) self.assertTrue( self._find_event(NotificationType.YOU_TOOK_CARD, events)) self.assertTrue( self._find_event(NotificationType.PLAYER_TOOK_CARDS, events))
def test_win_conditions(self): self._get_all_events() uno_round = UnoRound() player1 = uno_round.add_player('1', 'human') player2 = uno_round.add_player('2', 'human') uno_round.start_round() uno_round.deck._used_cards = [Card(color=Color.RED, number=1)] uno_round.current_player_index = 0 player1.cards = [ Card(color=Color.RED, number=2), Card(color=Color.YELLOW, number=5), ] player2.cards = [ Card(color=Color.YELLOW, number=2), Card(color=Color.RED, number=5), ] uno_round.process_input(player1, uno_service.UnoService.execute_command.request( Command.PUT, player1.cards[0] # red,2 ) ) uno_round.process_input(player2, uno_service.UnoService.execute_command.request( Command.PUT, player2.cards[0] # yellow,2 ) ) events = self._get_all_events() uno_round.process_input(player1, uno_service.UnoService.execute_command.request( Command.PUT, player1.cards[0] # yellow, 5 ) ) events = self._get_all_events() for ev in events: print(ev) self.assertTrue([i for i in events if i['code'] == NotificationType.ROUND_FINISHED])
def test_set_next_player(self): self._get_all_events() uno_round = UnoRound() player1 = uno_round.add_player('1') player2 = uno_round.add_player('2') player3 = uno_round.add_player('3') uno_round.current_player_index = 0 self.assertEqual(player1, uno_round.current_player) uno_round.set_next_player() self.assertEqual(player2, uno_round.current_player) uno_round.set_next_player() self.assertEqual(player3, uno_round.current_player) uno_round.set_next_player() self.assertEqual(player1, uno_round.current_player)