]> git.xonotic.org Git - xonotic/xonstat.git/blob - xonstat/batch/badges/gen_badges.py
More fine-tuning to the "minimal" skin; added options to enable/disable centering...
[xonotic/xonstat.git] / xonstat / batch / badges / gen_badges.py
1 #-*- coding: utf-8 -*-
2
3 import sys
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
11 from skin import Skin
12 from playerdata import PlayerData
13
14
15 # maximal number of query results (for testing, set to None to get all)
16 NUM_PLAYERS = None
17
18 # we look for players who have activity within the past DELTA hours
19 DELTA = 6
20
21
22 # classic skin WITHOUT NAME - writes PNGs into "output//###.png"
23 skin_classic = Skin( "",
24         bg              = "asfalt",
25         overlay         = "overlay_classic",
26     )
27
28 # more fancy skin [** WIP **]- writes PNGs into "output/archer/###.png"
29 skin_archer = Skin( "archer",
30         bg              = "background_archer-v1",
31         overlay         = None,
32     )
33
34 # minimal skin - writes PNGs into "output/minimal/###.png"
35 skin_minimal = Skin( "minimal",
36         bg              = None,
37         bgcolor         = (0.04, 0.04, 0.04, 1.0),
38         overlay         = "overlay_minimal",
39         width           = 560,
40         height          = 40,
41         num_gametypes   = 3,
42         gametype_pos    = (8,30),
43         gametype_color  = (0.8, 0.8, 0.8),
44         gametype_text   = "%s:",
45         gametype_width  = 112,
46         gametype_fontsize = 10,
47         gametype_center = False,
48         elo_pos         = (75,30),
49         elo_text        = "Elo %.0f",
50         elo_color       = (0.9, 0.9, 0.7),
51         elo_center      = True,
52         rank_pos        = None,
53         nostats_pos     = (75,30),
54         nostats_fontsize = 10,
55         nostats_angle   = 0,
56         nostats_text    = "no stats!",
57         nostats_color   = (0.8, 0.3, 0.3),
58         nostats_center  = True,
59         kdr_pos         = (392,15),
60         kdr_colortop    = (0.1, 0.8, 0.1),
61         kdr_colormid    = (0.6, 0.6, 0.1),
62         kdr_colorbot    = (0.8, 0.1, 0.1),
63         kills_pos       = None,
64         deaths_pos      = None,
65         winp_pos        = (508,15),
66         wins_pos        = None,
67         winp_colortop   = (0.1, 0.6, 0.6),
68         winp_colormid   = (0.2, 0.6, 0.2),
69         winp_colorbot   = (0.6, 0.6, 0.1),
70         loss_pos        = None,
71         ptime_pos       = (451,30),
72         ptime_color     = (0.7, 0.7, 0.9),
73     )
74
75
76 # parse cmdline parameters (for testing)
77
78 skins = []
79 for arg in sys.argv[1:]:
80     if arg.startswith("-"):
81         arg = arg[1:]
82         if arg == "force":
83             DELTA = 2**24   # large enough to enforce update, and doesn't result in errors
84         elif arg == "test":
85             NUM_PLAYERS = 100
86         else:
87             print """Usage:  gen_badges.py [options] [skin list]
88     Options:
89         -force      Force updating all badges (delta = 2^24)
90         -test       Limit number of players to 100 (for testing)
91         -help       Show this help text
92     Skin list:
93         Space-separated list of skins to use when creating badges.
94         Available skins:  classic, minimal, archer
95         If no skins are given, classic and minmal will be used by default.
96         NOTE: Output directories must exists before running the program!
97 """
98             sys.exit(-1)
99     else:
100         if arg == "classic":
101             skins.append(skin_classic)
102         elif arg == "minimal":
103             skins.append(skin_minimal)
104         elif arg == "archer":
105             skins.append(skin_archer)
106
107 if len(skins) == 0:
108     skins = [ skin_classic, skin_minimal ]
109
110
111 # environment setup
112 env = bootstrap('../../../development.ini')
113 req = env['request']
114 req.matchdict = {'id':3}
115
116 print "Requesting player data from db ..."
117 cutoff_dt = datetime.utcnow() - timedelta(hours=DELTA)
118 start = datetime.now()
119 players = []
120 if NUM_PLAYERS:
121     players = DBSession.query(distinct(Player.player_id)).\
122             filter(Player.player_id == PlayerElo.player_id).\
123             filter(Player.player_id == PlayerGameStat.player_id).\
124             filter(PlayerGameStat.create_dt > cutoff_dt).\
125             filter(Player.nick != None).\
126             filter(Player.player_id > 2).\
127             filter(Player.active_ind == True).\
128             limit(NUM_PLAYERS).all()
129 else:
130     players = DBSession.query(distinct(Player.player_id)).\
131             filter(Player.player_id == PlayerElo.player_id).\
132             filter(Player.player_id == PlayerGameStat.player_id).\
133             filter(PlayerGameStat.create_dt > cutoff_dt).\
134             filter(Player.nick != None).\
135             filter(Player.player_id > 2).\
136             filter(Player.active_ind == True).\
137             all()
138
139 playerdata = PlayerData()
140
141 if len(players) > 0:
142     stop = datetime.now()
143     td = stop-start
144     total_seconds = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
145     print "Query took %.2f seconds" % (total_seconds)
146
147     print "Creating badges for %d players ..." % len(players)
148     start = datetime.now()
149     data_time, render_time = 0,0
150     for player_id in players:
151         req.matchdict['id'] = player_id
152
153         sstart = datetime.now()
154         playerdata.get_data(player_id)
155         sstop = datetime.now()
156         td = sstop-sstart
157         total_seconds = float(td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
158         data_time += total_seconds
159
160         sstart = datetime.now()
161         for sk in skins:
162             sk.render_image(playerdata, "output/%s/%d.png" % (str(sk), player_id[0]))
163         sstop = datetime.now()
164         td = sstop-sstart
165         total_seconds = float(td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
166         render_time += total_seconds
167
168     stop = datetime.now()
169     td = stop-start
170     total_seconds = float(td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
171     print "Creating the badges took %.1f seconds (%.3f s per player)" % (total_seconds, total_seconds/float(len(players)))
172     print "Total time for rendering images: %.3f s" % render_time
173     print "Total time for getting data: %.3f s" % data_time
174
175 else:
176     print "No active players found!"
177