-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from Syriiin/add-score-mutations
Add score mutations
- Loading branch information
Showing
14 changed files
with
355 additions
and
62 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import pytest | ||
|
||
from common.osu.enums import Gamemode | ||
from leaderboards.services import create_membership, update_membership | ||
from profiles.services import fetch_scores | ||
|
||
|
||
@pytest.mark.django_db | ||
class TestMembershipServices: | ||
@pytest.fixture | ||
def membership(self, leaderboard, stub_user_stats): | ||
return create_membership(leaderboard.id, stub_user_stats.user_id) | ||
|
||
def test_create_membership(self, leaderboard, membership): | ||
assert membership.leaderboard == leaderboard | ||
assert membership.leaderboard.member_count == 2 | ||
assert membership.user.username == "Syrin" | ||
assert membership.score_count == 4 | ||
assert membership.pp == 1215.8880634205632 | ||
|
||
def test_update_membership(self, membership): | ||
fetch_scores(membership.user_id, [362949], Gamemode.STANDARD) | ||
membership = update_membership(membership.leaderboard, membership.user_id) | ||
assert membership.score_count == 5 | ||
assert membership.pp == 1399.645425686207 |
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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
from django.core.management.base import BaseCommand | ||
from django.db.models import F, FilteredRelation, Q, QuerySet | ||
from tqdm import tqdm | ||
|
||
from profiles.enums import ScoreMutation, ScoreResult | ||
from profiles.models import Score | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Checks all scores have required mutations created." | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"--fix", | ||
action="store_true", | ||
help="Whether create missing mutations after detecting them", | ||
) | ||
|
||
def handle(self, *args, **options): | ||
fix = options["fix"] | ||
self.check_nochoke_mutations(fix) | ||
|
||
def check_nochoke_mutations(self, create_missing_mutations: bool): | ||
choke_scores = Score.objects.filter_mutations().filter( | ||
result=F("result").bitand(ScoreResult.CHOKE) | ||
) | ||
|
||
choke_scores_missing_nochoke_mutation = ( | ||
Score.objects.filter_mutations() | ||
.filter(result=F("result").bitand(ScoreResult.CHOKE)) | ||
.annotate( | ||
nochoke_mutation=FilteredRelation( | ||
"mutations", | ||
condition=Q(mutations__mutation=ScoreMutation.NO_CHOKE), | ||
) | ||
) | ||
.filter(nochoke_mutation=None) | ||
) | ||
|
||
total_count = choke_scores.count() | ||
missing_count = choke_scores_missing_nochoke_mutation.count() | ||
|
||
if missing_count == 0: | ||
self.stdout.write( | ||
self.style.SUCCESS( | ||
f"All {total_count} choke scores have nochoke mutations created" | ||
) | ||
) | ||
return | ||
|
||
self.stdout.write( | ||
self.style.ERROR( | ||
f"{missing_count} / {total_count} choke scores are missing nochoke mutations ({(total_count - missing_count) / total_count * 100:.2f}% complete)" | ||
) | ||
) | ||
if create_missing_mutations: | ||
self.create_missing_nochoke_mutations(choke_scores_missing_nochoke_mutation) | ||
self.stdout.write( | ||
self.style.SUCCESS( | ||
f"Created {missing_count} missing nochoke mutations for choke scores" | ||
) | ||
) | ||
else: | ||
self.stdout.write( | ||
self.style.WARNING( | ||
"Use --fix to create missing mutations for choke scores" | ||
) | ||
) | ||
|
||
def create_missing_nochoke_mutations(self, choke_scores: QuerySet[Score]): | ||
with tqdm( | ||
desc="No-choke", total=choke_scores.count(), smoothing=0 | ||
) as progress_bar: | ||
while len(page := choke_scores.select_related("beatmap")[:2000]) > 0: | ||
self.create_missing_nochoke_mutations_page(page, progress_bar) | ||
|
||
def create_missing_nochoke_mutations_page( | ||
self, choke_scores: QuerySet[Score], progress_bar: tqdm | ||
): | ||
scores_to_create = [] | ||
for score in choke_scores: | ||
scores_to_create.append(score.get_nochoke_mutation()) | ||
progress_bar.update(1) | ||
Score.objects.bulk_create(scores_to_create) |
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
44 changes: 44 additions & 0 deletions
44
profiles/migrations/0019_remove_score_unique_score_score_mutation_and_more.py
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,44 @@ | ||
# Generated by Django 4.2.13 on 2024-06-09 12:59 | ||
|
||
import django.db.models.deletion | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
( | ||
"profiles", | ||
"0018_remove_difficultycalculation_unique_difficulty_calculation_and_more", | ||
), | ||
] | ||
|
||
operations = [ | ||
migrations.RemoveConstraint( | ||
model_name="score", | ||
name="unique_score", | ||
), | ||
migrations.AddField( | ||
model_name="score", | ||
name="mutation", | ||
field=models.IntegerField(default=0), | ||
preserve_default=False, | ||
), | ||
migrations.AddField( | ||
model_name="score", | ||
name="original_score", | ||
field=models.ForeignKey( | ||
blank=True, | ||
null=True, | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="mutations", | ||
to="profiles.score", | ||
), | ||
), | ||
migrations.AddConstraint( | ||
model_name="score", | ||
constraint=models.UniqueConstraint( | ||
fields=("user_stats_id", "date", "mutation"), name="unique_score" | ||
), | ||
), | ||
] |
Oops, something went wrong.