]> de.git.xonotic.org Git - xonotic/xonstat.git/blob - xonstat/batch/badges/gen_badges.py
Can't calculate the k:d ratio if d (deaths) is None.
[xonotic/xonstat.git] / xonstat / batch / badges / gen_badges.py
1 #-*- coding: utf-8 -*-
2
3 import cairo as C
4 from datetime import datetime
5 import sqlalchemy as sa
6 import sqlalchemy.sql.functions as func
7 from colorsys import rgb_to_hls, hls_to_rgb
8 from os import system
9 from pyramid.paster import bootstrap
10 from xonstat.models import *
11 from xonstat.util import qfont_decode
12
13
14 # similar to html_colors() from util.py
15 _contrast_threshold = 0.5
16
17 _dec_colors = [ (0.5,0.5,0.5),
18                 (1.0,0.0,0.0),
19                 (0.2,1.0,0.0),
20                 (1.0,1.0,0.0),
21                 (0.2,0.4,1.0),
22                 (0.2,1.0,1.0),
23                 (1.0,0.2,102),
24                 (1.0,1.0,1.0),
25                 (0.6,0.6,0.6),
26                 (0.5,0.5,0.5)
27             ]
28
29
30 # parameters to affect the output, could be changed via URL
31 params = {
32     'width':        560,
33     'height':        70,
34     'bg':           1,                      # 0 - black, 1 - dark_wall
35     'font':         0,                      # 0 - xolonium, 1 - dejavu sans
36 }
37
38
39 # maximal number of query results (for testing, set to 0 to get all)
40 NUM_PLAYERS = 100
41
42
43 def get_data(player):
44     """Return player data as dict.
45
46     This function is similar to the function in player.py but more optimized
47     for this purpose.
48     """
49
50     # total games
51     # wins/losses
52     # kills/deaths
53     # duel/dm/tdm/ctf elo + rank
54
55     player_id = player.player_id
56
57     total_stats = {}
58
59     games_played = DBSession.query(
60             Game.game_type_cd, func.count(), func.sum(PlayerGameStat.alivetime)).\
61             filter(Game.game_id == PlayerGameStat.game_id).\
62             filter(PlayerGameStat.player_id == player_id).\
63             group_by(Game.game_type_cd).\
64             order_by(func.count().desc()).\
65             limit(3).all()  # limit to 3 gametypes!
66
67     total_stats['games'] = 0
68     total_stats['games_breakdown'] = {}  # this is a dictionary inside a dictionary .. dictception?
69     total_stats['games_alivetime'] = {}
70     total_stats['gametypes'] = []
71     for (game_type_cd, games, alivetime) in games_played:
72         total_stats['games'] += games
73         total_stats['gametypes'].append(game_type_cd)
74         total_stats['games_breakdown'][game_type_cd] = games
75         total_stats['games_alivetime'][game_type_cd] = alivetime
76
77     (total_stats['kills'], total_stats['deaths'], total_stats['suicides'],
78      total_stats['alivetime'],) = DBSession.query(
79             func.sum(PlayerGameStat.kills),
80             func.sum(PlayerGameStat.deaths),
81             func.sum(PlayerGameStat.suicides),
82             func.sum(PlayerGameStat.alivetime)).\
83             filter(PlayerGameStat.player_id == player_id).\
84             one()
85
86     (total_stats['wins'],) = DBSession.query(
87             func.count("*")).\
88             filter(Game.game_id == PlayerGameStat.game_id).\
89             filter(PlayerGameStat.player_id == player_id).\
90             filter(Game.winner == PlayerGameStat.team or PlayerGameStat.rank == 1).\
91             one()
92
93     ranks = DBSession.query("game_type_cd", "rank", "max_rank").\
94             from_statement(
95                 "select pr.game_type_cd, pr.rank, overall.max_rank "
96                 "from player_ranks pr,  "
97                    "(select game_type_cd, max(rank) max_rank "
98                     "from player_ranks  "
99                     "group by game_type_cd) overall "
100                 "where pr.game_type_cd = overall.game_type_cd  "
101                 "and player_id = :player_id "
102                 "order by rank").\
103             params(player_id=player_id).all()
104
105     ranks_dict = {}
106     for gtc,rank,max_rank in ranks:
107         ranks_dict[gtc] = (rank, max_rank)
108
109     elos = DBSession.query(PlayerElo).\
110             filter_by(player_id=player_id).\
111             order_by(PlayerElo.elo.desc()).\
112             all()
113
114     elos_dict = {}
115     for elo in elos:
116         if elo.games > 32:
117             elos_dict[elo.game_type_cd] = elo.elo
118
119     data = {
120             'player':player,
121             'total_stats':total_stats,
122             'ranks':ranks_dict,
123             'elos':elos_dict,
124         }
125
126     #print data
127     return data
128
129
130 def render_image(data):
131     """Render an image from the given data fields."""
132
133     width, height = params['width'], params['height']
134     output = "output/%s.png" % data['player'].player_id
135
136     font = "Xolonium"
137     if params['font'] == 1:
138         font = "DejaVu Sans"
139
140     total_stats = data['total_stats']
141     total_games = total_stats['games']
142     elos = data["elos"]
143     ranks = data["ranks"]
144
145
146     ## create background
147
148     surf = C.ImageSurface(C.FORMAT_RGB24, width, height)
149     ctx = C.Context(surf)
150     ctx.set_antialias(C.ANTIALIAS_GRAY)
151
152     # draw background (just plain fillcolor)
153     if params['bg'] == 0:
154         ctx.rectangle(0, 0, width, height)
155         ctx.set_source_rgba(0.2, 0.2, 0.2, 1.0)
156         ctx.fill()
157
158     # draw background image (try to get correct tiling, too)
159     if params['bg'] > 0:
160         bg = None
161         if params['bg'] == 1:
162             bg = C.ImageSurface.create_from_png("img/dark_wall.png")
163
164         if bg:
165             bg_w, bg_h = bg.get_width(), bg.get_height()
166             bg_xoff = 0
167             while bg_xoff < width:
168                 bg_yoff = 0
169                 while bg_yoff < height:
170                     ctx.set_source_surface(bg, bg_xoff, bg_yoff)
171                     ctx.paint()
172                     bg_yoff += bg_h
173                 bg_xoff += bg_w
174
175
176     ## draw player's nickname with fancy colors
177
178     # fontsize is reduced if width gets too large
179     nick_xmax = 335
180     ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_NORMAL)
181     ctx.set_font_size(20)
182     xoff, yoff, tw, th = ctx.text_extents(player.stripped_nick)[:4]
183     if tw > nick_xmax:
184         ctx.set_font_size(18)
185         xoff, yoff, tw, th = ctx.text_extents(player.stripped_nick)[:4]
186         if tw > nick_xmax:
187             ctx.set_font_size(16)
188             xoff, yoff, tw, th = ctx.text_extents(player.stripped_nick)[:4]
189             if tw > nick_xmax:
190                 ctx.set_font_size(14)
191                 xoff, yoff, tw, th = ctx.text_extents(player.stripped_nick)[:4]
192                 if tw > nick_xmax:
193                     ctx.set_font_size(12)
194
195     # split up nick into colored segments and draw each of them
196     qstr = qfont_decode(player.nick).replace('^^', '^').replace('\x00', ' ')
197     txt_xoff = 0
198     txt_xpos, txt_ypos = 5,18
199
200     # split nick into colored segments
201     parts = []
202     pos = 1
203     while True:
204         npos = qstr.find('^', pos)
205         if npos < 0:
206             parts.append(qstr[pos-1:])
207             break;
208         parts.append(qstr[pos-1:npos])
209         pos = npos+1
210
211     for txt in parts:
212         r,g,b = _dec_colors[7]
213         try:
214             if txt.startswith('^'):
215                 txt = txt[1:]
216                 if txt.startswith('x'):
217                     r = int(txt[1] * 2, 16) / 255.0
218                     g = int(txt[2] * 2, 16) / 255.0
219                     b = int(txt[3] * 2, 16) / 255.0
220                     hue, light, satur = rgb_to_hls(r, g, b)
221                     if light < _contrast_threshold:
222                         light = _contrast_threshold
223                         r, g, b = hls_to_rgb(hue, light, satur)
224                     txt = txt[4:]
225                 else:
226                     r,g,b = _dec_colors[int(txt[0])]
227                     txt = txt[1:]
228         except:
229             r,g,b = _dec_colors[7]
230
231         if len(txt) < 1:
232             # only colorcode and no real text, skip this
233             continue
234
235         ctx.set_source_rgb(r, g, b)
236         ctx.move_to(txt_xpos + txt_xoff, txt_ypos)
237         ctx.show_text(txt)
238
239         xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
240         if tw == 0:
241             # only whitespaces, use some extra space
242             tw += 5*len(txt)
243
244         txt_xoff += tw + 1
245
246
247     ## print elos and ranks
248
249     games_x, games_y = 60,35
250     games_w = 110       # width of each gametype field
251
252     # show up to three gametypes the player has participated in
253     for gt in total_stats['gametypes'][:3]:
254         ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_BOLD)
255         ctx.set_font_size(10)
256         ctx.set_source_rgb(1.0, 1.0, 1.0)
257         txt = "[ %s ]" % gt.upper()
258         xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
259         ctx.move_to(games_x-xoff-tw/2,games_y-yoff-4)
260         ctx.show_text(txt)
261
262         old_aa = ctx.get_antialias()
263         ctx.set_antialias(C.ANTIALIAS_NONE)
264         ctx.set_source_rgb(0.8, 0.8, 0.8)
265         ctx.set_line_width(1)
266         ctx.move_to(games_x-games_w/2+5, games_y+8)
267         ctx.line_to(games_x+games_w/2-5, games_y+8)
268         ctx.stroke()
269         ctx.move_to(games_x-games_w/2+5, games_y+32)
270         ctx.line_to(games_x+games_w/2-5, games_y+32)
271         ctx.stroke()
272         ctx.set_antialias(old_aa)
273
274         if not elos.has_key(gt) or not ranks.has_key(gt):
275             ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_BOLD)
276             ctx.set_font_size(12)
277             ctx.set_source_rgb(0.8, 0.2, 0.2)
278             txt = "no stats yet!"
279             xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
280             ctx.move_to(games_x-xoff-tw/2,games_y+28-yoff-4)
281             ctx.save()
282             ctx.rotate(math.radians(-10))
283             ctx.show_text(txt)
284             ctx.restore()
285         else:
286             ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_NORMAL)
287             ctx.set_font_size(10)
288             ctx.set_source_rgb(1.0, 1.0, 0.5)
289             txt = "Elo: %.0f" % round(elos[gt], 0)
290             xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
291             ctx.move_to(games_x-xoff-tw/2,games_y+15-yoff-4)
292             ctx.show_text(txt)
293
294             ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_NORMAL)
295             ctx.set_font_size(8)
296             ctx.set_source_rgb(0.8, 0.8, 0.8)
297             txt = "Rank %d of %d" % ranks[gt]
298             xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
299             ctx.move_to(games_x-xoff-tw/2,games_y+25-yoff-3)
300             ctx.show_text(txt)
301
302         games_x += games_w
303
304
305     # print win percentage
306     win_x, win_y = 505,11
307     win_w, win_h = 100,14
308
309     ctx.rectangle(win_x-win_w/2,win_y-win_h/2,win_w,win_h)
310     ctx.set_source_rgba(0.8, 0.8, 0.8, 0.1)
311     ctx.fill();
312
313     ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_NORMAL)
314     ctx.set_font_size(10)
315     ctx.set_source_rgb(0.8, 0.8, 0.8)
316     txt = "Win Percentage"
317     xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
318     ctx.move_to(win_x-xoff-tw/2,win_y-yoff-3)
319     ctx.show_text(txt)
320
321     txt = "???"
322     if total_games > 0 and total_stats['wins'] is not None:
323         ratio = float(total_stats['wins'])/total_games
324         txt = "%.2f%%" % round(ratio * 100, 2)
325     ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_BOLD)
326     ctx.set_font_size(12)
327     if ratio >= 0.90:
328         ctx.set_source_rgb(0.2, 1.0, 1.0)
329     elif ratio >= 0.75:
330         ctx.set_source_rgb(0.5, 1.0, 1.0)
331     elif ratio >= 0.5:
332         ctx.set_source_rgb(0.5, 1.0, 0.8)
333     elif ratio >= 0.25:
334         ctx.set_source_rgb(0.8, 1.0, 0.5)
335     else:
336         ctx.set_source_rgb(1.0, 1.0, 0.5)
337     xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
338     ctx.move_to(win_x-xoff-tw/2,win_y+16-yoff-4)
339     ctx.show_text(txt)
340
341     ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_NORMAL)
342     ctx.set_font_size(8)
343     ctx.set_source_rgb(0.6, 0.8, 0.6)
344     txt = "%d wins" % total_stats["wins"]
345     xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
346     ctx.move_to(win_x-xoff-tw/2,win_y+28-yoff-3)
347     ctx.show_text(txt)
348     ctx.set_source_rgb(0.8, 0.6, 0.6)
349     txt = "%d losses" % (total_games-total_stats['wins'])
350     xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
351     ctx.move_to(win_x-xoff-tw/2,win_y+38-yoff-3)
352     ctx.show_text(txt)
353
354
355     # print kill/death ratio
356     kill_x, kill_y = 395,11
357     kill_w, kill_h = 100,14
358
359     ctx.rectangle(kill_x-kill_w/2,kill_y-kill_h/2,kill_w,kill_h)
360     ctx.set_source_rgba(0.8, 0.8, 0.8, 0.1)
361     ctx.fill()
362
363     ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_NORMAL)
364     ctx.set_font_size(10)
365     ctx.set_source_rgb(0.8, 0.8, 0.8)
366     txt = "Kill Ratio"
367     xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
368     ctx.move_to(kill_x-xoff-tw/2,kill_y-yoff-3)
369     ctx.show_text(txt)
370
371     txt = "???"
372     if total_stats['deaths'] > 0 and total_stats['kills'] is not None:
373         ratio = float(total_stats['kills'])/total_stats['deaths']
374         txt = "%.3f" % round(ratio, 3)
375     ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_BOLD)
376     ctx.set_font_size(12)
377     if ratio >= 3:
378         ctx.set_source_rgb(0.0, 1.0, 0.0)
379     elif ratio >= 2:
380         ctx.set_source_rgb(0.2, 1.0, 0.2)
381     elif ratio >= 1:
382         ctx.set_source_rgb(0.5, 1.0, 0.5)
383     elif ratio >= 0.5:
384         ctx.set_source_rgb(1.0, 0.5, 0.5)
385     else:
386         ctx.set_source_rgb(1.0, 0.2, 0.2)
387     xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
388     ctx.move_to(kill_x-xoff-tw/2,kill_y+16-yoff-4)
389     ctx.show_text(txt)
390
391     ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_NORMAL)
392     ctx.set_font_size(8)
393     ctx.set_source_rgb(0.6, 0.8, 0.6)
394     txt = "%d kills" % total_stats["kills"]
395     xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
396     ctx.move_to(kill_x-xoff-tw/2,kill_y+28-yoff-3)
397     ctx.show_text(txt)
398     ctx.set_source_rgb(0.8, 0.6, 0.6)
399     if total_stats['deaths'] is not None:
400         txt = "%d deaths" % total_stats['deaths']
401     xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
402     ctx.move_to(kill_x-xoff-tw/2,kill_y+38-yoff-3)
403     ctx.show_text(txt)
404
405
406     # print playing time
407     time_x, time_y = 450,64
408     time_w, time_h = 210,10
409
410     ctx.rectangle(time_x-time_w/2,time_y-time_h/2-1,time_w,time_y+time_h/2-1)
411     ctx.set_source_rgba(0.8, 0.8, 0.8, 0.6)
412     ctx.fill();
413
414     ctx.select_font_face(font, C.FONT_SLANT_NORMAL, C.FONT_WEIGHT_NORMAL)
415     ctx.set_font_size(10)
416     ctx.set_source_rgb(0.1, 0.1, 0.1)
417     txt = "Playing time: %s" % str(total_stats['alivetime'])
418     xoff, yoff, tw, th = ctx.text_extents(txt)[:4]
419     ctx.move_to(time_x-xoff-tw/2,time_y-yoff-4)
420     ctx.show_text(txt)
421
422
423     # save to PNG
424     surf.write_to_png(output)
425
426
427 # environment setup
428 env = bootstrap('../../../development.ini.home')
429 req = env['request']
430 req.matchdict = {'id':3}
431
432 print "Requesting player data from db ..."
433 start = datetime.now()
434 players = DBSession.query(Player).\
435         filter(Player.player_id == PlayerElo.player_id).\
436         filter(Player.nick != None).\
437         filter(Player.player_id > 2).\
438         filter(Player.active_ind == True).\
439         all()
440
441 stop = datetime.now()
442 td = stop-start
443 total_seconds = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
444 print "Query took %.2f seconds" % (total_seconds)
445
446 print "Creating badges for %d players ..." % len(players)
447 start = datetime.now()
448 data_time, render_time = 0,0
449 for player in players:
450     req.matchdict['id'] = player.player_id
451
452     sstart = datetime.now()
453     data = get_data(player)
454     sstop = datetime.now()
455     td = sstop-sstart
456     total_seconds = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
457     data_time += total_seconds
458
459     sstart = datetime.now()
460     render_image(data)
461     sstop = datetime.now()
462     td = sstop-sstart
463     total_seconds = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
464     render_time += total_seconds
465
466 stop = datetime.now()
467 td = stop-start
468 total_seconds = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
469 print "Creating the badges took %.2f seconds (%.2f s per player)" % (total_seconds, total_seconds/float(len(players)))
470 print "Total time for redering images: %.2f s" % render_time
471 print "Total time for getting data: %.2f s" % data_time