]> de.git.xonotic.org Git - xonotic/xonstat.git/blob - xonstat/views/server.py
Merge pull request #12 from nyov/nyov/conversion
[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 pyramid.response import Response
7 from sqlalchemy import desc
8 from webhelpers.paginate import Page, PageURL
9 from xonstat.models import *
10 from xonstat.util import page_url, html_colors
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_info_data(request):
41     server_id = request.matchdict['id']
42
43     try: 
44         leaderboard_lifetime = int(
45                 request.registry.settings['xonstat.leaderboard_lifetime'])
46     except:
47         leaderboard_lifetime = 30
48
49     leaderboard_count = 10
50     recent_games_count = 20
51
52     try:
53         server = DBSession.query(Server).filter_by(server_id=server_id).one()
54
55         # top maps by total times played
56         top_maps = DBSession.query(Game.map_id, Map.name, 
57                 func.count()).\
58                 filter(Map.map_id==Game.map_id).\
59                 filter(Game.server_id==server.server_id).\
60                 filter(Game.create_dt > 
61                     (datetime.utcnow() - timedelta(days=leaderboard_lifetime))).\
62                 order_by(expr.desc(func.count())).\
63                 group_by(Game.map_id).\
64                 group_by(Map.name).all()[0:leaderboard_count]
65
66         # top players by score
67         top_scorers = DBSession.query(Player.player_id, Player.nick, 
68                 func.sum(PlayerGameStat.score)).\
69                 filter(Player.player_id == PlayerGameStat.player_id).\
70                 filter(Game.game_id == PlayerGameStat.game_id).\
71                 filter(Game.server_id == server.server_id).\
72                 filter(Player.player_id > 2).\
73                 filter(PlayerGameStat.create_dt > 
74                         (datetime.utcnow() - timedelta(days=leaderboard_lifetime))).\
75                 order_by(expr.desc(func.sum(PlayerGameStat.score))).\
76                 group_by(Player.nick).\
77                 group_by(Player.player_id).all()[0:leaderboard_count]
78
79         top_scorers = [(player_id, html_colors(nick), score) \
80                 for (player_id, nick, score) in top_scorers]
81
82         # top players by playing time
83         top_players = DBSession.query(Player.player_id, Player.nick, 
84                 func.sum(PlayerGameStat.alivetime)).\
85                 filter(Player.player_id == PlayerGameStat.player_id).\
86                 filter(Game.game_id == PlayerGameStat.game_id).\
87                 filter(Game.server_id == server.server_id).\
88                 filter(Player.player_id > 2).\
89                 filter(PlayerGameStat.create_dt > 
90                         (datetime.utcnow() - timedelta(days=leaderboard_lifetime))).\
91                 order_by(expr.desc(func.sum(PlayerGameStat.alivetime))).\
92                 group_by(Player.nick).\
93                 group_by(Player.player_id).all()[0:leaderboard_count]
94
95         top_players = [(player_id, html_colors(nick), score) \
96                 for (player_id, nick, score) in top_players]
97
98         # recent games played in descending order
99         recent_games = DBSession.query(Game, Server, Map, PlayerGameStat).\
100             filter(Game.server_id==Server.server_id).\
101             filter(Game.map_id==Map.map_id).\
102             filter(PlayerGameStat.game_id==Game.game_id).\
103             filter(PlayerGameStat.rank==1).\
104             filter(Server.server_id==server.server_id).\
105             order_by(expr.desc(Game.start_dt)).all()[0:recent_games_count]
106
107     except Exception as e:
108         server = None
109         recent_games = None
110         top_players = None
111         raise e
112     return {'server':server,
113             'recent_games':recent_games,
114             'top_players': top_players,
115             'top_scorers': top_scorers,
116             'top_maps': top_maps,
117             }
118
119
120 def server_info(request):
121     """
122     List the stored information about a given server.
123     """
124     serverinfo_data =  _server_info_data(request)
125
126     # FIXME: code clone, should get these from _server_info_data
127     leaderboard_count = 10
128     recent_games_count = 20
129
130     for i in range(leaderboard_count-len(serverinfo_data['top_maps'])):
131         serverinfo_data['top_maps'].append(('-', '-', '-'))
132
133     for i in range(leaderboard_count-len(serverinfo_data['top_scorers'])):
134         serverinfo_data['top_scorers'].append(('-', '-', '-'))
135
136     for i in range(leaderboard_count-len(serverinfo_data['top_players'])):
137         serverinfo_data['top_players'].append(('-', '-', '-'))
138
139     for i in range(recent_games_count-len(serverinfo_data['recent_games'])):
140         serverinfo_data['recent_games'].append(('-', '-', '-', '-'))
141
142     return serverinfo_data
143
144
145 def _server_game_index_data(request):
146     server_id = request.matchdict['server_id']
147     current_page = request.matchdict['page']
148
149     try:
150         server = DBSession.query(Server).filter_by(server_id=server_id).one()
151
152         games_q = DBSession.query(Game, Server, Map).\
153                 filter(Game.server_id == server_id).\
154                 filter(Game.server_id == Server.server_id).\
155                 filter(Game.map_id == Map.map_id).\
156                 order_by(Game.game_id.desc())
157
158         games = Page(games_q, current_page, url=page_url)
159     except Exception as e:
160         server = None
161         games = None
162         raise e
163
164     return {'games':games,
165             'server':server}
166
167
168 def server_game_index(request):
169     """
170     List the games played on a given server. Paginated.
171     """
172     return _server_game_index_data(request)