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