From edc391976ee9853e804bca79754c1578c991649c Mon Sep 17 00:00:00 2001 From: Ant Zucaro Date: Wed, 18 Nov 2015 20:29:07 -0500 Subject: [PATCH] Provide a plaintext response to stats submissions. For all of XonStat's history the POST requests sent in by servers had plain HTTP responses. This begins to change with this commit, which responds back to the Xonotic server with meaningful information about /what happened/ during the match that was just submitted. Of particular importance are the player_ids and elo delta values that were stored as a result of the game. The template establishing the format for this is submit_stats.mako. It will be extended after we decide what other things we want to include. --- xonstat/__init__.py | 2 +- xonstat/templates/submit_stats.mako | 18 ++++++++++++++++++ xonstat/views/submission.py | 21 ++++++++++++++++++--- 3 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 xonstat/templates/submit_stats.mako diff --git a/xonstat/__init__.py b/xonstat/__init__.py index 55cce5b..760a6f3 100644 --- a/xonstat/__init__.py +++ b/xonstat/__init__.py @@ -55,7 +55,7 @@ def main(global_config, **settings): # MAIN SUBMISSION ROUTE config.add_route("submit_stats", "stats/submit") - config.add_view(submit_stats, route_name="submit_stats") + config.add_view(submit_stats, route_name="submit_stats", renderer="submit_stats.mako") # PLAYER ROUTES config.add_route("player_game_index", "/player/{player_id:\d+}/games") diff --git a/xonstat/templates/submit_stats.mako b/xonstat/templates/submit_stats.mako new file mode 100644 index 0000000..7f9af1b --- /dev/null +++ b/xonstat/templates/submit_stats.mako @@ -0,0 +1,18 @@ +V 1 +R XonStat/1.0 +T ${now} +S ${request.route_url('server_info', id=server.server_id)} +G ${request.route_url('game_info', id=game.game_id)} +M ${request.route_url('map_info', id=gmap.map_id)} +% for player_id in player_ids: +P ${hashkeys[player_id]} +i ${request.route_url('player_info', id=player_id)} +% if player_id in elos: +e elo ${elos[player_id].elo.elo} +e elo_delta ${elos[player_id].elo_delta} +e elo_games ${elos[player_id].elo.games} +e k-factor ${elos[player_id].k} +e score-per-second ${elos[player_id].score_per_second} +e alivetime ${elos[player_id].alivetime} +% endif +% endfor diff --git a/xonstat/views/submission.py b/xonstat/views/submission.py index dab96af..67afe6c 100644 --- a/xonstat/views/submission.py +++ b/xonstat/views/submission.py @@ -5,6 +5,7 @@ import pyramid.httpexceptions import re import time import sqlalchemy.sql.expression as expr +from calendar import timegm from pyramid.response import Response from sqlalchemy import Sequence from sqlalchemy.orm.exc import MultipleResultsFound, NoResultFound @@ -893,6 +894,7 @@ def submit_stats(request): # keep track of the players we've seen player_ids = [] pgstats = [] + hashkeys = {} for events in raw_players: player = get_or_create_player( session = session, @@ -904,11 +906,11 @@ def submit_stats(request): pgstats.append(pgstat) if player.player_id > 1: - anticheats = create_anticheats(session, pgstat, game, player, - events) + anticheats = create_anticheats(session, pgstat, game, player, events) if player.player_id > 2: player_ids.append(player.player_id) + hashkeys[player.player_id] = events['P'] if should_do_weapon_stats(game_type_cd) and player.player_id > 1: pwstats = create_weapon_stats(session, game_meta, game, player, @@ -929,7 +931,20 @@ def submit_stats(request): session.commit() log.debug('Success! Stats recorded.') - return Response('200 OK') + + # plain text response + request.response.content_type = 'text/plain' + + return { + "now" : timegm(datetime.datetime.utcnow().timetuple()), + "server" : server, + "game" : game, + "gmap" : gmap, + "player_ids" : player_ids, + "hashkeys" : hashkeys, + "elos" : ep.wip, + } + except Exception as e: if session: session.rollback() -- 2.39.2