def create_elos(session, game): """Elo handler for all game types.""" try: process_elos(game, session) except Exception as e: log.debug('Error (non-fatal): elo processing failed.')
def stats_submit(request): """ Entry handler for POST stats submissions. """ try: # placeholder for the actual session session = None log.debug("\n----- BEGIN REQUEST BODY -----\n" + request.body + "----- END REQUEST BODY -----\n\n") (idfp, status) = verify_request(request) if not idfp: log.debug("ERROR: Unverified request") raise pyramid.httpexceptions.HTTPUnauthorized("Unverified request") (game_meta, players) = parse_body(request) if not has_required_metadata(game_meta): log.debug("ERROR: Required game meta missing") raise pyramid.httpexceptions.HTTPUnprocessableEntity("Missing game meta") if not is_supported_gametype(game_meta['G']): log.debug("ERROR: Unsupported gametype") raise pyramid.httpexceptions.HTTPOk("OK") if not has_minimum_real_players(request.registry.settings, players): log.debug("ERROR: Not enough real players") raise pyramid.httpexceptions.HTTPOk("OK") if is_blank_game(players): log.debug("ERROR: Blank game") raise pyramid.httpexceptions.HTTPOk("OK") # the "duel" gametype is fake if num_real_players(players, count_bots=True) == 2 and \ game_meta['G'] == 'dm': game_meta['G'] = 'duel' # fix for DTG, who didn't #ifdef WATERMARK to set the revision info try: revision = game_meta['R'] except: revision = "unknown" #---------------------------------------------------------------------- # This ends the "precondition" section of sanity checks. All # functions not requiring a database connection go ABOVE HERE. #---------------------------------------------------------------------- session = DBSession() server = get_or_create_server(session=session, hashkey=idfp, name=game_meta['S'], revision=revision, ip_addr=get_remote_addr(request)) gmap = get_or_create_map(session=session, name=game_meta['M']) # FIXME: use the gmtime instead of utcnow() when the timezone bug is # fixed game = create_game(session=session, start_dt=datetime.datetime.utcnow(), #start_dt=datetime.datetime( #*time.gmtime(float(game_meta['T']))[:6]), server_id=server.server_id, game_type_cd=game_meta['G'], map_id=gmap.map_id, match_id=game_meta['I']) # find or create a record for each player # and add stats for each if they were present at the end # of the game for player_events in players: if 'n' in player_events: nick = player_events['n'] else: nick = None if 'matches' in player_events and 'scoreboardvalid' \ in player_events: player = get_or_create_player(session=session, hashkey=player_events['P'], nick=nick) log.debug('Creating stats for %s' % player_events['P']) create_player_stats(session=session, player=player, game=game, player_events=player_events, game_meta=game_meta) # update elos try: process_elos(game, session) except Exception as e: log.debug('Error (non-fatal): elo processing failed.') session.commit() log.debug('Success! Stats recorded.') return Response('200 OK') except Exception as e: if session: session.rollback() return e