def callCount(user): r = get_redis_instance() count = int(r.zscore('melenphone:leaderboards:alltime', str(user.id))) if count == 1: unlock_achievement("count_initie", user) if count == 5: unlock_achievement("count_apprenti", user) if count == 10: unlock_achievement("count_fan_rdls", user) if count == 20: unlock_achievement("count_militant", user) if count == 35: unlock_achievement("count_top", user) if count == 50: unlock_achievement("count_messager", user) if count == 70: unlock_achievement("count_animateur", user) if count == 100: unlock_achievement("count_artiste", user) if count == 150: unlock_achievement("count_lanceur", user) if count == 250: unlock_achievement("count_ambassadeur", user) if count == 375: unlock_achievement("count_mage", user) if count == 500: unlock_achievement("count_justicier", user) if count == 700: unlock_achievement("count_tribun", user) if count == 1000: unlock_achievement("count_heros", user) if count == 1500: unlock_achievement("count_laec", user) if count == 5000: unlock_achievement("count_legendaire", user)
def update_scores(user, tzdate=None): r = get_redis_instance() pipe = r.pipeline(transaction=False) # Cles necessaires : if tzdate is None: tzdate = timezone.now() today = format_date(tzdate) sevenPreviousDays = [today] for i in range(6): tzdate += datetime.timedelta(days=1) sevenPreviousDays.append(format_date(tzdate)) # Global counters pipe.incr('melenphone:call_count:alltime') for day in sevenPreviousDays: pipe.incr('melenphone:call_count:weekly:' + day) pipe.incr('melenphone:call_count:daily:' + today) # Leaderboards if not(user is None): pipe.zincrby('melenphone:leaderboards:alltime',str(user.id)) for day in sevenPreviousDays: pipe.zincrby('melenphone:leaderboards:weekly:' + day, str(user.id)) pipe.zincrby('melenphone:leaderboards:daily:' + today, str(user.id)) pipe.execute()
def leaderboards(user): r = get_redis_instance() if int(r.zrevrank('melenphone:leaderboards:alltime', str(user.id))) == 0: unlock_achievement("leaderboard_alltime", user) if int(r.zrevrank('melenphone:leaderboards:weekly:' + format_date(timezone.now()), str(user.id))) == 0: unlock_achievement("leaderboard_weekly", user) if int(r.zrevrank('melenphone:leaderboards:daily:' + format_date(timezone.now()), str(user.id))) == 0: unlock_achievement("leaderboard_daily", user)
def dailyCalls(user): r = get_redis_instance() dailyCalls = int(r.zscore('melenphone:leaderboards:daily:' + format_date(timezone.now()), str(user.id))) if dailyCalls == 50: unlock_achievement("daily_a_fond", user) if dailyCalls == 100: unlock_achievement("daily_acharne", user) if dailyCalls == 200: unlock_achievement("daily_dodo", user) if dailyCalls == 300: unlock_achievement("daily_holochon", user)
def get_global_scores(): r = get_redis_instance() daily_calls = int(r.get('melenphone:call_count:daily:' + format_date(timezone.now())) or 0) weekly_calls = int(r.get('melenphone:call_count:weekly:' + format_date(timezone.now())) or 0) alltime_calls = int(r.get('melenphone:call_count:alltime') or 0) daily_leaderboard = generate_leaderboard('daily', 10) return { 'dailyCalls': daily_calls, 'weeklyCalls': weekly_calls, 'alltimeCalls': alltime_calls, 'dailyLeaderboard': daily_leaderboard }
def get_leaderboard_from_redis(period, top): r = get_redis_instance() if period == 'alltime': return r.zrevrange( 'melenphone:leaderboards:alltime', 0, top - 1, withscores=True ) else: return r.zrevrange( 'melenphone:leaderboards:' + period + ':' + format_date(timezone.now()), 0, top - 1, withscores=True )
def consecutive(user): r = get_redis_instance() time = timezone.now() days = 0 while r.zscore('melenphone:leaderboards:daily:' + format_date(time), str(user.id)) != None: days += 1 if days == 2: unlock_achievement("consecutive_retour", user) elif days == 3: unlock_achievement("consecutive_fidele", user) elif days == 5: unlock_achievement("consecutive_infatigable", user) elif days == 10: unlock_achievement("consecutive_melenphonophile", user) time -= datetime.timedelta(days=1)
def handle(self, *args, **options): r = get_redis_instance() keys = r.keys('melenphone:*') pipe = r.pipeline(transaction=False) for key in keys: pipe.delete(key) pipe.execute() for call in Call.objects.all(): update_scores(call.user, call.date) print('Base redis reconstruite !')
def post(self, request): r = get_redis_instance() users = User.objects.all() nb = users.count() user = users[random.randint(0, nb - 1)] now = timezone.now().timestamp() callerLat, callerLng = randomLocation() calledLat, calledLng = randomLocation() lastCall = float(r.getset('lastcall:user:'******'appel à la bdd pour pouvoir reconstruire redis si besoin Call.objects.create(user=user) update_scores(user) update_achievements(user) global_scores = get_global_scores() message = { 'type': 'call', 'value': { 'call': { 'caller': { 'lat': callerLat, 'lng': callerLng, 'id': user.id, 'agentUsername': user.UserExtend.agentUsername }, 'target': { 'lat': calledLat, 'lng': calledLng } }, 'updatedData': global_scores } } Channel('send_message').send(message) return Response(status=200)
def earlyAdopters(user): r = get_redis_instance() callersCount = r.scard('melenphone:leaderbords:alltime') if callersCount < 100: unlock_achievement("early_y_etais", user)
def post(self, request): jsondata = request.body.decode('utf-8') data = json.loads(jsondata) # Latitude et longitude de l'appelant callerAgentUsername = data['data']['agent']['username'] callerLat, callerLng = getCallerLocation(callerAgentUsername) # Latitude et longitude de l'appellé calledNumber = data['data']['contact'] calledLat, calledLng = getCalledLocation(calledNumber) r = get_redis_instance() now = timezone.now().timestamp() try: user = UserExtend.objects.get( agentUsername=callerAgentUsername).user # On le récupère lastCall = float( r.getset('lastcall:user:'******'animation if user is None: id = None agentUsername = None else: id = user.id agentUsername = user.UserExtend.agentUsername global_scores = get_global_scores() message = { 'type': 'call', 'value': { 'call': { 'caller': { 'lat': callerLat, 'lng': callerLng, 'id': id, 'agentUsername': agentUsername }, 'target': { 'lat': calledLat, 'lng': calledLng } }, 'updatedData': global_scores } } Channel('send_message').send(message) return Response(status=200)