2 import sqlalchemy.sql.functions as func
3 import sqlalchemy.sql.expression as expr
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
12 log = logging.getLogger(__name__)
14 def _server_index_data(request):
15 if request.params.has_key('page'):
16 current_page = request.params['page']
21 server_q = DBSession.query(Server).\
22 order_by(Server.server_id.desc())
24 servers = Page(server_q, current_page, items_per_page=10, url=page_url)
27 except Exception as e:
30 return {'servers':servers, }
33 def server_index(request):
35 Provides a list of all the current servers.
37 return _server_index_data(request)
40 def server_index_json(request):
42 Provides a list of all the current servers. JSON.
44 return [{'status':'not implemented'}]
47 def _server_info_data(request):
48 server_id = request.matchdict['id']
51 leaderboard_lifetime = int(
52 request.registry.settings['xonstat.leaderboard_lifetime'])
54 leaderboard_lifetime = 30
56 leaderboard_count = 10
57 recent_games_count = 20
60 server = DBSession.query(Server).filter_by(server_id=server_id).one()
62 # top maps by total times played
63 top_maps = DBSession.query(Game.map_id, Map.name,
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]
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]
86 top_scorers = [(player_id, html_colors(nick), score) \
87 for (player_id, nick, score) in top_scorers]
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]
102 top_players = [(player_id, html_colors(nick), score) \
103 for (player_id, nick, score) in top_players]
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]
109 except Exception as 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,
122 def server_info(request):
124 List the stored information about a given server.
126 serverinfo_data = _server_info_data(request)
128 # FIXME: code clone, should get these from _server_info_data
129 leaderboard_count = 10
130 recent_games_count = 20
132 for i in range(leaderboard_count-len(serverinfo_data['top_maps'])):
133 serverinfo_data['top_maps'].append(('-', '-', '-'))
135 for i in range(leaderboard_count-len(serverinfo_data['top_scorers'])):
136 serverinfo_data['top_scorers'].append(('-', '-', '-'))
138 for i in range(leaderboard_count-len(serverinfo_data['top_players'])):
139 serverinfo_data['top_players'].append(('-', '-', '-'))
141 return serverinfo_data
144 def server_info_json(request):
146 List the stored information about a given server. JSON.
148 return [{'status':'not implemented'}]
151 def _server_game_index_data(request):
152 server_id = request.matchdict['server_id']
153 current_page = request.matchdict['page']
156 server = DBSession.query(Server).filter_by(server_id=server_id).one()
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())
164 games = Page(games_q, current_page, url=page_url)
165 except Exception as e:
170 return {'games':games,
174 def server_game_index(request):
176 List the games played on a given server. Paginated.
178 return _server_game_index_data(request)
181 def server_game_index_json(request):
183 List the games played on a given server. Paginated. JSON.
185 return [{'status':'not implemented'}]