]> de.git.xonotic.org Git - xonotic/xonstat.git/blob - xonstat/views/game.py
Provide an option to show elo deltas 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
59     if request.params.has_key('show_elo'):
60         show_elo = True
61     else:
62         show_elo = False
63
64     try:
65         notfound = False
66
67         (game, server, map) = DBSession.query(Game, Server, Map).\
68                 filter(Game.game_id == game_id).\
69                 filter(Game.server_id == Server.server_id).\
70                 filter(Game.map_id == Map.map_id).one()
71
72         pgstats = DBSession.query(PlayerGameStat).\
73                 filter(PlayerGameStat.game_id == game_id).\
74                 order_by(PlayerGameStat.rank).\
75                 order_by(PlayerGameStat.score).\
76                 all()
77
78         captimes = []
79         if game.game_type_cd == 'ctf':
80             for pgstat in pgstats:
81                 if pgstat.fastest_cap is not None:
82                     captimes.append(pgstat)
83
84             captimes = sorted(captimes, key=lambda x:x.fastest_cap)
85
86         pwstats = {}
87         for (pwstat, pgstat, weapon) in DBSession.query(PlayerWeaponStat, PlayerGameStat, Weapon).\
88                 filter(PlayerWeaponStat.game_id == game_id).\
89                 filter(PlayerWeaponStat.weapon_cd == Weapon.weapon_cd).\
90                 filter(PlayerWeaponStat.player_game_stat_id == \
91                     PlayerGameStat.player_game_stat_id).\
92                 order_by(PlayerGameStat.rank).\
93                 order_by(PlayerGameStat.score).\
94                 order_by(Weapon.descr).\
95                 all():
96                     if pgstat.player_game_stat_id not in pwstats:
97                         pwstats[pgstat.player_game_stat_id] = []
98
99                     # NOTE adding pgstat to position 6 in order to display nick.
100                     # You have to use a slice [0:5] to pass to the accuracy 
101                     # template
102                     pwstats[pgstat.player_game_stat_id].append((weapon.descr, 
103                         weapon.weapon_cd, pwstat.actual, pwstat.max, 
104                         pwstat.hit, pwstat.fired, pgstat))
105
106     except Exception as inst:
107         game = None
108         server = None
109         map = None
110         pgstats = None
111         pwstats = None
112         captimes = None
113         show_elo = False
114         raise inst
115
116     return {'game':game,
117             'server':server,
118             'map':map,
119             'pgstats':pgstats,
120             'pwstats':pwstats,
121             'captimes':captimes,
122             'show_elo':show_elo,
123             }
124
125
126 def game_info(request):
127     """
128     List the game stats (scoreboard) for a particular game. Paginated.
129     """
130     return _game_info_data(request)
131
132
133 def game_info_json(request):
134     """
135     List the game stats (scoreboard) for a particular game. Paginated. JSON.
136     """
137     return [{'status':'not implemented'}]
138
139
140 def _rank_index_data(request):
141     if request.params.has_key('page'):
142         current_page = request.params['page']
143     else:
144         current_page = 1
145
146     game_type_cd = request.matchdict['game_type_cd']
147
148     ranks_q = DBSession.query(PlayerRank).\
149             filter(PlayerRank.game_type_cd==game_type_cd).\
150             order_by(PlayerRank.rank)
151
152     ranks = Page(ranks_q, current_page, url=page_url)
153
154     if len(ranks) == 0:
155         ranks = None
156
157     return {
158             'ranks':ranks,
159             'game_type_cd':game_type_cd,
160            }
161
162
163 def rank_index(request):
164     """
165     Provide a list of gametype ranks, paginated.
166     """
167     return _rank_index_data(request)
168
169
170 def rank_index_json(request):
171     """
172     Provide a list of gametype ranks, paginated. JSON.
173     """
174     return [{'status':'not implemented'}]