]> de.git.xonotic.org Git - xonotic/xonstat.git/blob - xonstat/views/map.py
Merge branch 'master' of github.com:antzucaro/XonStat
[xonotic/xonstat.git] / xonstat / views / map.py
1 import logging
2 import sqlalchemy.sql.functions as func
3 import sqlalchemy.sql.expression as expr
4 from datetime import datetime, timedelta
5 from pyramid.response import Response
6 from sqlalchemy import desc
7 from webhelpers.paginate import Page, PageURL
8 from xonstat.models import *
9 from xonstat.util import page_url
10
11 log = logging.getLogger(__name__)
12
13 def _map_index_data(request):
14     if request.params.has_key('page'):
15         current_page = request.params['page']
16     else:
17         current_page = 1
18
19     try:
20         map_q = DBSession.query(Map).\
21                 order_by(Map.map_id.desc())
22
23         maps = Page(map_q, current_page, items_per_page=10, url=page_url)
24
25     except Exception as e:
26         maps = None
27
28     return {'maps':maps, }
29
30
31 def map_index(request):
32     """
33     Provides a list of all the current maps. 
34     """
35     return _map_index_data(request)
36
37
38 def map_index_json(request):
39     """
40     Provides a JSON-serialized list of all the current maps. 
41     """
42     view_data =  _map_index_data(request)
43
44     maps = [m.to_dict() for m in view_data['maps']]
45
46     return maps
47
48
49 def _map_info_data(request):
50     map_id = request.matchdict['id']
51
52     try: 
53         leaderboard_lifetime = int(
54                 request.registry.settings['xonstat.leaderboard_lifetime'])
55     except:
56         leaderboard_lifetime = 30
57
58     leaderboard_count = 10
59     recent_games_count = 20
60
61     try:
62         gmap = DBSession.query(Map).filter_by(map_id=map_id).one()
63
64         # recent games on this map
65         recent_games = DBSession.query(Game, Server, Map, PlayerGameStat).\
66             filter(Game.server_id==Server.server_id).\
67             filter(Game.map_id==Map.map_id).\
68             filter(Game.map_id==map_id).\
69             filter(PlayerGameStat.game_id==Game.game_id).\
70             filter(PlayerGameStat.rank==1).\
71             order_by(expr.desc(Game.start_dt)).all()[0:recent_games_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.map_id == map_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.map_id == map_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         # top servers using/playing this map
106         top_servers = DBSession.query(Server.server_id, Server.name, 
107                 func.count(Game.game_id)).\
108                 filter(Game.server_id == Server.server_id).\
109                 filter(Game.map_id == map_id).\
110                 filter(Game.create_dt > 
111                         (datetime.utcnow() - timedelta(days=leaderboard_lifetime))).\
112                 order_by(expr.desc(func.count(Game.game_id))).\
113                 group_by(Server.name).\
114                 group_by(Server.server_id).all()[0:leaderboard_count]
115
116     except Exception as e:
117         gmap = None
118     return {'gmap':gmap,
119             'recent_games':recent_games,
120             'top_scorers':top_scorers,
121             'top_players':top_players,
122             'top_servers':top_servers,
123             }
124
125
126 def map_info(request):
127     """
128     List the information stored about a given map.
129     """
130     mapinfo_data =  _map_info_data(request)
131
132     # FIXME: code clone, should get these from _map_info_data
133     leaderboard_count = 10
134     recent_games_count = 20
135
136     for i in range(recent_games_count-len(mapinfo_data['recent_games'])):
137         mapinfo_data['recent_games'].append(('-', '-', '-', '-'))
138
139     for i in range(leaderboard_count-len(mapinfo_data['top_scorers'])):
140         mapinfo_data['top_scorers'].append(('-', '-', '-'))
141
142     for i in range(leaderboard_count-len(mapinfo_data['top_players'])):
143         mapinfo_data['top_players'].append(('-', '-', '-'))
144
145     for i in range(leaderboard_count-len(mapinfo_data['top_servers'])):
146         mapinfo_data['top_servers'].append(('-', '-', '-'))
147
148     return mapinfo_data