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