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 xonstat.models import *
7 from xonstat.util import *
9 log = logging.getLogger(__name__)
11 def _main_index_data(request):
13 leaderboard_lifetime = int(
14 request.registry.settings['xonstat.leaderboard_lifetime'])
16 leaderboard_lifetime = 30
18 leaderboard_count = 10
19 recent_games_count = 20
22 duel_ranks = DBSession.query(PlayerRank.player_id, PlayerRank.nick,
24 filter(PlayerRank.game_type_cd=='duel').\
25 order_by(PlayerRank.rank).\
26 limit(leaderboard_count).all()
28 duel_ranks = [(player_id, html_colors(nick), elo) \
29 for (player_id, nick, elo) in duel_ranks]
32 ctf_ranks = DBSession.query(PlayerRank.player_id, PlayerRank.nick,
34 filter(PlayerRank.game_type_cd=='ctf').\
35 order_by(PlayerRank.rank).\
36 limit(leaderboard_count).all()
38 ctf_ranks = [(player_id, html_colors(nick), elo) \
39 for (player_id, nick, elo) in ctf_ranks]
42 dm_ranks = DBSession.query(PlayerRank.player_id, PlayerRank.nick,
44 filter(PlayerRank.game_type_cd=='dm').\
45 order_by(PlayerRank.rank).\
46 limit(leaderboard_count).all()
48 dm_ranks = [(player_id, html_colors(nick), elo) \
49 for (player_id, nick, elo) in dm_ranks]
51 right_now = datetime.utcnow()
52 back_then = datetime.utcnow() - timedelta(days=leaderboard_lifetime)
54 # top players by playing time
55 top_players = DBSession.query(Player.player_id, Player.nick,
56 func.sum(PlayerGameStat.alivetime)).\
57 filter(Player.player_id == PlayerGameStat.player_id).\
58 filter(Player.player_id > 2).\
59 filter(expr.between(PlayerGameStat.create_dt, back_then, right_now)).\
60 order_by(expr.desc(func.sum(PlayerGameStat.alivetime))).\
61 group_by(Player.nick).\
62 group_by(Player.player_id).limit(leaderboard_count).all()
64 top_players = [(player_id, html_colors(nick), score) \
65 for (player_id, nick, score) in top_players]
67 # top servers by number of total players played
68 top_servers = DBSession.query(Server.server_id, Server.name,
70 filter(Game.server_id==Server.server_id).\
71 filter(expr.between(Game.create_dt, back_then, right_now)).\
72 order_by(expr.desc(func.count(Game.game_id))).\
73 group_by(Server.server_id).\
74 group_by(Server.name).limit(leaderboard_count).all()
76 # top maps by total times played
77 top_maps = DBSession.query(Game.map_id, Map.name,
79 filter(Map.map_id==Game.map_id).\
80 filter(expr.between(Game.create_dt, back_then, right_now)).\
81 order_by(expr.desc(func.count())).\
82 group_by(Game.map_id).\
83 group_by(Map.name).limit(leaderboard_count).all()
85 # recent games played in descending order
86 recent_games = DBSession.query(Game, Server, Map, PlayerGameStat).\
87 filter(Game.server_id==Server.server_id).\
88 filter(Game.map_id==Map.map_id).\
89 filter(PlayerGameStat.game_id==Game.game_id).\
90 filter(PlayerGameStat.rank==1).\
91 filter(expr.between(Game.create_dt, back_then, right_now)).\
92 order_by(expr.desc(Game.start_dt)).limit(recent_games_count).all()
94 return {'top_players':top_players,
95 'top_servers':top_servers,
97 'recent_games':recent_games,
98 'duel_ranks':duel_ranks,
99 'ctf_ranks':ctf_ranks,
104 def main_index(request):
106 Display the main page information.
108 mainindex_data = _main_index_data(request)
110 # FIXME: code clone, should get these from _main_index_data
111 leaderboard_count = 10
112 recent_games_count = 20
114 for i in range(leaderboard_count-len(mainindex_data['duel_ranks'])):
115 mainindex_data['duel_ranks'].append(('-', '-', '-'))
117 for i in range(leaderboard_count-len(mainindex_data['ctf_ranks'])):
118 mainindex_data['ctf_ranks'].append(('-', '-', '-'))
120 for i in range(leaderboard_count-len(mainindex_data['dm_ranks'])):
121 mainindex_data['dm_ranks'].append(('-', '-', '-'))
123 for i in range(leaderboard_count-len(mainindex_data['top_players'])):
124 mainindex_data['top_players'].append(('-', '-', '-'))
126 for i in range(leaderboard_count-len(mainindex_data['top_servers'])):
127 mainindex_data['top_servers'].append(('-', '-', '-'))
129 for i in range(leaderboard_count-len(mainindex_data['top_maps'])):
130 mainindex_data['top_maps'].append(('-', '-', '-'))
132 for i in range(recent_games_count-len(mainindex_data['recent_games'])):
133 mainindex_data['recent_games'].append(('-', '-', '-', '-'))
135 return mainindex_data