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 *
12 from playerdata import PlayerData
15 # maximal number of query results (for testing, set to None to get all)
18 # we look for players who have activity within the past DELTA hours
22 # classic skin WITHOUT NAME - writes PNGs into "output//###.png"
23 skin_classic = Skin( "",
25 overlay = "overlay_classic",
28 # more fancy skin [** WIP **]- writes PNGs into "output/archer/###.png"
29 skin_archer = Skin( "archer",
30 bg = "background_archer-v1",
34 # minimal skin - writes PNGs into "output/minimal/###.png"
35 skin_minimal = Skin( "minimal",
37 bgcolor = (0.04, 0.04, 0.04, 1.0),
38 overlay = "overlay_minimal",
42 gametype_pos = (8,30),
43 gametype_color = (0.8, 0.8, 0.8),
44 gametype_text = "%s:",
46 gametype_fontsize = 10,
47 gametype_center = False,
49 elo_text = "Elo %.0f",
50 elo_color = (0.9, 0.9, 0.7),
53 nostats_pos = (75,30),
54 nostats_fontsize = 10,
56 nostats_text = "no stats!",
57 nostats_color = (0.8, 0.3, 0.3),
58 nostats_center = True,
60 kdr_colortop = (0.1, 0.8, 0.1),
61 kdr_colormid = (0.6, 0.6, 0.1),
62 kdr_colorbot = (0.8, 0.1, 0.1),
67 winp_colortop = (0.1, 0.6, 0.6),
68 winp_colormid = (0.2, 0.6, 0.2),
69 winp_colorbot = (0.6, 0.6, 0.1),
72 ptime_color = (0.7, 0.7, 0.9),
76 # parse cmdline parameters (for testing)
79 for arg in sys.argv[1:]:
80 if arg.startswith("-"):
83 DELTA = 2**24 # large enough to enforce update, and doesn't result in errors
87 print """Usage: gen_badges.py [options] [skin list]
89 -force Force updating all badges (delta = 2^24)
90 -test Limit number of players to 100 (for testing)
91 -help Show this help text
93 Space-separated list of skins to use when creating badges.
94 Available skins: classic, minimal, archer
95 If no skins are given, classic and minmal will be used by default.
96 NOTE: Output directories must exists before running the program!
101 skins.append(skin_classic)
102 elif arg == "minimal":
103 skins.append(skin_minimal)
104 elif arg == "archer":
105 skins.append(skin_archer)
108 skins = [ skin_classic, skin_minimal ]
112 env = bootstrap('../../../development.ini')
114 req.matchdict = {'id':3}
116 print "Requesting player data from db ..."
117 cutoff_dt = datetime.utcnow() - timedelta(hours=DELTA)
118 start = datetime.now()
121 players = DBSession.query(distinct(Player.player_id)).\
122 filter(Player.player_id == PlayerElo.player_id).\
123 filter(Player.player_id == PlayerGameStat.player_id).\
124 filter(PlayerGameStat.create_dt > cutoff_dt).\
125 filter(Player.nick != None).\
126 filter(Player.player_id > 2).\
127 filter(Player.active_ind == True).\
128 limit(NUM_PLAYERS).all()
130 players = DBSession.query(distinct(Player.player_id)).\
131 filter(Player.player_id == PlayerElo.player_id).\
132 filter(Player.player_id == PlayerGameStat.player_id).\
133 filter(PlayerGameStat.create_dt > cutoff_dt).\
134 filter(Player.nick != None).\
135 filter(Player.player_id > 2).\
136 filter(Player.active_ind == True).\
139 playerdata = PlayerData()
142 stop = datetime.now()
144 total_seconds = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
145 print "Query took %.2f seconds" % (total_seconds)
147 print "Creating badges for %d players ..." % len(players)
148 start = datetime.now()
149 data_time, render_time = 0,0
150 for player_id in players:
151 req.matchdict['id'] = player_id
153 sstart = datetime.now()
154 playerdata.get_data(player_id)
155 sstop = datetime.now()
157 total_seconds = float(td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
158 data_time += total_seconds
160 sstart = datetime.now()
162 sk.render_image(playerdata, "output/%s/%d.png" % (str(sk), player_id[0]))
163 sstop = datetime.now()
165 total_seconds = float(td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
166 render_time += total_seconds
168 stop = datetime.now()
170 total_seconds = float(td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
171 print "Creating the badges took %.1f seconds (%.3f s per player)" % (total_seconds, total_seconds/float(len(players)))
172 print "Total time for rendering images: %.3f s" % render_time
173 print "Total time for getting data: %.3f s" % data_time
176 print "No active players found!"