From: Ant Zucaro Date: Sun, 27 Oct 2013 02:03:02 +0000 (-0400) Subject: Add a view to show the most active players. X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=40c7131a7da4087c1fa07d7f369a12765eddb497;p=xonotic%2Fxonstat.git Add a view to show the most active players. For the longest time we have had a box on the front page showing the "most active players" by playing time. We only showed ten at a time, and I've since received tons of requests for more. This commit provides that more. --- diff --git a/xonstat/__init__.py b/xonstat/__init__.py index 1c80afb..6f21dd2 100644 --- a/xonstat/__init__.py +++ b/xonstat/__init__.py @@ -81,6 +81,9 @@ def main(global_config, **settings): config.add_route("player_weaponstats_data_json", "/player/{id:\d+}/weaponstats.json") config.add_view(player_weaponstats_data_json, route_name="player_weaponstats_data_json", renderer="jsonp") + config.add_route("top_players_by_time", "/topactive") + config.add_view(top_players_by_time, route_name="top_players_by_time", renderer="top_players_by_time.mako") + # GAME ROUTES config.add_route("game_info", "/game/{id:\d+}") config.add_view(game_info, route_name="game_info", renderer="game_info.mako") diff --git a/xonstat/views/__init__.py b/xonstat/views/__init__.py index a71c29d..7eabd5b 100644 --- a/xonstat/views/__init__.py +++ b/xonstat/views/__init__.py @@ -26,4 +26,4 @@ from xonstat.views.search import search_json from xonstat.views.exceptions import notfound -from xonstat.views.main import main_index +from xonstat.views.main import main_index, top_players_by_time diff --git a/xonstat/views/main.py b/xonstat/views/main.py index 30833e4..465b747 100644 --- a/xonstat/views/main.py +++ b/xonstat/views/main.py @@ -8,6 +8,7 @@ from pyramid.response import Response from xonstat.models import * from xonstat.util import * from xonstat.views.helpers import RecentGame, recent_games_q +from webhelpers.paginate import Page log = logging.getLogger(__name__) @@ -88,28 +89,46 @@ def get_ranks(game_type_cd): return ranks -@cache_region('hourly_term') -def top_players_by_time(cutoff_days): +def top_players_by_time_q(cutoff_days): """ - The top players by the amount of time played during a date range. + Query for the top players by the amount of time played during a date range. Games older than cutoff_days days old are ignored. """ - # how many to retrieve - count = 10 # only games played during this range are considered right_now = datetime.utcnow() cutoff_dt = right_now - timedelta(days=cutoff_days) - top_players = DBSession.query(Player.player_id, Player.nick, + top_players_q = DBSession.query(Player.player_id, Player.nick, func.sum(PlayerGameStat.alivetime)).\ filter(Player.player_id == PlayerGameStat.player_id).\ filter(Player.player_id > 2).\ filter(expr.between(PlayerGameStat.create_dt, cutoff_dt, right_now)).\ order_by(expr.desc(func.sum(PlayerGameStat.alivetime))).\ group_by(Player.nick).\ - group_by(Player.player_id).limit(count).all() + group_by(Player.player_id) + + return top_players_q + + +@cache_region('hourly_term') +def get_top_players_by_time(cutoff_days): + """ + The top players by the amount of time played during a date range. + + Games older than cutoff_days days old are ignored. + """ + # how many to retrieve + count = 10 + + # only games played during this range are considered + right_now = datetime.utcnow() + cutoff_dt = right_now - timedelta(days=cutoff_days) + + top_players_q = top_players_by_time_q(cutoff_days) + + top_players = top_players_q.limit(count).all() top_players = [(player_id, html_colors(nick), score) \ for (player_id, nick, score) in top_players] @@ -131,7 +150,7 @@ def top_servers_by_players(cutoff_days): right_now = datetime.utcnow() cutoff_dt = right_now - timedelta(days=cutoff_days) - top_servers = DBSession.query(Server.server_id, Server.name, + top_servers = DBSession.query(Server.server_id, Server.name, func.count()).\ filter(Game.server_id==Server.server_id).\ filter(expr.between(Game.create_dt, cutoff_dt, right_now)).\ @@ -194,7 +213,7 @@ def _main_index_data(request): back_then = datetime.utcnow() - timedelta(days=leaderboard_lifetime) # top players by playing time - top_players = top_players_by_time(leaderboard_lifetime) + top_players = get_top_players_by_time(leaderboard_lifetime) # top servers by number of total players played top_servers = top_servers_by_players(leaderboard_lifetime) @@ -242,3 +261,19 @@ def main_index_json(request): JSON output of the main page information. """ return [{'status':'not implemented'}] + + +def top_players_by_time(request): + current_page = request.params.get('page', 1) + + cutoff_days = int(request.registry.settings.\ + get('xonstat.leaderboard_lifetime', 30)) + + top_players_q = top_players_by_time_q(cutoff_days) + + top_players = Page(top_players_q, current_page, items_per_page=25, url=page_url) + + top_players.items = [(player_id, html_colors(nick), score) \ + for (player_id, nick, score) in top_players.items] + + return {'top_players':top_players}