From b8191d441a2a7b0353331e619a3481d00e324c70 Mon Sep 17 00:00:00 2001
From: Ant Zucaro <azucaro@gmail.com>
Date: Thu, 19 Jan 2012 21:56:25 -0500
Subject: [PATCH] Add rankings to the main page.

---
 xonstat/static/css/style.css      |  12 +-
 xonstat/static/js/default.js      |   2 +-
 xonstat/templates/main_index.mako | 198 +++++++++++++++++++++++-------
 xonstat/views/main.py             |  39 ++++++
 4 files changed, 206 insertions(+), 45 deletions(-)

diff --git a/xonstat/static/css/style.css b/xonstat/static/css/style.css
index 2dde72c..8f50faa 100755
--- a/xonstat/static/css/style.css
+++ b/xonstat/static/css/style.css
@@ -369,10 +369,10 @@ input[type=text]:hover, .textbox:hover { background-position:-128px -32px; }
 /* Content Specific */
 
 /*Tables */
-#top-players, #top-servers, #top-maps {
+#top-players, #top-servers, #top-maps, .rank-table{
     float: left;
     margin-bottom: 10px;
-    width:100%;
+    width: 290px;
 }
 #player-index-table, #server-index-table{
     width: 600px;
@@ -424,6 +424,10 @@ tr.even.blue {
     width: 150px;
 }
 
+.play-time{
+    width: 70px;
+}
+
 /* Containers */
 #filter {
 	float:left;
@@ -496,3 +500,7 @@ tr.even.blue {
 	color:#A4B7CA !important;
 	text-decoration:underline;
 }
+.note {
+    font-size: 10px;
+	color:#6D8399;
+}
diff --git a/xonstat/static/js/default.js b/xonstat/static/js/default.js
index af05cd4..f0096e8 100644
--- a/xonstat/static/js/default.js
+++ b/xonstat/static/js/default.js
@@ -60,7 +60,7 @@ function init_datatables() {
 		$("#recent-games").dataTable({
 			"bPaginate": false,
 			"bLengthChange": false,
-			"bFilter": true,
+			"bFilter": false,
 			"bSort": true,
 			"bInfo": false,
 			"bAutoWidth": false,
diff --git a/xonstat/templates/main_index.mako b/xonstat/templates/main_index.mako
index 132a943..397d1c9 100755
--- a/xonstat/templates/main_index.mako
+++ b/xonstat/templates/main_index.mako
@@ -4,57 +4,113 @@
 Main Page - ${parent.title()}
 </%block>
 
-
-##### RECENT GAMES #####
-<h2>Recent Games</h2>
-<table id="recent-games">
+##### DUEL RANKS #####
+<div class="table_block">
+<h2>Duel Ranks</h2>
+<table class="rank-table">
 	<thead>
 		<tr>
-			<th>Game #</th>
-			<th>Type</th>
-			<th>Server</th>
-			<th>Map</th>
-			<th>Time</th>
-			<th>Winner</th>
+			<th>#</th>
+			<th>Nick</th>
+			<th>Elo</th>
 		</tr>
 	</thead>
 	<tbody>
-	% for (game, server, map, pgstat) in recent_games:
-		% if game != '-':
+	<% i = 1 %>
+	% for (player_id, nick, elo) in duel_ranks:
 		<tr>
-			<td><a href="${request.route_url('game_info', id=game.game_id)}" title="View detailed information about this game">${game.game_id}</a></td>
-			<td class="gt_icon"><img title="${game.game_type_cd}" src="/static/images/icons/24x24/${game.game_type_cd}.png" alt="${game.game_type_cd}" /></td>
-			<td><a href="${request.route_url('server_info', id=server.server_id)}" title="Go to the detail page for this server">${server.name}</a></td>
-			<td><a href="${request.route_url('map_info', id=map.map_id)}" title="Go to the map detail page for this map">${map.name}</a></td>
-			<td>${game.start_dt.strftime('%m/%d/%Y %H:%M')}</td>
-			<td class=
-            % if pgstat.team == 5:
-            "blue"
-            % elif pgstat.team == 14:
-            "red"
-            % elif pgstat.team == 13:
-            "yellow"
+			<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}</a></td>
+			% else:
+			<td>${nick}</td>
+			% endif
+            % if elo != '-':
+			<td>${round(elo, 3)}</td>
+            % else:
+			<td>${elo}</td>
             % endif
-            >
-            % if pgstat.player_id > 2:
-            <a href="${request.route_url('player_info', id=pgstat.player_id)}" title="Go to the player info page for this player">${pgstat.nick_html_colors()|n}</a></td>
+		</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>
+
+
+
+##### CTF RANKS #####
+<div class="table_block">
+<h2>CTF Ranks</h2>
+<table class="rank-table">
+	<thead>
+		<tr>
+			<th>#</th>
+			<th>Nick</th>
+			<th>Elo</th>
+		</tr>
+	</thead>
+	<tbody>
+	<% i = 1 %>
+	% for (player_id, nick, elo) in ctf_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}</a></td>
+			% else:
+			<td>${nick}</td>
+			% endif
+            % if elo != '-':
+			<td>${round(elo, 3)}</td>
             % else:
-            ${pgstat.nick_html_colors()|n}</td>
+			<td>${elo}</td>
             % endif
 		</tr>
-		% else:
+		<% 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>
+</div>
+
+
+
+##### DM RANKS #####
+<div class="table_block">
+<h2>DM Ranks</h2>
+<table class="rank-table">
+	<thead>
 		<tr>
-			<td>-</td>
-			<td>-</td>
-			<td>-</td>
-			<td>-</td>
-			<td>-</td>
-			<td>-</td>
+			<th>#</th>
+			<th>Nick</th>
+			<th>Elo</th>
 		</tr>
-		% endif
-    % endfor
-    </tbody>
+	</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}</a></td>
+			% else:
+			<td>${nick}</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>
+
+
 
 ##### TOP PLAYERS #####
 <div class="table_block">
