Remove Persona since it was shut down. :(
[xonotic/xonstat.git] / xonstat / models / player.py
1 """
2 Models related to players.
3 """
4
5 from calendar import timegm
6
7 from xonstat.models.mixins import FuzzyDateMixin, EpochMixin, NickColorsMixin
8 from xonstat.util import strip_colors, pretty_date, qfont_decode
9
10
11 class Player(EpochMixin, NickColorsMixin, FuzzyDateMixin):
12     """
13     A player, which can represent either a human or a bot.
14     """
15
16     def nick_strip_colors(self):
17         if self.nick is None:
18             return "Anonymous Player"
19         else:
20             return strip_colors(self.nick)
21
22     def __repr__(self):
23         return "<Player({}, {})>".format(self.player_id, self.nick.encode('utf-8'))
24
25     def to_dict(self):
26         return {
27             'player_id': self.player_id,
28             'nick': self.nick,
29             'joined': self.create_dt.strftime('%Y-%m-%dT%H:%M:%SZ'),
30             'active_ind': self.active_ind,
31             'location': self.location,
32             'stripped_nick': qfont_decode(self.stripped_nick),
33         }
34
35
36 class Achievement(object):
37     """
38     A type of achievement. Referenced implicitly in PlayerAchievement.
39     """
40
41     def __repr__(self):
42         return "<Achievement({0.achievement_cd}, {0.descr}, {0.limit})>".format(self)
43
44     def to_dict(self):
45         return {
46             'achievement_cd': self.achievement_cd,
47             'name': self.descr,
48             'limit':self.limit,
49         }
50
51
52 class PlayerAchievement(object):
53     """
54     Achievements a player has earned.
55     """
56
57     def __repr__(self):
58         return "<PlayerAchievement({0.player_id}, {0.achievement_cd})>".format(self)
59
60     def to_dict(self):
61         return {
62             'player_id': self.player_id,
63             'achievement_cd': self.achievement_cd,
64         }
65
66
67 class Hashkey(object):
68     """
69     A player's identifying key from the d0_blind_id library.
70     """
71
72     def __init__(self, player_id=None, hashkey=None):
73         self.player_id = player_id
74         self.hashkey = hashkey
75
76     def __repr__(self):
77         return "<Hashkey({0.player_id}, {0.hashkey})>".format(self)
78
79     def to_dict(self):
80         return {
81             'player_id': self.player_id,
82             'hashkey': self.hashkey
83         }
84
85
86 class PlayerNick(object):
87     """
88     A single nickname a player has used in a game.
89     """
90
91     def __repr__(self):
92         return "<PlayerNick({0.player_id}, {0.stripped_nick})>".format(self)
93
94     def to_dict(self):
95         return {
96             'player_id': self.player_id,
97             'name': qfont_decode(self.stripped_nick),
98         }
99
100
101 class PlayerElo(object):
102     """
103     A player's skill for a particular game type, as determined by a modified Elo algorithm.
104     """
105
106     def __init__(self, player_id=None, game_type_cd=None, elo=None):
107         self.player_id = player_id
108         self.game_type_cd = game_type_cd
109         self.elo = elo
110         self.score = 0
111         self.games = 0
112
113     def __repr__(self):
114         return ("<PlayerElo(pid={0.player_id}, gametype={0.game_type_cd}, elo={0.elo}, "
115                 "games={0.games})>".format(self))
116
117     def to_dict(self):
118         return {
119             'player_id': self.player_id,
120             'game_type_cd': self.game_type_cd,
121             'elo': self.elo,
122             'games': self.games,
123         }
124
125
126 class PlayerRank(NickColorsMixin):
127     """
128     A player's rank for a given game type.
129     """
130
131     def __repr__(self):
132         return ("<PlayerRank(pid={0.player_id}, gametype={0.game_type_cd}, rank={0.rank})>"
133                 .format(self))
134
135     def to_dict(self):
136         return {
137             'player_id': self.player_id,
138             'game_type_cd': self.game_type_cd,
139             'rank': self.rank
140         }
141
142
143 class PlayerCaptime(FuzzyDateMixin, EpochMixin):
144     """
145     A flag capture time for a player on a given map.
146     """
147
148     def __init__(self, player_id=None, game_id=None, map_id=None, fastest_cap=None, mod=None):
149         self.player_id = player_id
150         self.game_id = game_id
151         self.map_id = map_id
152         self.fastest_cap = fastest_cap
153         self.mod = mod
154
155     def __repr__(self):
156         return "<PlayerCaptime(pid={0.player_id}, map_id={0.map_id}, mod={0.mod})>".format(self)
157
158
159 class PlayerGroups(object):
160     """
161     An authorization group a player belongs to. Used to control access.
162     """
163
164     def __init__(self, player_id=None, group_name=None):
165         self.player_id  = player_id
166         self.group_name = group_name
167
168     def __repr__(self):
169         return "<PlayerGroups({0.player_id}, {0.group_name})>".format(self)
170
171
172 # TODO: determine if this is a real model (it is very similar to PlayerCaptime from above)
173 class PlayerCapTime(object):
174     """
175     Fastest flag capture times per player.
176     """
177
178     def __init__(self, row):
179         self.fastest_cap = row.fastest_cap
180         self.create_dt = row.create_dt
181         self.create_dt_epoch = timegm(row.create_dt.timetuple())
182         self.create_dt_fuzzy = pretty_date(row.create_dt)
183         self.player_id = row.player_id
184         self.game_id = row.game_id
185         self.map_id = row.map_id
186         self.map_name = row.map_name
187         self.server_id = row.server_id
188         self.server_name = row.server_name
189
190     def to_dict(self):
191         return {
192             "fastest_cap" : self.fastest_cap.total_seconds(),
193             "create_dt_epoch": self.create_dt_epoch,
194             "create_dt_fuzzy": self.create_dt_fuzzy,
195             "game_id":self.game_id,
196             "map_id": self.map_id,
197             "map_name": self.map_name,
198             "server_id": self.server_id,
199             "server_name": self.server_name,
200         }
201
202
203 class PlayerMedal(object):
204     """
205     A medal a player has earned in a large tournament.
206     """
207
208     def __repr__(self):
209         return "<PlayerMedal(pid={0.player_id}, place={0.place}, alt={0.alt})>".format(self)