]> git.rm.cloudns.org Git - xonotic/xonstat.git/commitdiff
Add a cutoff date.
authorAnt Zucaro <azucaro@gmail.com>
Wed, 29 Aug 2012 02:01:12 +0000 (22:01 -0400)
committerAnt Zucaro <azucaro@gmail.com>
Wed, 29 Aug 2012 02:01:12 +0000 (22:01 -0400)
The cutoff date is the date before which games are not considered.
In other words, a player must play within the cutoff date range in
order to have his or her badge regenerated. This is to prevent
generating images for every single player, every single run.

xonstat/batch/badges/gen_badges.py
xonstat/batch/badges/render.py

index cd1a73c38dfb5bd8ec2e60900230dc815fa76065..86725620c6312b6a0a3a1b495100a59d5272a4da 100644 (file)
@@ -4,6 +4,7 @@ import sys
 from datetime import datetime
 import sqlalchemy as sa
 import sqlalchemy.sql.functions as func
+from sqlalchemy import distinct
 from pyramid.paster import bootstrap
 from xonstat.models import *
 
@@ -13,6 +14,9 @@ from render import Skin
 # maximal number of query results (for testing, set to 0 to get all)
 #NUM_PLAYERS = 100
 
+# we look for players who have activity within the past DELTA hours
+DELTA = 6
+
 skin_classic = Skin(
         bg              = "asfalt",
     )
@@ -69,18 +73,23 @@ req = env['request']
 req.matchdict = {'id':3}
 
 print "Requesting player data from db ..."
+cutoff_dt = datetime.utcnow() - timedelta(hours=DELTA)
 start = datetime.now()
 players = []
 if locals().has_key('NUM_PLAYERS'):
-    players = DBSession.query(Player).\
+    players = DBSession.query(distinct(Player.player_id)).\
             filter(Player.player_id == PlayerElo.player_id).\
+            filter(Player.player_id == PlayerGameStat.player_id).\
+            filter(PlayerGameStat.create_dt > cutoff_dt).\
             filter(Player.nick != None).\
             filter(Player.player_id > 2).\
             filter(Player.active_ind == True).\
             limit(NUM_PLAYERS).all()
 else:
-    players = DBSession.query(Player).\
+    players = DBSession.query(distinct(Player.player_id)).\
             filter(Player.player_id == PlayerElo.player_id).\
+            filter(Player.player_id == PlayerGameStat.player_id).\
+            filter(PlayerGameStat.create_dt > cutoff_dt).\
             filter(Player.nick != None).\
             filter(Player.player_id > 2).\
             filter(Player.active_ind == True).\
@@ -94,18 +103,18 @@ print "Query took %.2f seconds" % (total_seconds)
 print "Creating badges for %d players ..." % len(players)
 start = datetime.now()
 data_time, render_time = 0,0
-for player in players:
-    req.matchdict['id'] = player.player_id
+for player_id in players:
+    req.matchdict['id'] = player_id
 
     sstart = datetime.now()
-    skin.get_data(player)
+    skin.get_data(player_id)
     sstop = datetime.now()
     td = sstop-sstart
     total_seconds = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
     data_time += total_seconds
 
     sstart = datetime.now()
-    skin.render_image("output/%d.png" % player.player_id)
+    skin.render_image("output/%d.png" % player_id)
     sstop = datetime.now()
     td = sstop-sstart
     total_seconds = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
index 07da08490892caffc2f959fb888f5aec241e97cb..d0fa9979731f257fbb6ff9d7e353ea8706165af8 100644 (file)
@@ -442,9 +442,12 @@ class Skin:
             ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_NORMAL)
             ctx.set_font_size(self.deaths_fontsize)
             ctx.set_source_rgb(self.deaths_color[0], self.deaths_color[1], self.deaths_color[2])
-            txt = "%d death" % deaths
-            if deaths != 1:
-                txt += "s"
+            if deaths is not None:
+                txt = "%d death" % deaths
+                if deaths != 1:
+                    txt += "s"
+            else:
+                txt = ""
             xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
             ctx.move_to(self.deaths_pos[0]-xoff-tw/2, self.deaths_pos[1]+yoff)
             ctx.show_text(txt)
@@ -475,7 +478,7 @@ class Skin:
         writepng(output_filename, imgdata, self.width, self.height)
 
 
-    def get_data(self, player):
+    def get_data(self, player_id):
         """Return player data as dict.
 
         This function is similar to the function in player.py but more optimized
@@ -486,8 +489,7 @@ class Skin:
         # wins/losses
         # kills/deaths
         # duel/dm/tdm/ctf elo + rank
-
-        player_id = player.player_id
+        player = DBSession.query(Player).filter(Player.player_id == player_id).one()
 
         games_played = DBSession.query(
                 Game.game_type_cd, func.count(), func.sum(PlayerGameStat.alivetime)).\