@@ -64,7 +120,7 @@ Main Page - ${parent.title()}
 		<tr>
 			<th>#</th>
 			<th>Nick</th>
-			<th>Playing Time</th>
+			<th class="play-time">Play Time</th>
 		</tr>
 	</thead>
 	<tbody>
@@ -77,12 +133,13 @@ Main Page - ${parent.title()}
 			% else:
 			<td>${nick}</td>
 			% endif
-			<td>${alivetime}</td>
+			<td class="play-time">${alivetime}</td>
 		</tr>
 		<% i = i+1 %>
 	% endfor
 	</tbody>
 </table>
+<p class="note">*Most active stats are from the past 7 days</p>
 </div>
 
 ##### TOP SERVERS #####
@@ -93,7 +150,7 @@ Main Page - ${parent.title()}
 		<tr>
 			<th>#</th>
 			<th>Server</th>
-			<th># Games</th>
+			<th>Games</th>
 		</tr>
 	</thead>
 	<tbody>
@@ -122,7 +179,7 @@ Main Page - ${parent.title()}
 		<tr>
 			<th>#</th>
 			<th>Map</th>
-			<th># Games</th>
+			<th>Games</th>
 		</tr>
 	</thead>
 	<tbody>
@@ -142,3 +199,60 @@ Main Page - ${parent.title()}
 	</tbody>
 </table>
 </div>
