]> git.xonotic.org Git - xonotic/xonstat.git/blobdiff - xonstat/batch/badges/gen_badges.py
Order gametypes on player badges by playing time, not number of games
[xonotic/xonstat.git] / xonstat / batch / badges / gen_badges.py
index 1d3acf1a02c1ac49ba69a73f69129a420560aa0d..d0d6fb226e5c0b46bb629a2eaf84ac8fc401c9c1 100644 (file)
@@ -7,6 +7,7 @@ import sqlalchemy.sql.functions as func
 from sqlalchemy import distinct
 from pyramid.paster import bootstrap
 from xonstat.models import *
+from xonstat.util import datetime_seconds
 
 from skin import Skin
 from playerdata import PlayerData
@@ -15,9 +16,15 @@ from playerdata import PlayerData
 # maximal number of query results (for testing, set to None to get all)
 NUM_PLAYERS = None
 
+# filter by player id (for testing, set to None to disable)
+PLAYER_ID = None
+
 # we look for players who have activity within the past DELTA hours
 DELTA = 6
 
+VERBOSE = False
+
+INIFILE = None  # keep this set to "None"
 
 # classic skin WITHOUT NAME - writes PNGs into "output//###.png"
 skin_classic = Skin( "",
@@ -27,8 +34,21 @@ skin_classic = Skin( "",
 
 # more fancy skin [** WIP **]- writes PNGs into "output/archer/###.png"
 skin_archer = Skin( "archer",
-        bg              = "background_archer-v1",
-        overlay         = None,
+        #bg              = "background_archer-v2_full",
+        bg              = "background_archer-v3",
+        overlay         = "",
+        nick_maxwidth  = 260,
+        gametype_pos    = (91,33),
+        nostats_pos            = (91,59),
+        elo_pos        = (91,47),
+        rank_pos    = (91,58),
+        winp_pos       = (509,20),
+        wins_pos       = (508,35),
+        loss_pos       = (508,45),
+        kdr_pos                = (392,20),
+        kills_pos      = (392,35),
+        deaths_pos     = (392,45),
+        ptime_color    = (0.05, 0.05, 0.1),
     )
 
 # minimal skin - writes PNGs into "output/minimal/###.png"
@@ -41,7 +61,7 @@ skin_minimal = Skin( "minimal",
         nick_fontsize   = 16,
         nick_pos        = (36,16),
         num_gametypes   = 3,
-        nick_maxwidth   = 300,
+        nick_maxwidth   = 280,
         gametype_pos    = (70,30),
         gametype_color  = (0.0, 0.0, 0.0),
         gametype_text   = "%s:",
@@ -84,17 +104,31 @@ skin_minimal = Skin( "minimal",
 skins = []
 for arg in sys.argv[1:]:
     if arg.startswith("-"):
-        arg = arg[1:]
-        if arg == "force":
+        try:
+            key,value = arg[1:].split("=")
+        except:
+            key,value = arg[1:], ""
+        if key == "force":
             DELTA = 2**24   # large enough to enforce update, and doesn't result in errors
-        elif arg == "test":
+        elif key == "delta":
+            DELTA = float(value)
+        elif key == "test":
             NUM_PLAYERS = 100
+        elif key == "player":
+            PLAYER_ID = int(value)
+        elif key == "verbose":
+            VERBOSE = True
         else:
-            print """Usage:  gen_badges.py [options] [skin list]
+            print """Usage:  gen_badges.py [options] <ini-file> [skin list]
     Options:
         -force      Force updating all badges (delta = 2^24)
+        -delta n    Manually set an update interval (delta)
         -test       Limit number of players to 100 (for testing)
+        -player #   Filter by given player id
+        -verbose    Show more verbose output
         -help       Show this help text
+    Ini-File:
+        Name of a Pyramid ini-file to use (e.g. prodution.ini or development.ini).
     Skin list:
         Space-separated list of skins to use when creating badges.
         Available skins:  classic, minimal, archer
@@ -103,19 +137,25 @@ for arg in sys.argv[1:]:
 """
             sys.exit(-1)
     else:
-        if arg == "classic":
-            skins.append(skin_classic)
-        elif arg == "minimal":
-            skins.append(skin_minimal)
-        elif arg == "archer":
-            skins.append(skin_archer)
+        if INIFILE == None:
+            INIFILE = arg
+        else:
+            if arg == "classic":
+                skins.append(skin_classic)
+            elif arg == "minimal":
+                skins.append(skin_minimal)
+            elif arg == "archer":
+                       skins.append(skin_archer)
 
 if len(skins) == 0:
-    skins = [ skin_classic, skin_minimal ]
+    skins = [ skin_classic, skin_minimal, skin_archer ]
 
+if not INIFILE:
+    print "You must provide the name of an ini-file to use! Type 'gen_badges.py -h' for help."
+    sys.exit(-1)
 
 # environment setup
-env = bootstrap('../../../development.ini')
+env = bootstrap(INIFILE)
 req = env['request']
 req.matchdict = {'id':3}
 
@@ -132,6 +172,15 @@ if NUM_PLAYERS:
             filter(Player.player_id > 2).\
             filter(Player.active_ind == True).\
             limit(NUM_PLAYERS).all()
+elif PLAYER_ID:
+    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 == PLAYER_ID).\
+            filter(Player.active_ind == True).\
+            limit(NUM_PLAYERS).all()
 else:
     players = DBSession.query(distinct(Player.player_id)).\
             filter(Player.player_id == PlayerElo.player_id).\
@@ -142,13 +191,12 @@ else:
             filter(Player.active_ind == True).\
             all()
 
-playerdata = PlayerData()
+playerdata = PlayerData
 
 if len(players) > 0:
     stop = datetime.now()
     td = stop-start
-    total_seconds = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
-    print "Query took %.2f seconds" % (total_seconds)
+    print "Query took %.2f seconds" % (datetime_seconds(td))
 
     print "Creating badges for %d players ..." % len(players)
     start = datetime.now()
@@ -160,20 +208,21 @@ if len(players) > 0:
         playerdata.get_data(player_id)
         sstop = datetime.now()
         td = sstop-sstart
-        total_seconds = float(td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
-        data_time += total_seconds
+        data_time += datetime_seconds(td)
 
         sstart = datetime.now()
         for sk in skins:
-            sk.render_image(playerdata, "output/%s/%d.png" % (str(sk), player_id[0]))
+            sk.render_image(playerdata.data, "output/%s/%d.png" % (str(sk), player_id[0]))
         sstop = datetime.now()
         td = sstop-sstart
-        total_seconds = float(td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
-        render_time += total_seconds
+        render_time += datetime_seconds(td)
+
+        if VERBOSE == True:
+            print player_id, unicode(playerdata.data['player'].nick)
 
     stop = datetime.now()
     td = stop-start
-    total_seconds = float(td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
+    total_seconds = datetime_seconds(td)
     print "Creating the badges took %.1f seconds (%.3f s per player)" % (total_seconds, total_seconds/float(len(players)))
     print "Total time for rendering images: %.3f s" % render_time
     print "Total time for getting data: %.3f s" % data_time