Add a leaderboard lifetime. Default to 30. New config value in ini file for this.
authorAnt Zucaro <azucaro@gmail.com>
Thu, 27 Oct 2011 15:50:04 +0000 (11:50 -0400)
committerAnt Zucaro <azucaro@gmail.com>
Thu, 27 Oct 2011 15:50:04 +0000 (11:50 -0400)
development.ini [changed mode: 0644->0755]
xonstat/views/main.py

old mode 100644 (file)
new mode 100755 (executable)
index b3c8372..c3d1cf8
@@ -15,10 +15,18 @@ session.secret = CHANGEMECHANGEMECHANGEME
 jinja2.directories = xonstat:templates
 mako.directories = xonstat:templates
 
 jinja2.directories = xonstat:templates
 mako.directories = xonstat:templates
 
+##### XONSTAT CONFIG SETTINGS #####
+
 # how many "real" players are required before the data
 # is stored in the database
 xonstat.minimum_required_players = 1
 
 # how many "real" players are required before the data
 # is stored in the database
 xonstat.minimum_required_players = 1
 
+# how far back to reach for calculating leaderboard rank
+# e.g. set to 7 and only the current week's data is used
+xonstat.leaderboard_lifetime = 30
+
+##### END XONSTAT CONFIG SETTINGS #####
+
 [pipeline:main]
 pipeline =
     egg:WebError#evalerror
 [pipeline:main]
 pipeline =
     egg:WebError#evalerror
index 29fe1f71a26265839fdbc09f38814c893c9fe7e3..205c8686e7db439c588ec445821a4c14334760b8 100755 (executable)
@@ -1,6 +1,8 @@
 import logging
 import sqlalchemy.sql.functions as func
 import sqlalchemy.sql.expression as expr
 import logging
 import sqlalchemy.sql.functions as func
 import sqlalchemy.sql.expression as expr
+from datetime import datetime, timedelta
+from pyramid.config import get_current_registry
 from pyramid.response import Response
 from xonstat.models import *
 from xonstat.util import *
 from pyramid.response import Response
 from xonstat.models import *
 from xonstat.util import *
@@ -8,6 +10,13 @@ from xonstat.util import *
 log = logging.getLogger(__name__)
 
 def main_index(request):
 log = logging.getLogger(__name__)
 
 def main_index(request):
+    settings = get_current_registry().settings
+    try: 
+        leaderboard_lifetime = int(
+                settings['xonstat.leaderboard_lifetime'])
+    except:
+        leaderboard_lifetime = 30
+
     leaderboard_count = 10
     recent_games_count = 32
 
     leaderboard_count = 10
     recent_games_count = 32
 
@@ -16,6 +25,8 @@ def main_index(request):
             func.sum(PlayerGameStat.score)).\
             filter(Player.player_id == PlayerGameStat.player_id).\
             filter(Player.player_id > 2).\
             func.sum(PlayerGameStat.score)).\
             filter(Player.player_id == PlayerGameStat.player_id).\
             filter(Player.player_id > 2).\
+            filter(PlayerGameStat.create_dt > 
+                    (datetime.now() - timedelta(days=leaderboard_lifetime))).\
             order_by(expr.desc(func.sum(PlayerGameStat.score))).\
             group_by(Player.nick).\
             group_by(Player.player_id).all()[0:10]
             order_by(expr.desc(func.sum(PlayerGameStat.score))).\
             group_by(Player.nick).\
             group_by(Player.player_id).all()[0:10]
@@ -30,6 +41,8 @@ def main_index(request):
     top_servers = DBSession.query(Server.server_id, Server.name, 
             func.count()).\
             filter(Game.server_id==Server.server_id).\
     top_servers = DBSession.query(Server.server_id, Server.name, 
             func.count()).\
             filter(Game.server_id==Server.server_id).\
+            filter(Game.create_dt > 
+                (datetime.now() - timedelta(days=leaderboard_lifetime))).\
             order_by(expr.desc(func.count(Game.game_id))).\
             group_by(Server.server_id).\
             group_by(Server.name).all()[0:10]
             order_by(expr.desc(func.count(Game.game_id))).\
             group_by(Server.server_id).\
             group_by(Server.name).all()[0:10]
@@ -41,6 +54,8 @@ def main_index(request):
     top_maps = DBSession.query(Game.map_id, Map.name, 
             func.count()).\
             filter(Map.map_id==Game.map_id).\
     top_maps = DBSession.query(Game.map_id, Map.name, 
             func.count()).\
             filter(Map.map_id==Game.map_id).\
+            filter(Game.create_dt > 
+                (datetime.now() - timedelta(days=leaderboard_lifetime))).\
             order_by(expr.desc(func.count())).\
             group_by(Game.map_id).\
             group_by(Map.name).all()[0:10]
             order_by(expr.desc(func.count())).\
             group_by(Game.map_id).\
             group_by(Map.name).all()[0:10]