29fe1f71a26265839fdbc09f38814c893c9fe7e3
[xonotic/xonstat.git] / xonstat / views / main.py
1 import logging
2 import sqlalchemy.sql.functions as func
3 import sqlalchemy.sql.expression as expr
4 from pyramid.response import Response
5 from xonstat.models import *
6 from xonstat.util import *
7
8 log = logging.getLogger(__name__)
9
10 def main_index(request):
11     leaderboard_count = 10
12     recent_games_count = 32
13
14     # top players by score
15     top_players = DBSession.query(Player.player_id, Player.nick, 
16             func.sum(PlayerGameStat.score)).\
17             filter(Player.player_id == PlayerGameStat.player_id).\
18             filter(Player.player_id > 2).\
19             order_by(expr.desc(func.sum(PlayerGameStat.score))).\
20             group_by(Player.nick).\
21             group_by(Player.player_id).all()[0:10]
22
23     top_players = [(player_id, html_colors(nick), score) \
24             for (player_id, nick, score) in top_players]
25
26     for i in range(leaderboard_count-len(top_players)):
27         top_players.append(('-', '-', '-'))
28
29     # top servers by number of total players played
30     top_servers = DBSession.query(Server.server_id, Server.name, 
31             func.count()).\
32             filter(Game.server_id==Server.server_id).\
33             order_by(expr.desc(func.count(Game.game_id))).\
34             group_by(Server.server_id).\
35             group_by(Server.name).all()[0:10]
36
37     for i in range(leaderboard_count-len(top_servers)):
38         top_servers.append(('-', '-', '-'))
39
40     # top maps by total times played
41     top_maps = DBSession.query(Game.map_id, Map.name, 
42             func.count()).\
43             filter(Map.map_id==Game.map_id).\
44             order_by(expr.desc(func.count())).\
45             group_by(Game.map_id).\
46             group_by(Map.name).all()[0:10]
47
48     for i in range(leaderboard_count-len(top_maps)):
49         top_maps.append(('-', '-', '-'))
50
51     recent_games = DBSession.query(Game, Server, Map).\
52             filter(Game.server_id==Server.server_id).\
53             filter(Game.map_id==Map.map_id).\
54             order_by(expr.desc(Game.start_dt)).all()[0:recent_games_count]
55
56     for i in range(recent_games_count-len(recent_games)):
57         recent_games.append(('-', '-', '-'))
58
59     return {'top_players':top_players,
60             'top_servers':top_servers,
61             'top_maps':top_maps,
62             'recent_games':recent_games,
63             }