]> de.git.xonotic.org Git - xonotic/xonstat.git/blobdiff - xonstat/views/game.py
Merge branch 'master' into zykure/wip
[xonotic/xonstat.git] / xonstat / views / game.py
index 70ac7e09be074b492a2deee44d4eca61e8d16761..58d48f89d4a356dafdc853a390373dc0d13a5286 100644 (file)
@@ -5,11 +5,14 @@ import time
 from collections import OrderedDict
 from pyramid.response import Response
 from sqlalchemy import desc, func, over
+from collections import namedtuple
 from webhelpers.paginate import Page, PageURL
 from xonstat.models import *
 from xonstat.util import page_url
 from xonstat.views.helpers import RecentGame, recent_games_q
 
+import random
+
 log = logging.getLogger(__name__)
 
 
@@ -86,10 +89,7 @@ def _game_info_data(request):
     else:
         show_elo = False
 
-    if request.params.has_key('show_latency'):
-        show_latency = True
-    else:
-        show_latency = False
+    show_latency = False
 
     try:
         notfound = False
@@ -106,17 +106,28 @@ def _game_info_data(request):
                 order_by(PlayerGameStat.score).\
                 all()
 
-        tgstats = DBSession.query(TeamGameStat).\
-                filter(TeamGameStat.game_id == game_id).\
-                order_by(TeamGameStat.score).\
-                all()
+        # if at least one player has a valid latency, we'll show the column
+        for pgstat in pgstats:
+            if pgstat.avg_latency is not None:
+                show_latency = True
+
+        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())
+
+        tgstats = q.all()
 
         stats_by_team = OrderedDict()
         for pgstat in pgstats:
             if pgstat.team not in stats_by_team.keys():
                 stats_by_team[pgstat.team] = []
             stats_by_team[pgstat.team].append(pgstat)
-        log.debug(stats_by_team)
 
         captimes = []
         if game.game_type_cd == 'ctf':
@@ -125,6 +136,47 @@ def _game_info_data(request):
                     captimes.append(pgstat)
             captimes = sorted(captimes, key=lambda x:x.fastest)
 
+        teamscores = {}
+        for pgstat in pgstats:
+            if pgstat.team in [5,14,13,10]:
+                team = pgstat.team_html_color()
+                if pgstat.teamscore is not None:
+                    if not teamscores.has_key(team):
+                        teamscores[team] = pgstat.teamscore
+                    else:
+                        if teamscores[team] != pgstat.teamscore:  # this should not happen!
+                            teamscores[team] = None
+        if len(teamscores) == 0:
+            teamscores = None
+            
+        ### RANDOM SCORES FOR TESTING
+        teams = ["red","blue","yellow","pink"]
+        random.shuffle(teams)
+        teamscores = {}
+        for k in range(random.randint(2,4)):
+            team = teams[k-1]
+            teamscores[team] = random.randint(-5,150)
+        ### END
+        
+        #TeamInfo = namedtuple('TeamInfo', ['team','scoreboardpos','playercount','teamscore'])
+        #
+        #teams = {}
+        #last_pgs = pgstats[0]
+        #for pgstat in pgstats:
+        #    if pgstat.team != last_pgs.team:
+        #        teams[last_pgs.scoreboardpos] = TeamInfo(
+        #                team=last_pgs.team,
+        #                scoreboardpos=last_pgs.scoreboardpos,
+        #                playercount=pgstat.scoreboardpos-last_pgs.scoreboardpos,
+        #                teamscore=last_pgs.teamscore)
+        #        last_pgs = pgstat
+        #teams[last_pgs.scoreboardpos] = TeamInfo(
+        #        team=last_pgs.team,
+        #        scoreboardpos=last_pgs.scoreboardpos,
+        #        playercount=pgstat.scoreboardpos-last_pgs.scoreboardpos,
+        #        teamscore=last_pgs.teamscore)
+        #print teams
+
         pwstats = {}
         for (pwstat, pgstat, weapon) in DBSession.query(PlayerWeaponStat, PlayerGameStat, Weapon).\
                 filter(PlayerWeaponStat.game_id == game_id).\
@@ -154,6 +206,7 @@ def _game_info_data(request):
         tgstats = None
         pwstats = None
         captimes = None
+        teams = None
         show_elo = False
         show_latency = False
         stats_by_team = None
@@ -167,6 +220,8 @@ def _game_info_data(request):
             'tgstats':tgstats,
             'pwstats':pwstats,
             'captimes':captimes,
+            'teams':teams,
+            'teamscores':teamscores,
             'show_elo':show_elo,
             'show_latency':show_latency,
             'stats_by_team':stats_by_team,