]> de.git.xonotic.org Git - xonotic/xonstat.git/blob - xonstat/batch/badges/gen_badges.py
Fixed crash when no active players were found
[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 Skin
12
13
14 # maximal number of query results (for testing, set to 0 to get all)
15 #NUM_PLAYERS = 200
16
17 # we look for players who have activity within the past DELTA hours
18 DELTA = 6
19
20
21 skin_classic = Skin(
22         bg              = "asfalt",
23     )
24
25 skin_archer = Skin(
26         bg              = "background_archer-v1",
27         overlay         = "",
28     )
29
30 skin_minimal = Skin(
31         bg              = None,
32         bgcolor         = (0.04, 0.04, 0.04, 1.0),
33         overlay         = "overlay_minimal",
34         width           = 560,
35         height          = 40,
36         num_gametypes   = 4,
37         gametype_pos    = (25,30),
38         gametype_text   = "%s :",
39         gametype_width  = 120,
40         gametype_fontsize = 10,
41         elo_pos         = (75,30),
42         elo_text        = "Elo %.0f",
43         elo_color       = (1.0, 1.0, 0.6),
44         rank_pos        = None,
45         nostats_pos     = (80,30),
46         nostats_fontsize = 10,
47         nostats_angle   = 0,
48         nostats_text    = "no stats!",
49         kdr_pos         = (392,15),
50         kills_pos       = None,
51         deaths_pos      = None,
52         winp_pos        = (508,15),
53         wins_pos        = None,
54         loss_pos        = None,
55         ptime_pos       = (458,30),
56         ptime_color     = (0.8, 0.8, 0.9),
57     )
58
59 # parse cmdline parameters (for testing)
60 skin = skin_classic
61 if len(sys.argv) > 1:
62     arg = sys.argv[1].lower()
63     if arg == "classic":
64         skin = skin_classic
65     elif arg == "minimal":
66         skin = skin_minimal
67     elif arg == "archer":
68         skin = skin_archer
69
70
71 # environment setup
72 env = bootstrap('../../../development.ini')
73 req = env['request']
74 req.matchdict = {'id':3}
75
76 print "Requesting player data from db ..."
77 cutoff_dt = datetime.utcnow() - timedelta(hours=DELTA)
78 start = datetime.now()
79 players = []
80 if locals().has_key('NUM_PLAYERS'):
81     players = DBSession.query(distinct(Player.player_id)).\
82             filter(Player.player_id == PlayerElo.player_id).\
83             filter(Player.player_id == PlayerGameStat.player_id).\
84             filter(PlayerGameStat.create_dt > cutoff_dt).\
85             filter(Player.nick != None).\
86             filter(Player.player_id > 2).\
87             filter(Player.active_ind == True).\
88             limit(NUM_PLAYERS).all()
89 else:
90     players = DBSession.query(distinct(Player.player_id)).\
91             filter(Player.player_id == PlayerElo.player_id).\
92             filter(Player.player_id == PlayerGameStat.player_id).\
93             filter(PlayerGameStat.create_dt > cutoff_dt).\
94             filter(Player.nick != None).\
95             filter(Player.player_id > 2).\
96             filter(Player.active_ind == True).\
97             all()
98
99 if len(players) > 0:
100     stop = datetime.now()
101     td = stop-start
102     total_seconds = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
103     print "Query took %.2f seconds" % (total_seconds)
104
105     print "Creating badges for %d players ..." % len(players)
106     start = datetime.now()
107     data_time, render_time = 0,0
108     for player_id in players:
109         req.matchdict['id'] = player_id
110
111         sstart = datetime.now()
112         skin.get_data(player_id)
113         sstop = datetime.now()
114         td = sstop-sstart
115         total_seconds = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
116         data_time += total_seconds
117
118         sstart = datetime.now()
119         skin.render_image("output/%d.png" % player_id)
120         sstop = datetime.now()
121         td = sstop-sstart
122         total_seconds = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
123         render_time += total_seconds
124
125     stop = datetime.now()
126     td = stop-start
127     total_seconds = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
128     print "Creating the badges took %.1f seconds (%.3f s per player)" % (total_seconds, total_seconds/float(len(players)))
129     print "Total time for redering images: %.3f s" % render_time
130     print "Total time for getting data: %.3f s" % data_time
131
132 else:
133     print "No active players found!"
134