def register(username: str, password: str): profile = Profile.get_or_none(Profile.username == username) if profile is not None: raise BadRequest('A user with this username already exists') hashed = bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode() profile = Profile.create( username=username, hashed_password=hashed, ) global_game = Game.get_or_none(Game.name == 'Global Indefinite') GameProfile.create(game=global_game, profile=profile, cash=global_game.starting_cash) global_game = Game.get_or_none(Game.name == 'Global Timed') GameProfile.create(game=global_game, profile=profile, cash=global_game.starting_cash) send_notification(profile, 'Welcome to Fortune!') send_notification( profile, 'Click the Play button in the menubar on top to create a new game.') send_notification( profile, 'To adjust account options, see achiements, and add friends, click on your username on the top and select Profile' ) return create_auth_token(profile)
def test_get_coins_with_invalid_time_span(self): profile = Profile.get_or_none(Profile.username == 'theusername') GameProfile.create(game=1, profile=profile, cash=10000) res = self.client.get( '/game/1/coins?timeSpan=6&sortBy=0&numPerPage=10&pageNum=1', headers={'Authorization': 'Bearer ' + self.token}) self.assertEqual(int(HTTPStatus.BAD_REQUEST), res._status_code)
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 up(db): with db.atomic(): migrator = PostgresqlMigrator(db) db.bind(MODELS, bind_refs=False, bind_backrefs=False) db.create_tables(MODELS) if Coin.get_or_none(Coin.id == 1) is None: Coin.create(name='Bitcoin', symbol='BTC') Coin.create(name='Ethereum', symbol='ETH') Coin.create(name='Litecoin', symbol='LTC') Coin.create(name='Coin 3', symbol='CO3') Coin.create(name='Coin 4', symbol='CO4') Coin.create(name='Coin 5', symbol='CO5') global_indef = Game.create(name='Global Indefinite', starting_cash=10000.00, shareable_link='INDEF', shareable_code='INDEF', ends_at=None) # insert achievements into database Achievement.create( name="Win", description="Finish in first place in a private game") Achievement.create( name="Double net worth", description="Achieved by doubling your net worth in a game") Achievement.create(name="Identity Crisis", description="Change your username") # insert goals into database Goal.create(name="Entrepreneur", description="Create a private game") all_coins = Coin.select() for coin in all_coins: GameCoin.create(game=global_indef, coin=coin) 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()) # from auth.services import register hashed = bcrypt.hashpw("admin".encode(), bcrypt.gensalt()).decode() admin = Profile.create(username="******", hashed_password=hashed, is_admin=True) # Required so that admin can still view graphs in the landing page GameProfile.create(profile=admin, game=global_indef, cash=0.0)
def test_buy_coin_without_cash(self): profile = Profile.get_or_none(Profile.username == 'theusername') GameProfile.create(game=1, profile=profile, cash=0) GameCoin.create(game=1, coin=1) Ticker.create(coin=1, price=10, price_change_day_pct=1.1) res = self.client.post( '/game/1/coin', data=json.dumps({ 'coinId': '1', 'coinAmount': '1', }), content_type='application/json', headers={'Authorization': 'Bearer ' + self.token}) self.assertEqual(int(HTTPStatus.BAD_REQUEST), res._status_code)
def test_get_coins_success(self): profile = Profile.get_or_none(Profile.username == 'theusername') GameProfile.create(game=1, profile=profile, cash=10000) GameCoin.create( game=1, coin=1, ) for coin in Coin.select(): Ticker.create( coin=coin, price=30.0, captured_at=(datetime.utcnow()).isoformat(), price_change_day_pct=1.1, ) res = self.client.get( '/game/1/coins?timeSpan=1&sortBy=0&numPerPage=10&pageNum=1', headers={'Authorization': 'Bearer ' + self.token}) self.assertEqual(int(HTTPStatus.OK), res._status_code)
def create_game(name, starting_cash, shareable_link, shareable_code, ends_at, active_coins, profile): # bounds check, validate if ends_at < datetime.utcnow().replace(tzinfo=pytz.utc): raise BadRequest('Invalid game ending date') if starting_cash <= Decimal(0): raise BadRequest('Starting cash must be positive') game = Game.create( name=name, starting_cash=starting_cash, shareable_link=shareable_link, shareable_code=shareable_code, ends_at=ends_at, ) create_gamecoins_for_game(game, active_coins) GameProfile.create( game=game, profile=profile, cash=game.starting_cash, ) return game
def setUp(self): super().setUp() with db.atomic() as txn: self.game = Game.create( name='Game', starting_cash=10000.00, shareable_link='aaaabbbbccccdddd', shareable_code='aaaa', ends_at=(datetime.utcnow().replace(tzinfo=pytz.utc) + timedelta(days=7)).isoformat()) profile = Profile.create(username='******', hashed_password='******') GameProfile.create(game=self.game, profile=profile, cash=-1.0) Message.create( game=self.game.id, profile=profile.id, content="first message", # use default value for created_on ) self.token = AuthToken.create(profile=profile, token='thevalidtoken').token
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 test_liquefy_success(self): profile = Profile.get_or_none(Profile.username == 'theusername') game_profile = GameProfile.create(game=1, profile=profile, cash=0) GameCoin.create(game=1, coin=1) GameProfileCoin.create(game_profile=game_profile, coin=1, coin_amount=2) Ticker.create( coin=1, price=10, captured_at=(datetime.utcnow()).isoformat(), price_change_day_pct=1.1, ) res = self.client.delete( '/game/1/coins', headers={'Authorization': 'Bearer ' + self.token}) self.assertEqual(int(HTTPStatus.OK), res._status_code)
def test_sell_coin_success(self): profile = Profile.get_or_none(Profile.username == 'theusername') game_profile = GameProfile.create(game=1, profile=profile, cash=0) GameCoin.create(game=1, coin=1) GameProfileCoin.create(game_profile=game_profile, coin=1, coin_amount=2) Ticker.create( coin=1, price=10, captured_at=(datetime.utcnow()).isoformat(), price_change_day_pct=1.1, ) res = self.client.post( '/game/1/coin', data=json.dumps({ 'coinId': '1', 'coinAmount': '-1', }), content_type='application/json', headers={'Authorization': 'Bearer ' + self.token}) self.assertEqual(int(HTTPStatus.OK), res._status_code)