]> de.git.xonotic.org Git - xonotic/xonstat.git/commitdiff
Save the match_id as game_meta, and use it to keep games unique.
authorAnt Zucaro <azucaro@gmail.com>
Mon, 5 Dec 2011 17:10:18 +0000 (12:10 -0500)
committerAnt Zucaro <azucaro@gmail.com>
Mon, 5 Dec 2011 17:10:18 +0000 (12:10 -0500)
POST requests have a "match ID" parameter being sent as an "I" record
within the header. This should be stored in the games table accordingly.
Furthermore, a unique constraint should be placed on the games table
such that all combinations of server_id and match_id are unique in that
table. This prevents duplicate games from being entered by either manual
means or via xonstat-queue.

This change stores the 'I' record in the games table upon submission and
will raise an HTTP 200 exception class if a duplicate entry is found. It
is set to 200 because this is an OK submission, but a duplicate and thus
should not be resubmitted.

xonstat/views/submission.py

index e0048d6d2d23cde93bfb42d261f926a999577499..09c0bb6156bdaa061d626410d22b8fc67f283e6d 100755 (executable)
@@ -81,6 +81,7 @@ def has_required_metadata(metadata):
     if 'T' not in metadata or\\r
         'G' not in metadata or\\r
         'M' not in metadata or\\r
+        'I' not in metadata or\\r
         'S' not in metadata:\r
             flg_has_req_metadata = False\r
 \r
@@ -215,7 +216,7 @@ def get_or_create_map(session=None, name=None):
 \r
 \r
 def create_game(session=None, start_dt=None, game_type_cd=None, \r
-        server_id=None, map_id=None, winner=None):\r
+        server_id=None, map_id=None, winner=None, match_id=None):\r
     """\r
     Creates a game. Parameters:\r
 \r
@@ -230,10 +231,20 @@ def create_game(session=None, start_dt=None, game_type_cd=None,
     game_id = session.execute(seq)\r
     game = Game(game_id=game_id, start_dt=start_dt, game_type_cd=game_type_cd,\r
                 server_id=server_id, map_id=map_id, winner=winner)\r
-    session.add(game)\r
-    log.debug("Created game id {0} on server {1}, map {2} at \\r
-            {3}".format(game.game_id, \r
-                server_id, map_id, start_dt))\r
+    game.match_id = match_id\r
+\r
+    try:\r
+        session.query(Game).filter(Game.server_id==server_id).\\r
+                filter(Game.match_id==match_id).one()\r
+        # if a game under the same server and match_id found, \r
+        # this is a duplicate game and can be ignored\r
+        raise pyramid.httpexceptions.HTTPOk\r
+    except NoResultFound, e:\r
+        # server_id/match_id combination not found. game is ok to insert\r
+        session.add(game)\r
+        log.debug("Created game id {0} on server {1}, map {2} at \\r
+                {3}".format(game.game_id, \r
+                    server_id, map_id, start_dt))\r
 \r
     return game\r
 \r
@@ -437,7 +448,7 @@ def parse_body(request):
             if key in 'S' 'n':\r
                 value = unicode(value, 'utf-8')\r
 \r
-            if key in 'V' 'T' 'G' 'M' 'S' 'C' 'R' 'W':\r
+            if key in 'V' 'T' 'G' 'M' 'S' 'C' 'R' 'W' 'I':\r
                 game_meta[key] = value\r
 \r
             if key == 'P':\r
@@ -525,7 +536,7 @@ def stats_submit(request):
                 start_dt=datetime.datetime(\r
                     *time.gmtime(float(game_meta['T']))[:6]), \r
                 server_id=server.server_id, game_type_cd=game_meta['G'], \r
-                   map_id=gmap.map_id)\r
+                   map_id=gmap.map_id, match_id=game_meta['I'])\r
         log.debug(gmap)\r
 \r
         # find or create a record for each player\r