from xonstat.models import *
from xonstat.util import *
from xonstat.views.helpers import RecentGame, recent_games_q
+from webhelpers.paginate import Page
log = logging.getLogger(__name__)
return ranks
-@cache_region('hourly_term')
-def top_players_by_time(cutoff_days):
+def top_players_by_time_q(cutoff_days):
"""
- The top players by the amount of time played during a date range.
+ Query for the top players by the amount of time played during a date range.
Games older than cutoff_days days old are ignored.
"""
- # how many to retrieve
- count = 10
# only games played during this range are considered
right_now = datetime.utcnow()
cutoff_dt = right_now - timedelta(days=cutoff_days)
- top_players = DBSession.query(Player.player_id, Player.nick,
+ top_players_q = DBSession.query(Player.player_id, Player.nick,
func.sum(PlayerGameStat.alivetime)).\
filter(Player.player_id == PlayerGameStat.player_id).\
filter(Player.player_id > 2).\
filter(expr.between(PlayerGameStat.create_dt, cutoff_dt, right_now)).\
order_by(expr.desc(func.sum(PlayerGameStat.alivetime))).\
group_by(Player.nick).\
- group_by(Player.player_id).limit(count).all()
+ group_by(Player.player_id)
+
+ return top_players_q
+
+
+@cache_region('hourly_term')
+def get_top_players_by_time(cutoff_days):
+ """
+ The top players by the amount of time played during a date range.
+
+ Games older than cutoff_days days old are ignored.
+ """
+ # how many to retrieve
+ count = 10
+
+ # only games played during this range are considered
+ right_now = datetime.utcnow()
+ cutoff_dt = right_now - timedelta(days=cutoff_days)
+
+ top_players_q = top_players_by_time_q(cutoff_days)
+
+ top_players = top_players_q.limit(count).all()
top_players = [(player_id, html_colors(nick), score) \
for (player_id, nick, score) in top_players]
right_now = datetime.utcnow()
cutoff_dt = right_now - timedelta(days=cutoff_days)
- top_servers = DBSession.query(Server.server_id, Server.name,
+ top_servers = DBSession.query(Server.server_id, Server.name,
func.count()).\
filter(Game.server_id==Server.server_id).\
filter(expr.between(Game.create_dt, cutoff_dt, right_now)).\
back_then = datetime.utcnow() - timedelta(days=leaderboard_lifetime)
# top players by playing time
- top_players = top_players_by_time(leaderboard_lifetime)
+ top_players = get_top_players_by_time(leaderboard_lifetime)
# top servers by number of total players played
top_servers = top_servers_by_players(leaderboard_lifetime)
JSON output of the main page information.
"""
return [{'status':'not implemented'}]
+
+
+def top_players_by_time(request):
+ current_page = request.params.get('page', 1)
+
+ cutoff_days = int(request.registry.settings.\
+ get('xonstat.leaderboard_lifetime', 30))
+
+ top_players_q = top_players_by_time_q(cutoff_days)
+
+ top_players = Page(top_players_q, current_page, items_per_page=25, url=page_url)
+
+ top_players.items = [(player_id, html_colors(nick), score) \
+ for (player_id, nick, score) in top_players.items]
+
+ return {'top_players':top_players}