def get(self): for match in Match.query(Match.winning_faction == None, Match.date <= datetime.datetime.now()): match.play_match() for match_player in MatchPlayer.query(MatchPlayer.match == match.key): player = match_player.player.get() player.doing = None player.put() websocket_notify_player("Match_Finished", player.key, None, match.get_data("full")) websocket_notify_player("Player_StatsChanged", player.key, "player", {'stats': player.get_stats_data()})
def add_match(match_id): """Queries a match from the Steam API and adds it to the database. Returns True if it succeeds or if match already exists, or False if it fails. """ match = api.get_match_details(match_id)["result"] if "error" in match: return False try: new_match = Match.create(**match) except IntegrityError: return True except PyDotaError: return False player_list = [dict(match_id=new_match.match_id, **player) for player in match["players"]] with db.atomic(): MatchPlayer.insert_many(player_list).execute() return True
def post(self): request_data = json.loads(self.request.body) logging.info(request_data) player = current_user_player() match_key = ndb.Key(Match, int(request_data['match_id'])) match = match_key.get() match_player = MatchPlayer.query(MatchPlayer.match == match_key, MatchPlayer.player == player.key).get() if match.state() == "finished": error_400(self.response, "ERROR_FINISHED_GAME", "You can not bet on a finished game.") return if not validate_request_data(self.response, request_data, ['match_id', 'bet']): return if not match_player: error_400(self.response, "ERROR_NOT_OWN_GAME", "You have to bet on your own game.") return if request_data['bet']['id']: bet = Bet.get_by_id(int(request_data['bet']['id'])) else: bet = Bet(match=match_key, player=player.key, winning_faction=match_player.faction) new_bet_value = int(request_data['bet']['value']) bet_value_dif = new_bet_value - bet.value if bet_value_dif < 0: error_400(self.response, "ERROR_BET_LOWER", "Your new bet %s is lower than your previous bet %s" % (new_bet_value, bet.value)) return if bet_value_dif > player.cash: error_400(self.response, "ERROR_NOT_ENOUGH_CASH", "Not enough cash. Your player has %s cash and your new bet requires %s" % (player.cash, bet_value_dif)) return bet.value = new_bet_value bet.put() player.cash -= bet_value_dif player.put() client_object_path = "player.matches.[%s].bets.[%s]" % (match_key.id(), bet.key.id()) match.websocket_notify_players("Match_UpdatedOrNewBet", client_object_path, bet.get_data(), player.key) set_json_response(self.response, {'bet': bet.get_data(), 'cash': player.cash})