]> de.git.xonotic.org Git - xonotic/xonstat.git/blobdiff - xonstat/views/player.py
Implemented some (preliminary) JSON API to retrieve player data
[xonotic/xonstat.git] / xonstat / views / player.py
index 6185598e32ec556d22597a39e0507f6ff0e9efcf..a783785324bf8a2ab021503717152b192194caa4 100644 (file)
@@ -10,7 +10,7 @@ from pyramid.url import current_route_url
 from sqlalchemy import desc, distinct
 from webhelpers.paginate import Page, PageURL
 from xonstat.models import *
-from xonstat.util import page_url
+from xonstat.util import page_url, datetime_seconds
 
 log = logging.getLogger(__name__)
 
@@ -83,6 +83,7 @@ def _get_total_stats(player_id):
 
     games = how many games a player has played
     games_breakdown = how many games of given type a player has played (dictionary)
+    games_alivetime = how many time a player has spent in a give game type (dictionary)
     kills = how many kills a player has over all games
     deaths = how many deaths a player has over all games
     suicides = how many suicides a player has over all games
@@ -100,7 +101,7 @@ def _get_total_stats(player_id):
     total_stats = {}
 
     games_played = DBSession.query(
-            Game.game_type_cd, func.count()).\
+            Game.game_type_cd, func.count(), func.sum(PlayerGameStat.alivetime)).\
             filter(Game.game_id == PlayerGameStat.game_id).\
             filter(PlayerGameStat.player_id == player_id).\
             group_by(Game.game_type_cd).\
@@ -109,9 +110,11 @@ def _get_total_stats(player_id):
 
     total_stats['games'] = 0
     total_stats['games_breakdown'] = {}  # this is a dictionary inside a dictionary .. dictception?
-    for (game_type_cd, games) in games_played:
+    total_stats['games_alivetime'] = {}
+    for (game_type_cd, games, alivetime) in games_played:
         total_stats['games'] += games
         total_stats['games_breakdown'][game_type_cd] = games
+        total_stats['games_alivetime'][game_type_cd] = alivetime
 
      # more fields can be added here, e.g. 'collects' for kh games
     (total_stats['kills'], total_stats['deaths'], total_stats['suicides'],
@@ -135,12 +138,22 @@ def _get_total_stats(player_id):
             filter(PlayerGameStat.create_dt > one_month_ago).\
             one()
 
-    (total_stats['wins'],) = DBSession.query(
-            func.count("*")).\
-            filter(Game.game_id == PlayerGameStat.game_id).\
-            filter(PlayerGameStat.player_id == player_id).\
-            filter(Game.winner == PlayerGameStat.team or PlayerGameStat.rank == 1).\
-            one()
+    (total_stats['wins'],) = DBSession.\
+            query("total_wins").\
+            from_statement(
+                "select count(*) total_wins "
+                "from games g, player_game_stats pgs "
+                "where g.game_id = pgs.game_id "
+                "and player_id=:player_id "
+                "and (g.winner = pgs.team or pgs.rank = 1)"
+            ).params(player_id=player_id).one()
+
+#    (total_stats['wins'],) = DBSession.query(
+#            func.count("*")).\
+#            filter(Game.game_id == PlayerGameStat.game_id).\
+#            filter(PlayerGameStat.player_id == player_id).\
+#            filter(Game.winner == PlayerGameStat.team or PlayerGameStat.rank == 1).\
+#            one()
 
     (total_stats['duel_wins'],) = DBSession.query(
             func.count("*")).\
@@ -539,7 +552,44 @@ def player_info_json(request):
     """
     Provides detailed information on a specific player. JSON.
     """
-    return [{'status':'not implemented'}]
+    player_info = player_info_data(request)
+    json_result = {
+        'player': {
+            'id':               player_info['player'].player_id,
+            'nick':             player_info['player'].nick.encode('utf-8'),
+            'stripped_nick':    player_info['player'].nick_strip_colors(),
+            'joined':           player_info['player'].create_dt.isoformat(),
+            },
+        'elos':         player_info['elos'],
+        'ranks':        player_info['ranks'],
+        'total_stats':  {
+                'games':            player_info['total_stats']['games'],
+                'games_breakdown':  player_info['total_stats']['games_breakdown'],
+                'alivetime':        datetime_seconds(player_info['total_stats']['alivetime']),
+                'kills':            player_info['total_stats']['kills'],
+                'deaths':           player_info['total_stats']['deaths'],
+                'suicides':         player_info['total_stats']['suicides'],
+                'wins':             player_info['total_stats']['wins'],
+                # FIXME - current "wins" query is flawed!
+                #'losses':           player_info['total_stats']['loses'],
+            },
+        'recent_games': [
+                {
+                    'game_id':      game.game_id,
+                    'game_type':    game.game_type_cd,
+                    'server':       server.name,
+                    'map':          map.name,
+                    'team':         gamestat.team,
+                    'rank':         gamestat.rank,
+                    'win':          ((gamestat.team != None and gamestat.team == game.winner)
+                                        or (gamestat.team == None and gamestat.rank == 1)),
+                    'time':         game.create_dt.isoformat(),
+                }
+                for (gamestat, game, server, map) in player_info['recent_games'][:5]
+            ],
+        }
+    print json_result
+    return json_result
 
 
 def player_game_index_data(request):