Skip to content

Commit

Permalink
Update score_filter json mods to be acronym arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
Syriiin committed Dec 21, 2024
1 parent 974239c commit e1ce8e1
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 41 deletions.
5 changes: 5 additions & 0 deletions common/osu/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@ def get_json_mods(mods: int, add_classic: bool) -> dict:
return mods_dict


def get_mod_acronyms(mods: int) -> list[str]:
json_mods = get_json_mods(mods, add_classic=False)
return list(json_mods.keys())


def get_bitwise_mods(acronyms: list[str]) -> int:
bitwise_mods = 0
for acronym in acronyms:
Expand Down
2 changes: 1 addition & 1 deletion conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def score(user_stats: UserStats, beatmap: Beatmap):
@pytest.fixture
def score_filter():
return ScoreFilter.objects.create(
required_mods=Mods.HIDDEN, required_mods_json={NewMods.HIDDEN: {}}
required_mods=Mods.HIDDEN, required_mods_json=[NewMods.HIDDEN]
)


Expand Down
13 changes: 5 additions & 8 deletions leaderboards/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from collections import OrderedDict

from django.core.cache import cache
from django.core.exceptions import ValidationError
from django.core.validators import URLValidator
from rest_framework import permissions, status
Expand All @@ -10,7 +9,7 @@

from common.osu.difficultycalculator import get_default_difficulty_calculator_class
from common.osu.enums import Gamemode, Mods
from common.osu.utils import get_json_mods
from common.osu.utils import get_mod_acronyms
from common.utils import parse_int_or_none
from leaderboards.enums import LeaderboardAccessType
from leaderboards.models import Invite, Leaderboard, Membership
Expand Down Expand Up @@ -155,14 +154,12 @@ def post(self, request, leaderboard_type, gamemode):
lowest_cs=score_filter_data.get("lowest_cs"),
highest_cs=score_filter_data.get("highest_cs"),
required_mods=score_filter_data.get("required_mods", Mods.NONE),
required_mods_json=get_json_mods(
score_filter_data.get("required_mods", Mods.NONE),
add_classic=False,
required_mods_json=get_mod_acronyms(
score_filter_data.get("required_mods", Mods.NONE)
),
disqualified_mods=score_filter_data.get("disqualified_mods", Mods.NONE),
disqualified_mods_json=get_json_mods(
score_filter_data.get("disqualified_mods", Mods.NONE),
add_classic=False,
disqualified_mods_json=get_mod_acronyms(
score_filter_data.get("disqualified_mods", Mods.NONE)
),
lowest_accuracy=score_filter_data.get("lowest_accuracy"),
highest_accuracy=score_filter_data.get("highest_accuracy"),
Expand Down
10 changes: 5 additions & 5 deletions profiles/management/commands/backfilljsonmods.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.core.management.base import BaseCommand
from tqdm import tqdm

from common.osu.utils import get_json_mods
from common.osu.utils import get_mod_acronyms
from profiles.models import ScoreFilter


Expand All @@ -11,10 +11,10 @@ class Command(BaseCommand):
def handle(self, *args, **options):
score_filters = ScoreFilter.objects.all()
for score_filter in tqdm(score_filters):
score_filter.required_mods_json = get_json_mods(
score_filter.required_mods, False
score_filter.required_mods_json = get_mod_acronyms(
score_filter.required_mods
)
score_filter.disqualified_mods_json = get_json_mods(
score_filter.disqualified_mods, False
score_filter.disqualified_mods_json = get_mod_acronyms(
score_filter.disqualified_mods
)
score_filter.save()
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 5.1.4 on 2024-12-21 05:27

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("profiles", "0026_scorefilter_disqualified_mods_json_and_more"),
]

operations = [
migrations.AlterField(
model_name="scorefilter",
name="disqualified_mods_json",
field=models.JSONField(blank=True, default=list),
),
migrations.AlterField(
model_name="scorefilter",
name="required_mods_json",
field=models.JSONField(blank=True, default=list),
),
]
4 changes: 2 additions & 2 deletions profiles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,9 +626,9 @@ class ScoreFilter(models.Model):
lowest_cs = models.FloatField(null=True, blank=True)
highest_cs = models.FloatField(null=True, blank=True)
required_mods = models.IntegerField(default=Mods.NONE)
required_mods_json = models.JSONField(default=dict, blank=True)
required_mods_json = models.JSONField(default=list, blank=True)
disqualified_mods = models.IntegerField(default=Mods.NONE)
disqualified_mods_json = models.JSONField(default=dict, blank=True)
disqualified_mods_json = models.JSONField(default=list, blank=True)
lowest_accuracy = models.FloatField(null=True, blank=True)
highest_accuracy = models.FloatField(null=True, blank=True)
lowest_length = models.FloatField(null=True, blank=True)
Expand Down
16 changes: 4 additions & 12 deletions profiles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from rest_framework.views import APIView

from common.osu.enums import Gamemode, Mods
from common.osu.utils import get_json_mods
from common.osu.utils import get_json_mods, get_mod_acronyms
from common.utils import parse_float_or_none, parse_int_or_none
from leaderboards.models import Membership
from leaderboards.serialisers import UserMembershipSerialiser
Expand Down Expand Up @@ -121,21 +121,13 @@ def get(self, request, user_id, gamemode):
highest_cs=parse_float_or_none(request.query_params.get("highest_cs")),
required_mods=required_mods,
required_mods_json=(
get_json_mods(
required_mods,
add_classic=False,
)
if required_mods is not None
else {}
get_mod_acronyms(required_mods) if required_mods is not None else []
),
disqualified_mods=disqualified_mods,
disqualified_mods_json=(
get_json_mods(
disqualified_mods,
add_classic=False,
)
get_mod_acronyms(disqualified_mods)
if disqualified_mods is not None
else {}
else []
),
lowest_accuracy=parse_float_or_none(
request.query_params.get("lowest_accuracy")
Expand Down
8 changes: 4 additions & 4 deletions users/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ def test_post(self, arf, view, user):
assert response.status_code == HTTPStatus.OK
assert response.data["name"] == "test sudden death filter"
assert response.data["score_filter"]["required_mods"] == Mods.SUDDEN_DEATH
assert response.data["score_filter"]["required_mods_json"] == {
NewMods.SUDDEN_DEATH: {}
}
assert response.data["score_filter"]["required_mods_json"] == [
NewMods.SUDDEN_DEATH
]


@pytest.mark.django_db
Expand Down Expand Up @@ -127,7 +127,7 @@ def test_put(self, arf, view, user, score_filter_preset):
assert response.status_code == HTTPStatus.OK
assert response.data["name"] == "new name"
assert response.data["score_filter"]["required_mods"] == Mods.NONE
assert response.data["score_filter"]["required_mods_json"] == {}
assert response.data["score_filter"]["required_mods_json"] == []
assert response.data["score_filter"]["highest_ar"] == 8

def test_delete(self, arf, view, user, score_filter_preset):
Expand Down
16 changes: 7 additions & 9 deletions users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from rest_framework.views import APIView

from common.osu.enums import Mods
from common.osu.utils import get_json_mods
from common.osu.utils import get_json_mods, get_mod_acronyms
from leaderboards.models import Invite
from leaderboards.serialisers import UserInviteSerialiser
from osuauth.serialisers import UserSerialiser
Expand Down Expand Up @@ -92,14 +92,12 @@ def post(self, request):
lowest_cs=score_filter_data.get("lowest_cs"),
highest_cs=score_filter_data.get("highest_cs"),
required_mods=score_filter_data.get("required_mods", Mods.NONE),
required_mods_json=get_json_mods(
score_filter_data.get("required_mods", Mods.NONE),
add_classic=False,
required_mods_json=get_mod_acronyms(
score_filter_data.get("required_mods", Mods.NONE)
),
disqualified_mods=score_filter_data.get("disqualified_mods", Mods.NONE),
disqualified_mods_json=get_json_mods(
score_filter_data.get("disqualified_mods", Mods.NONE),
add_classic=False,
disqualified_mods_json=get_mod_acronyms(
score_filter_data.get("disqualified_mods", Mods.NONE)
),
lowest_accuracy=score_filter_data.get("lowest_accuracy"),
highest_accuracy=score_filter_data.get("highest_accuracy"),
Expand Down Expand Up @@ -164,13 +162,13 @@ def put(self, request, score_filter_preset_id):
score_filter.required_mods = score_filter_data.get("required_mods", Mods.NONE)
score_filter.required_mods_json = get_json_mods(
score_filter_data.get("required_mods", Mods.NONE), add_classic=False
)
).keys()
score_filter.disqualified_mods = score_filter_data.get(
"disqualified_mods", Mods.NONE
)
score_filter.disqualified_mods_json = get_json_mods(
score_filter_data.get("disqualified_mods", Mods.NONE), add_classic=False
)
).keys()
score_filter.lowest_accuracy = score_filter_data.get("lowest_accuracy")
score_filter.highest_accuracy = score_filter_data.get("highest_accuracy")
score_filter.lowest_length = score_filter_data.get("lowest_length")
Expand Down

0 comments on commit e1ce8e1

Please sign in to comment.