Example #1
0
def add_score(request):
    game = Game.objects.get(id=int(request.GET["game_id"]))
    level = Level.objects.get(id=int(request.GET["level_id"]))
    score_amount = int(request.GET["score"])
    user = request.user

    score = Score(game=game, level=level, user=user, score=score_amount)
    score.save()

    # Calculate score rank
    personal_rank = score_rank_personal(level.id, game.id, user.id, score_amount)
    friend_rank = score_rank_friends(level.id, game.id, user.id, score_amount)

    # Personal high scores
    scores_personal = scores_json(high_scores_personal(game, level, user, 3))
    if not any([score.id == sc["id"] for sc in scores_personal]):
        scores_personal.append(score_json(score, user, personal_rank))

    # Friend high scores
    scores_friends = scores_json(high_scores_friends(game, level, user, 3))
    if not any([score.id == sc["id"] for sc in scores_friends]):
        scores_friends.append(score_json(score, user, friend_rank))

    return {
        "id": score.id,
        "personal_rank": personal_rank,
        "friend_rank": friend_rank,
        "personal_high_scores": scores_personal,
        "friends_high_scores": scores_friends,
    }
Example #2
0
def all_high_scores(req):
    game = Game.objects.get(id=int(req.GET["game_id"]))
    level = Level.objects.get(id=int(req.GET["level_id"]))
    user = req.user

    scores = Score.objects.raw(
        """
        select
            vocab_score.*,
            social_auth_usersocialauth.uid as fb_uid,
            auth_user.first_name as first_name,
            auth_user.last_name as last_name
        from vocab_score
        left join social_auth_usersocialauth
            ON social_auth_usersocialauth.user_id = vocab_score.user_id
        left join auth_user
            ON auth_user.id = vocab_score.user_id
        where
            vocab_score.game_id = %s and
            vocab_score.level_id = %s
        ORDER BY score DESC
        """,
        [game.id, level.id],
    )

    return scores_json(scores)