]> de.git.xonotic.org Git - xonotic/xonstat.git/blob - xonstat/batch/badges/gen_badges.py
More fine-tuning of the badges generator
[xonotic/xonstat.git] / xonstat / batch / badges / gen_badges.py
1 #-*- coding: utf-8 -*-
2
3 import cairo as C
4 import sqlalchemy as sa
5 import sqlalchemy.sql.functions as func
6 from datetime import datetime
7 from mako.template import Template
8 from os import system
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
14
15
16 # similar to html_colors() from util.py
17 _contrast_threshold = 0.5
18
19 _dec_colors = [ (0.5,0.5,0.5),
20                 (1.0,0.0,0.0),
21                 (0.2,1.0,0.0),
22                 (1.0,1.0,0.0),
23                 (0.2,0.4,1.0),
24                 (0.2,1.0,1.0),
25                 (1.0,0.2,102),
26                 (1.0,1.0,1.0),
27                 (0.6,0.6,0.6),
28                 (0.5,0.5,0.5)
29             ]
30
31
32 # parameters to affect the output, could be changed via URL
33 params = {
34     'width':        560,
35     'height':        70,
36     'bg':           1,                      # 0 - black, 1 - dark_wall
37     'font':         0,                      # 0 - xolonium, 1 - dejavu sans
38 }
39
40
41 # maximal number of query results (for testing, set to 0 to get all)
42 NUM_PLAYERS = 100
43
44
45 def get_data(player):
46     """Return player data as dict.
47     
48     This function is similar to the function in player.py but more optimized
49     for this purpose.
50     """
51
52     # total games
53     # wins/losses
54     # kills/deaths
55     # duel/dm/tdm/ctf elo + rank
56     
57     player_id = player.player_id
58     
59     total_stats = {}
60     
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!
68     
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
78     
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).\
86             one()
87     
88     (total_stats['wins'],) = DBSession.query(
89             func.count("*")).\
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).\
93             one()
94     
95     ranks = DBSession.query("game_type_cd", "rank", "max_rank").\
96             from_statement(
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 "
100                     "from player_ranks  "
101                     "group by game_type_cd) overall "
102                 "where pr.game_type_cd = overall.game_type_cd  "
103                 "and player_id = :player_id "
104                 "order by rank").\
105             params(player_id=player_id).all()
106     
107     ranks_dict = {}
108     for gtc,rank,max_rank in ranks:
109         ranks_dict[gtc] = (rank, max_rank)
110
111     elos = DBSession.query(PlayerElo).\
112             filter_by(player_id=player_id).\
113             order_by(PlayerElo.elo.desc()).\
114             all()
115     
116     elos_dict = {}
117     for elo in elos:
118         if elo.games > 32:
119             elos_dict[elo.game_type_cd] = elo.elo
120     
121     data = {
122             'player':player,
123             'total_stats':total_stats,
124             'ranks':ranks_dict,
125             'elos':elos_dict,
126         }
127         
128     #print data
129     return data
130
131
132 def render_image(data):
133     """Render an image from the given data fields."""
134     
135     width, height = params['width'], params['height']
136     output = "output/%s.png" % data['player'].player_id
137     
138     font = "Xolonium"
139     if params['font'] == 1:
140         font = "DejaVu Sans"
141
142     total_stats = data['total_stats']
143     total_games = total_stats['games']
144     elos = data["elos"]
145     ranks = data["ranks"]
146
147
148     ## create background
149
150     surf = C.ImageSurface(C.FORMAT_RGB24, width, height)
151     ctx = C.Context(surf)
152     ctx.set_antialias(C.ANTIALIAS_GRAY)
153
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)
158         ctx.fill()
159     
160     # draw background image (try to get correct tiling, too)
161     if params['bg'] > 0:
162         bg = None
163         if params['bg'] == 1:
164             bg = C.ImageSurface.create_from_png("img/dark_wall.png")
165         
166         if bg:
167             bg_w, bg_h = bg.get_width(), bg.get_height()
168             bg_xoff = 0
169             while bg_xoff < width:
170                 bg_yoff = 0
171                 while bg_yoff < height:
172                     ctx.set_source_surface(bg, bg_xoff, bg_yoff)
173                     ctx.paint()
174                     bg_yoff += bg_h
175                 bg_xoff += bg_w
176
177
178     ## draw player's nickname with fancy colors
179     
180     # fontsize is reduced if width gets too large
181     nick_xmax = 335
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]
185     if tw > nick_xmax:
186         ctx.set_font_size(18)
187         xoff, yoff, tw, th = ctx.text_extents(player.stripped_nick)[:4]
188         if tw > nick_xmax:
189             ctx.set_font_size(16)
190             xoff, yoff, tw, th = ctx.text_extents(player.stripped_nick)[:4]
191             if tw > nick_xmax:
192                 ctx.set_font_size(14)
193                 xoff, yoff, tw, th = ctx.text_extents(player.stripped_nick)[:4]
194                 if tw > nick_xmax:
195                     ctx.set_font_size(12)
196     
197     # split up nick into colored segments and draw each of them
198     qstr = qfont_decode(player.nick).replace('^^', '^').replace('\x00', ' ')
199     txt_xoff = 0
200     txt_xpos, txt_ypos = 5,18
201     
202     # split nick into colored segments
203     parts = []
204     pos = 1
205     while True:
206         npos = qstr.find('^', pos)
207         if npos < 0:
208             parts.append(qstr[pos-1:])
209             break;
210         parts.append(qstr[pos-1:npos])
211         pos = npos+1
212     
213     for txt in parts:
214         r,g,b = _dec_colors[7]
215         try:
216             if txt.startswith('^'):
217                 txt = txt[1:]
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)
226                     txt = txt[4:]
227                 else:
228                     r,g,b = _dec_colors[int(txt[0])]
229                     txt = txt[1:]
230         except:
231             r,g,b = _dec_colors[7]
232         
233         if len(txt) < 1:
234             # only colorcode and no real text, skip this
235             continue
236         
237         ctx.set_source_rgb(r, g, b)
238         ctx.move_to(txt_xpos + txt_xoff, txt_ypos)
239         ctx.show_text(txt)
240
241         xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
242         if tw == 0:
243             # only whitespaces, use some extra space
244             tw += 5*len(txt)
245         
246         txt_xoff += tw + 1
247
248
249     ## print elos and ranks
250     
251     games_x, games_y = 60,35
252     games_w = 110       # width of each gametype field
253     
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)
262         ctx.show_text(txt)
263         
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)
270         ctx.stroke()
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)
273         ctx.stroke()
274         ctx.set_antialias(old_aa)
275         
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)
283             ctx.save()
284             ctx.rotate(math.radians(-10))
285             ctx.show_text(txt)
286             ctx.restore()
287         else:
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)
294             ctx.show_text(txt)
295             
296             ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_NORMAL)
297             ctx.set_font_size(8)
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)
302             ctx.show_text(txt)
303         
304         games_x += games_w
305
306
307     # print win percentage
308     win_x, win_y = 505,11
309     win_w, win_h = 100,14
310     
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)
313     ctx.fill();
314     
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)
321     ctx.show_text(txt)
322     
323     txt = "???"
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)
329     if ratio >= 0.90:
330         ctx.set_source_rgb(0.2, 1.0, 1.0)
331     elif ratio >= 0.75:
332         ctx.set_source_rgb(0.5, 1.0, 1.0)
333     elif ratio >= 0.5:
334         ctx.set_source_rgb(0.5, 1.0, 0.8)
335     elif ratio >= 0.25:
336         ctx.set_source_rgb(0.8, 1.0, 0.5)
337     else:
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)
341     ctx.show_text(txt)
342     
343     ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_NORMAL)
344     ctx.set_font_size(8)
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)
349     ctx.show_text(txt)
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)
354     ctx.show_text(txt)
355
356
357     # print kill/death ratio
358     kill_x, kill_y = 395,11
359     kill_w, kill_h = 100,14
360     
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)
363     ctx.fill()
364     
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)
368     txt = "Kill Ratio"
369     xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
370     ctx.move_to(kill_x-xoff-tw/2,kill_y-yoff-3)
371     ctx.show_text(txt)
372     
373     txt = "???"
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)
379     if ratio >= 3:
380         ctx.set_source_rgb(0.0, 1.0, 0.0)
381     elif ratio >= 2:
382         ctx.set_source_rgb(0.2, 1.0, 0.2)
383     elif ratio >= 1:
384         ctx.set_source_rgb(0.5, 1.0, 0.5)
385     elif ratio >= 0.5:
386         ctx.set_source_rgb(1.0, 0.5, 0.5)
387     else:
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)
391     ctx.show_text(txt)
392
393     ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_NORMAL)
394     ctx.set_font_size(8)
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)
399     ctx.show_text(txt)
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)
404     ctx.show_text(txt)
405
406
407     # print playing time
408     time_x, time_y = 450,64
409     time_w, time_h = 210,10
410     
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)
413     ctx.fill();
414     
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)
421     ctx.show_text(txt)
422
423
424     # save to PNG
425     surf.write_to_png(output)
426
427
428 # environment setup
429 env = bootstrap('../../../development.ini')
430 req = env['request']
431 req.matchdict = {'id':3}
432
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()
443
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
449     
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()
455     
456     print "\t#%d (%s)" % (player.player_id, player.stripped_nick)
457
458     sstart = datetime.now()
459     render_image(data)
460     sstop = datetime.now()
461     render_time += (sstop-sstart).total_seconds()
462
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
467