]> de.git.xonotic.org Git - xonotic/xonstat.git/commitdiff
Simplify the rank table calculations. Woohoo, negative LOC!
authorAnt Zucaro <azucaro@gmail.com>
Sat, 22 Dec 2012 20:59:17 +0000 (15:59 -0500)
committerJan Behrens <zykure@web.de>
Sun, 23 Dec 2012 14:18:35 +0000 (15:18 +0100)
Before the main view was running three separate, similar queries
to grab the rank data for the three game types shown on the page.
This was factored out into a separate function that can be called
and cached (currently hourly_term). Also if no ranks are available,
don't show tables filled with '-' characters, but rather an
informative message instead.

xonstat/templates/main_index.mako
xonstat/views/main.py

index 6ed4e17f6f190e19480669297d0242a567946454..a1de18ca120dbec4810afa8218a7ea00a479907d 100644 (file)
@@ -20,44 +20,26 @@ Leaderboard
       </div>
 </%block>
 
+% if len(ranks) < 3:
 <div class="row">
+  <div class="span12">
+    <p style="text-align: center;"><i class="icon-white icon-info-sign"> </i> You don't seem to have any ranks yet.</p>
+  </div> <!-- span12 -->
+</div> <!-- row -->
+% else:
+<div class="row">
+% for rs in ranks[:3]:
+  % if len(rs) > 0:
   <div class="span4">
-    ##### DUEL RANKS #####
-    <h3>Duel Ranks</h3>
-    <table class="table table-bordered table-condensed">
-      <thead>
-        <tr>
-          <th>#</th>
-          <th>Nick</th>
-          <th>Elo</th>
-        </tr>
-      </thead>
-      <tbody>
-      <% i = 1 %>
-      % for (player_id, nick, elo) in duel_ranks:
-        <tr>
-          <td>${i}</td>
-          % if player_id != '-':
-          <td><a href="${request.route_url('player_info', id=player_id)}" title="Go to the player info page for this player">${nick|n}</a></td>
-          % else:
-          <td>${nick|n}</td>
-          % endif
-          % if elo != '-':
-          <td>${round(elo, 3)}</td>
-          % else:
-          <td>${elo}</td>
-          % endif
-        </tr>
-        <% i = i+1 %>
-      % endfor
-      </tbody>
-    </table>
-    <p class="note"><a href="${request.route_url('rank_index', page=1, game_type_cd='duel')}" title="See more duel rankings">More...</a></p>
-  </div> <!-- /span4 -->
 
-  <div class="span4">
-    ##### CTF RANKS #####
+    % if rs[0].game_type_cd == 'duel':
+    <h3>Duel Ranks</h3>
+    % elif rs[0].game_type_cd == 'ctf':
     <h3>CTF Ranks</h3>
+    % elif rs[0].game_type_cd == 'dm':
+    <h3>DM Ranks</h3>
+    % endif
+
     <table class="table table-bordered table-condensed">
       <thead>
         <tr>
@@ -68,61 +50,24 @@ Leaderboard
       </thead>
       <tbody>
       <% i = 1 %>
-      % for (player_id, nick, elo) in ctf_ranks:
+      % for r in rs:
         <tr>
           <td>${i}</td>
-          % if player_id != '-':
-          <td><a href="${request.route_url('player_info', id=player_id)}" title="Go to the player info page for this player">${nick|n}</a></td>
-          % else:
-          <td>${nick|n}</td>
-          % endif
-          % if elo != '-':
-          <td>${round(elo, 3)}</td>
-          % else:
-          <td>${elo}</td>
-          % endif
+          <td><a href="${request.route_url('player_info', id=r.player_id)}" title="Go to the player info page for this player">${r.nick_html_colors()|n}</a></td>
+          <td>${round(r.elo, 3)}</td>
         </tr>
         <% i = i+1 %>
       % endfor
       </tbody>
     </table>
-    <p class="note"><a href="${request.route_url('rank_index', page=1, game_type_cd='ctf')}" title="See more CTF rankings">More...</a></p>
+    <p class="note"><a href="${request.route_url('rank_index', page=1, game_type_cd=rs[0].game_type_cd)}" title="See more ${rs[0].game_type_cd} rankings">More...</a></p>
   </div> <!-- /span4 -->
+  % endif
+
+% endfor
+</div> <!-- row -->
+% endif
 
