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