]> de.git.xonotic.org Git - xonotic/xonstat.git/blob - xonstat/views/game.py
Fix merge conflict, change indentation a bit.
[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         pwstats = {}
73         for (pwstat, pgstat, weapon) in DBSession.query(PlayerWeaponStat, PlayerGameStat, Weapon).\
74                 filter(PlayerWeaponStat.game_id == game_id).\
75                 filter(PlayerWeaponStat.weapon_cd == Weapon.weapon_cd).\
76                 filter(PlayerWeaponStat.player_game_stat_id == \
77                     PlayerGameStat.player_game_stat_id).\
78                 order_by(PlayerGameStat.rank).\
79                 order_by(PlayerGameStat.score).\
80                 order_by(Weapon.descr).\
81                 all():
82                     if pgstat.player_game_stat_id not in pwstats:
83                         pwstats[pgstat.player_game_stat_id] = []
84
85                     # NOTE adding pgstat to position 6 in order to display nick.
86                     # You have to use a slice [0:5] to pass to the accuracy 
87                     # template
88                     pwstats[pgstat.player_game_stat_id].append((weapon.descr, 
89                         weapon.weapon_cd, pwstat.actual, pwstat.max, 
90                         pwstat.hit, pwstat.fired, pgstat))
91
92     except Exception as inst:
93         game = None
94         server = None
95         map = None
96         pgstats = None
97         pwstats = None
98         raise inst
99
100     return {'game':game,
101             'server':server,
102             'map':map,
103             'pgstats':pgstats,
104             'pwstats':pwstats,
105             }
106
107
108 def game_info(request):
109     """
110     List the game stats (scoreboard) for a particular game. Paginated.
111     """
112     return _game_info_data(request)
113
114
115 def game_info_json(request):
116     """
117     List the game stats (scoreboard) for a particular game. Paginated. JSON.
118     """
119     return [{'status':'not implemented'}]
120
121
122 def _rank_index_data(request):
123     if request.params.has_key('page'):
124         current_page = request.params['page']
125     else:
126         current_page = 1
127
128     game_type_cd = request.matchdict['game_type_cd']
129
130     ranks_q = DBSession.query(PlayerRank).\
131             filter(PlayerRank.game_type_cd==game_type_cd).\
132             order_by(PlayerRank.rank)
133
134     ranks = Page(ranks_q, current_page, url=page_url)
135
136     if len(ranks) == 0:
137         ranks = None
138
139     return {
140             'ranks':ranks,
141             'game_type_cd':game_type_cd,
142            }
143
144
145 def rank_index(request):
146     """
147     Provide a list of gametype ranks, paginated.
148     """
149     return _rank_index_data(request)
150
151
152 def rank_index_json(request):
153     """
154     Provide a list of gametype ranks, paginated. JSON.
155     """
156     return [{'status':'not implemented'}]