]> de.git.xonotic.org Git - xonotic/xonstat.git/blob - xonstat/views/game.py
Show the best flag capture times on the game info page.
[xonotic/xonstat.git] / xonstat / views / game.py
1 import datetime
2 import logging
3 import re
4 import time
5 from pyramid.response import Response
6 from sqlalchemy import desc, func, over
7 from webhelpers.paginate import Page, PageURL
8 from xonstat.models import *
9 from xonstat.util import page_url
10
11 log = logging.getLogger(__name__)
12
13
14 def _game_index_data(request):
15     if request.params.has_key('page'):
16         current_page = request.params['page']
17     else:
18         current_page = 1
19
20     games_q = DBSession.query(Game, Server, Map).\
21             filter(Game.server_id == Server.server_id).\
22             filter(Game.map_id == Map.map_id).\
23             order_by(Game.game_id.desc())
24
25     games = Page(games_q, current_page, items_per_page=10, url=page_url)
26
27     pgstats = {}
28     for (game, server, map) in games:
29         pgstats[game.game_id] = DBSession.query(PlayerGameStat).\
30                 filter(PlayerGameStat.game_id == game.game_id).\
31                 order_by(PlayerGameStat.rank).\
32                 order_by(PlayerGameStat.score).all()
33
34     return {'games':games,
35             'pgstats':pgstats}
36
37
38 def game_index(request):
39     """
40     Provides a list of current games, with the associated game stats.
41     These games are ordered by game_id, with the most current ones first.
42     Paginated.
43     """
44     return _game_index_data(request)
45
46
47 def game_index_json(request):
48     """
49     Provides a list of current games, with the associated game stats.
50     These games are ordered by game_id, with the most current ones first.
51     Paginated. JSON.
52     """
53     return [{'status':'not implemented'}]
54
55
56 def _game_info_data(request):
57     game_id = request.matchdict['id']
58     try:
59         notfound = False
60
61         (game, server, map) = DBSession.query(Game, Server, Map).\
62                 filter(Game.game_id == game_id).\
63                 filter(Game.server_id == Server.server_id).\
64                 filter(Game.map_id == Map.map_id).one()
65
66         pgstats = DBSession.query(PlayerGameStat).\
67                 filter(PlayerGameStat.game_id == game_id).\
68                 order_by(PlayerGameStat.rank).\
69                 order_by(PlayerGameStat.score).\
70                 all()
71
72         captimes = []
73         if game.game_type_cd == 'ctf':
74             for pgstat in pgstats:
75                 if pgstat.fastest_cap is not None:
76                     captimes.append(pgstat)
77
78             captimes = sorted(captimes, key=lambda x:x.fastest_cap)
79
80         pwstats = {}
81         for (pwstat, pgstat, weapon) in DBSession.query(PlayerWeaponStat, PlayerGameStat, Weapon).\
82                 filter(PlayerWeaponStat.game_id == game_id).\
83                 filter(PlayerWeaponStat.weapon_cd == Weapon.weapon_cd).\
84                 filter(PlayerWeaponStat.player_game_stat_id == \
85                     PlayerGameStat.player_game_stat_id).\
86                 order_by(PlayerGameStat.rank).\
87                 order_by(PlayerGameStat.score).\
88                 order_by(Weapon.descr).\
89                 all():
90                     if pgstat.player_game_stat_id not in pwstats:
91                         pwstats[pgstat.player_game_stat_id] = []
92
93                     # NOTE adding pgstat to position 6 in order to display nick.
94                     # You have to use a slice [0:5] to pass to the accuracy 
95                     # template
96                     pwstats[pgstat.player_game_stat_id].append((weapon.descr, 
97                         weapon.weapon_cd, pwstat.actual, pwstat.max, 
98                         pwstat.hit, pwstat.fired, pgstat))
99
100     except Exception as inst:
101         game = None
102         server = None
103         map = None
104         pgstats = None
105         pwstats = None
106         captimes = None
107         raise inst
108
109     return {'game':game,
110             'server':server,
111             'map':map,
112             'pgstats':pgstats,
113             'pwstats':pwstats,
114             'captimes':captimes,
115             }
116
117
118 def game_info(request):
119     """
120     List the game stats (scoreboard) for a particular game. Paginated.
121     """
122     return _game_info_data(request)
123
124
125 def game_info_json(request):
126     """
127     List the game stats (scoreboard) for a particular game. Paginated. JSON.
128     """
129     return [{'status':'not implemented'}]
130
131
132 def _rank_index_data(request):
133     if request.params.has_key('page'):
134         current_page = request.params['page']
135     else:
136         current_page = 1
137
138     game_type_cd = request.matchdict['game_type_cd']
139
140     ranks_q = DBSession.query(PlayerRank).\
141             filter(PlayerRank.game_type_cd==game_type_cd).\
142             order_by(PlayerRank.rank)
143
144     ranks = Page(ranks_q, current_page, url=page_url)
145
146     if len(ranks) == 0:
147         ranks = None
148
149     return {
150             'ranks':ranks,
151             'game_type_cd':game_type_cd,
152            }
153
154
155 def rank_index(request):
156     """
157     Provide a list of gametype ranks, paginated.
158     """
159     return _rank_index_data(request)
160
161
162 def rank_index_json(request):
163     """
164     Provide a list of gametype ranks, paginated. JSON.
165     """
166     return [{'status':'not implemented'}]