]> de.git.xonotic.org Git - xonotic/xonstat.git/blob - xonstat/batch/badges/gen_badges.py
Further adjust font sizes and positions in badges
[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 from xonstat.util import datetime_seconds
11
12 from skin import Skin
13 from playerdata import PlayerData
14
15
16 # maximal number of query results (for testing, set to None to get all)
17 NUM_PLAYERS = None
18
19 # filter by player id (for testing, set to None to disable)
20 PLAYER_ID = None
21
22 # we look for players who have activity within the past DELTA hours
23 DELTA = 6
24
25 VERBOSE = False
26
27 INIFILE = None  # keep this set to "None"
28
29 # classic skin WITHOUT NAME - writes PNGs into "output//###.png"
30 skin_classic = Skin( "",
31         bg              = "broken_noise",
32         overlay         = "overlay_classic",
33     )
34
35 # more fancy skin [** WIP **]- writes PNGs into "output/archer/###.png"
36 skin_archer = Skin( "archer",
37         #bg              = "background_archer-v2_full",
38         bg              = "background_archer-v3",
39         overlay         = "",
40         nick_maxwidth   = 260,
41         gametype_pos    = (95,33),
42         nostats_pos     = (95,59),
43         elo_pos         = (95,47),
44         rank_pos        = (95,58),
45         winp_pos        = (509,19),
46         wins_pos        = (508,34),
47         loss_pos        = (508,45),
48         kdr_pos         = (392,19),
49         kills_pos       = (392,34),
50         deaths_pos      = (392,45),
51         ptime_color     = (0.05, 0.05, 0.1),
52     )
53
54 # minimal skin - writes PNGs into "output/minimal/###.png"
55 skin_minimal = Skin( "minimal",
56         bg              = None,
57         bgcolor         = (0.04, 0.04, 0.04, 1.0),
58         overlay         = "overlay_minimal",
59         width           = 560,
60         height          = 40,
61         nick_fontsize   = 16,
62         nick_pos        = (36,17),
63         num_gametypes   = 3,
64         nick_maxwidth   = 280,
65         gametype_pos    = (70,30),
66         gametype_color  = (0.0, 0.0, 0.0),
67         gametype_text   = "%s:",
68         gametype_width  = 100,
69         gametype_fontsize = 10,
70         gametype_align  = -1,
71         gametype_upper  = False,
72         elo_pos         = (75,30),
73         elo_text        = "Elo %.0f",
74         elo_color       = (0.7, 0.7, 0.7),
75         elo_align       = 1,
76         rank_pos        = None,
77         nostats_pos     = None,
78         #nostats_pos     = (75,30),
79         #nostats_fontsize = 10,
80         #nostats_angle   = 0,
81         #nostats_text    = "no stats yet!",
82         #nostats_color   = (0.7, 0.4, 0.4),
83         kdr_pos         = (392,15),
84         kdr_fontsize    = 11,
85         kdr_colortop    = (0.6, 0.8, 0.6),
86         kdr_colormid    = (0.6, 0.6, 0.6),
87         kdr_colorbot    = (0.8, 0.6, 0.6),
88         kills_pos       = None,
89         deaths_pos      = None,
90         winp_pos        = (509,15),
91         winp_fontsize   = 11,
92         winp_colortop   = (0.6, 0.8, 0.8),
93         winp_colormid   = (0.6, 0.6, 0.6),
94         winp_colorbot   = (0.8, 0.8, 0.6),
95         wins_pos        = None,
96         loss_pos        = None,
97         ptime_pos       = (451,30),
98         ptime_color     = (0.7, 0.7, 0.7),
99     )
100
101
102 # parse cmdline parameters (for testing)
103
104 skins = []
105 for arg in sys.argv[1:]:
106     if arg.startswith("-"):
107         try:
108             key,value = arg[1:].split("=")
109         except:
110             key,value = arg[1:], ""
111         if key == "force":
112             DELTA = 2**24   # large enough to enforce update, and doesn't result in errors
113         elif key == "delta":
114             DELTA = float(value)
115         elif key == "test":
116             NUM_PLAYERS = 100
117         elif key == "player":
118             PLAYER_ID = int(value)
119         elif key == "verbose":
120             VERBOSE = True
121         else:
122             print """Usage:  gen_badges.py [options] <ini-file> [skin list]
123     Options:
124         -force      Force updating all badges (delta = 2^24)
125         -delta n    Manually set an update interval (delta)
126         -test       Limit number of players to 100 (for testing)
127         -player #   Filter by given player id
128         -verbose    Show more verbose output
129         -help       Show this help text
130     Ini-File:
131         Name of a Pyramid ini-file to use (e.g. prodution.ini or development.ini).
132     Skin list:
133         Space-separated list of skins to use when creating badges.
134         Available skins:  classic, minimal, archer
135         If no skins are given, classic and minmal will be used by default.
136         NOTE: Output directories must exists before running the program!
137 """
138             sys.exit(-1)
139     else:
140         if INIFILE == None:
141             INIFILE = arg
142         else:
143             if arg == "classic":
144                 skins.append(skin_classic)
145             elif arg == "minimal":
146                 skins.append(skin_minimal)
147             elif arg == "archer":
148                         skins.append(skin_archer)
149
150 if len(skins) == 0:
151     skins = [ skin_classic, skin_minimal, skin_archer ]
152
153 if not INIFILE:
154     print "You must provide the name of an ini-file to use! Type 'gen_badges.py -h' for help."
155     sys.exit(-1)
156
157 # environment setup
158 env = bootstrap(INIFILE)
159 req = env['request']
160 req.matchdict = {'id':3}
161
162 print "Requesting player data from db ..."
163 cutoff_dt = datetime.utcnow() - timedelta(hours=DELTA)
164 start = datetime.now()
165 players = []
166 if NUM_PLAYERS:
167     players = DBSession.query(distinct(Player.player_id)).\
168             filter(Player.player_id == PlayerElo.player_id).\
169             filter(Player.player_id == PlayerGameStat.player_id).\
170             filter(PlayerGameStat.create_dt > cutoff_dt).\
171             filter(Player.nick != None).\
172             filter(Player.player_id > 2).\
173             filter(Player.active_ind == True).\
174             limit(NUM_PLAYERS).all()
175 elif PLAYER_ID:
176     players = DBSession.query(distinct(Player.player_id)).\
177             filter(Player.player_id == PlayerElo.player_id).\
178             filter(Player.player_id == PlayerGameStat.player_id).\
179             filter(PlayerGameStat.create_dt > cutoff_dt).\
180             filter(Player.nick != None).\
181             filter(Player.player_id == PLAYER_ID).\
182             filter(Player.active_ind == True).\
183             limit(NUM_PLAYERS).all()
184 else:
185     players = DBSession.query(distinct(Player.player_id)).\
186             filter(Player.player_id == PlayerElo.player_id).\
187             filter(Player.player_id == PlayerGameStat.player_id).\
188             filter(PlayerGameStat.create_dt > cutoff_dt).\
189             filter(Player.nick != None).\
190             filter(Player.player_id > 2).\
191             filter(Player.active_ind == True).\
192             all()
193
194 playerdata = PlayerData
195
196 if len(players) > 0:
197     stop = datetime.now()
198     td = stop-start
199     print "Query took %.2f seconds" % (datetime_seconds(td))
200
201     print "Creating badges for %d players ..." % len(players)
202     start = datetime.now()
203     data_time, render_time = 0,0
204     for player_id in players:
205         req.matchdict['id'] = player_id
206
207         sstart = datetime.now()
208         playerdata.get_data(player_id)
209         sstop = datetime.now()
210         td = sstop-sstart
211         data_time += datetime_seconds(td)
212
213         sstart = datetime.now()
214         for sk in skins:
215             sk.render_image(playerdata.data, "output/%s/%d.png" % (str(sk), player_id[0]))
216         sstop = datetime.now()
217         td = sstop-sstart
218         render_time += datetime_seconds(td)
219
220         if VERBOSE == True:
221             print player_id, unicode(playerdata.data['player'].nick)
222
223     stop = datetime.now()
224     td = stop-start
225     total_seconds = datetime_seconds(td)
226     print "Creating the badges took %.1f seconds (%.3f s per player)" % (total_seconds, total_seconds/float(len(players)))
227     print "Total time for rendering images: %.3f s" % render_time
228     print "Total time for getting data: %.3f s" % data_time
229
230 else:
231     print "No active players found!"
232