def post(self): request_data = json.loads(self.request.body) logging.info(request_data) player = current_user_player() # VALIDATION if not validate_request_data(self.response, request_data, ['type']): return if player.doing: error_400(self.response, "ERROR_PLAYER_BUSY", "Player is busy.") return # JOIN QUEUE match_queue_key = MatchSoloQueue( player=player.key, type=request_data['type'] ).put() player.doing = match_queue_key player.put() ndb.get_context().clear_cache() # If it is not cleared the following count of queued players wont count this match_queue = match_queue_key.get() self._notify_players_new_queue_size(match_queue.type) set_json_response(self.response, {'doing': match_queue.get_data()})
def _trigger_played_match(self, match_type): if MatchSoloQueue.query(MatchSoloQueue.type == match_type).count() >= 2: match_solo_queues = [match_queue for match_queue in MatchSoloQueue.query(MatchSoloQueue.type == match_type).fetch(10)] players = [match_queue.player.get() for match_queue in match_solo_queues] match = Match(type=match_type) match.setup_soloqueue_match(players) ndb.delete_multi([queue.key for queue in match_solo_queues]) for player in players: player.doing = match.key player.put() websocket_notify_player("Player_MatchFound", player.key, None, match.get_data())
def _notify_players_new_queue_size(self, match_queue_type): all_match_queues = MatchSoloQueue.query(MatchSoloQueue.type == match_queue_type) all_match_queues_count = all_match_queues.count() for match_queue in all_match_queues: websocket_notify_player("Match_NewQueueCount", match_queue.player, "player.doing", {'queued': all_match_queues_count})