Add a JSON Elo view to support autobalance.
authorAnt Zucaro <azucaro@gmail.com>
Tue, 11 Dec 2012 22:28:02 +0000 (17:28 -0500)
committerAnt Zucaro <azucaro@gmail.com>
Tue, 11 Dec 2012 22:28:02 +0000 (17:28 -0500)
The view at /elo/<hashkey> will send just Elo info about a player.
This is to facilitate a possible autobalance feature where the
Xonotic server will request Elo values for all of the
players entering the game, then use it (among other things) to
figure out who needs to be switched to make the teams right.

xonstat/__init__.py
xonstat/views/__init__.py
xonstat/views/player.py

index 5ee0e4641684e92cbab1c16049903f34dbb7304e..2c02f99cd7dff656751a03bf3a4a38f75213ca9c 100644 (file)
@@ -43,6 +43,9 @@ def main(global_config, **settings):
     config.add_route("player_hashkey_info_json", "/hashkey/{hashkey}")
     config.add_view(player_hashkey_info_json, route_name="player_hashkey_info_json", renderer="jsonp")
 
+    config.add_route("player_elo_info_json", "/elo/{hashkey}")
+    config.add_view(player_elo_info_json, route_name="player_elo_info_json", renderer="jsonp")
+
     config.add_route("player_accuracy",      "/player/{id:\d+}/accuracy")
     config.add_route("player_accuracy_json", "/player/{id:\d+}/accuracy.json")
     config.add_view(player_accuracy_json, route_name="player_accuracy",      renderer="jsonp")
index 879f546e6392618c7312ef53d7e16b47a1190d62..44578491827a197893827a145623cc25c5156afb 100644 (file)
@@ -4,6 +4,7 @@ from xonstat.views.player import player_accuracy
 from xonstat.views.player import player_index_json, player_info_json
 from xonstat.views.player import player_game_index_json, player_accuracy_json
 from xonstat.views.player import player_damage_json, player_hashkey_info_json
+from xonstat.views.player import player_elo_info_json
 
 from xonstat.views.game   import game_index, game_info, rank_index
 from xonstat.views.game   import game_index_json, game_info_json, rank_index_json
index 66e74eb3e78b14871dbc773bd43582ff4405dfd7..91d0ebfb577be773de6650023fbf8a7a8908c082 100644 (file)
@@ -1,6 +1,7 @@
 import datetime
 import json
 import logging
+import pyramid.httpexceptions
 import re
 import sqlalchemy as sa
 import sqlalchemy.sql.functions as func
@@ -860,3 +861,39 @@ def player_hashkey_info_json(request):
         'elos':             elos,
         'ranks':            ranks,
     }]
+
+
+def player_elo_info_data(request):
+    """
+    Provides elo information on a specific player. Raw data is returned.
+    """
+    hashkey = request.matchdict['hashkey']
+    try:
+        player = DBSession.query(Player).\
+                filter(Player.player_id == Hashkey.player_id).\
+                filter(Player.active_ind == True).\
+                filter(Hashkey.hashkey == hashkey).one()
+
+        elos = get_elos(player.player_id)
+
+    except Exception as e:
+        log.debug(e)
+        raise pyramid.httpexceptions.HTTPNotFound
+
+    return {'elos':elos}
+
+
+def player_elo_info_json(request):
+    """
+    Provides elo information on a specific player. JSON.
+    """
+    elo_info = player_elo_info_data(request)
+
+    elos = {}
+    for gt, elo in elo_info['elos'].items():
+        elos[gt] = to_json(elo.to_dict())
+
+    return [{
+        'version':          1,
+        'elos':             elos,
+    }]