]> de.git.xonotic.org Git - xonotic/xonstat.git/blob - xonstat/batch/badges/gen_badges.py
A collection of smaller improvements to the badges generator
[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    = (25,30),
43         gametype_text   = "%s :",
44         gametype_width  = 115,
45         gametype_fontsize = 10,
46         elo_pos         = (75,30),
47         elo_text        = "Elo %.0f",
48         elo_color       = (1.0, 1.0, 0.6),
49         rank_pos        = None,
50         nostats_pos     = (80,30),
51         nostats_fontsize = 10,
52         nostats_angle   = 0,
53         nostats_text    = "no stats!",
54         kdr_pos         = (392,15),
55         kills_pos       = None,
56         deaths_pos      = None,
57         winp_pos        = (508,15),
58         wins_pos        = None,
59         loss_pos        = None,
60         ptime_pos       = (458,30),
61         ptime_color     = (0.8, 0.8, 0.9),
62     )
63
64
65 # parse cmdline parameters (for testing)
66
67 skins = []
68 for arg in sys.argv[1:]:
69     if arg.startswith("-"):
70         arg = arg[1:]
71         if arg == "force":
72             DELTA = 2**24   # large enough to enforce update, and doesn't result in errors
73         elif arg == "test":
74             NUM_PLAYERS = 100
75         else:
76             print """Usage:  gen_badges.py [options] [skin list]
77     Options:
78         -force      Force updating all badges (delta = 2^24)
79         -test       Limit number of players to 100 (for testing)
80         -help       Show this help text
81     Skin list:
82         Space-separated list of skins to use when creating badges.
83         Available skins:  classic, minimal, archer
84         If no skins are given, classic and minmal will be used by default.
85         NOTE: Output directories must exists before running the program!
86 """
87             sys.exit(-1)
88     else:
89         if arg == "classic":
90             skins.append(skin_classic)
91         elif arg == "minimal":
92             skins.append(skin_minimal)
93         elif arg == "archer":
94             skins.append(skin_archer)
95
96 if len(skins) == 0:
97     skins = [ skin_classic, skin_minimal ]
98
99
100 # environment setup
101 env = bootstrap('../../../development.ini')
102 req = env['request']
103 req.matchdict = {'id':3}
104
105 print "Requesting player data from db ..."
106 cutoff_dt = datetime.utcnow() - timedelta(hours=DELTA)
107 start = datetime.now()
108 players = []
109 if NUM_PLAYERS:
110     players = DBSession.query(distinct(Player.player_id)).\
111             filter(Player.player_id == PlayerElo.player_id).\
112             filter(Player.player_id == PlayerGameStat.player_id).\
113             filter(PlayerGameStat.create_dt > cutoff_dt).\
114             filter(Player.nick != None).\
115             filter(Player.player_id > 2).\
116             filter(Player.active_ind == True).\
117             limit(NUM_PLAYERS).all()
118 else:
119     players = DBSession.query(distinct(Player.player_id)).\
120             filter(Player.player_id == PlayerElo.player_id).\
121             filter(Player.player_id == PlayerGameStat.player_id).\
122             filter(PlayerGameStat.create_dt > cutoff_dt).\
123             filter(Player.nick != None).\
124             filter(Player.player_id > 2).\
125             filter(Player.active_ind == True).\
126             all()
127
128 playerdata = PlayerData()
129
130 if len(players) > 0:
131     stop = datetime.now()
132     td = stop-start
133     total_seconds = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
134     print "Query took %.2f seconds" % (total_seconds)
135
136     print "Creating badges for %d players ..." % len(players)
137     start = datetime.now()
138     data_time, render_time = 0,0
139     for player_id in players:
140         req.matchdict['id'] = player_id
141
142         sstart = datetime.now()
143         playerdata.get_data(player_id)
144         sstop = datetime.now()
145         td = sstop-sstart
146         total_seconds = float(td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
147         data_time += total_seconds
148
149         sstart = datetime.now()
150         for sk in skins:
151             sk.render_image(playerdata, "output/%s/%d.png" % (str(sk), player_id[0]))
152         sstop = datetime.now()
153         td = sstop-sstart
154         total_seconds = float(td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
155         render_time += total_seconds
156
157     stop = datetime.now()
158     td = stop-start
159     total_seconds = float(td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
160     print "Creating the badges took %.1f seconds (%.3f s per player)" % (total_seconds, total_seconds/float(len(players)))
161     print "Total time for rendering images: %.3f s" % render_time
162     print "Total time for getting data: %.3f s" % data_time
163
164 else:
165     print "No active players found!"
166