def test_active_player_and_game_tick_count_cycles_after_each_move(self): game = Game.objects.create( game_type=Game.GAME_TYPE_CHOICE_CONNECT_QUAT, name="fooo", is_started=True, max_players=2) self.player1.game = game self.player2.game = game self.player3.game = game self.player1.turn_order = 2 self.player2.turn_order = 3 self.player3.turn_order = 1 self.player1.save() self.player2.save() self.player3.save() game.archived_players.set([self.player1, self.player2, self.player3]) board_state = cq_lib.board_obj_to_serialized_state({ Board.STATE_KEY_NEXT_PLAYER_TO_ACT: self.player3.id, Board.STATE_KEY_BOARD_LIST: [[None for i in range(7)] for j in range(7)] }) board = Board.objects.create(game=game, board_state=board_state, board_length_x=7, board_length_y=7) url = reverse('api-connectquat-move') data = {'column_index': 0} self.client.login(username='******', password='******') response = self.client.post(url, data, format="json") self.assertEqual(response.status_code, status.HTTP_200_OK) board.refresh_from_db() game.refresh_from_db() board_state = cq_lib.board_state_to_obj(board) self.assertEqual(board_state[Board.STATE_KEY_NEXT_PLAYER_TO_ACT], self.player1.id) self.assertEqual(game.tick_count, 1) self.client.login(username='******', password='******') response = self.client.post(url, data, format="json") self.assertEqual(response.status_code, status.HTTP_200_OK) board.refresh_from_db() game.refresh_from_db() board_state = cq_lib.board_state_to_obj(board) self.assertEqual(board_state[Board.STATE_KEY_NEXT_PLAYER_TO_ACT], self.player2.id) self.assertEqual(game.tick_count, 2) self.client.login(username='******', password='******') response = self.client.post(url, data, format="json") self.assertEqual(response.status_code, status.HTTP_200_OK) board.refresh_from_db() game.refresh_from_db() board_state = cq_lib.board_state_to_obj(board) self.assertEqual(board_state[Board.STATE_KEY_NEXT_PLAYER_TO_ACT], self.player3.id) self.assertEqual(game.tick_count, 3)
def test_drop_chip_empty_board(self): cq_lib.drop_chip(self.board, self.player1, 3) self.board.refresh_from_db() board_state = cq_lib.board_state_to_obj(self.board) board_list = board_state[Board.STATE_KEY_BOARD_LIST] self.assertTrue(board_list[6][3], self.player1.id) cq_lib.drop_chip(self.board, self.player2, 3) self.board.refresh_from_db() board_state = cq_lib.board_state_to_obj(self.board) board_list = board_state[Board.STATE_KEY_BOARD_LIST] self.assertTrue(board_list[6][3], self.player1.id) self.assertTrue(board_list[5][3], self.player2.id)
def test_player_can_start_connect_quatro_with_enough_players_game(self): """ Test player can start a game with 2+ players. """ self.client.login(username='******', password='******') self.user2 = User.objects.create_user('*****@*****.**', password='******') self.player2 = Player.objects.create(user=self.user2, handle="foobar") game = Game.objects.create( game_type=Game.GAME_TYPE_CHOICE_CONNECT_QUAT, name="foo", is_started=False, max_players=2) board = Board.objects.create( game=game, board_length_x=7,board_length_y=7) self.player1.game = game self.player1.is_lobby_owner = True self.player1.save() self.player2.game = game self.player2.lobby_status = Player.LOBBY_STATUS_READY self.player2.save() url = reverse("api-lobby-start") response = self.client.post(url) self.assertEqual(response.status_code, status.HTTP_200_OK) game.refresh_from_db() board.refresh_from_db() self.player2.refresh_from_db() self.player1.refresh_from_db() self.assertTrue(game.is_started) self.assertTrue(self.player1 in game.archived_players.all()) self.assertTrue(self.player2 in game.archived_players.all()) self.assertIsNotNone(self.player1.turn_order) self.assertIsNotNone(self.player2.turn_order) self.assertNotEqual(self.player1.turn_order, self.player2.turn_order) self.assertIsNotNone(self.player1.color) self.assertIsNotNone(self.player2.color) self.assertNotEqual(self.player1.color, self.player2.color) self.assertEqual(self.player1.lobby_status, Player.LOBBY_STATUS_JOINED) self.assertEqual(self.player2.lobby_status, Player.LOBBY_STATUS_JOINED) board_state = cq_lib.board_state_to_obj(board) board_list = board_state[Board.STATE_KEY_BOARD_LIST] self.assertEqual(len(board_list), 7) for row in board_list: self.assertEqual(len(row), 7) self.assertFalse(any(row)) self.mock_alert_game_lobby_game_started.assert_called_once_with(game) self.mock_update_lobby_list_remove_game.assert_called_once_with(game) active_player_id = cq_lib.get_active_player_id_from_board(game.board) self.mock_cycle_player_turn_if_inactive.assert_called_once_with(game.id, active_player_id, game.tick_count)
def decorated_function(request, *args, **kwargs): try: player, game = get_user_player_game(request)[1:] except AttributeError as e: print(e) return Response("game not found", status.HTTP_404_NOT_FOUND) if not game: return Response("game not found", status.HTTP_404_NOT_FOUND) if game.game_type != Game.GAME_TYPE_CHOICE_CONNECT_QUAT or not game.is_started: return Response("invalid game state", status.HTTP_400_BAD_REQUEST) if game.is_over: return Response("game over", status.HTTP_400_BAD_REQUEST) board_state = cq_lib.board_state_to_obj(game.board) next_player_id = board_state[Board.STATE_KEY_NEXT_PLAYER_TO_ACT] if next_player_id != player.id: return Response("turn order error", status.HTTP_400_BAD_REQUEST) return function(request, *args, **kwargs)
def test_player_can_drop_chip_on_empty_board_when_its_their_turn(self): game = Game.objects.create( game_type=Game.GAME_TYPE_CHOICE_CONNECT_QUAT, name="fooo", is_started=True, max_players=2) self.player1.game = game self.player2.game = game self.player1.save() self.player2.save() board_state = cq_lib.board_obj_to_serialized_state({ Board.STATE_KEY_NEXT_PLAYER_TO_ACT: self.player1.id, Board.STATE_KEY_BOARD_LIST: [[None for i in range(7)] for j in range(7)] }) board = Board.objects.create(game=game, board_state=board_state, board_length_x=7, board_length_y=7) self.client.login(username='******', password='******') url = reverse('api-connectquat-move') data = {'column_index': 3} response = self.client.post(url, data, format="json") self.assertEqual(response.status_code, status.HTTP_200_OK) board.refresh_from_db() board_state = cq_lib.board_state_to_obj(board) board_list = board_state[Board.STATE_KEY_BOARD_LIST] self.assertEqual(board_list[6][3], self.player1.id) chip_counts = Counter() for row in board_list: for val in row: chip_counts[val] += 1 self.assertEqual(chip_counts[self.player1.id], 1) self.assertEqual(chip_counts[None], 48)