]> git.rm.cloudns.org Git - xonotic/xonstat.git/commitdiff
Add a view to show the most active players.
authorAnt Zucaro <azucaro@gmail.com>
Sun, 27 Oct 2013 02:03:02 +0000 (22:03 -0400)
committerAnt Zucaro <azucaro@gmail.com>
Sun, 27 Oct 2013 02:03:02 +0000 (22:03 -0400)
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.

xonstat/__init__.py
xonstat/views/__init__.py
xonstat/views/main.py

index 1c80afbaea234f388927113b7c16d8fb5255c4dc..6f21dd2d24a12221dcc04ecb9414322a3acbffd1 100644 (file)
@@ -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")
index a71c29d5ec2dcfa0961a90ba144bbc229a69d397..7eabd5b5540abfc103be6fd196a93eb0703b4af4 100644 (file)
@@ -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
index 30833e48838dbd01b9d572c7adb320674b7411c7..465b7478a75168d06772846dbe0e514ff22badde 100644 (file)
@@ -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}