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