4 from datetime import datetime
5 import sqlalchemy as sa
6 import sqlalchemy.sql.functions as func
7 from sqlalchemy import distinct
8 from pyramid.paster import bootstrap
9 from xonstat.models import *
11 from render import PlayerData, Skin
14 # maximal number of query results (for testing, set to 0 to get all)
17 # we look for players who have activity within the past DELTA hours
21 # classic skin WITHOUT NAME - writes PNGs into "output//###.png"
22 skin_classic = Skin( "",
26 # more fancy skin [** WIP **]- writes PNGs into "output/archer/###.png"
27 skin_archer = Skin( "archer",
28 bg = "background_archer-v1",
32 # minimal skin - writes PNGs into "output/minimal/###.png"
33 skin_minimal = Skin( "minimal",
35 bgcolor = (0.04, 0.04, 0.04, 1.0),
36 overlay = "overlay_minimal",
40 gametype_pos = (25,30),
41 gametype_text = "%s :",
43 gametype_fontsize = 10,
45 elo_text = "Elo %.0f",
46 elo_color = (1.0, 1.0, 0.6),
48 nostats_pos = (80,30),
49 nostats_fontsize = 10,
51 nostats_text = "no stats!",
59 ptime_color = (0.8, 0.8, 0.9),
62 # parse cmdline parameters (for testing)
65 for arg in sys.argv[1:]:
66 if arg.startswith("-"):
69 DELTA = 2**24 # large enough to enforce update, and doesn't result in errors
73 print """Usage: gen_badges.py [options] [skin list]
75 -force Force updating all badges (delta = 2^24)
76 -testing Limit number of players to 200 (for testing)
77 -help Show this help text
79 Space-separated list of skins to use when creating badges.
80 Available skins: classic, minimal, archer
81 If no skins are given, classic and minmal will be used by default.
82 NOTE: Output directories must exists before running the program!
87 skins.append(skin_classic)
88 elif arg == "minimal":
89 skins.append(skin_minimal)
91 skins.append(skin_archer)
94 skins = [ skin_classic, skin_minimal ]
98 env = bootstrap('../../../development.ini')
100 req.matchdict = {'id':3}
102 print "Requesting player data from db ..."
103 cutoff_dt = datetime.utcnow() - timedelta(hours=DELTA)
104 start = datetime.now()
107 players = DBSession.query(distinct(Player.player_id)).\
108 filter(Player.player_id == PlayerElo.player_id).\
109 filter(Player.player_id == PlayerGameStat.player_id).\
110 filter(PlayerGameStat.create_dt > cutoff_dt).\
111 filter(Player.nick != None).\
112 filter(Player.player_id > 2).\
113 filter(Player.active_ind == True).\
114 limit(NUM_PLAYERS).all()
116 players = DBSession.query(distinct(Player.player_id)).\
117 filter(Player.player_id == PlayerElo.player_id).\
118 filter(Player.player_id == PlayerGameStat.player_id).\
119 filter(PlayerGameStat.create_dt > cutoff_dt).\
120 filter(Player.nick != None).\
121 filter(Player.player_id > 2).\
122 filter(Player.active_ind == True).\
125 playerdata = PlayerData()
128 stop = datetime.now()
130 total_seconds = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
131 print "Query took %.2f seconds" % (total_seconds)
133 print "Creating badges for %d players ..." % len(players)
134 start = datetime.now()
135 data_time, render_time = 0,0
136 for player_id in players:
137 req.matchdict['id'] = player_id
139 sstart = datetime.now()
140 playerdata.get_data(player_id)
141 sstop = datetime.now()
143 total_seconds = float(td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
144 data_time += total_seconds
146 sstart = datetime.now()
148 sk.render_image(playerdata, "output/%s/%d.png" % (str(sk), player_id[0]))
149 sstop = datetime.now()
151 total_seconds = float(td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
152 render_time += total_seconds
154 stop = datetime.now()
156 total_seconds = float(td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
157 print "Creating the badges took %.1f seconds (%.3f s per player)" % (total_seconds, total_seconds/float(len(players)))
158 print "Total time for rendering images: %.3f s" % render_time
159 print "Total time for getting data: %.3f s" % data_time
162 print "No active players found!"