2 import sqlalchemy.sql.functions as func
3 import sqlalchemy.sql.expression as expr
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
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_info_data(request):
41 server_id = request.matchdict['id']
44 leaderboard_lifetime = int(
45 request.registry.settings['xonstat.leaderboard_lifetime'])
47 leaderboard_lifetime = 30
49 leaderboard_count = 10
50 recent_games_count = 20
53 server = DBSession.query(Server).filter_by(server_id=server_id).one()
55 # top maps by total times played
56 top_maps = DBSession.query(Game.map_id, Map.name,
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]
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]
79 top_scorers = [(player_id, html_colors(nick), score) \
80 for (player_id, nick, score) in top_scorers]
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]
95 top_players = [(player_id, html_colors(nick), score) \
96 for (player_id, nick, score) in top_players]
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]
107 except Exception as 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,
120 def server_info(request):
122 List the stored information about a given server.
124 serverinfo_data = _server_info_data(request)
126 # FIXME: code clone, should get these from _server_info_data
127 leaderboard_count = 10
128 recent_games_count = 20
130 for i in range(leaderboard_count-len(serverinfo_data['top_maps'])):
131 serverinfo_data['top_maps'].append(('-', '-', '-'))
133 for i in range(leaderboard_count-len(serverinfo_data['top_scorers'])):
134 serverinfo_data['top_scorers'].append(('-', '-', '-'))
136 for i in range(leaderboard_count-len(serverinfo_data['top_players'])):
137 serverinfo_data['top_players'].append(('-', '-', '-'))
139 for i in range(recent_games_count-len(serverinfo_data['recent_games'])):
140 serverinfo_data['recent_games'].append(('-', '-', '-', '-'))
142 return serverinfo_data
145 def _server_game_index_data(request):
146 server_id = request.matchdict['server_id']
147 current_page = request.matchdict['page']
150 server = DBSession.query(Server).filter_by(server_id=server_id).one()
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())
158 games = Page(games_q, current_page, url=page_url)
159 except Exception as e:
164 return {'games':games,
168 def server_game_index(request):
170 List the games played on a given server. Paginated.
172 return _server_game_index_data(request)