Remove Persona since it was shut down. :(
[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         return {
49             'player_id': self.player_id,
50             'game_id': self.game_id,
51             'create_dt': self.create_dt.strftime('%Y-%m-%dT%H:%M:%SZ'),
52             'alivetime': self.alivetime,
53             'rank': self.rank,
54             'score': self.score,
55             'team': self.team
56         }
57
58     def nick_stripped(self):
59         if self.nick is None:
60             return "Anonymous Player"
61         else:
62             return strip_colors(self.nick)
63
64     def nick_html_colors(self, limit=None):
65         if self.nick is None:
66             return "Anonymous Player"
67         else:
68             return html_colors(self.nick, limit)
69
70     def team_html_color(self):
71         if self.team == 5:
72             return "red"
73         if self.team == 14:
74             return "blue"
75         if self.team == 13:
76             return "yellow"
77         if self.team == 10:
78             return "pink"
79
80
81 class PlayerWeaponStat(object):
82     """
83     The metrics for a single weapon in a game for a player.
84     """
85
86     def __init__(self, player_id=None, game_id=None, weapon_cd=None):
87         self.player_id = player_id
88         self.game_id = game_id
89         self.weapon_cd = weapon_cd
90         self.fired = 0
91         self.max = 0
92         self.hit = 0
93         self.actual = 0
94         self.frags = 0
95
96     def __repr__(self):
97         return ("<PlayerWeaponStat({0.player_weapon_stats_id}, {0.player_id}, {0.game_id})>"
98                 .format(self))
99
100     def to_dict(self):
101         return {
102             'weapon_cd': self.weapon_cd,
103             'player_weapon_stats_id': self.player_weapon_stats_id,
104             'player_id': self.player_id,
105             'game_id': self.game_id,
106             'fired': self.fired,
107             'max': self.max,
108             'hit': self.hit,
109             'actual': self.actual,
110             'frags': self.frags,
111         }
112
113
114 class TeamGameStat(object):
115     """
116     Team level metrics.
117     """
118
119     def __init__(self, team_game_stat_id=None, create_dt=None):
120         self.team_game_stat_id = team_game_stat_id
121         self.create_dt = create_dt
122
123     def __repr__(self):
124         return "<TeamGameStat({0.team_game_stat_id}, {0.game_id}, {0.team})>".format(self)
125
126     def to_dict(self):
127         return {
128             'team_game_stat_id': self.team_game_stat_id,
129             'game_id': self.game_id,
130             'team': self.team,
131             'score': self.score,
132             'rounds': self.rounds,
133             'caps': self.caps,
134             'create_dt': self.create_dt.strftime('%Y-%m-%dT%H:%M:%SZ'),
135         }
136
137     # TODO: move this function to util
138     def team_html_color(self):
139         if self.team == 5:
140             return "red"
141         if self.team == 14:
142             return "blue"
143         if self.team == 13:
144             return "yellow"
145         if self.team == 10:
146             return "pink"
147
148
149 class PlayerGameAnticheat(object):
150     """
151     Anticheat metrics sent by the server to identify odd patterns.
152     """
153
154     def __init__(self, player_id=None, game_id=None, key=None, value=None, create_dt=None):
155         self.player_id = player_id
156         self.game_id = game_id
157         self.key = key
158         self.value = value
159         self.create_dt = create_dt
160
161     def __repr__(self):
162         return "<PlayerGameAnticheat({0.key}, {0.value})>".format(self)
163
164
165 class GameType(object):
166     """
167     A particular type of game.
168     """
169
170     def __repr__(self):
171         return "<GameType({0.game_type_cd}, {0.descr}, {0.active_ind})>".format(self)
172
173     def to_dict(self):
174         return {
175             'game_type_cd': self.game_type_cd,
176             'name': self.descr,
177             'active': self.active_ind,
178         }
179
180
181 class Weapon(object):
182     """
183     A particular type of weapon.
184     """
185
186     def __repr__(self):
187         return "<Weapon({0.weapon_cd}, {0.descr}, {0.active_ind})>".format(self)
188
189     def to_dict(self):
190         return {
191             'weapon_cd': self.weapon_cd,
192             'name': self.descr,
193             'active': self.active_ind,
194         }