From: Ant Zucaro Date: Sat, 7 Jan 2017 23:42:18 +0000 (-0500) Subject: More work towards refactoring the map views. X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=60c16fe702bbeca8cd9f499304be88e2797ff611;p=xonotic%2Fxonstat.git More work towards refactoring the map views. --- diff --git a/xonstat/templates/map_top_servers.mako b/xonstat/templates/map_top_servers.mako new file mode 100644 index 0000000..8613c5d --- /dev/null +++ b/xonstat/templates/map_top_servers.mako @@ -0,0 +1,57 @@ +<%inherit file="base.mako"/> +<%namespace name="nav" file="nav.mako" /> + +<%block name="navigation"> + ${nav.nav('servers')} + + +<%block name="title"> + Server Active Players Index + + +% if not top_players and start is not None: +

Sorry, no more active players!

+ +% elif not top_players and start is None: +

No active players found. Yikes, get playing!

+ +% else: + ##### ACTIVE PLAYERS ##### +
+
+ + + + + + + + + + + % for tp in top_players: + + + + + + % endfor + +
#NickPlay Time
${tp.rank}${tp.nick|n}${tp.alivetime}
+

Note: these figures are from the past ${lifetime} days +

+
+ + % if len(top_players) == 20: +
+
+ +
+
+ % endif + +% endif diff --git a/xonstat/views/map.py b/xonstat/views/map.py index 9c4dc29..331a257 100644 --- a/xonstat/views/map.py +++ b/xonstat/views/map.py @@ -217,7 +217,75 @@ class MapTopPlayers(MapInfoBase): return { "map_id": self.map_id, - "top_scorers": top_players, + "top_active": top_players, + } + + +class MapTopServers(MapInfoBase): + """Returns the top servers for a given map, by times played.""" + + def __init__(self, request, limit=INDEX_COUNT, last=None): + """Common parameter parsing.""" + super(MapTopServers, self).__init__(request, limit, last) + self.top_servers = self.get_top_servers() + + def get_top_servers(self): + """Top servers by times played. Shared by all renderers.""" + cutoff = self.now - timedelta(days=self.lifetime) + + top_servers_q = DBSession.query( + fg.row_number().over(order_by=expr.desc(func.count())).label("rank"), + Player.player_id, Player.nick, func.count().label("times_played"))\ + .filter(Player.player_id == PlayerGameStat.player_id)\ + .filter(Game.game_id == PlayerGameStat.game_id)\ + .filter(Game.map_id == self.map_id)\ + .filter(Player.player_id > 2) \ + .filter(PlayerGameStat.create_dt > cutoff)\ + .group_by(Player.player_id)\ + .group_by(Player.nick)\ + .order_by(expr.desc(func.count())) + + if self.last: + top_servers_q = top_servers_q.offset(self.last) + + if self.limit: + top_servers_q = top_servers_q.limit(self.limit) + + top_servers = top_servers_q.all() + + return top_servers + + def html(self): + """Returns an HTML-ready representation.""" + TopServer = namedtuple("TopServer", ["rank", "player_id", "nick", "times_played"]) + + top_servers = [TopServer(ts.rank, ts.player_id, html_colors(ts.nick), ts.alivetime) + for ts in self.top_servers] + + # build the query string + query = {} + if len(top_servers) > 1: + query['last'] = top_servers[-1].rank + + return { + "map_id": self.map_id, + "top_servers": top_servers, + "lifetime": self.lifetime, + "query": query, + } + + def json(self): + """For rendering this data using JSON.""" + top_servers = [{ + "rank": ts.rank, + "player_id": ts.player_id, + "nick": ts.nick, + "times_played": ts.times_played, + } for ts in self.top_servers] + + return { + "map_id": self.map_id, + "top_servers": top_servers, }