2 import sqlalchemy.sql.functions as func
\r
3 import sqlalchemy.sql.expression as expr
\r
4 from datetime import datetime, timedelta
\r
5 from pyramid.response import Response
\r
6 from sqlalchemy import desc
\r
7 from webhelpers.paginate import Page, PageURL
\r
8 from xonstat.models import *
\r
9 from xonstat.util import page_url
\r
11 log = logging.getLogger(__name__)
\r
13 def _map_index_data(request):
\r
15 Provides a list of all the current maps.
\r
17 if request.params.has_key('page'):
\r
18 current_page = request.params['page']
\r
23 map_q = DBSession.query(Map).\
\r
24 order_by(Map.map_id.desc())
\r
26 maps = Page(map_q, current_page, items_per_page=10, url=page_url)
\r
28 except Exception as e:
\r
31 return {'maps':maps, }
\r
34 def map_index(request):
\r
36 Provides a list of all the current maps.
\r
38 return _map_index_data(request)
\r
41 def map_index_json(request):
\r
43 Provides a JSON-serialized list of all the current maps.
\r
45 view_data = _map_index_data(request)
\r
47 maps = [m.to_dict() for m in view_data['maps']]
\r
52 def map_info(request):
\r
54 List the information stored about a given map.
\r
56 map_id = request.matchdict['id']
\r
59 leaderboard_lifetime = int(
\r
60 request.registry.settings['xonstat.leaderboard_lifetime'])
\r
62 leaderboard_lifetime = 30
\r
64 leaderboard_count = 10
\r
65 recent_games_count = 20
\r
68 gmap = DBSession.query(Map).filter_by(map_id=map_id).one()
\r
70 # recent games on this map
\r
71 recent_games = DBSession.query(Game, Server, Map, PlayerGameStat).\
\r
72 filter(Game.server_id==Server.server_id).\
\r
73 filter(Game.map_id==Map.map_id).\
\r
74 filter(Game.map_id==map_id).\
\r
75 filter(PlayerGameStat.game_id==Game.game_id).\
\r
76 filter(PlayerGameStat.rank==1).\
\r
77 order_by(expr.desc(Game.start_dt)).all()[0:recent_games_count]
\r
79 for i in range(recent_games_count-len(recent_games)):
\r
80 recent_games.append(('-', '-', '-', '-'))
\r
83 # top players by score
\r
84 top_scorers = DBSession.query(Player.player_id, Player.nick,
\r
85 func.sum(PlayerGameStat.score)).\
\r
86 filter(Player.player_id == PlayerGameStat.player_id).\
\r
87 filter(Game.game_id == PlayerGameStat.game_id).\
\r
88 filter(Game.map_id == map_id).\
\r
89 filter(Player.player_id > 2).\
\r
90 filter(PlayerGameStat.create_dt >
\r
91 (datetime.utcnow() - timedelta(days=leaderboard_lifetime))).\
\r
92 order_by(expr.desc(func.sum(PlayerGameStat.score))).\
\r
93 group_by(Player.nick).\
\r
94 group_by(Player.player_id).all()[0:10]
\r
96 top_scorers = [(player_id, html_colors(nick), score) \
\r
97 for (player_id, nick, score) in top_scorers]
\r
99 for i in range(leaderboard_count-len(top_scorers)):
\r
100 top_scorers.append(('-', '-', '-'))
\r
102 # top players by playing time
\r
103 top_players = DBSession.query(Player.player_id, Player.nick,
\r
104 func.sum(PlayerGameStat.alivetime)).\
\r
105 filter(Player.player_id == PlayerGameStat.player_id).\
\r
106 filter(Game.game_id == PlayerGameStat.game_id).\
\r
107 filter(Game.map_id == map_id).\
\r
108 filter(Player.player_id > 2).\
\r
109 filter(PlayerGameStat.create_dt >
\r
110 (datetime.utcnow() - timedelta(days=leaderboard_lifetime))).\
\r
111 order_by(expr.desc(func.sum(PlayerGameStat.alivetime))).\
\r
112 group_by(Player.nick).\
\r
113 group_by(Player.player_id).all()[0:10]
\r
115 top_players = [(player_id, html_colors(nick), score) \
\r
116 for (player_id, nick, score) in top_players]
\r
118 for i in range(leaderboard_count-len(top_players)):
\r
119 top_players.append(('-', '-', '-'))
\r
121 # top servers using/playing this map
\r
122 top_servers = DBSession.query(Server.server_id, Server.name,
\r
123 func.count(Game.game_id)).\
\r
124 filter(Game.server_id == Server.server_id).\
\r
125 filter(Game.map_id == map_id).\
\r
126 filter(Game.create_dt >
\r
127 (datetime.utcnow() - timedelta(days=leaderboard_lifetime))).\
\r
128 order_by(expr.desc(func.count(Game.game_id))).\
\r
129 group_by(Server.name).\
\r
130 group_by(Server.server_id).all()[0:10]
\r
132 for i in range(leaderboard_count-len(top_servers)):
\r
133 top_servers.append(('-', '-', '-'))
\r
135 except Exception as e:
\r
137 return {'gmap':gmap,
\r
138 'recent_games':recent_games,
\r
139 'top_scorers':top_scorers,
\r
140 'top_players':top_players,
\r
141 'top_servers':top_servers,
\r