]> de.git.xonotic.org Git - xonotic/xonstat.git/blobdiff - xonstat/views/game.py
Add headers and rearrange so things look clean.
[xonotic/xonstat.git] / xonstat / views / game.py
index 629b8ece3a808d65c3c198d83ada1e19a9e73e2a..479b645d2c4142ac7d489d4d8892b6883cbfd00d 100644 (file)
@@ -1,18 +1,14 @@
-import datetime
 import logging
-import re
-import time
 from collections import OrderedDict
+
 from pyramid import httpexceptions
-from pyramid.response import Response
-from sqlalchemy import desc, func, over
 from sqlalchemy.orm.exc import *
-from webhelpers.paginate import Page, PageURL
-from xonstat.models import *
+from webhelpers.paginate import Page
+from xonstat.models import DBSession, Server, Map, Game, PlayerGameStat, PlayerWeaponStat
+from xonstat.models import PlayerGameFragMatrix, TeamGameStat, PlayerRank, GameType, Weapon
 from xonstat.util import page_url
 from xonstat.views.helpers import RecentGame, recent_games_q
 
-
 log = logging.getLogger(__name__)
 
 
@@ -23,17 +19,18 @@ def _game_info_data(request):
     show_elo = bool(request.params.get("show_elo", False))
 
     try:
-        (game, server, map, gametype) = DBSession.query(Game, Server, Map, GameType).\
-                filter(Game.game_id == game_id).\
-                filter(Game.server_id == Server.server_id).\
-                filter(Game.map_id == Map.map_id).\
-                filter(Game.game_type_cd == GameType.game_type_cd).one()
-
-        pgstats = DBSession.query(PlayerGameStat).\
-                filter(PlayerGameStat.game_id == game_id).\
-                order_by(PlayerGameStat.scoreboardpos).\
-                order_by(PlayerGameStat.score).\
-                all()
+        (game, server, map, gametype) = DBSession.query(Game, Server, Map, GameType)\
+            .filter(Game.game_id == game_id)\
+            .filter(Game.server_id == Server.server_id)\
+            .filter(Game.map_id == Map.map_id)\
+            .filter(Game.game_type_cd == GameType.game_type_cd)\
+            .one()
+
+        pgstats = DBSession.query(PlayerGameStat)\
+            .filter(PlayerGameStat.game_id == game_id)\
+            .order_by(PlayerGameStat.scoreboardpos)\
+            .order_by(PlayerGameStat.score)\
+            .all()
 
         # Really old games don't have latency sent, so we have to check. If at
         # least one player has a latency value, we'll show the ping column.
@@ -43,13 +40,12 @@ def _game_info_data(request):
                 show_latency = True
                 break
 
-        q = DBSession.query(TeamGameStat).\
-                filter(TeamGameStat.game_id == game_id)
+        q = DBSession.query(TeamGameStat).filter(TeamGameStat.game_id == game_id)
+
         if game.game_type_cd == 'ctf':
             q = q.order_by(TeamGameStat.caps.desc())
         elif game.game_type_cd == 'ca':
             q = q.order_by(TeamGameStat.rounds.desc())
-        # dom -> ticks, rc -> laps, nb -> goals, as -> objectives
 
         q = q.order_by(TeamGameStat.score.desc())
 
@@ -69,11 +65,11 @@ def _game_info_data(request):
             captimes = sorted(captimes, key=lambda x:x.fastest)
 
         pwstats = {}
-        for (pwstat, weapon) in DBSession.query(PlayerWeaponStat, Weapon).\
-                filter(PlayerWeaponStat.game_id == game_id).\
-                filter(PlayerWeaponStat.weapon_cd == Weapon.weapon_cd).\
-                order_by(PlayerWeaponStat.actual.desc()).\
-                all():
+        for (pwstat, weapon) in DBSession.query(PlayerWeaponStat, Weapon)\
+                .filter(PlayerWeaponStat.game_id == game_id)\
+                .filter(PlayerWeaponStat.weapon_cd == Weapon.weapon_cd)\
+                .order_by(PlayerWeaponStat.actual.desc())\
+                .all():
                     if pwstat.player_game_stat_id not in pwstats:
                         pwstats[pwstat.player_game_stat_id] = []
 
@@ -81,24 +77,37 @@ def _game_info_data(request):
                         weapon.weapon_cd, pwstat.actual, pwstat.max,
                         pwstat.hit, pwstat.fired, pwstat.frags))
 
+        frag_matrix = DBSession.query(PlayerGameFragMatrix)\
+            .filter(PlayerGameFragMatrix.game_id == game_id)\
+            .all()
+
+        matrix_by_pgstat_id = {e.player_game_stat_id: e for e in frag_matrix}
+        if len(matrix_by_pgstat_id):
+            show_frag_matrix = True
+        else:
+            show_frag_matrix = False
+
     except NoResultFound as e:
         raise httpexceptions.HTTPNotFound("Could not find that game!")
 
     except Exception as e:
-        raise inst
-
-    return {'game':game,
-            'server':server,
-            'map':map,
-            'gametype':gametype,
-            'pgstats':pgstats,
-            'tgstats':tgstats,
-            'pwstats':pwstats,
-            'captimes':captimes,
-            'show_elo':show_elo,
-            'show_latency':show_latency,
-            'stats_by_team':stats_by_team,
-            }
+        raise e
+
+    return {
+        'game': game,
+        'server': server,
+        'map': map,
+        'gametype': gametype,
+        'pgstats': pgstats,
+        'tgstats': tgstats,
+        'pwstats': pwstats,
+        'captimes': captimes,
+        'show_elo': show_elo,
+        'show_latency': show_latency,
+        'stats_by_team': stats_by_team,
+        'show_frag_matrix': show_frag_matrix,
+        'matrix_by_pgstat_id': matrix_by_pgstat_id,
+    }
 
 
 def game_info(request):
@@ -116,17 +125,22 @@ def game_info_json(request):
 
 
 def _rank_index_data(request):
-    if request.params.has_key('page'):
-        current_page = request.params['page']
-    else:
-        current_page = 1
+    current_page = request.params.get("page", 1)
+
+    # game type whitelist
+    game_types_allowed = ["ca", "ctf", "dm", "duel", "ft", "ka", "tdm"]
 
     game_type_cd = request.matchdict['game_type_cd']
+    if game_type_cd not in game_types_allowed:
+        raise httpexceptions.HTTPNotFound()
 
     ranks_q = DBSession.query(PlayerRank).\
             filter(PlayerRank.game_type_cd==game_type_cd).\
             order_by(PlayerRank.rank)
 
+    game_type = DBSession.query(GameType).\
+            filter(GameType.game_type_cd == game_type_cd).one()
+
     ranks = Page(ranks_q, current_page, url=page_url)
 
     if len(ranks) == 0:
@@ -135,6 +149,7 @@ def _rank_index_data(request):
     return {
             'ranks':ranks,
             'game_type_cd':game_type_cd,
+            'game_type': game_type,
            }