def stats(request, refresh): game = util.get_running_game() scores = None try: refresh = bool(int(refresh)) except: refresh = False if refresh: memcache.delete('scores-stats-%s' % game.key()) else: scores = memcache.get('scores-stats-%s' % game.key()) if scores is not None: return render(request, 'stats.html', {'scores': scores}) hits = Hit.all().ancestor(game).order('start').fetch(1000) scores = util.prepare_scores_stats(hits) memcache.set('scores-stats-%s' % game.key(), scores, time=3600) return render(request, 'stats.html', {'scores': scores})
def transaction(game, player): # get previous hit previous = util.get_last_hit(game) if previous is None: # insert new hit and return hit = Hit(player=player, start=now, parent=player) hit.put() memcache.delete('last-hit-%s' % game.key()) return DNOTEPTB_FIRST_PLAYER elif previous.player.key() == player.key(): if (now - previous.start).seconds < 3600: return DNOTEPTB_TOO_SOON_ERROR # insert new hit hit = Hit(player=player, start=now, parent=player) hit.put() memcache.delete('last-hit-%s' % game.key()) # mark previous hit as completed previous.end = now previous.completed = True previous.put() # an user doesn't get points if he/she was the last user who pressed the # button if player.key() == previous.player.key(): return DNOTEPTB_SAME_PLAYER # increase current player's score score = util.timedelta2seconds(previous.end - previous.start) player.score += score player.put() # decrease previous player's score previous.player.score -= score previous.player.put() memcache.delete('players-%s' % game.key()) return DNOTEPTB_OK
def get_last_hit(game): last = memcache.get('last-hit-%s' % game.key()) if last is None: last = Hit.all().ancestor(game).order('-created').get() memcache.set('last-hit-%s' % game.key(), last) return last