Merge branch 'master' into zykure/wip
[xonotic/xonstat.git] / xonstat / views / player.py
index 5e56b08dafd6baca972d978914a2310fe38699d5..6b0b9459f8abe760bdf0f3e211b6fee0e26c3bdb 100644 (file)
@@ -3,6 +3,7 @@ import logging
 import pyramid.httpexceptions
 import sqlalchemy as sa
 import sqlalchemy.sql.functions as func
+import sqlalchemy.sql.expression as expr
 from calendar import timegm
 from collections import namedtuple
 from webhelpers.paginate import Page
@@ -788,7 +789,11 @@ def player_hashkey_info_data(request):
     # hashkey = unquote(hashkey)
 
     # if using request verification to obtain the hashkey
-    (hashkey, status) = verify_request(request)
+    (idfp, status) = verify_request(request)
+    log.debug("d0_blind_id verification: idfp={0} status={1}\n".format(idfp, status))
+
+    log.debug("\n----- BEGIN REQUEST BODY -----\n" + request.body +
+            "----- END REQUEST BODY -----\n\n")
 
     # if config is to *not* verify requests and we get nothing back, this
     # query will return nothing and we'll 404.
@@ -796,7 +801,7 @@ def player_hashkey_info_data(request):
         player = DBSession.query(Player).\
                 filter(Player.player_id == Hashkey.player_id).\
                 filter(Player.active_ind == True).\
-                filter(Hashkey.hashkey == hashkey).one()
+                filter(Hashkey.hashkey == idfp).one()
 
         games_played      = get_games_played(player.player_id)
         overall_stats     = get_overall_stats(player.player_id)
@@ -809,7 +814,7 @@ def player_hashkey_info_data(request):
         raise pyramid.httpexceptions.HTTPNotFound
 
     return {'player':player,
-            'hashkey':hashkey,
+            'hashkey':idfp,
             'games_played':games_played,
             'overall_stats':overall_stats,
             'fav_maps':fav_maps,
@@ -912,6 +917,12 @@ def player_elo_info_data(request):
     """
     Provides elo information on a specific player. Raw data is returned.
     """
+    (idfp, status) = verify_request(request)
+    log.debug("d0_blind_id verification: idfp={0} status={1}\n".format(idfp, status))
+
+    log.debug("\n----- BEGIN REQUEST BODY -----\n" + request.body +
+            "----- END REQUEST BODY -----\n\n")
+
     hashkey = request.matchdict['hashkey']
 
     # the incoming hashkey is double quoted, and WSGI unquotes once...
@@ -979,41 +990,58 @@ def player_elo_info_text(request):
 
 
 def player_captimes_data(request):
-    player_id = int(request.matchdict['id'])
+    player_id = int(request.matchdict['player_id'])
     if player_id <= 2:
         player_id = -1;
 
-    PlayerCaptimes = namedtuple('PlayerCaptimes', ['fastest_cap', 'create_dt', 'create_dt_epoch', 'create_dt_fuzzy',
-        'player_id', 'game_id', 'map_id', 'map_name', 'server_id', 'server_name'])
+    if request.params.has_key('page'):
+        current_page = request.params['page']
+    else:
+        current_page = 1
 
-    dbquery = DBSession.query('fastest_cap', 'create_dt', 'player_id', 'game_id', 'map_id',
-                'map_name', 'server_id', 'server_name').\
-            from_statement(
-                "SELECT ct.fastest_cap, "
-                       "ct.create_dt, "
-                       "ct.player_id, "
-                       "ct.game_id, "
-                       "ct.map_id, "
-                       "m.name map_name, "
-                       "g.server_id, "
-                       "s.name server_name "
-                "FROM   player_map_captimes ct, "
-                       "games g, "
-                       "maps m, "
-                       "servers s "
-                "WHERE  ct.player_id = :player_id "
-                  "AND  g.game_id = ct.game_id "
-                  "AND  g.server_id = s.server_id "
-                  "AND  m.map_id = ct.map_id "
-                #"ORDER  BY ct.fastest_cap "
-                "ORDER  BY ct.create_dt desc"
-            ).params(player_id=player_id).all()
+    PlayerCaptimes = namedtuple('PlayerCaptimes', ['fastest_cap',
+            'create_dt', 'create_dt_epoch', 'create_dt_fuzzy',
+            'player_id', 'game_id', 'map_id', 'map_name', 'server_id', 'server_name'])
 
     player = DBSession.query(Player).filter_by(player_id=player_id).one()
 
-    player_captimes = []
-    for row in dbquery:
-        player_captimes.append(PlayerCaptimes(
+    #pct_q = DBSession.query('fastest_cap', 'create_dt', 'player_id', 'game_id', 'map_id',
+    #            'map_name', 'server_id', 'server_name').\
+    #        from_statement(
+    #            "SELECT ct.fastest_cap, "
+    #                   "ct.create_dt, "
+    #                   "ct.player_id, "
+    #                   "ct.game_id, "
+    #                   "ct.map_id, "
+    #                   "m.name map_name, "
+    #                   "g.server_id, "
+    #                   "s.name server_name "
+    #            "FROM   player_map_captimes ct, "
+    #                   "games g, "
+    #                   "maps m, "
+    #                   "servers s "
+    #            "WHERE  ct.player_id = :player_id "
+    #              "AND  g.game_id = ct.game_id "
+    #              "AND  g.server_id = s.server_id "
+    #              "AND  m.map_id = ct.map_id "
+    #            #"ORDER  BY ct.fastest_cap "
+    #            "ORDER  BY ct.create_dt desc"
+    #        ).params(player_id=player_id)
+
+    try:
+        pct_q = DBSession.query(PlayerCaptime.fastest_cap, PlayerCaptime.create_dt,
+                PlayerCaptime.player_id, PlayerCaptime.game_id, PlayerCaptime.map_id,
+                Map.name.label('map_name'), Game.server_id, Server.name.label('server_name')).\
+                filter(PlayerCaptime.player_id==player_id).\
+                filter(PlayerCaptime.game_id==Game.game_id).\
+                filter(PlayerCaptime.map_id==Map.map_id).\
+                filter(Game.server_id==Server.server_id).\
+                order_by(expr.asc(PlayerCaptime.fastest_cap))  # or PlayerCaptime.create_dt
+
+        player_captimes = Page(pct_q, current_page, items_per_page=20, url=page_url)
+
+        # replace the items in the canned pagination class with more rich ones
+        player_captimes.items = [PlayerCaptimes(
                 fastest_cap=row.fastest_cap,
                 create_dt=row.create_dt,
                 create_dt_epoch=timegm(row.create_dt.timetuple()),
@@ -1023,14 +1051,18 @@ def player_captimes_data(request):
                 map_id=row.map_id,
                 map_name=row.map_name,
                 server_id=row.server_id,
-                server_name=row.server_name,
-            ))
+                server_name=row.server_name
+                ) for row in player_captimes.items]
+
+    except Exception as e:
+        player = None
+        player_captimes = None
 
     return {
-            'captimes':player_captimes,
             'player_id':player_id,
-            'player_url':request.route_url('player_info', id=player_id),
             'player':player,
+            'captimes':player_captimes,
+            #'player_url':request.route_url('player_info', id=player_id),
         }