def test_user_automatically_joins_new_global_time_game(self): # register user res = self.client.post('/auth/register', data=json.dumps({ 'username': '******', 'password': '******', 'password': '******', }), headers={'Content-Type': 'application/json'}) self.assertEqual(200, res._status_code) game_before_check = Game.get(Game.name == 'Global Timed') game_before_check.ends_at = datetime.utcnow() game_before_check.save() # assert deletes old GameProfile profile = Profile.get_by_id(1) qry = GameProfile.select().join(Game).where( (GameProfile.profile == profile) & (Game.name == 'Global Timed')) self.assertEqual(2, len(profile.game_profiles) ) # should have 2: global timed and indefinite self.assertEqual(1, qry.count()) # just to make sure gp_old = qry.get() check_global_timed_game() # assert creates new GameProfile qry = GameProfile.select().join(Game).where( (GameProfile.profile == profile) & (Game.name == 'Global Timed')) self.assertEqual(1, qry.count()) gp_new = qry.get() # but the ids should not be the same self.assertNotEqual(gp_old.id, gp_new.id)
def add_to_game(profile_id, game): game_profile = GameProfile.select().where(GameProfile.profile == profile_id, GameProfile.game == game.id) if game_profile.count() == 0: GameProfile.create( game=game.id, profile=profile_id, cash=game.starting_cash )
def check_global_timed_game(): game = Game.get(Game.shareable_link == 'TIMED') if game.ends_at < datetime.utcnow(): # end global timed game, and start another profiles = [] for game_profile in GameProfile.select().where(GameProfile.game == game): profiles.append(game_profile.profile) send_notification(game_profile.profile, 'The global timed game has expired') game_profile.delete_instance(recursive=True) game.delete_instance(recursive=True) global_timed = Game.create(name='Global Timed', starting_cash=10000.00, shareable_link='TIMED', shareable_code='TIMED', ends_at=datetime.utcnow() + timedelta(minutes=1)) # CHANGEME for devel purposes, making it 1 min for now GameCoin.create(game=global_timed, coin=Coin.get()) for profile in profiles: GameProfile.create( game=global_timed, profile=profile, cash=global_timed.starting_cash )
def get_players_in_a_game(game_id: int): game_profiles = GameProfile.select( GameProfile, Profile).join(Profile).where(GameProfile.game == game_id).execute() return [gp.profile for gp in game_profiles]
def test_joining_game_more_than_once_is_idempotent(self): game = Game.get(Game.name == 'Global Timed') before = GameProfile.select().count() self.client.get(f'/join?code={game.shareable_code}') after = GameProfile.select().count() self.assertEqual(before, after)