]> de.git.xonotic.org Git - xonotic/xonstat.git/blob - xonstat/models/game.py
Add a better to_dict() implementation for pgstats.
[xonotic/xonstat.git] / xonstat / models / game.py
1 """
2 Models related to games.
3 """
4
5 from xonstat.models.mixins import FuzzyDateMixin, EpochMixin
6 from xonstat.util import strip_colors, html_colors
7
8
9 class Game(FuzzyDateMixin, EpochMixin):
10     """
11     An individual game.
12     """
13
14     def __init__(self, game_id=None, start_dt=None, game_type_cd=None, server_id=None, map_id=None,
15                  winner=None):
16         self.game_id = game_id
17         self.start_dt = start_dt
18         self.game_type_cd = game_type_cd
19         self.server_id = server_id
20         self.map_id = map_id
21         self.winner = winner
22
23     def __repr__(self):
24         return "<Game({0.game_id}, {0.start_dt}, {0.game_type_cd}, {0.server_id})>".format(self)
25
26     def to_dict(self):
27         return {
28             'game_id': self.game_id,
29             'start': self.start_dt.strftime('%Y-%m-%dT%H:%M:%SZ'),
30             'game_type_cd': self.game_type_cd,
31             'server_id': self.server_id
32         }
33
34
35 class PlayerGameStat(object):
36     """
37     The individual statistics a player has gained/lost during a game.
38     """
39
40     def __init__(self, player_game_stat_id=None, create_dt=None):
41         self.player_game_stat_id = player_game_stat_id
42         self.create_dt = create_dt
43
44     def __repr__(self):
45         return "<PlayerGameStat({0.player_id}, {0.game_id}, {0.create_dt})>".format(self)
46
47     def to_dict(self):
48
49         if self.fastest:
50             fastest = round(float(self.fastest.seconds) + (self.fastest.microseconds/1000000.0), 2)
51         else:
52             fastest = None
53
54         return {
55             'player_game_stat_id': self.player_game_stat_id,
56             'player_id': self.player_id,
57             'game_id': self.game_id,
58             'nick': self.nick,
59             'stripped_nick': self.stripped_nick,
60             'team': self.team,
61             'alivetime': self.alivetime.total_seconds(),
62             'kills': self.kills,
63             'deaths': self.deaths,
64             'suicides': self.suicides,
65             'score': self.score,
66             'time': self.time.total_seconds() if self.time else None,
67             'captures': self.captures,
68             'pickups': self.pickups,
69             'drops': self.drops,
70             'returns': self.returns,
71             'collects': self.collects,
72             'destroys': self.destroys,
73             'pushes': self.pushes,
74             'carrier_frags': self.carrier_frags,
75             'fastest': fastest,
76             'scoreboardpos': self.scoreboardpos,
77             'laps': self.laps,
78             'revivals': self.revivals,
79             'lives': self.lives,
80             'rank': self.rank,
81             'create_dt': self.create_dt.strftime('%Y-%m-%dT%H:%M:%SZ'),
82         }
83
84     def nick_stripped(self):
85         if self.nick is None:
86             return "Anonymous Player"
87         else:
88             return strip_colors(self.nick)
89
90     def nick_html_colors(self, limit=None):
91         if self.nick is None:
92             return "Anonymous Player"
93         else:
94             return html_colors(self.nick, limit)
95
96     def team_html_color(self):
97         if self.team == 5:
98             return "red"
99         if self.team == 14:
100             return "blue"
101         if self.team == 13:
102             return "yellow"
103         if self.team == 10:
104             return "pink"
105
106
107 class PlayerWeaponStat(object):
108     """
109     The metrics for a single weapon in a game for a player.
110     """
111
112     def __init__(self, player_id=None, game_id=None, weapon_cd=None):
113         self.player_id = player_id
114         self.game_id = game_id
115         self.weapon_cd = weapon_cd
116         self.fired = 0
117         self.max = 0
118         self.hit = 0
119         self.actual = 0
120         self.frags = 0
121
122     def __repr__(self):
123         return ("<PlayerWeaponStat({0.player_weapon_stats_id}, {0.player_id}, {0.game_id})>"
124                 .format(self))
125
126     def to_dict(self):
127         return {
128             'weapon_cd': self.weapon_cd,
129             'player_weapon_stats_id': self.player_weapon_stats_id,
130             'player_id': self.player_id,
131             'game_id': self.game_id,
132             'fired': self.fired,
133             'max': self.max,
134             'hit': self.hit,
135             'actual': self.actual,
136             'frags': self.frags,
137         }
138
139
140 class TeamGameStat(object):
141     """
142     Team level metrics.
143     """
144
145     def __init__(self, team_game_stat_id=None, create_dt=None):
146         self.team_game_stat_id = team_game_stat_id
147         self.create_dt = create_dt
148
149     def __repr__(self):
150         return "<TeamGameStat({0.team_game_stat_id}, {0.game_id}, {0.team})>".format(self)
151
152     def to_dict(self):
153         return {
154             'team_game_stat_id': self.team_game_stat_id,
155             'game_id': self.game_id,
156             'team': self.team,
157             'score': self.score,
158             'rounds': self.rounds,
159             'caps': self.caps,
160             'create_dt': self.create_dt.strftime('%Y-%m-%dT%H:%M:%SZ'),
161         }
162
163     # TODO: move this function to util
164     def team_html_color(self):
165         if self.team == 5:
166             return "red"
167         if self.team == 14:
168             return "blue"
169         if self.team == 13:
170             return "yellow"
171         if self.team == 10:
172             return "pink"
173
174
175 class PlayerGameAnticheat(object):
176     """
177     Anticheat metrics sent by the server to identify odd patterns.
178     """
179
180     def __init__(self, player_id=None, game_id=None, key=None, value=None, create_dt=None):
181         self.player_id = player_id
182         self.game_id = game_id
183         self.key = key
184         self.value = value
185         self.create_dt = create_dt
186
187     def __repr__(self):
188         return "<PlayerGameAnticheat({0.key}, {0.value})>".format(self)
189
190
191 class GameType(object):
192     """
193     A particular type of game.
194     """
195
196     def __repr__(self):
197         return "<GameType({0.game_type_cd}, {0.descr}, {0.active_ind})>".format(self)
198
199     def to_dict(self):
200         return {
201             'game_type_cd': self.game_type_cd,
202             'name': self.descr,
203             'active': self.active_ind,
204         }
205
206
207 class Weapon(object):
208     """
209     A particular type of weapon.
210     """
211
212     def __repr__(self):
213         return "<Weapon({0.weapon_cd}, {0.descr}, {0.active_ind})>".format(self)
214
215     def to_dict(self):
216         return {
217             'weapon_cd': self.weapon_cd,
218             'name': self.descr,
219             'active': self.active_ind,
220         }
221
222
223 class PlayerGameFragMatrix(object):
224     """
225     Frags made by an individual player in a single game.
226     """
227
228     def __init__(self, game_id, player_game_stat_id, player_id, player_index, matrix):
229         self.game_id = game_id
230         self.player_game_stat_id = player_game_stat_id
231         self.player_id = player_id
232         self.player_index = player_index
233         self.matrix = matrix
234
235     def __repr__(self):
236         return "<PlayerGameFragMatrix({0.game_id}, {0.player_game_stat_id})>".format(self)