+
+
+
+##### RECENT GAMES #####
+<div class="table_block">
+<h2>Recent Games</h2>
+<table id="recent-games">
+	<thead>
+		<tr>
+			<th>Game #</th>
+			<th>Type</th>
+			<th>Server</th>
+			<th>Map</th>
+			<th>Time</th>
+			<th>Winner</th>
+		</tr>
+	</thead>
+	<tbody>
+	% for (game, server, map, pgstat) in recent_games:
+		% if game != '-':
+		<tr>
+			<td><a href="${request.route_url('game_info', id=game.game_id)}" title="View detailed information about this game">${game.game_id}</a></td>
+			<td class="gt_icon"><img title="${game.game_type_cd}" src="/static/images/icons/24x24/${game.game_type_cd}.png" alt="${game.game_type_cd}" /></td>
+			<td><a href="${request.route_url('server_info', id=server.server_id)}" title="Go to the detail page for this server">${server.name}</a></td>
+			<td><a href="${request.route_url('map_info', id=map.map_id)}" title="Go to the map detail page for this map">${map.name}</a></td>
+			<td>${game.start_dt.strftime('%m/%d/%Y %H:%M')}</td>
+			<td class=
+            % if pgstat.team == 5:
+            "blue"
+            % elif pgstat.team == 14:
+            "red"
+            % elif pgstat.team == 13:
+            "yellow"
+            % endif
+            >
+            % if pgstat.player_id > 2:
+            <a href="${request.route_url('player_info', id=pgstat.player_id)}" title="Go to the player info page for this player">${pgstat.nick_html_colors()|n}</a></td>
+            % else:
+            ${pgstat.nick_html_colors()|n}</td>
+            % endif
+		</tr>
+		% else:
+		<tr>
+			<td>-</td>
+			<td>-</td>
+			<td>-</td>
+			<td>-</td>
+			<td>-</td>
+			<td>-</td>
+		</tr>
+		% endif
+    % endfor
+    </tbody>
+</table>
+</div>
+
+
diff --git a/xonstat/views/main.py b/xonstat/views/main.py
index 22b1291..d3a4725 100755
--- a/xonstat/views/main.py
+++ b/xonstat/views/main.py
@@ -18,6 +18,42 @@ def main_index(request):
     leaderboard_count = 10
     recent_games_count = 20
 
+    # top ranked duelers
+    duel_ranks = DBSession.query(Player.player_id, Player.nick, PlayerElo.elo).\
+            filter(Player.player_id==PlayerElo.player_id).\
+            filter(PlayerElo.game_type_cd=='duel').\
+            order_by(expr.desc(PlayerElo.elo)).all()[0:10]
+
+    duel_ranks = [(player_id, html_colors(nick), elo) \
+            for (player_id, nick, elo) in duel_ranks]
+
+    for i in range(leaderboard_count-len(duel_ranks)):
+        duel_ranks.append(('-', '-', '-'))
+
+    # top ranked CTF-ers
+    ctf_ranks = DBSession.query(Player.player_id, Player.nick, PlayerElo.elo).\
+            filter(Player.player_id==PlayerElo.player_id).\
+            filter(PlayerElo.game_type_cd=='ctf').\
+            order_by(expr.desc(PlayerElo.elo)).all()[0:10]
+
+    ctf_ranks = [(player_id, html_colors(nick), elo) \
+            for (player_id, nick, elo) in ctf_ranks]
+
+    for i in range(leaderboard_count-len(ctf_ranks)):
+        ctf_ranks.append(('-', '-', '-'))
+
+    # top ranked DM-ers
+    dm_ranks = DBSession.query(Player.player_id, Player.nick, PlayerElo.elo).\
+            filter(Player.player_id==PlayerElo.player_id).\
+            filter(PlayerElo.game_type_cd=='dm').\
+            order_by(expr.desc(PlayerElo.elo)).all()[0:10]
+
+    dm_ranks = [(player_id, html_colors(nick), elo) \
+            for (player_id, nick, elo) in dm_ranks]
+
+    for i in range(leaderboard_count-len(dm_ranks)):
+        dm_ranks.append(('-', '-', '-'))
+
     # top players by playing time
     top_players = DBSession.query(Player.player_id, Player.nick, 
             func.sum(PlayerGameStat.alivetime)).\
@@ -76,4 +112,7 @@ def main_index(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,
             }
-- 
2.39.5