Clean up the graph, make a new JSON view for accuracy data.
authorAnt Zucaro <azucaro@gmail.com>
Wed, 25 Apr 2012 01:10:49 +0000 (21:10 -0400)
committerAnt Zucaro <azucaro@gmail.com>
Wed, 25 Apr 2012 01:10:49 +0000 (21:10 -0400)
xonstat/__init__.py
xonstat/views/__init__.py
xonstat/views/player.py

index 25b93bb4e7747f9fdca8ccca8f125f8e218e5f15..b1f08d2c5ab93f395167037c548dcda6b7f1c4b7 100755 (executable)
@@ -50,6 +50,10 @@ def main(global_config, **settings):
     config.add_view(player_info, route_name="player_info",
         renderer="player_info.mako")
 
+    config.add_route("player_accuracy", "/player/{id:\d+}/accuracy")
+    config.add_view(player_accuracy, route_name="player_accuracy",
+        renderer="json")
+
     # GAME ROUTES
     config.add_route("game_index", "/games")
     config.add_view(game_index, route_name="game_index",
index 87c5e76ed398caa068060a257abf5436de0beda9..06ab55a5fc304e15b46e0425462ac39b9187f5e7 100755 (executable)
@@ -1,5 +1,6 @@
 from xonstat.views.submission import stats_submit\r
 from xonstat.views.player import player_index, player_info, player_game_index\r
+from xonstat.views.player import player_accuracy\r
 from xonstat.views.game import game_index, game_info, rank_index\r
 from xonstat.views.map import map_info, map_index\r
 from xonstat.views.server import server_info, server_game_index, server_index\r
index 1a0d78ff00d54d8cba7c2ba457f417e77af923ca..1f06b47f4abe46b5402599228108c15f5a7b9843 100755 (executable)
@@ -133,21 +133,27 @@ def get_accuracy_stats(player_id, weapon_cd, games):
 \r
         # Determine the raw accuracy (hit, fired) numbers for $games games\r
         # This is then enumerated to create parameters for a flot graph\r
-        raw_accs = DBSession.query(PlayerWeaponStat.hit, PlayerWeaponStat.fired).\\r
+        raw_accs = DBSession.query(PlayerWeaponStat.game_id, \r
+            PlayerWeaponStat.hit, PlayerWeaponStat.fired).\\r
                 filter(PlayerWeaponStat.player_id == player_id).\\r
                 filter(PlayerWeaponStat.weapon_cd == weapon_cd).\\r
-                order_by(PlayerWeaponStat.create_dt).\\r
+                order_by(PlayerWeaponStat.game_id.desc()).\\r
                 limit(games).\\r
                 all()\r
 \r
+        # they come out in opposite order, so flip them in the right direction\r
+        raw_accs.reverse()\r
+\r
+        games = []\r
         accs = []\r
         for i in range(len(raw_accs)):\r
-            accs.append((i, round(float(raw_accs[i][0])/raw_accs[i][1]*100, 2)))\r
+            games.append((i, raw_accs[i][0]))\r
+            accs.append((i, round(float(raw_accs[i][1])/raw_accs[i][2]*100, 2)))\r
     except:\r
         accs = 0\r
         avg = 0\r
 \r
-    return (avg, accs)\r
+    return (games, avg, accs)\r
 \r
 \r
 def player_info(request):\r
@@ -186,7 +192,7 @@ def player_info(request):
 \r
         # data for the accuracy graph, which is converted into a JSON array for\r
         # usage by flot\r
-        (avg, accs) = get_accuracy_stats(player_id, 'nex', 20)\r
+        (games, avg, accs) = get_accuracy_stats(player_id, 'nex', 20)\r
 \r
         avg = json.dumps(avg)\r
         accs = json.dumps(accs)\r
@@ -258,3 +264,26 @@ def player_game_index(request):
     return {'player_id':player_id,\r
             'games':games,\r
             'pgstats':pgstats}\r
+\r
+def player_accuracy(request):\r
+    """\r
+    Provides a JSON response representing the accuracy for the given weapon.\r
+\r
+    Parameters:\r
+       weapon = which weapon to display accuracy for. Valid values are 'nex',\r
+                'shotgun', 'uzi', and 'minstanex'.\r
+       games = over how many games to display accuracy. Can be up to 50.\r
+    """\r
+    player_id = request.matchdict['id']\r
+    allowed_weapons = ['nex', 'shotgun', 'uzi', 'minstanex']\r
+    weapon_cd = 'nex'\r
+    games = 20\r
+\r
+    if request.params.has_key('weapon'):\r
+        if request.params['weapon'] in allowed_weapons:\r
+            weapon_cd = request.params['weapon']\r
+\r
+    (games, avg, accs) = get_accuracy_stats(player_id, weapon_cd, games)\r
+\r
+    return {'weapon':weapon_cd, 'games':games, 'avg':avg, 'accs':accs}\r
+\r