def route_match_bet(match_id): try: items = map(int, json.loads(request.values.get("items"))) team = int(request.values.get("team")) except Exception as e: raise APIError("Invalid Request: %s" % e) # Make sure this seems mildly valid apiassert(0 < len(items) <= 4, "Too many items") match = g.cursor.select("matches", "id", "teams", "lock_date", "match_date", "public_date", "active", "max_value_item", "max_value_total", id=match_id).fetchone() itemvs = g.cursor.execute("SELECT id, name, price FROM items WHERE id IN %s", (tuple(items), )).fetchall(as_list=True) apiassert(len(itemvs) == len(items), "Invalid Item ID's") # Make sure we haven't bet too much shit if match.max_value_item: for item in itemvs: apiassert(item.price < match.max_value_item, "Price of item %s is too high!" % item.name) if match.max_value_total: apiassert(sum(map(lambda i: i.price, itemvs)) < match.max_value_total, "Total value placed is too high!") # Make sure we have a valid match apiassert(match, "Invalid match ID") apiassert(match.active, "Invalid match ID") apiassert(match.public_date.replace(tzinfo=None) < datetime.utcnow(), "Invalid match ID") apiassert(match.lock_date.replace(tzinfo=None) > datetime.utcnow(), "Match is locked") apiassert(len(match.teams) > team, "Invalid Team") # Make sure this user doesn't already have a bet on this match g.cursor.execute("SELECT * FROM bets WHERE better=%s AND match=%s", (g.user, match.id)) apiassert(g.cursor.fetchone() == None, "Bet already created") # Ensure we have space for the items used, avail = get_bot_space() apiassert(avail > len(items), "No space left for bet") return APIResponse({ "bet": create_bet(g.user, match_id, team, itemvs) })
def test_create_bet(self): user = self.get_user(TEST_STEAM_ID) match = self.get_match() id = create_bet(user, match, 0, []) self.assertGreaterEqual(id, 1)