4 import sqlalchemy as sa
5 import sqlalchemy.sql.functions as func
6 from datetime import datetime
7 from mako.template import Template
9 from pyramid.paster import bootstrap
10 from xonstat.models import *
11 from xonstat.views.player import player_info_data
12 from xonstat.util import qfont_decode
13 from colorsys import rgb_to_hls, hls_to_rgb
16 # similar to html_colors() from util.py
17 _contrast_threshold = 0.5
19 _dec_colors = [ (0.5,0.5,0.5),
32 # parameters to affect the output, could be changed via URL
36 'bg': 1, # 0 - black, 1 - dark_wall
37 'font': 0, # 0 - xolonium, 1 - dejavu sans
41 # maximal number of query results (for testing, set to 0 to get all)
46 """Return player data as dict.
48 This function is similar to the function in player.py but more optimized
55 # duel/dm/tdm/ctf elo + rank
57 player_id = player.player_id
61 games_played = DBSession.query(
62 Game.game_type_cd, func.count(), func.sum(PlayerGameStat.alivetime)).\
63 filter(Game.game_id == PlayerGameStat.game_id).\
64 filter(PlayerGameStat.player_id == player_id).\
65 group_by(Game.game_type_cd).\
66 order_by(func.count().desc()).\
67 limit(3).all() # limit to 3 gametypes!
69 total_stats['games'] = 0
70 total_stats['games_breakdown'] = {} # this is a dictionary inside a dictionary .. dictception?
71 total_stats['games_alivetime'] = {}
72 total_stats['gametypes'] = []
73 for (game_type_cd, games, alivetime) in games_played:
74 total_stats['games'] += games
75 total_stats['gametypes'].append(game_type_cd)
76 total_stats['games_breakdown'][game_type_cd] = games
77 total_stats['games_alivetime'][game_type_cd] = alivetime
79 (total_stats['kills'], total_stats['deaths'], total_stats['suicides'],
80 total_stats['alivetime'],) = DBSession.query(
81 func.sum(PlayerGameStat.kills),
82 func.sum(PlayerGameStat.deaths),
83 func.sum(PlayerGameStat.suicides),
84 func.sum(PlayerGameStat.alivetime)).\
85 filter(PlayerGameStat.player_id == player_id).\
88 (total_stats['wins'],) = DBSession.query(
90 filter(Game.game_id == PlayerGameStat.game_id).\
91 filter(PlayerGameStat.player_id == player_id).\
92 filter(Game.winner == PlayerGameStat.team or PlayerGameStat.rank == 1).\
95 ranks = DBSession.query("game_type_cd", "rank", "max_rank").\
97 "select pr.game_type_cd, pr.rank, overall.max_rank "
98 "from player_ranks pr, "
99 "(select game_type_cd, max(rank) max_rank "
101 "group by game_type_cd) overall "
102 "where pr.game_type_cd = overall.game_type_cd "
103 "and player_id = :player_id "
105 params(player_id=player_id).all()
108 for gtc,rank,max_rank in ranks:
109 ranks_dict[gtc] = (rank, max_rank)
111 elos = DBSession.query(PlayerElo).\
112 filter_by(player_id=player_id).\
113 order_by(PlayerElo.elo.desc()).\
119 elos_dict[elo.game_type_cd] = elo.elo
123 'total_stats':total_stats,
132 def render_image(data):
133 """Render an image from the given data fields."""
135 width, height = params['width'], params['height']
136 output = "output/%s.png" % data['player'].player_id
139 if params['font'] == 1:
142 total_stats = data['total_stats']
143 total_games = total_stats['games']
145 ranks = data["ranks"]
150 surf = C.ImageSurface(C.FORMAT_RGB24, width, height)
151 ctx = C.Context(surf)
152 ctx.set_antialias(C.ANTIALIAS_GRAY)
154 # draw background (just plain fillcolor)
155 if params['bg'] == 0:
156 ctx.rectangle(0, 0, width, height)
157 ctx.set_source_rgba(0.2, 0.2, 0.2, 1.0)
160 # draw background image (try to get correct tiling, too)
163 if params['bg'] == 1:
164 bg = C.ImageSurface.create_from_png("img/dark_wall.png")
167 bg_w, bg_h = bg.get_width(), bg.get_height()
169 while bg_xoff < width:
171 while bg_yoff < height:
172 ctx.set_source_surface(bg, bg_xoff, bg_yoff)
178 ## draw player's nickname with fancy colors
180 # fontsize is reduced if width gets too large
182 ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_NORMAL)
183 ctx.set_font_size(20)
184 xoff, yoff, tw, th = ctx.text_extents(player.stripped_nick)[:4]
186 ctx.set_font_size(18)
187 xoff, yoff, tw, th = ctx.text_extents(player.stripped_nick)[:4]
189 ctx.set_font_size(16)
190 xoff, yoff, tw, th = ctx.text_extents(player.stripped_nick)[:4]
192 ctx.set_font_size(14)
193 xoff, yoff, tw, th = ctx.text_extents(player.stripped_nick)[:4]
195 ctx.set_font_size(12)
197 # split up nick into colored segments and draw each of them
198 qstr = qfont_decode(player.nick).replace('^^', '^').replace('\x00', ' ')
200 txt_xpos, txt_ypos = 5,18
202 # split nick into colored segments
206 npos = qstr.find('^', pos)
208 parts.append(qstr[pos-1:])
210 parts.append(qstr[pos-1:npos])
214 r,g,b = _dec_colors[7]
216 if txt.startswith('^'):
218 if txt.startswith('x'):
219 r = int(txt[1] * 2, 16) / 255.0
220 g = int(txt[2] * 2, 16) / 255.0
221 b = int(txt[3] * 2, 16) / 255.0
222 hue, light, satur = rgb_to_hls(r, g, b)
223 if light < _contrast_threshold:
224 light = _contrast_threshold
225 r, g, b = hls_to_rgb(hue, light, satur)
228 r,g,b = _dec_colors[int(txt[0])]
231 r,g,b = _dec_colors[7]
234 # only colorcode and no real text, skip this
237 ctx.set_source_rgb(r, g, b)
238 ctx.move_to(txt_xpos + txt_xoff, txt_ypos)
241 xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
243 # only whitespaces, use some extra space
249 ## print elos and ranks
251 games_x, games_y = 60,35
252 games_w = 110 # width of each gametype field
254 # show up to three gametypes the player has participated in
255 for gt in total_stats['gametypes'][:3]:
256 ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_BOLD)
257 ctx.set_font_size(10)
258 ctx.set_source_rgb(1.0, 1.0, 1.0)
259 txt = "[ %s ]" % gt.upper()
260 xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
261 ctx.move_to(games_x-xoff-tw/2,games_y-yoff-4)
264 old_aa = ctx.get_antialias()
265 ctx.set_antialias(C.ANTIALIAS_NONE)
266 ctx.set_source_rgb(0.8, 0.8, 0.8)
267 ctx.set_line_width(1)
268 ctx.move_to(games_x-games_w/2+5, games_y+8)
269 ctx.line_to(games_x+games_w/2-5, games_y+8)
271 ctx.move_to(games_x-games_w/2+5, games_y+32)
272 ctx.line_to(games_x+games_w/2-5, games_y+32)
274 ctx.set_antialias(old_aa)
276 if not elos.has_key(gt) or not ranks.has_key(gt):
277 ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_BOLD)
278 ctx.set_font_size(12)
279 ctx.set_source_rgb(0.8, 0.2, 0.2)
280 txt = "no stats yet!"
281 xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
282 ctx.move_to(games_x-xoff-tw/2,games_y+28-yoff-4)
284 ctx.rotate(math.radians(-10))
288 ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_NORMAL)
289 ctx.set_font_size(10)
290 ctx.set_source_rgb(1.0, 1.0, 0.5)
291 txt = "Elo: %.0f" % round(elos[gt], 0)
292 xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
293 ctx.move_to(games_x-xoff-tw/2,games_y+15-yoff-4)
296 ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_NORMAL)
298 ctx.set_source_rgb(0.8, 0.8, 0.8)
299 txt = "Rank %d of %d" % ranks[gt]
300 xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
301 ctx.move_to(games_x-xoff-tw/2,games_y+25-yoff-3)
307 # print win percentage
308 win_x, win_y = 505,11
309 win_w, win_h = 100,14
311 ctx.rectangle(win_x-win_w/2,win_y-win_h/2,win_w,win_h)
312 ctx.set_source_rgba(0.8, 0.8, 0.8, 0.1)
315 ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_NORMAL)
316 ctx.set_font_size(10)
317 ctx.set_source_rgb(0.8, 0.8, 0.8)
318 txt = "Win Percentage"
319 xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
320 ctx.move_to(win_x-xoff-tw/2,win_y-yoff-3)
324 if total_games > 0 and total_stats['wins'] is not None:
325 ratio = float(total_stats['wins'])/total_games
326 txt = "%.2f%%" % round(ratio * 100, 2)
327 ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_BOLD)
328 ctx.set_font_size(12)
330 ctx.set_source_rgb(0.2, 1.0, 1.0)
332 ctx.set_source_rgb(0.5, 1.0, 1.0)
334 ctx.set_source_rgb(0.5, 1.0, 0.8)
336 ctx.set_source_rgb(0.8, 1.0, 0.5)
338 ctx.set_source_rgb(1.0, 1.0, 0.5)
339 xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
340 ctx.move_to(win_x-xoff-tw/2,win_y+16-yoff-4)
343 ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_NORMAL)
345 ctx.set_source_rgb(0.6, 0.8, 0.6)
346 txt = "%d wins" % total_stats["wins"]
347 xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
348 ctx.move_to(win_x-xoff-tw/2,win_y+28-yoff-3)
350 ctx.set_source_rgb(0.8, 0.6, 0.6)
351 txt = "%d losses" % (total_games-total_stats['wins'])
352 xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
353 ctx.move_to(win_x-xoff-tw/2,win_y+38-yoff-3)
357 # print kill/death ratio
358 kill_x, kill_y = 395,11
359 kill_w, kill_h = 100,14
361 ctx.rectangle(kill_x-kill_w/2,kill_y-kill_h/2,kill_w,kill_h)
362 ctx.set_source_rgba(0.8, 0.8, 0.8, 0.1)
365 ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_NORMAL)
366 ctx.set_font_size(10)
367 ctx.set_source_rgb(0.8, 0.8, 0.8)
369 xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
370 ctx.move_to(kill_x-xoff-tw/2,kill_y-yoff-3)
374 if total_stats['deaths'] > 0 and total_stats['kills'] is not None:
375 ratio = float(total_stats['kills'])/total_stats['deaths']
376 txt = "%.3f" % round(ratio, 3)
377 ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_BOLD)
378 ctx.set_font_size(12)
380 ctx.set_source_rgb(0.0, 1.0, 0.0)
382 ctx.set_source_rgb(0.2, 1.0, 0.2)
384 ctx.set_source_rgb(0.5, 1.0, 0.5)
386 ctx.set_source_rgb(1.0, 0.5, 0.5)
388 ctx.set_source_rgb(1.0, 0.2, 0.2)
389 xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
390 ctx.move_to(kill_x-xoff-tw/2,kill_y+16-yoff-4)
393 ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_NORMAL)
395 ctx.set_source_rgb(0.6, 0.8, 0.6)
396 txt = "%d kills" % total_stats["kills"]
397 xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
398 ctx.move_to(kill_x-xoff-tw/2,kill_y+28-yoff-3)
400 ctx.set_source_rgb(0.8, 0.6, 0.6)
401 txt = "%d deaths" % total_stats['deaths']
402 xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
403 ctx.move_to(kill_x-xoff-tw/2,kill_y+38-yoff-3)
408 time_x, time_y = 450,64
409 time_w, time_h = 210,10
411 ctx.rectangle(time_x-time_w/2,time_y-time_h/2-1,time_w,time_y+time_h/2-1)
412 ctx.set_source_rgba(0.8, 0.8, 0.8, 0.6)
415 ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_NORMAL)
416 ctx.set_font_size(10)
417 ctx.set_source_rgb(0.1, 0.1, 0.1)
418 txt = "Playing time: %s" % str(total_stats['alivetime'])
419 xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
420 ctx.move_to(time_x-xoff-tw/2,time_y-yoff-4)
425 surf.write_to_png(output)
429 env = bootstrap('../../../development.ini')
431 req.matchdict = {'id':3}
433 print "Requesting player data from db ..."
434 start = datetime.now()
435 players = DBSession.query(Player).\
436 filter(Player.player_id == PlayerElo.player_id).\
437 filter(Player.nick != None).\
438 filter(Player.player_id > 2).\
439 filter(Player.active_ind == True).\
440 limit(NUM_PLAYERS).all()
441 stop = datetime.now()
442 print "Query took %.2f seconds" % (stop-start).total_seconds()
444 print "Creating badges for %d players ..." % len(players)
445 start = datetime.now()
446 data_time, render_time = 0,0
447 for player in players:
448 req.matchdict['id'] = player.player_id
450 sstart = datetime.now()
451 #data = player_info_data(req)
452 data = get_data(player)
453 sstop = datetime.now()
454 data_time += (sstop-sstart).total_seconds()
456 print "\t#%d (%s)" % (player.player_id, player.stripped_nick)
458 sstart = datetime.now()
460 sstop = datetime.now()
461 render_time += (sstop-sstart).total_seconds()
463 stop = datetime.now()
464 print "Creating the badges took %.2f seconds (%.2f s per player)" % ((stop-start).total_seconds(), (stop-start).total_seconds()/float(len(players)))
465 print "Total time for getting data: %.2f s" % data_time
466 print "Total time for redering images: %.2f s" % render_time