-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe5711e
commit dbf9495
Showing
9 changed files
with
98 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,40 @@ | ||
|
||
import math | ||
|
||
from lego.apps.achievements.constants import EVENT_IDENTIFIER, EVENT_PRICE_IDENTIFIER, EVENT_RANK_IDENTIFIER, MEETING_IDENTIFIER, PENALTY_IDENTIFIER, POLL_IDENTIFIER, QUOTE_IDENTIFIER | ||
|
||
ACHIEVEMENT_DATA = { | ||
EVENT_IDENTIFIER: [0, 1, 2, 3, 4, 6], | ||
EVENT_RANK_IDENTIFIER: [7, 8, 9], | ||
QUOTE_IDENTIFIER: [2], | ||
EVENT_PRICE_IDENTIFIER: [2, 3, 5], | ||
MEETING_IDENTIFIER: [2], | ||
POLL_IDENTIFIER: [0, 2, 4], | ||
PENALTY_IDENTIFIER: [0, 3, 5, 6], | ||
from lego.apps.achievements.constants import ( | ||
EVENT_IDENTIFIER, | ||
EVENT_PRICE_IDENTIFIER, | ||
EVENT_RANK_IDENTIFIER, | ||
MEETING_IDENTIFIER, | ||
PENALTY_IDENTIFIER, | ||
POLL_IDENTIFIER, | ||
QUOTE_IDENTIFIER, | ||
) | ||
|
||
ACHIEVEMENT_RARITIES = { | ||
EVENT_IDENTIFIER: [0, 1, 2, 3, 4, 6], | ||
EVENT_RANK_IDENTIFIER: [7, 8, 9], | ||
QUOTE_IDENTIFIER: [1], | ||
EVENT_PRICE_IDENTIFIER: [2, 3, 5], | ||
MEETING_IDENTIFIER: [1], | ||
POLL_IDENTIFIER: [0, 2, 4], | ||
PENALTY_IDENTIFIER: [0, 3, 5, 6], | ||
} | ||
|
||
delta = 0.1 | ||
# Remember to update this rarity list when adding new achievement | ||
MAX_POSSIBLE_SCORE = sum( | ||
max(rarity_list) + 1 + (max(rarity_list) * delta) | ||
for key, rarity_list in ACHIEVEMENT_RARITIES.items() | ||
if key != EVENT_RANK_IDENTIFIER | ||
) | ||
|
||
def calculate_user_rank(user, alpha=0.68, beta=0.45, gamma=0.43, w=0.75): | ||
N = len(ACHIEVEMENT_DATA) | ||
highest_rarity = 0 | ||
weighted_rarities_product = 1 | ||
achievement_count = user.achievements.count() | ||
|
||
if achievement_count == 0: | ||
return 0 | ||
|
||
for achievement in user.achievements.all(): | ||
identifier = achievement.identifier | ||
level = achievement.level | ||
|
||
rarity_list = ACHIEVEMENT_DATA.get(identifier, []) | ||
rarity = rarity_list[level] if level < len(rarity_list) else 0 | ||
|
||
highest_rarity = max(highest_rarity, rarity) | ||
|
||
weighted_rarity = math.log(rarity + 2) ** 2 | ||
weighted_rarities_product *= weighted_rarity | ||
|
||
G = math.pow(weighted_rarities_product, 1 / achievement_count) | ||
|
||
G_normalized = G / (math.log(9 + 2) ** 2) | ||
|
||
baseline = (highest_rarity + 1) / 2 | ||
def calculate_user_rank(user): | ||
score = 0.0 | ||
|
||
G_blended = w * G_normalized + (1 - w) * baseline / (math.log(9 + 2) ** 2) | ||
user_achievements = user.achievements.all() | ||
for achievement in user_achievements: | ||
rarity_list = ACHIEVEMENT_RARITIES.get(achievement.identifier, []) | ||
|
||
highest_rarity_component = alpha * (math.sqrt(highest_rarity + 1) / math.sqrt(10)) | ||
geometric_mean_component = beta * G_blended | ||
achievement_count_component = gamma * (math.log(achievement_count + 1) / math.log(N + 1)) | ||
value = rarity_list[achievement.level] | ||
score += value + 1 + (achievement.level * delta) | ||
|
||
rank = (100 * (highest_rarity_component + geometric_mean_component + achievement_count_component)) / (alpha + beta + gamma) | ||
rounded = round(rank, 2) | ||
return rounded | ||
return score if MAX_POSSIBLE_SCORE else 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,22 @@ | ||
from rest_framework import permissions, mixins, viewsets | ||
from rest_framework import mixins, permissions, viewsets | ||
from rest_framework.response import Response | ||
|
||
from lego.apps.users.serializers.users import PublicUserWithGroupsSerializer | ||
from lego.apps.users.models import User | ||
from lego.apps.users.serializers.users import PublicUserWithGroupsSerializer | ||
|
||
|
||
class LeaderBoardViewSet(mixins.ListModelMixin, viewsets.GenericViewSet): | ||
serializer_class = PublicUserWithGroupsSerializer | ||
permission_classes = [permissions.IsAuthenticated] | ||
|
||
def get_queryset(self): | ||
return User.objects.filter(achievements__isnull=False).distinct() | ||
return ( | ||
User.objects.filter(achievements__isnull=False) | ||
.order_by("-achievements_score") | ||
.distinct()[:50] | ||
) | ||
|
||
def list(self, request, *args, **kwargs): | ||
queryset = self.get_queryset() | ||
serializer = self.get_serializer(queryset, many=True) | ||
|
||
sorted_data = sorted( | ||
serializer.data, key=lambda x: x["achievement_score"], reverse=True | ||
)[:50] | ||
|
||
return Response(sorted_data) | ||
return Response(serializer.data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from django.apps import apps | ||
from django.db import migrations, models | ||
|
||
|
||
def populate_achievements_score(apps, schema_editor): | ||
from lego.apps.achievements.utils.calculation_utils import calculate_user_rank | ||
|
||
User = apps.get_model("users", "User") | ||
|
||
for user in User.objects.all(): | ||
user.achievements_score = calculate_user_rank(user) | ||
user.save(update_fields=["achievements_score"]) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("users", "0044_alter_membership_role_alter_membershiphistory_role"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="user", | ||
name="achievements_score", | ||
field=models.FloatField(default=0), | ||
), | ||
migrations.RunPython(populate_achievements_score), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters