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 = (25,30),
43 gametype_text = "%s :",
45 gametype_fontsize = 10,
47 elo_text = "Elo %.0f",
48 elo_color = (1.0, 1.0, 0.6),
50 nostats_pos = (80,30),
51 nostats_fontsize = 10,
53 nostats_text = "no stats!",
61 ptime_color = (0.8, 0.8, 0.9),
65 # parse cmdline parameters (for testing)
68 for arg in sys.argv[1:]:
69 if arg.startswith("-"):
72 DELTA = 2**24 # large enough to enforce update, and doesn't result in errors
76 print """Usage: gen_badges.py [options] [skin list]
78 -force Force updating all badges (delta = 2^24)
79 -test Limit number of players to 100 (for testing)
80 -help Show this help text
82 Space-separated list of skins to use when creating badges.
83 Available skins: classic, minimal, archer
84 If no skins are given, classic and minmal will be used by default.
85 NOTE: Output directories must exists before running the program!
90 skins.append(skin_classic)
91 elif arg == "minimal":
92 skins.append(skin_minimal)
94 skins.append(skin_archer)
97 skins = [ skin_classic, skin_minimal ]
101 env = bootstrap('../../../development.ini')
103 req.matchdict = {'id':3}
105 print "Requesting player data from db ..."
106 cutoff_dt = datetime.utcnow() - timedelta(hours=DELTA)
107 start = datetime.now()
110 players = DBSession.query(distinct(Player.player_id)).\
111 filter(Player.player_id == PlayerElo.player_id).\
112 filter(Player.player_id == PlayerGameStat.player_id).\
113 filter(PlayerGameStat.create_dt > cutoff_dt).\
114 filter(Player.nick != None).\
115 filter(Player.player_id > 2).\
116 filter(Player.active_ind == True).\
117 limit(NUM_PLAYERS).all()
119 players = DBSession.query(distinct(Player.player_id)).\
120 filter(Player.player_id == PlayerElo.player_id).\
121 filter(Player.player_id == PlayerGameStat.player_id).\
122 filter(PlayerGameStat.create_dt > cutoff_dt).\
123 filter(Player.nick != None).\
124 filter(Player.player_id > 2).\
125 filter(Player.active_ind == True).\
128 playerdata = PlayerData()
131 stop = datetime.now()
133 total_seconds = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
134 print "Query took %.2f seconds" % (total_seconds)
136 print "Creating badges for %d players ..." % len(players)
137 start = datetime.now()
138 data_time, render_time = 0,0
139 for player_id in players:
140 req.matchdict['id'] = player_id
142 sstart = datetime.now()
143 playerdata.get_data(player_id)
144 sstop = datetime.now()
146 total_seconds = float(td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
147 data_time += total_seconds
149 sstart = datetime.now()
151 sk.render_image(playerdata, "output/%s/%d.png" % (str(sk), player_id[0]))
152 sstop = datetime.now()
154 total_seconds = float(td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
155 render_time += total_seconds
157 stop = datetime.now()
159 total_seconds = float(td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
160 print "Creating the badges took %.1f seconds (%.3f s per player)" % (total_seconds, total_seconds/float(len(players)))
161 print "Total time for rendering images: %.3f s" % render_time
162 print "Total time for getting data: %.3f s" % data_time
165 print "No active players found!"