def get_pricing_by_coins(coins, start_time): coins_and_prices = [] for currentCoin in coins: prices_query = Ticker.select()\ .join(Coin)\ .where(Coin.id == currentCoin.id, Ticker.captured_at > start_time)\ .order_by(-Ticker.captured_at) if prices_query.count() == 0: raise BadRequest("Coin's prices not found") prices = [] for price in prices_query: prices.append(price) coins_and_prices.append({'coin': currentCoin, 'prices': prices}) return coins_and_prices
def sell_coin(coin_id, coin_amount, game_profile): gameProfileCoin = GameProfileCoin.get_or_none( GameProfileCoin.game_profile == game_profile.id, GameProfileCoin.coin == coin_id) if gameProfileCoin is None or gameProfileCoin.coin_amount < coin_amount: raise BadRequest('Not enough of these coins to sell') ticker = Ticker.select().where(Ticker.coin == coin_id).order_by( Ticker.captured_at.desc()) if ticker.count() == 0: raise BadRequest('Coin has no prices') ticker = ticker.get() new_cash = game_profile.cash + (ticker.price * coin_amount) new_coin_amount = gameProfileCoin.coin_amount - coin_amount GameProfileCoin.update(coin_amount=new_coin_amount).where( GameProfileCoin.id == gameProfileCoin.id).execute() GameProfile.update(cash=new_cash).where( GameProfile.id == game_profile.id).execute() return new_coin_amount
def get_net_worth_by_game_profile_id(game_profile_id): gameProfile = GameProfile.get_or_none(GameProfile.id == game_profile_id) if not gameProfile: raise BadRequest('User not in game') netWorth = gameProfile.cash gameProfileCoins = get_game_profile_coins_by_game_profile_id( game_profile_id) for gameProfileCoin in gameProfileCoins: ticker = Ticker.select().where( Ticker.coin == gameProfileCoin.coin).order_by( Ticker.captured_at.desc()) if ticker.count() == 0: raise BadRequest('One coin did not have prices') ticker = ticker.get() if not ticker: raise BadRequest('One coin did not exist') netWorth += ticker.price * gameProfileCoin.coin_amount return netWorth
def buy_coin(coin_id, coin_amount, game_profile): ticker = Ticker.select().where(Ticker.coin == coin_id).order_by( Ticker.captured_at.desc()) if ticker.count() == 0: raise BadRequest('Coin has no prices') ticker = ticker.get() new_cash = game_profile.cash - (ticker.price * coin_amount) if new_cash < 0: raise BadRequest('Not enough cash to buy this coin amount') game_coin = GameCoin.get_or_none(GameCoin.game == game_profile.game, GameCoin.coin == coin_id) if game_coin is None: raise BadRequest('Coin does not exist in this game') gameProfileCoin = GameProfileCoin.get_or_none( GameProfileCoin.game_profile == game_profile.id, GameProfileCoin.coin == coin_id) if gameProfileCoin is None: GameProfileCoin.create(game_profile=game_profile.id, coin=coin_id, coin_amount=coin_amount) rows = GameProfile.update(cash=new_cash).where( GameProfile == game_profile).execute() if rows == 0: raise BadRequest('Money could not be removed from your account') return coin_amount else: new_coin_amount = gameProfileCoin.coin_amount + coin_amount rows = GameProfile.update(cash=new_cash).where( GameProfile == game_profile).execute() if rows == 0: raise BadRequest('Money could not be removed from your account') GameProfileCoin.update(coin_amount=new_coin_amount).where( GameProfileCoin.id == gameProfileCoin.id).execute() return new_coin_amount
def test_parsing_with_no_coins(self): num_tickers_before = Ticker.select().count() ping() num_tickers_after = Ticker.select().count() self.assertEqual(num_tickers_before, num_tickers_after)
def get_tickers_24hr(): yesterday = datetime.utcnow() - timedelta(days=1) tickers = Ticker.select().where(Ticker.captured_at > yesterday) if not tickers: return [] return tickers
def get_ticker_price_by_coin_id(coin_id): return Ticker.select(Ticker.price).where(coin_id == Ticker.coin)