]> git.xonotic.org Git - xonotic/xonstat.git/commitdiff
Add a "leaderboard"-like view to the map info page.
authorAnt Zucaro <azucaro@gmail.com>
Thu, 8 Dec 2011 20:56:55 +0000 (15:56 -0500)
committerAnt Zucaro <azucaro@gmail.com>
Thu, 8 Dec 2011 20:56:55 +0000 (15:56 -0500)
xonstat/templates/map_info.mako
xonstat/views/map.py

index d6a49651e2aa24f3091b98ffac7668e08a762e89..c454e0ce186057f56d7d19914f2718e89562c8c5 100755 (executable)
@@ -13,7 +13,138 @@ ${parent.title()}
 <h2>Sorry, that map wasn't found!</h2>
 
 % else:
-<h2>Map Detail</h2>
-ID: ${gmap.map_id} <br />
-Name: ${gmap.name} <br />
+<h2>Map Detail - ${gmap.name}</h2>
+
+##### RECENT GAMES #####
+<h2>Recent Games</h2>
+<table id="recent-games">
+    <thead>
+        <tr>
+            <th>Game #</th>
+            <th>Type</th>
+            <th>Time</th>
+            <th>Winner</th>
+        </tr>
+    </thead>
+    <tbody>
+    % for (game, srv, 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>${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>
+        </tr>
+        % endif
+    % endfor
+    </tbody>
+</table>
+
+
+##### TOP SCORERS #####
+<div class="table_block">
+<h2>Top Scoring Players</h2>
+<table>
+    <thead>
+        <tr>
+            <th>#</th>
+            <th>Nick</th>
+            <th>Score</th>
+        </tr>
+    </thead>
+    <tbody>
+    <% i = 1 %>
+    % for (score_player_id, score_nick, score_value) in top_scorers:
+        <tr>
+            <td>${i}</td>
+            % if score_player_id != '-':
+            <td><a href="${request.route_url('player_info', id=score_player_id)}" title="Go to the player info page for this player">${score_nick|n}</a></td>
+            % else:
+            <td>${score_nick}</td>
+            % endif
+            <td>${score_value}</td>
+        </tr>
+        <% i = i+1 %>
+    % endfor
+    </tbody>
+</table>
+</div>
+
+
+##### TOP PLAYERS #####
+<div class="table_block">
+<h2>Most Active Players</h2>
+<table id="top-players">
+    <thead>
+        <tr>
+            <th>#</th>
+            <th>Nick</th>
+            <th>Playing Time</th>
+        </tr>
+    </thead>
+    <tbody>
+    <% i = 1 %>
+    % for (player_id, nick, alivetime) in top_players:
+        <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}</td>
+            % endif
+            <td>${alivetime}</td>
+        </tr>
+        <% i = i+1 %>
+    % endfor
+    </tbody>
+</table>
+</div>
+
+
+##### TOP SERVERS #####
+<div class="table_block">
+<h2>Most Active Servers</h2>
+<table id="top-servers">
+    <thead>
+        <tr>
+            <th>#</th>
+            <th>Name</th>
+            <th>Times Played</th>
+        </tr>
+    </thead>
+    <tbody>
+    <% i = 1 %>
+    % for (server_id, name, times_played) in top_servers:
+        <tr>
+            <td>${i}</td>
+            <td><a href="${request.route_url('server_info', id=server_id)}" title="Go to the server info page for this server">${name}</a></td>
+            <td>${times_played}</td>
+        </tr>
+        <% i = i+1 %>
+    % endfor
+    </tbody>
+</table>
+</div>
+
+
 % endif
index 659f04f0673606e771ff5cd11f4a70597cfe2b88..fff321f9e3af10b6c25210822b58fa03e505ed40 100755 (executable)
@@ -1,4 +1,7 @@
 import logging\r
+import sqlalchemy.sql.functions as func\r
+import sqlalchemy.sql.expression as expr\r
+from datetime import datetime, timedelta\r
 from pyramid.response import Response\r
 from sqlalchemy import desc\r
 from webhelpers.paginate import Page, PageURL\r
@@ -34,8 +37,89 @@ def map_info(request):
     List the information stored about a given map. \r
     """\r
     map_id = request.matchdict['id']\r
+\r
+    try: \r
+        leaderboard_lifetime = int(\r
+                request.registry.settings['xonstat.leaderboard_lifetime'])\r
+    except:\r
+        leaderboard_lifetime = 30\r
+\r
+    leaderboard_count = 10\r
+    recent_games_count = 20\r
+\r
     try:\r
         gmap = DBSession.query(Map).filter_by(map_id=map_id).one()\r
-    except:\r
+\r
+        # recent games on this map\r
+        recent_games = DBSession.query(Game, Server, Map, PlayerGameStat).\\r
+            filter(Game.server_id==Server.server_id).\\r
+            filter(Game.map_id==Map.map_id).\\r
+            filter(Game.map_id==map_id).\\r
+            filter(PlayerGameStat.game_id==Game.game_id).\\r
+            filter(PlayerGameStat.rank==1).\\r
+            order_by(expr.desc(Game.start_dt)).all()[0:recent_games_count]\r
+\r
+        for i in range(recent_games_count-len(recent_games)):\r
+            recent_games.append(('-', '-', '-', '-'))\r
+\r
+\r
+        # top players by score\r
+        top_scorers = DBSession.query(Player.player_id, Player.nick,\r
+                func.sum(PlayerGameStat.score)).\\r
+                filter(Player.player_id == PlayerGameStat.player_id).\\r
+                filter(Game.game_id == PlayerGameStat.game_id).\\r
+                filter(Game.map_id == map_id).\\r
+                filter(Player.player_id > 2).\\r
+                filter(PlayerGameStat.create_dt > \r
+                        (datetime.utcnow() - timedelta(days=leaderboard_lifetime))).\\r
+                order_by(expr.desc(func.sum(PlayerGameStat.score))).\\r
+                group_by(Player.nick).\\r
+                group_by(Player.player_id).all()[0:10]\r
+\r
+        top_scorers = [(player_id, html_colors(nick), score) \\r
+                for (player_id, nick, score) in top_scorers]\r
+\r
+        for i in range(leaderboard_count-len(top_scorers)):\r
+            top_scorers.append(('-', '-', '-'))\r
+\r
+        # top players by playing time\r
+        top_players = DBSession.query(Player.player_id, Player.nick, \r
+                func.sum(PlayerGameStat.alivetime)).\\r
+                filter(Player.player_id == PlayerGameStat.player_id).\\r
+                filter(Game.game_id == PlayerGameStat.game_id).\\r
+                filter(Game.map_id == map_id).\\r
+                filter(Player.player_id > 2).\\r
+                filter(PlayerGameStat.create_dt > \r
+                        (datetime.utcnow() - timedelta(days=leaderboard_lifetime))).\\r
+                order_by(expr.desc(func.sum(PlayerGameStat.alivetime))).\\r
+                group_by(Player.nick).\\r
+                group_by(Player.player_id).all()[0:10]\r
+\r
+        top_players = [(player_id, html_colors(nick), score) \\r
+                for (player_id, nick, score) in top_players]\r
+\r
+        for i in range(leaderboard_count-len(top_players)):\r
+            top_players.append(('-', '-', '-'))\r
+\r
+        # top servers using/playing this map\r
+        top_servers = DBSession.query(Server.server_id, Server.name, \r
+                func.count(Game.game_id)).\\r
+                filter(Game.server_id == Server.server_id).\\r
+                filter(Game.map_id == map_id).\\r
+                filter(Game.create_dt > \r
+                        (datetime.utcnow() - timedelta(days=leaderboard_lifetime))).\\r
+                order_by(expr.desc(func.count(Game.game_id))).\\r
+                group_by(Server.name).\\r
+                group_by(Server.server_id).all()[0:10]\r
+\r
+        for i in range(leaderboard_count-len(top_servers)):\r
+            top_servers.append(('-', '-', '-'))\r
+\r
+    except Exception as e:\r
         gmap = None\r
-    return {'gmap':gmap}\r
+    return {'gmap':gmap,\r
+            'recent_games':recent_games,\r
+            'top_scorers':top_scorers,\r
+            'top_players':top_players,\r
+            'top_servers':top_servers,\r
+            }\r