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 *
10 from xonstat.util import datetime_seconds
13 from playerdata import PlayerData
16 # maximal number of query results (for testing, set to None to get all)
19 # we look for players who have activity within the past DELTA hours
23 # classic skin WITHOUT NAME - writes PNGs into "output//###.png"
24 skin_classic = Skin( "",
26 overlay = "overlay_classic",
29 # more fancy skin [** WIP **]- writes PNGs into "output/archer/###.png"
30 skin_archer = Skin( "archer",
31 #bg = "background_archer-v2_full",
32 bg = "background_archer-v3",
35 gametype_pos = (91,33),
36 nostats_pos = (91,59),
44 deaths_pos = (392,45),
45 ptime_color = (0.05, 0.05, 0.1),
48 # minimal skin - writes PNGs into "output/minimal/###.png"
49 skin_minimal = Skin( "minimal",
51 bgcolor = (0.04, 0.04, 0.04, 1.0),
52 overlay = "overlay_minimal",
59 gametype_pos = (70,30),
60 gametype_color = (0.0, 0.0, 0.0),
61 gametype_text = "%s:",
63 gametype_fontsize = 10,
65 gametype_upper = False,
67 elo_text = "Elo %.0f",
68 elo_color = (0.7, 0.7, 0.7),
72 #nostats_pos = (75,30),
73 #nostats_fontsize = 10,
75 #nostats_text = "no stats yet!",
76 #nostats_color = (0.7, 0.4, 0.4),
79 kdr_colortop = (0.6, 0.8, 0.6),
80 kdr_colormid = (0.6, 0.6, 0.6),
81 kdr_colorbot = (0.8, 0.6, 0.6),
86 winp_colortop = (0.6, 0.8, 0.8),
87 winp_colormid = (0.6, 0.6, 0.6),
88 winp_colorbot = (0.8, 0.8, 0.6),
92 ptime_color = (0.7, 0.7, 0.7),
96 # parse cmdline parameters (for testing)
99 for arg in sys.argv[1:]:
100 if arg.startswith("-"):
103 DELTA = 2**24 # large enough to enforce update, and doesn't result in errors
107 print """Usage: gen_badges.py [options] [skin list]
109 -force Force updating all badges (delta = 2^24)
110 -test Limit number of players to 100 (for testing)
111 -help Show this help text
113 Space-separated list of skins to use when creating badges.
114 Available skins: classic, minimal, archer
115 If no skins are given, classic and minmal will be used by default.
116 NOTE: Output directories must exists before running the program!
121 skins.append(skin_classic)
122 elif arg == "minimal":
123 skins.append(skin_minimal)
124 elif arg == "archer":
125 skins.append(skin_archer)
128 skins = [ skin_classic, skin_minimal, skin_archer ]
131 env = bootstrap('../../../development.ini')
133 req.matchdict = {'id':3}
135 print "Requesting player data from db ..."
136 cutoff_dt = datetime.utcnow() - timedelta(hours=DELTA)
137 start = datetime.now()
140 players = DBSession.query(distinct(Player.player_id)).\
141 filter(Player.player_id == PlayerElo.player_id).\
142 filter(Player.player_id == PlayerGameStat.player_id).\
143 filter(PlayerGameStat.create_dt > cutoff_dt).\
144 filter(Player.nick != None).\
145 filter(Player.player_id > 2).\
146 filter(Player.active_ind == True).\
147 limit(NUM_PLAYERS).all()
149 players = DBSession.query(distinct(Player.player_id)).\
150 filter(Player.player_id == PlayerElo.player_id).\
151 filter(Player.player_id == PlayerGameStat.player_id).\
152 filter(PlayerGameStat.create_dt > cutoff_dt).\
153 filter(Player.nick != None).\
154 filter(Player.player_id > 2).\
155 filter(Player.active_ind == True).\
158 playerdata = PlayerData()
161 stop = datetime.now()
163 print "Query took %.2f seconds" % (datetime_seconds(td))
165 print "Creating badges for %d players ..." % len(players)
166 start = datetime.now()
167 data_time, render_time = 0,0
168 for player_id in players:
169 req.matchdict['id'] = player_id
171 sstart = datetime.now()
172 playerdata.get_data(player_id)
173 sstop = datetime.now()
175 data_time += datetime_seconds(td)
177 sstart = datetime.now()
179 sk.render_image(playerdata, "output/%s/%d.png" % (str(sk), player_id[0]))
180 sstop = datetime.now()
182 render_time += datetime_seconds(td)
184 stop = datetime.now()
186 total_seconds = datetime_seconds(td)
187 print "Creating the badges took %.1f seconds (%.3f s per player)" % (total_seconds, total_seconds/float(len(players)))
188 print "Total time for rendering images: %.3f s" % render_time
189 print "Total time for getting data: %.3f s" % data_time
192 print "No active players found!"