]> de.git.xonotic.org Git - xonotic/xonstat.git/blob - xonstat/views/server.py
Make recent games more bullet-resistant :)
[xonotic/xonstat.git] / xonstat / views / server.py
1 import logging
2 import sqlalchemy.sql.functions as func
3 import sqlalchemy.sql.expression as expr
4 import time
5 from datetime import datetime, timedelta
6 from sqlalchemy import desc
7 from webhelpers.paginate import Page, PageURL
8 from xonstat.models import *
9 from xonstat.util import page_url, html_colors
10 from xonstat.views.helpers import RecentGame, recent_games_q
11
12 log = logging.getLogger(__name__)
13
14 def _server_index_data(request):
15     if request.params.has_key('page'):
16         current_page = request.params['page']
17     else:
18         current_page = 1
19
20     try:
21         server_q = DBSession.query(Server).\
22                 order_by(Server.server_id.desc())
23
24         servers = Page(server_q, current_page, items_per_page=10, url=page_url)
25
26         
27     except Exception as e:
28         servers = None
29
30     return {'servers':servers, }
31
32
33 def server_index(request):
34     """
35     Provides a list of all the current servers.
36     """
37     return _server_index_data(request)
38
39
40 def server_index_json(request):
41     """
42     Provides a list of all the current servers. JSON.
43     """
44     return [{'status':'not implemented'}]
45
46
47 def _server_info_data(request):
48     server_id = request.matchdict['id']
49
50     try: 
51         leaderboard_lifetime = int(
52                 request.registry.settings['xonstat.leaderboard_lifetime'])
53     except:
54         leaderboard_lifetime = 30
55
56     leaderboard_count = 10
57     recent_games_count = 20
58
59     try:
60         server = DBSession.query(Server).filter_by(server_id=server_id).one()
61
62         # top maps by total times played
63         top_maps = DBSession.query(Game.map_id, Map.name, 
64                 func.count()).\
65                 filter(Map.map_id==Game.map_id).\
66                 filter(Game.server_id==server.server_id).\
67                 filter(Game.create_dt > 
68                     (datetime.utcnow() - timedelta(days=leaderboard_lifetime))).\
69                 order_by(expr.desc(func.count())).\
70                 group_by(Game.map_id).\
71                 group_by(Map.name).all()[0:leaderboard_count]
72
73         # top players by score
74         top_scorers = DBSession.query(Player.player_id, Player.nick, 
75                 func.sum(PlayerGameStat.score)).\
76                 filter(Player.player_id == PlayerGameStat.player_id).\
77                 filter(Game.game_id == PlayerGameStat.game_id).\
78                 filter(Game.server_id == server.server_id).\
79                 filter(Player.player_id > 2).\
80                 filter(PlayerGameStat.create_dt > 
81                         (datetime.utcnow() - timedelta(days=leaderboard_lifetime))).\
82                 order_by(expr.desc(func.sum(PlayerGameStat.score))).\
83                 group_by(Player.nick).\
84                 group_by(Player.player_id).all()[0:leaderboard_count]
85
86         top_scorers = [(player_id, html_colors(nick), score) \
87                 for (player_id, nick, score) in top_scorers]
88
89         # top players by playing time
90         top_players = DBSession.query(Player.player_id, Player.nick, 
91                 func.sum(PlayerGameStat.alivetime)).\
92                 filter(Player.player_id == PlayerGameStat.player_id).\
93                 filter(Game.game_id == PlayerGameStat.game_id).\
94                 filter(Game.server_id == server.server_id).\
95                 filter(Player.player_id > 2).\
96                 filter(PlayerGameStat.create_dt > 
97                         (datetime.utcnow() - timedelta(days=leaderboard_lifetime))).\
98                 order_by(expr.desc(func.sum(PlayerGameStat.alivetime))).\
99                 group_by(Player.nick).\
100                 group_by(Player.player_id).all()[0:leaderboard_count]
101
102         top_players = [(player_id, html_colors(nick), score) \
103                 for (player_id, nick, score) in top_players]
104
105         # recent games played in descending order
106         rgs = recent_games_q(server_id=server_id).limit(recent_games_count).all()
107         recent_games = [RecentGame(row) for row in rgs]
108
109     except Exception as e:
110         server = None
111         recent_games = None
112         top_players = None
113         raise e
114     return {'server':server,
115             'recent_games':recent_games,
116             'top_players': top_players,
117             'top_scorers': top_scorers,
118             'top_maps': top_maps,
119             }
120
121
122 def server_info(request):
123     """
124     List the stored information about a given server.
125     """
126     serverinfo_data =  _server_info_data(request)
127
128     # FIXME: code clone, should get these from _server_info_data
129     leaderboard_count = 10
130     recent_games_count = 20
131
132     for i in range(leaderboard_count-len(serverinfo_data['top_maps'])):
133         serverinfo_data['top_maps'].append(('-', '-', '-'))
134
135     for i in range(leaderboard_count-len(serverinfo_data['top_scorers'])):
136         serverinfo_data['top_scorers'].append(('-', '-', '-'))
137
138     for i in range(leaderboard_count-len(serverinfo_data['top_players'])):
139         serverinfo_data['top_players'].append(('-', '-', '-'))
140
141     return serverinfo_data
142
143
144 def server_info_json(request):
145     """
146     List the stored information about a given server. JSON.
147     """
148     return [{'status':'not implemented'}]
149
150
151 def _server_game_index_data(request):
152     server_id = request.matchdict['server_id']
153     current_page = request.matchdict['page']
154
155     try:
156         server = DBSession.query(Server).filter_by(server_id=server_id).one()
157
158         games_q = DBSession.query(Game, Server, Map).\
159                 filter(Game.server_id == server_id).\
160                 filter(Game.server_id == Server.server_id).\
161                 filter(Game.map_id == Map.map_id).\
162                 order_by(Game.game_id.desc())
163
164         games = Page(games_q, current_page, url=page_url)
165     except Exception as e:
166         server = None
167         games = None
168         raise e
169
170     return {'games':games,
171             'server':server}
172
173
174 def server_game_index(request):
175     """
176     List the games played on a given server. Paginated.
177     """
178     return _server_game_index_data(request)
179
180
181 def server_game_index_json(request):
182     """
183     List the games played on a given server. Paginated. JSON.
184     """
185     return [{'status':'not implemented'}]