From fdda891447025c8d24c0f0806f3bb21290ddf004 Mon Sep 17 00:00:00 2001 From: Ant Zucaro Date: Sat, 22 Dec 2012 15:59:17 -0500 Subject: [PATCH] Simplify the rank table calculations. Woohoo, negative LOC! 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 | 105 +++++++----------------------- xonstat/views/main.py | 67 ++++++++----------- 2 files changed, 51 insertions(+), 121 deletions(-) diff --git a/xonstat/templates/main_index.mako b/xonstat/templates/main_index.mako index 6ed4e17..a1de18c 100644 --- a/xonstat/templates/main_index.mako +++ b/xonstat/templates/main_index.mako @@ -20,44 +20,26 @@ Leaderboard +% if len(ranks) < 3:
+
+

You don't seem to have any ranks yet.

+
+
+% else: +
+% for rs in ranks[:3]: + % if len(rs) > 0:
- ##### DUEL RANKS ##### -

Duel Ranks

- - - - - - - - - - <% i = 1 %> - % for (player_id, nick, elo) in duel_ranks: - - - % if player_id != '-': - - % else: - - % endif - % if elo != '-': - - % else: - - % endif - - <% i = i+1 %> - % endfor - -
#NickElo
${i}${nick|n}${nick|n}${round(elo, 3)}${elo}
-

More...

-
-
- ##### CTF RANKS ##### + % if rs[0].game_type_cd == 'duel': +

Duel Ranks

+ % elif rs[0].game_type_cd == 'ctf':

CTF Ranks

+ % elif rs[0].game_type_cd == 'dm': +

DM Ranks

+ % endif + @@ -68,61 +50,24 @@ Leaderboard <% i = 1 %> - % for (player_id, nick, elo) in ctf_ranks: + % for r in rs: - % if player_id != '-': - - % else: - - % endif - % if elo != '-': - - % else: - - % endif + + <% i = i+1 %> % endfor
${i}${nick|n}${nick|n}${round(elo, 3)}${elo}${r.nick_html_colors()|n}${round(r.elo, 3)}
-

More...

+

More...

+ % endif + +% endfor +
+% endif -
- ##### DM RANKS ##### -

DM Ranks

- - - - - - - - - - <% i = 1 %> - % for (player_id, nick, elo) in dm_ranks: - - - % if player_id != '-': - - % else: - - % endif - % if elo != '-': - - % else: - - % endif - - <% i = i+1 %> - % endfor - -
#NickElo
${i}${nick|n}${nick|n}${round(elo, 3)}${elo}
-

More...

-
-
diff --git a/xonstat/views/main.py b/xonstat/views/main.py index d22b594..f8fb9ea 100644 --- a/xonstat/views/main.py +++ b/xonstat/views/main.py @@ -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(('-', '-', '-')) -- 2.39.2