def setUp(self): self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() self.g1 = \ Game(opponent='test', date=datetime.date(2015, 10, 01), venue='test', team=Team.TEST, weight=0.4) self.g2 = \ Game(opponent='test', date=datetime.date(2015, 11, 01), venue='test', team=Team.TEST, weight=0.6) players = [] for i in range(0, 10): p = Player(name='Player ' + str(i)) p.put() players.append(p) self.g1.players = [p.key for p in players] self.g1.put() self.g2.players = [p.key for p in players] self.g2.put() v = Vote(game=self.g1.key, three=players[7].key, two=players[9].key, one=players[3].key) v.put() v = Vote(game=self.g2.key, three=players[9].key, two=players[7].key, one=players[3].key) v.put()
def setUp(self): self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() g = Game(opponent='test', date=datetime.date.today(), venue='test', team=Team.TEST) players = [] for i in range(0, 10): p = Player(name='Player ' + str(i)) p.put() players.append(p) g.players = [p.key for p in players] g.put() # Rankings: # Player 3 - 7 votes (two threes) # Player 4 - 7 votes (one three, two twos) # Player 7 - 7 votes (one three, one two) v = Vote(game=g.key, three=players[3].key, two=players[4].key, one=players[7].key) v.put() v = Vote(game=g.key, three=players[7].key, two=players[4].key, one=players[5].key) v.put() v = Vote(game=g.key, three=players[3].key, two=players[9].key, one=players[7].key) v.put() v = Vote(game=g.key, three=players[4].key, two=players[7].key, one=players[3].key) v.put()
def setUp(self): self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() g = Game(opponent='test', date=datetime.date.today(), venue='test', team=Team.TEST) players = [] for i in range(0, 10): p = Player(name='Player ' + str(i)) p.put() players.append(p) g.players = [p.key for p in players] g.put()
def get_or_create_lichess_game(headers: dict, white: UUID, black: UUID, raw: str) -> Game: chess_game = Game.query.filter_by(site=headers.get("Site")).one_or_none() if chess_game: return chess_game chess_game = Game.lichess(**headers, white=white, black=black, raw=raw) chess_game.store() return chess_game
def testGameResults(self): g = Game.query().get() results = game_results(g) self.assertEqual('Player 4', results.three.get().name) self.assertEqual('Player 7', results.two.get().name) self.assertEqual('Player 1', results.one.get().name) self.assertEqual(10, results.voters)
def get_or_create_game(headers: dict, white: UUID, black: UUID, raw: str) -> Game: chess_game = Game.query.filter_by(raw=raw).one_or_none() if chess_game: return chess_game chess_game = Game.standard(**headers, white=white, black=black, raw=raw) if not chess_game.store(): logging.error("Couldn't store chess game.") return chess_game
def start_new_game(players: List[str], init_pot=100, small_blind_stake=5, max_rounds=100, use_local=True): new_game = Game(players=players, init_pot=init_pot, small_blind_stake=small_blind_stake) LOGGER.info('New game: %s', new_game) game_result = GameResult(None) game_state = GameState(players=players, player_pots=[init_pot for _ in range(len(players))]) LOGGER.debug('New game state: %s', game_state) # test with only max_rounds for pos in range(max_rounds): if len(game_state.remaining_players) == 1: game_result.set_winner(game_state.remaining_players[0]) break deck = list(Card) random.shuffle(deck) game_round = GameRound( pos=pos, small_blind=game_state.get_small_blind_player(pos), big_blind=game_state.get_big_blind_player(pos), players=game_state.remaining_players, player_pots=game_state.remaining_player_pots, player_hands=game_state.generate_player_hands(deck), small_blind_stake=small_blind_stake) LOGGER.debug('New game round: %s', game_round) state_index = -1 board_cards: List[Card] = [] # pre-flop state state_index = state_index + 1 next_move, pre_flop_state = game_state_move(state_index, "pre-flop", board_cards, game_round, game_state, None, use_local) if not next_move: round_complete(game_state, pre_flop_state) continue # post-flop state board_cards.extend([deck.pop() for __ in range(3)]) LOGGER.debug('Board cards: %s', board_cards) state_index = state_index + 1 next_move, post_flop_state = game_state_move(state_index, "post-flop", board_cards, game_round, game_state, pre_flop_state, use_local) if not next_move: round_complete(game_state, pre_flop_state, post_flop_state) continue # turn state board_cards.extend([deck.pop() for __ in range(1)]) LOGGER.debug('Board cards: %s', board_cards) state_index = state_index + 1 next_move, turn_state = game_state_move(state_index, "turn", board_cards, game_round, game_state, post_flop_state, use_local) if not next_move: round_complete(game_state, pre_flop_state, post_flop_state, turn_state) continue # river state board_cards.extend([deck.pop() for __ in range(1)]) LOGGER.debug('Board cards: %s', board_cards) state_index = state_index + 1 next_move, river_state = game_state_move(state_index, "river", board_cards, game_round, game_state, turn_state, use_local) round_complete(game_state, pre_flop_state, post_flop_state, turn_state, river_state) if not game_result.winner: # determine winner in the remaining players by number of chips game_result.set_winner(game_state.get_player_with_most_chips()) LOGGER.debug('End game. Winner is %s', game_result.winner) return game_result.winner
def get(self): results = Game.query.all() return Game.serialize_list(results), HTTPStatus.OK
def testGameResults(self): g = Game.query().get() results = game_results(g) self.assertIsNone(results)
class WeightedTestCase(unittest.TestCase): def setUp(self): self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() self.g1 = \ Game(opponent='test', date=datetime.date(2015, 10, 01), venue='test', team=Team.TEST, weight=0.4) self.g2 = \ Game(opponent='test', date=datetime.date(2015, 11, 01), venue='test', team=Team.TEST, weight=0.6) players = [] for i in range(0, 10): p = Player(name='Player ' + str(i)) p.put() players.append(p) self.g1.players = [p.key for p in players] self.g1.put() self.g2.players = [p.key for p in players] self.g2.put() v = Vote(game=self.g1.key, three=players[7].key, two=players[9].key, one=players[3].key) v.put() v = Vote(game=self.g2.key, three=players[9].key, two=players[7].key, one=players[3].key) v.put() def testGameResults(self): results = game_results(self.g1) self.assertEqual('Player 7', results.three.get().name) self.assertEqual('Player 9', results.two.get().name) self.assertEqual('Player 3', results.one.get().name) self.assertEqual(1, results.voters) results = game_results(self.g2) self.assertEqual('Player 9', results.three.get().name) self.assertEqual('Player 7', results.two.get().name) self.assertEqual('Player 3', results.one.get().name) self.assertEqual(1, results.voters) def testOverallResults(self): results = overall_results(Team.TEST) # Overall rankings self.assertEqual('Player 9', results.player_votes[0].player.get().name) self.assertEqual('Player 7', results.player_votes[1].player.get().name) self.assertEqual('Player 3', results.player_votes[2].player.get().name) self.assertAlmostEqual(2.6, results.player_votes[0].total, places=3) self.assertAlmostEqual(2.4, results.player_votes[1].total, places=3) self.assertAlmostEqual(1.0, results.player_votes[2].total, places=3) # Game 1 self.assertEqual('Player 7', results.game_votes[0].three.get().name) self.assertEqual('Player 9', results.game_votes[0].two.get().name) self.assertEqual('Player 3', results.game_votes[0].one.get().name) self.assertEqual(1, results.game_votes[0].voters) # Game 2 self.assertEqual('Player 9', results.game_votes[1].three.get().name) self.assertEqual('Player 7', results.game_votes[1].two.get().name) self.assertEqual('Player 3', results.game_votes[1].one.get().name) self.assertEqual(1, results.game_votes[1].voters) def tearDown(self): self.testbed.deactivate()
from src.models.deck import Deck, Card from src.models.game import Round, Game # main class for temporary testing # Basic test: First player with highest card should win game = Game() round = Round(game) round.play(Card('Spades', 'Jack'), 'p1') # Winner round.play(Card('Spades', 'Jack'), 'p2') round.play(Card('Spades', '7'), 'p3') round.play(Card('Spades', 'Jack'), 'p4') winner, score = round.end() print("Test#1 :: {0} won by {1}".format(winner, score)) # Trump test: player with trump should win. Assuming trump was called in previous game game = Game() game.call_trump() game.trump = Card('Hearts', '6') round = Round(game) round.play(Card('Spades', 'Jack'), 'p1') round.play(Card('Spades', 'Jack'), 'p2') round.play(Card('Hearts', '6'), 'p3') # Winner round.play(Card('Spades', 'Jack'), 'p4') winner, score = round.end() print("Test#2 :: {0} won by {1}".format(winner, score)) # Trump test: player with trump should win. Assuming trump was called before the play game = Game() game.trump = Card('Hearts', '6') round = Round(game)