This repository has been archived by the owner on Apr 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
models.py
474 lines (360 loc) · 13.7 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# -*- coding: utf-8 -*-
import datetime
import hashlib
import copy
import time
import relativedelta
import elo
import cgi
import os
import StringIO
from datetime import date
from google.appengine.ext import db
from google.appengine.api import memcache
from google.appengine.api import users
from mako.template import Template
from mako.lookup import TemplateLookup
SCORE = [ 0, 1, 2, 3 ]
DEFAULT_SCORE = 500.0
class Match(db.Model):
date = db.DateProperty(auto_now_add=True)
player1 = db.UserProperty()
player2 = db.UserProperty()
nickname1 = db.StringProperty()
nickname2 = db.StringProperty()
score1 = db.IntegerProperty(choices=SCORE)
score2 = db.IntegerProperty(choices=SCORE)
###############################################################
class User(db.Model):
user = db.UserProperty()
nickname = db.StringProperty()
score = db.FloatProperty(default=DEFAULT_SCORE)
wins = db.IntegerProperty(default=0)
loses = db.IntegerProperty(default=0)
rank = db.IntegerProperty(default=0)
avatar = db.BlobProperty()
retired = db.BooleanProperty(default=False)
###############################################################
class Score(db.Model):
user = db.UserProperty()
date = db.DateProperty()
score = db.FloatProperty(default=DEFAULT_SCORE)
###############################################################
class PendingUser(db.Model):
user = db.UserProperty()
###############################################################
class Comment(db.Model):
sender = db.StringProperty(required=True)
senderid = db.IntegerProperty(required=True)
text = db.TextProperty(required=True)
date = db.DateTimeProperty(auto_now_add=True)
###############################################################
def relative_time(date):
delta = relativedelta.relativedelta(date.today(), date)
if delta.years == 1:
time = 'Il y a environ 1 an'
elif delta.years > 1:
time = 'Il y a environ %s ans' % delta.years
elif delta.months == 1:
time = 'Il y a environ 1 mois'
elif delta.months > 1:
time = 'Il y a environ %s mois' % delta.months
elif delta.days == 1:
time = "Hier"
elif delta.days > 1:
time = 'Il y a %s jours' % delta.days
else:
time = 'Aujourd\'hui'
return time
###############################################################
def rfc3339date(date):
"""Formats the given date in RFC 3339 format for feeds."""
if not date: return ''
date = date + datetime.timedelta(seconds=-time.timezone)
if time.daylight:
date += datetime.timedelta(seconds=time.altzone)
return date.strftime('%Y-%m-%dT%H:%M:%SZ')
###############################################################
def is_registered(user):
if User.all().filter('user =', user).get() is None:
return False
return True
###############################################################
def is_pending(user):
if PendingUser.all().filter('user =', user).get() is None:
return False
return True
###############################################################
def register_user(user):
if user is None or User.all().filter('user =', user).get():
return
else:
user_entry = User()
user_entry.user = user
user_entry.nickname = user.nickname()
user_entry.put()
return
###############################################################
def add_pending_user(user):
if user is None or PendingUser.all().filter('user =', user).get():
return
else:
pending_user = PendingUser()
pending_user.user = user
pending_user.put()
return
###############################################################
def remove_pending_user(user):
if user is None:
return
pending_user = PendingUser.all().filter('user =', user).get()
pending_user.delete()
return
###############################################################
def get_pending_user(id):
return PendingUser.get_by_id(id)
###############################################################
def get_pending_users():
return PendingUser.all().fetch(10)
###############################################################
def get_possible_opponents():
return User.all().order('user').fetch(100)
###############################################################
def get_jids():
users = User.all().fetch(30)
jids = []
for user in users:
jids.append(user.user.email())
return jids
###############################################################
def update_avatar(user, data):
user_obj = get_user_(user)
if user_obj is None:
return
user_obj.avatar = db.Blob(data)
user_obj.put()
###############################################################
def get_new_players():
return User.all().filter('score =', 500.0).fetch(100)
###############################################################
def create_new_match(me, request):
match = Match()
match.player1 = me
match.nickname1 = User.all().filter('user =', me).get().nickname
user2_obj = User.get_by_id(long(request.get('player2')))
match.player2 = user2_obj.user
match.nickname2 = user2_obj.nickname
match.score1 = long(request.get('score1'))
match.score2 = long(request.get('score2'))
match.date = date.today() - datetime.timedelta(days=long(request.get('date')))
# check score is ok
if (match.score1 != 3 and match.score2 != 3) or (match.score1 == 3 and match.score2 == 3):
return None
# update wins and loses of the players
[ winner, looser, gap ] = get_winner_looser(match)
update_wins_loses(winner, looser)
match.put()
return match.key().id()
###############################################################
def delete_match(matchid):
match_to_delete = Match.get_by_id(matchid)
if match_to_delete is None:
return None
# retrieve the match juste before this one (for recomputing scores)
# FIXME we assume there is at least one match the same day or before
match_for_computation = Match.all().filter('date <=', match_to_delete.date).get()
# decrement counters
[ winner, looser, gap ] = get_winner_looser(current_match)
decrement_wins_loses(winner, looser)
# delete match
match_to_delete.delete()
return match_for_computation.key().id()
###############################################################
def get_recent_matches(n=10):
return Match.all().order('-date').fetch(n)
###############################################################
def get_user(userid):
return User.get_by_id(long(userid))
###############################################################
def get_user_(user):
return User.all().filter('user =', user).get()
###############################################################
def match_compare(x, y):
if x.date > y.date:
return -1
elif x.date == y.date:
return 0
else:
return 1
###############################################################
def get_user_matches(user):
matches = Match.all().order('-date').filter('player1 =', user).fetch(100) \
+ Match.all().order('-date').filter('player2 =', user).fetch(100)
matches.sort(match_compare)
return matches
###############################################################
def get_last_score(user, date):
score_obj = Score.all().filter('date <=', date).filter('user =', user).order('-date').get()
if not score_obj:
return DEFAULT_SCORE
else:
return score_obj.score
###############################################################
def get_scores(userid):
user = User.get_by_id(userid)
if user is None:
return None
scores = Score.all().order('date').filter('user =', user.user).fetch(100)
return scores
###############################################################
def get_winner_looser(match):
if match.score1 > match.score2:
return [ match.player1, match.player2, abs(match.score1 - match.score2) ]
else:
return [ match.player2, match.player1, abs(match.score1 - match.score2) ]
###############################################################
def update_or_create_score(score, user, date, win=True):
# update or create Score object
score_obj = Score.all().filter('date =', date).filter('user =', user).get()
if not score_obj:
score_obj = Score()
score_obj.date = date
score_obj.user = user
score_obj.score = float(score)
score_obj.put()
# update User object
user_obj = User.all().filter('user =', user).get()
user_obj.score = float(score)
user_obj.put()
###############################################################
def update_wins_loses(winner, looser):
winner_obj = User.all().filter('user =', winner).get()
looser_obj = User.all().filter('user =', looser).get()
winner_obj.wins += 1
looser_obj.loses += 1
db.put([ winner_obj, looser_obj ])
###############################################################
def decrement_wins_loses(winner, looser):
winner_obj = User.all().filter('user =', winner).get()
looser_obj = User.all().filter('user =', looser).get()
winner_obj.wins -= 1
looser_obj.loses -= 1
db.put([ winner_obj, looser_obj ])
###############################################################
def compute_ranks():
users = User.all().filter('score !=', 500.0).filter('retired =', False).order('-score').fetch(1000) # we suppose we will never have that much users..
rank = 0
previous_user_score = 0
for user in users:
if user.score != previous_user_score:
rank += 1
previous_user_score = user.score
user.rank = rank
db.put(users)
###############################################################
def update_scores(match_id):
current_match = Match.get_by_id(match_id)
# get all matches that took place after this one, or the same day
# FIXME what if some matches happen the same day? -> we don't really care
matches = Match.all().order('date').filter('date >=', current_match.date).fetch(100)
# erase scores that need to be re-computed
obsolete_scores = Score.all().filter('date >=', current_match.date).fetch(1000)
db.delete(obsolete_scores)
for match in matches:
[ winner, looser, gap ] = get_winner_looser(match)
winner_previous_score = get_last_score(winner, match.date)
looser_previous_score = get_last_score(looser, match.date)
[ winner_new_score, looser_new_score ] = elo.compute_score(winner_previous_score, looser_previous_score, gap)
update_or_create_score(winner_new_score, winner, match.date, True)
update_or_create_score(looser_new_score, looser, match.date, False)
compute_ranks()
###############################################################
def update_nickname(user, new_nickname):
user_obj = get_user_(user)
if user_obj is None or user_obj.nickname == cgi.escape(new_nickname):
return
# update User
user_obj.nickname = cgi.escape(new_nickname)
user_obj.put()
# update matches
matches = Match.all().filter('player1 =', user).fetch(100) # we don't care if it isn't chnaged for older matches....
for match in matches:
match.nickname1 = cgi.escape(new_nickname)
db.put(matches)
matches = Match.all().filter('player2 =', user).fetch(100)
for match in matches:
match.nickname2 = cgi.escape(new_nickname)
db.put(matches)
###############################################################
def create_comment(sender, text):
comment = Comment(sender=sender.nickname, senderid=sender.key().id(), text=cgi.escape(text))
comment.put()
###############################################################
def get_recent_comments():
data = memcache.get("comments")
if data is not None:
return data
else:
data = get_recent_comments_tpl()
memcache.add("comments", data)
return data
###############################################################
def get_recent_comments_tpl(n=10):
recent_comments = Comment.all().order('-date').fetch(n)
output = StringIO.StringIO()
for comment in recent_comments:
output.write("<div class=\"comment\"><img src=\"/avatar/%s\" alt=\"avatar\" /> " % comment.senderid)
output.write("<b>%s</b><br />%s</div>\n" % (comment.sender, comment.text))
return output.getvalue()
###############################################################
def get_retired_players():
return User.all().filter('retired =', True).fetch(100)
###############################################################
def get_ranking():
data = memcache.get("ranks")
if data is not None:
return data
else:
data = get_ranking_tpl()
memcache.add("ranks", data)
return data
###############################################################
def get_ranking_tpl():
users = User.all().filter('score !=', 500.0).filter('retired =', False).order('-score').fetch(100)
output = StringIO.StringIO()
i = 0
for user in users:
output.write("<tr class=\"color%s\">" % (i%2))
output.write("<td><span class=\"rank_number\">%s.</span><span class=\"rank_chkbox\">" % user.rank)
output.write("<input type=\"checkbox\" id=\"chk_%s\" /></span></td>" % user.key().id())
output.write("<td class=\"player_name\"><a href=\"/user/%s\">%s</a></td>" % (user.key().id(), user.nickname))
output.write("<td>%0.2f</td></tr>" % user.score)
i += 1
return output.getvalue()
###############################################################
def get_recent_matches_home():
if users.is_current_user_admin():
key = "matches_home_admin"
else:
key = "matches_home"
data = memcache.get(key)
if data is not None:
return data
else:
data = get_recent_matches_home_tpl()
memcache.add(key, data, 3600)
return data
###############################################################
def get_recent_matches_home_tpl():
matches = get_recent_matches()
output = StringIO.StringIO()
mylookup = TemplateLookup(directories=['templates'])
template_file = os.path.join(os.path.dirname(__file__), 'templates/base_match.html')
for match in matches:
template_values = {
'is_admin': users.is_current_user_admin(),
'match': match,
'user': None
}
output.write(Template(filename=template_file,lookup=mylookup).render_unicode(**template_values))
return output.getvalue()