]> git.xonotic.org Git - xonotic/xonstat.git/commitdiff
Add a view for top maps by times played.
authorAnt Zucaro <azucaro@gmail.com>
Sun, 27 Oct 2013 20:32:45 +0000 (16:32 -0400)
committerAnt Zucaro <azucaro@gmail.com>
Sun, 27 Oct 2013 20:32:45 +0000 (16:32 -0400)
xonstat/__init__.py
xonstat/templates/top_maps_by_times_played.mako [new file with mode: 0644]
xonstat/views/__init__.py
xonstat/views/main.py

index c066e2cb0567ac634e205643a6c6f4f72b15ab23..b2314b3a1f64415a148dac76b43fc4fb9fdc6db4 100644 (file)
@@ -87,6 +87,9 @@ def main(global_config, **settings):
     config.add_route("top_servers_by_players", "/topservers")
     config.add_view(top_servers_by_players, route_name="top_servers_by_players", renderer="top_servers_by_players.mako")
 
+    config.add_route("top_maps_by_times_played", "/topmaps")
+    config.add_view(top_maps_by_times_played, route_name="top_maps_by_times_played", renderer="top_maps_by_times_played.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/templates/top_maps_by_times_played.mako b/xonstat/templates/top_maps_by_times_played.mako
new file mode 100644 (file)
index 0000000..8de413e
--- /dev/null
@@ -0,0 +1,50 @@
+<%inherit file="base.mako"/>
+<%namespace name="nav" file="nav.mako" />
+<%namespace file="navlinks.mako" import="navlinks" />
+
+<%block name="navigation">
+${nav.nav('maps')}
+</%block>
+
+<%block name="title">
+Active Maps Index
+</%block>
+
+% if not top_maps:
+<h2>Sorry, no maps yet. Get playing!</h2>
+
+% else:
+##### ACTIVE SERVERS #####
+  <div class="span6 offset3">
+    <table class="table table-hover table-condensed">
+      <thead>
+        <tr>
+          <th style="width:40px;">#</th>
+          <th style="width:180px;">Map</th>
+          <th style="width:60px;">Games</th>
+        </tr>
+      </thead>
+      <tbody>
+      ##### this is to get around the actual row_number/rank of the map not being in the actual query
+      <% i = 1 + (top_maps.page-1) * 25%>
+      % for (map_id, name, count) in top_maps:
+        <tr>
+          <td>${i}</td>
+          % if map_id != '-':
+          <td class="nostretch" style="max-width:180px;"><a href="${request.route_url('map_info', id=map_id)}" title="Go to the map info page for ${name}">${name}</a></td>
+          % else:
+          <td class="nostretch" style="max-width:180px;">${name}</td>
+          % endif
+          <td>${count}</td>
+        </tr>
+        <% i = i+1 %>
+      % endfor
+      </tbody>
+    </table>
+    <p class="note">*figures are from the past 7 days</p>
+  </div> <!-- /span4 -->
+% endif
+
+${navlinks("top_maps_by_times_played", top_maps.page, top_maps.last_page)}
+  </div> <!-- /span4 -->
+</div> <!-- /row -->
index 04f73709195ca75969ff892a6242c08d58da15b4..4e65f2c36366580e8e07cfb7dc35cb65319ce36a 100644 (file)
@@ -27,4 +27,4 @@ from xonstat.views.search import search_json
 from xonstat.views.exceptions   import notfound
 
 from xonstat.views.main   import main_index, top_players_by_time, top_servers_by_players
-from xonstat.views.main   import top_servers_by_players
+from xonstat.views.main   import top_servers_by_players, top_maps_by_times_played
index 0320a599a9aa6554b45e00c29c6bc6345f85a6c8..76eeb073aac229a113e1c15e5e0bce9b72ea7607 100644 (file)
@@ -173,27 +173,39 @@ def get_top_servers_by_players(cutoff_days):
     return top_servers
 
 
-@cache_region('hourly_term')
-def top_maps_by_times_played(cutoff_days):
+def top_maps_by_times_played_q(cutoff_days):
     """
-    The top maps by the amount of times it was played during a date range.
+    Query to retrieve the top maps by the amount of times it was 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_maps = DBSession.query(Game.map_id, Map.name,
+    top_maps_q = DBSession.query(Game.map_id, Map.name,
             func.count()).\
             filter(Map.map_id==Game.map_id).\
             filter(expr.between(Game.create_dt, cutoff_dt, right_now)).\
             order_by(expr.desc(func.count())).\
             group_by(Game.map_id).\
-            group_by(Map.name).limit(count).all()
+            group_by(Map.name)
+
+    return top_maps_q
+
+
+@cache_region('hourly_term')
+def get_top_maps_by_times_played(cutoff_days):
+    """
+    The top maps by the amount of times it was played during a date range.
+
+    Games older than cutoff_days days old are ignored.
+    """
+    # how many to retrieve
+    count = 10
+
+    top_maps = top_maps_by_times_played_q(cutoff_days).limit(count).all()
 
     return top_maps
 
@@ -231,7 +243,7 @@ def _main_index_data(request):
     top_servers = get_top_servers_by_players(leaderboard_lifetime)
 
     # top maps by total times played
-    top_maps = top_maps_by_times_played(leaderboard_lifetime)
+    top_maps = get_top_maps_by_times_played(leaderboard_lifetime)
 
     # recent games played in descending order
     rgs = recent_games_q(cutoff=back_then).limit(recent_games_count).all()
@@ -302,3 +314,16 @@ def top_servers_by_players(request):
     top_servers = Page(top_servers_q, current_page, items_per_page=25, url=page_url)
 
     return {'top_servers':top_servers}
+
+
+def top_maps_by_times_played(request):
+    current_page = request.params.get('page', 1)
+
+    cutoff_days = int(request.registry.settings.\
+        get('xonstat.leaderboard_lifetime', 30))
+
+    top_maps_q = top_maps_by_times_played_q(cutoff_days)
+
+    top_maps = Page(top_maps_q, current_page, items_per_page=25, url=page_url)
+
+    return {'top_maps':top_maps}