Skip to content

Commit

Permalink
Merge pull request #57 from Syriiin/fix-membership-missing-unique
Browse files Browse the repository at this point in the history
Fix membership missing unique
  • Loading branch information
Syriiin authored May 31, 2024
2 parents 27390ff + 60d6d75 commit f456758
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
19 changes: 19 additions & 0 deletions leaderboards/migrations/0021_membership_unique_memberships.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.2.11 on 2024-05-31 14:28

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("leaderboards", "0020_membershipscore_performance_total_and_more"),
]

operations = [
migrations.AddConstraint(
model_name="membership",
constraint=models.UniqueConstraint(
fields=("leaderboard_id", "user_id"), name="unique_memberships"
),
),
]
30 changes: 6 additions & 24 deletions leaderboards/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,34 +196,16 @@ class Membership(models.Model):
CommunityMembershipQuerySet
)()

def recalculate(self):
"""
Recalculates the memberships pp from its score list
"""
if self.leaderboard.score_set == ScoreSet.NORMAL:
self.pp = calculate_pp_total(
score.performance_total
for score in self.scores.order_by("-performance_total").all()
)
elif self.leaderboard.score_set == ScoreSet.NEVER_CHOKE:
self.pp = calculate_pp_total(
(
score.nochoke_performance_total
if score.result & ScoreResult.CHOKE
else score.performance_total
)
for score in self.scores.order_by("-performance_total").all()
)
elif self.leaderboard.score_set == ScoreSet.ALWAYS_FULL_COMBO:
self.pp = calculate_pp_total(
score.nochoke_performance_total
for score in self.scores.order_by("-performance_total").all()
)

def __str__(self):
return f"{self.leaderboard}: {self.user.username}"

class Meta:
constraints = [
models.UniqueConstraint(
fields=["leaderboard_id", "user_id"], name="unique_memberships"
)
]

indexes = [models.Index(fields=["leaderboard"]), models.Index(fields=["user"])]


Expand Down
2 changes: 1 addition & 1 deletion leaderboards/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def get(self, request, leaderboard_type, gamemode, leaderboard_id):
1800,
)
else:
scores = leaderboard.get_top_scores(limit=5)
scores = leaderboard.get_top_scores(limit=limit)

serialiser = LeaderboardScoreSerialiser(scores, many=True)
return Response(serialiser.data)
Expand Down
3 changes: 2 additions & 1 deletion profiles/management/commands/recalculate.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
)
from common.osu.enums import Gamemode
from leaderboards.models import Membership
from leaderboards.services import update_membership
from profiles.models import Beatmap, Score, UserStats
from profiles.services import (
update_difficulty_calculations,
Expand Down Expand Up @@ -410,7 +411,7 @@ def recalculate_memberships(
with tqdm(desc="Memberships", total=memberships.count(), smoothing=0) as pbar:
for page in paginator:
for membership in page:
membership.recalculate()
update_membership(membership.leaderboard, membership.user_id)
pbar.update()
Membership.objects.bulk_update(page, ["pp"])

Expand Down

0 comments on commit f456758

Please sign in to comment.