def test_snapshot(self): mem_db = tappy.connect_memory_db() tappy.create_game_db(mem_db) empty_game = tappy.TappyTerrorGame() snapshot_id = empty_game.snapshot_to_db(mem_db) db_snapshot_id = mem_db.execute('SELECT id FROM game_snapshots ORDER BY update_time DESC LIMIT 1').fetchone()[0] self.assertEqual(snapshot_id, db_snapshot_id) roundtrip = tappy.TappyTerrorGame.load_from_snapshot(mem_db) self.assertIsInstance(roundtrip, tappy.TappyTerrorGame) self.assertEqual(empty_game.game_board.keys(), roundtrip.game_board.keys()) self.assertEqual(empty_game.game_board, roundtrip.game_board) self.assertEqual(empty_game.active_players, roundtrip.active_players) self.assertEqual(empty_game.team_points, roundtrip.team_points) full_game = tappy.TappyTerrorGame() full_game.tick() self.assertNotEqual(empty_game, full_game) db_snapshot_id = mem_db.execute('SELECT id FROM game_snapshots ORDER BY update_time DESC LIMIT 1').fetchone()[0] self.assertEqual(snapshot_id, db_snapshot_id) snapshot_id = full_game.snapshot_to_db(mem_db) roundtrip = tappy.TappyTerrorGame.load_from_snapshot(mem_db) self.assertEqual(full_game.game_board, roundtrip.game_board) self.assertEqual(full_game.active_players, roundtrip.active_players) self.assertEqual(full_game.team_points, roundtrip.team_points)
def test_snapshot(self): mem_db = tappy.connect_memory_db() tappy.create_game_db(mem_db) empty_game = tappy.TappyTerrorGame() snapshot_id = empty_game.snapshot_to_db(mem_db) db_snapshot_id = mem_db.execute( 'SELECT id FROM game_snapshots ORDER BY update_time DESC LIMIT 1' ).fetchone()[0] self.assertEqual(snapshot_id, db_snapshot_id) roundtrip = tappy.TappyTerrorGame.load_from_snapshot(mem_db) self.assertIsInstance(roundtrip, tappy.TappyTerrorGame) self.assertEqual(empty_game.game_board.keys(), roundtrip.game_board.keys()) self.assertEqual(empty_game.game_board, roundtrip.game_board) self.assertEqual(empty_game.active_players, roundtrip.active_players) self.assertEqual(empty_game.team_points, roundtrip.team_points) full_game = tappy.TappyTerrorGame() full_game.tick() self.assertNotEqual(empty_game, full_game) db_snapshot_id = mem_db.execute( 'SELECT id FROM game_snapshots ORDER BY update_time DESC LIMIT 1' ).fetchone()[0] self.assertEqual(snapshot_id, db_snapshot_id) snapshot_id = full_game.snapshot_to_db(mem_db) roundtrip = tappy.TappyTerrorGame.load_from_snapshot(mem_db) self.assertEqual(full_game.game_board, roundtrip.game_board) self.assertEqual(full_game.active_players, roundtrip.active_players) self.assertEqual(full_game.team_points, roundtrip.team_points)
def test_position_update_changes_active(self): game = tappy.TappyTerrorGame() # Equivelent to asserting that there are no active players self.assertFalse(game.active_players) user_updates = self.get_location_dump() no_button_updates = [x for x in user_updates if not x.get('button')] button_updates = [x for x in user_updates if x.get('button')] inactive_users = [x['user'] for x in no_button_updates] expected_active_users = [x['user'] for x in button_updates] # update without buttons shouldn't make users appear game.update_player_positions(no_button_updates) self.assertFalse(game.active_players) # now update all of them game.update_player_positions(user_updates) self.members_correct(game.active_players, expected_active_users, inactive_users) # performing another update without any button folks shouldn't # make the button folks leave active game.update_player_positions(no_button_updates) self.members_correct(game.active_players, expected_active_users, inactive_users) # updating just the button folks shouldn't change anything game.update_player_positions(button_updates) self.members_correct(game.active_players, expected_active_users, inactive_users) # make a few random users toggle their button, make sure update works random_users = random.sample(user_updates, 3) for user in random_users: if user.get('button'): user['button'] = False else: user['button'] = True inactive_users.remove(user['user']) expected_active_users.append(user['user']) game.update_player_positions(user_updates) self.members_correct(game.active_players, expected_active_users, inactive_users) # confirm that our assumptions survive a round trip mem_db = tappy.connect_memory_db() tappy.create_game_db(mem_db) game.snapshot_to_db(mem_db) roundtrip = tappy.TappyTerrorGame.load_from_snapshot(mem_db) self.assertEquals(game.active_players, roundtrip.active_players)
def test_assign_teams(self): game = tappy.TappyTerrorGame() user_updates = self.get_location_dump() for user in user_updates: user['team'] = None game._assign_team(user) assigned_teams = {user['user']: user['team'] for user in user_updates} for user in user_updates: user['team'] = None game.update_player_positions(user_updates) for (user_id, player) in game.active_players.items(): self.assertEqual(player.team, assigned_teams[player.amd_user_id]) mem_db = tappy.connect_memory_db() tappy.create_game_db(mem_db) game.snapshot_to_db(mem_db) roundtrip = tappy.TappyTerrorGame.load_from_snapshot(mem_db) self.assertEqual(game.active_players, roundtrip.active_players)