Update submission handler to use new Q event for teams, handle teamscores (can be...
authorJan Behrens <zykure@web.de>
Sun, 21 Apr 2013 11:21:32 +0000 (13:21 +0200)
committerJan Behrens <zykure@web.de>
Sun, 21 Apr 2013 11:21:32 +0000 (13:21 +0200)
xonstat/views/submission.py

index 1f41dab..5134bd9 100644 (file)
@@ -8,6 +8,7 @@ import sqlalchemy.sql.expression as expr
 from pyramid.response import Response\r
 from sqlalchemy import Sequence\r
 from sqlalchemy.orm.exc import MultipleResultsFound, NoResultFound\r
+from collections import namedtuple\r
 from xonstat.d0_blind_id import d0_blind_id_verify\r
 from xonstat.elo import process_elos\r
 from xonstat.models import *\r
@@ -25,7 +26,9 @@ def parse_stats_submission(body):
     game_meta = {}\r
     events = {}\r
     players = []\r
+    teams = []\r
 \r
+    last_key = None\r
     for line in body.split('\n'):\r
         try:\r
             (key, value) = line.strip().split(' ', 1)\r
@@ -33,18 +36,24 @@ def parse_stats_submission(body):
             # Server (S) and Nick (n) fields can have international characters.\r
             if key in 'S' 'n':\r
                 value = unicode(value, 'utf-8')\r
+                last_key = key\r
 \r
-            if key not in 'P' 'n' 'e' 't' 'i':\r
+            if key not in 'Q' 'P' 'n' 'e' 't' 'i':\r
                 game_meta[key] = value\r
+                last_key = key\r
 \r
-            if key == 'P':\r
-                # if we were working on a player record already, append\r
-                # it and work on a new one (only set team info)\r
+            if key in 'P' 'Q':\r
+                # if we were working on a player or team record already,\r
+                # append it and work on a new one (only set team info)\r
                 if len(events) > 0:\r
-                    players.append(events)\r
+                    if last_key == 'P':\r
+                        players.append(events)\r
+                    elif last_key == 'Q':\r
+                        teams.append(events)\r
                     events = {}\r
 \r
                 events[key] = value\r
+                last_key = key\r
 \r
             if key == 'e':\r
                 (subkey, subvalue) = value.split(' ', 1)\r
@@ -59,9 +68,12 @@ def parse_stats_submission(body):
 \r
     # add the last player we were working on\r
     if len(events) > 0:\r
-        players.append(events)\r
+        if last_key == 'P':\r
+            players.append(events)\r
+        elif last_key == 'Q':\r
+            teams.append(events)\r
 \r
-    return (game_meta, players)\r
+    return (game_meta, players, teams)\r
 \r
 \r
 def is_blank_game(gametype, players):\r
@@ -597,7 +609,7 @@ def create_default_game_stat(session, game_type_cd):
     return pgstat\r
 \r
 \r
-def create_game_stat(session, game_meta, game, server, gmap, player, events):\r
+def create_game_stat(session, game_meta, game, server, gmap, player, teams, events):\r
     """Game stats handler for all game types"""\r
 \r
     game_type_cd = game.game_type_cd\r
@@ -662,6 +674,8 @@ def create_game_stat(session, game_meta, game, server, gmap, player, events):
                 update_fastest_cap(session, player.player_id, game.game_id,\r
                         gmap.map_id, pgstat.fastest)\r
 \r
+    pgstat.teamscore = teams[pgstat.team].score\r
+\r
     # there is no "winning team" field, so we have to derive it\r
     if wins and pgstat.team is not None and game.winner is None:\r
         game.winner = pgstat.team\r
@@ -756,7 +770,7 @@ def submit_stats(request):
                 "----- END REQUEST BODY -----\n\n")\r
 \r
         (idfp, status) = verify_request(request)\r
-        (game_meta, raw_players) = parse_stats_submission(request.body)\r
+        (game_meta, raw_players, raw_teams) = parse_stats_submission(request.body)\r
         revision = game_meta.get('R', 'unknown')\r
         duration = game_meta.get('D', None)\r
 \r
@@ -802,6 +816,16 @@ def submit_stats(request):
                 duration     = duration,\r
                 mod          = game_meta.get('O', None))\r
 \r
+        TeamInfo = namedtuple("TeamInfo", ['team','score'])\r
+        teams = {}\r
+        for events in raw_teams:\r
+            team = events['Q']\r
+            if team.startswith("team#"):\r
+                t = int(team[5:])\r
+                for (key,value) in events.items():\r
+                    if key == 'scoreboard-teamscore':\r
+                        teams[t] = TeamInfo(team=t, score=int(value))\r
+\r
         for events in raw_players:\r
             player = get_or_create_player(\r
                 session = session,\r
@@ -809,7 +833,7 @@ def submit_stats(request):
                 nick    = events.get('n', None))\r
 \r
             pgstat = create_game_stat(session, game_meta, game, server,\r
-                    gmap, player, events)\r
+                    gmap, player, teams, events)\r
 \r
             if should_do_weapon_stats(game_type_cd) and player.player_id > 1:\r
                 pwstats = create_weapon_stats(session, game_meta, game, player,\r