-  <div class="span4">
-    ##### DM RANKS #####
-    <h3>DM Ranks</h3>
-    <table class="table table-bordered table-condensed">
-      <thead>
-        <tr>
-          <th>#</th>
-          <th>Nick</th>
-          <th>Elo</th>
-        </tr>
-      </thead>
-      <tbody>
-      <% i = 1 %>
-      % for (player_id, nick, elo) in dm_ranks:
-        <tr>
-          <td>${i}</td>
-          % if player_id != '-':
-          <td><a href="${request.route_url('player_info', id=player_id)}" title="Go to the player info page for this player">${nick|n}</a></td>
-          % else:
-          <td>${nick|n}</td>
-          % endif
-          % if elo != '-':
-          <td>${round(elo, 3)}</td>
-          % else:
-          <td>${elo}</td>
-          % endif
-        </tr>
-        <% i = i+1 %>
-      % endfor
-    </tbody>
-  </table>
-  <p class="note"><a href="${request.route_url('rank_index', page=1, game_type_cd='dm')}" title="See more deathmatch rankings">More...</a></p>
-  </div> <!-- /span4 -->
-</div> <!-- /row -->
 
 <div class="row">
   <div class="span4">
index d22b594517d11cd5a3dccbb62d7b540f5f0f6e82..f8fb9ea032e63118bd31f4f8ea0f7adbb4c5e956 100644 (file)
@@ -2,6 +2,7 @@ import logging
 import sqlalchemy.sql.functions as func
 import sqlalchemy.sql.expression as expr
 from beaker.cache import cache_regions, cache_region
+from collections import namedtuple
 from datetime import datetime, timedelta
 from pyramid.response import Response
 from xonstat.models import *
@@ -64,6 +65,24 @@ def get_summary_stats():
     return summary_stats
 
 
+@cache_region('hourly_term')
+def get_ranks(game_type_cd):
+    """
+    """
+    # how many ranks we want to fetch
+    leaderboard_count = 10
+
+    # only a few game modes are actually ranked
+    if game_type_cd not in 'duel' 'dm' 'ctf' 'tdm':
+        return None
+
+    ranks = DBSession.query(PlayerRank).\
+            filter(PlayerRank.game_type_cd==game_type_cd).\
+            order_by(PlayerRank.rank).\
+            limit(leaderboard_count).all()
+
+    return ranks
+
 def _main_index_data(request):
     try:
         leaderboard_lifetime = int(
@@ -80,35 +99,12 @@ def _main_index_data(request):
     except:
         summary_stats = None
 
-    # top ranked duelers
-    duel_ranks = DBSession.query(PlayerRank.player_id, PlayerRank.nick, 
-            PlayerRank.elo).\
-            filter(PlayerRank.game_type_cd=='duel').\
-            order_by(PlayerRank.rank).\
-            limit(leaderboard_count).all()
-
-    duel_ranks = [(player_id, html_colors(nick), elo) \
-            for (player_id, nick, elo) in duel_ranks]
-
-    # top ranked CTF-ers
-    ctf_ranks = DBSession.query(PlayerRank.player_id, PlayerRank.nick, 
-            PlayerRank.elo).\
-            filter(PlayerRank.game_type_cd=='ctf').\
-            order_by(PlayerRank.rank).\
-            limit(leaderboard_count).all()
-
-    ctf_ranks = [(player_id, html_colors(nick), elo) \
-            for (player_id, nick, elo) in ctf_ranks]
-
-    # top ranked DM-ers
-    dm_ranks = DBSession.query(PlayerRank.player_id, PlayerRank.nick, 
-            PlayerRank.elo).\
-            filter(PlayerRank.game_type_cd=='dm').\
-            order_by(PlayerRank.rank).\
-            limit(leaderboard_count).all()
-
-    dm_ranks = [(player_id, html_colors(nick), elo) \
-            for (player_id, nick, elo) in dm_ranks]
+    # the three top ranks tables
+    ranks = []
+    for gtc in ['duel', 'ctf', 'dm']:
+        rank = get_ranks(gtc)
+        if len(rank) != 0:
+            ranks.append(rank)
 
     right_now = datetime.utcnow()
     back_then = datetime.utcnow() - timedelta(days=leaderboard_lifetime)
@@ -152,9 +148,7 @@ def _main_index_data(request):
             'top_servers':top_servers,
             'top_maps':top_maps,
             'recent_games':recent_games,
-            'duel_ranks':duel_ranks,
-            'ctf_ranks':ctf_ranks,
-            'dm_ranks':dm_ranks,
+            'ranks':ranks,
             'summary_stats':summary_stats,
             }
 
@@ -169,15 +163,6 @@ def main_index(request):
     leaderboard_count = 10
     recent_games_count = 20
 
-    for i in range(leaderboard_count-len(mainindex_data['duel_ranks'])):
-        mainindex_data['duel_ranks'].append(('-', '-', '-'))
-
-    for i in range(leaderboard_count-len(mainindex_data['ctf_ranks'])):
-        mainindex_data['ctf_ranks'].append(('-', '-', '-'))
-
-    for i in range(leaderboard_count-len(mainindex_data['dm_ranks'])):
-        mainindex_data['dm_ranks'].append(('-', '-', '-'))
-
     for i in range(leaderboard_count-len(mainindex_data['top_players'])):
         mainindex_data['top_players'].append(('-', '-', '-'))