Skip to content

Commit

Permalink
Remove oppai difficulty calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
Syriiin committed Aug 23, 2024
1 parent 16adbac commit 1f7b330
Show file tree
Hide file tree
Showing 6 changed files with 3 additions and 248 deletions.
70 changes: 0 additions & 70 deletions common/osu/difficultycalculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@
from typing import Iterable, NamedTuple, Optional, Type

import httpx
import oppaipy
from django.conf import settings
from django.utils.module_loading import import_string
from oppaipy.oppaipy import OppaiError

from common.osu.beatmap_provider import BeatmapNotFoundException, BeatmapProvider
from common.osu.enums import Gamemode

OPPAIPY_VERSION = metadata.version("oppaipy")

# TODO: lazy load this instead of doing at import
difficalcy_osu_info = httpx.get(f"{settings.DIFFICALCY_OSU_URL}/api/info").json()
difficalcy_taiko_info = httpx.get(f"{settings.DIFFICALCY_TAIKO_URL}/api/info").json()
Expand Down Expand Up @@ -175,72 +171,6 @@ def gamemode() -> Gamemode:
raise NotImplementedError()


class OppaiDifficultyCalculator(AbstractDifficultyCalculator):
def __init__(self):
super().__init__()

self.oppai_calc = None
self.beatmap_path = None

def _close(self):
if self.oppai_calc is not None:
self.oppai_calc.close()

def _reset(self):
if self.oppai_calc is not None:
self.oppai_calc.reset()

def set_beatmap(self, beatmap_id: str) -> None:
beatmap_provider = BeatmapProvider()
try:
self.beatmap_path = beatmap_provider.get_beatmap_file(beatmap_id)
except BeatmapNotFoundException as e:
raise InvalidBeatmapException(
f"An error occured in fetching the beatmap {beatmap_id}"
) from e
self.oppai_calc = oppaipy.Calculator(self.beatmap_path)

def set_accuracy(self, count_100: int, count_50: int):
self.oppai_calc.set_accuracy(count_100, count_50)

def set_misses(self, count_miss: int):
self.oppai_calc.set_misses(count_miss)

def set_combo(self, combo: int):
self.oppai_calc.set_combo(combo)

def set_mods(self, mods: int):
self.oppai_calc.set_mods(mods)

def _calculate(self):
try:
self.oppai_calc.calculate()
except OppaiError as e:
raise CalculationException(
f'An error occured in calculating the beatmap "{self.beatmap_path}"'
) from e

@property
def difficulty_total(self):
return self.oppai_calc.stars

@property
def performance_total(self):
return self.oppai_calc.pp

@staticmethod
def engine():
return "oppaipy"

@staticmethod
def version():
return OPPAIPY_VERSION

@staticmethod
def gamemode():
return Gamemode.STANDARD


class AbstractDifficalcyDifficultyCalculator(AbstractDifficultyCalculator):
def __init__(self):
super().__init__()
Expand Down
119 changes: 0 additions & 119 deletions common/osu/test_difficultycalculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,135 +3,16 @@
from common.osu.difficultycalculator import (
Calculation,
CalculationException,
CalculatorClosedException,
DifficalcyCatchDifficultyCalculator,
DifficalcyManiaDifficultyCalculator,
DifficalcyOsuDifficultyCalculator,
DifficalcyPerformancePlusDifficultyCalculator,
DifficalcyTaikoDifficultyCalculator,
InvalidBeatmapException,
OppaiDifficultyCalculator,
Score,
)
from common.osu.enums import Mods


class TestOppaiDifficultyCalculator:
def test_enigne(self):
assert OppaiDifficultyCalculator.engine() == "oppaipy"

def test_version(self):
assert OppaiDifficultyCalculator.version() == "1.0.4"

def test_context_manager(self):
with OppaiDifficultyCalculator() as calc:
calc.set_beatmap("307618")
calc.calculate()
assert calc.difficulty_total == 4.200401306152344

def test_invalid_beatmap(self):
with pytest.raises(InvalidBeatmapException):
with OppaiDifficultyCalculator() as calc:
calc.set_beatmap("notarealbeatmap")
calc.calculate()

def test_calculate_score(self):
calc = OppaiDifficultyCalculator()
score = Score(
"307618",
mods=int(Mods.DOUBLETIME + Mods.HIDDEN),
count_100=14,
count_50=1,
count_miss=1,
combo=2000,
)
assert calc.calculate_score(score) == Calculation(
difficulty_values={"total": 5.919765949249268},
performance_values={"total": 298.1595153808594},
)

def test_calculate_score_batch(self):
calc = OppaiDifficultyCalculator()
scores = [
Score(
"307618",
mods=int(Mods.DOUBLETIME + Mods.HIDDEN),
count_100=14,
count_50=1,
count_miss=1,
combo=2000,
),
Score(
"307618",
mods=int(Mods.DOUBLETIME + Mods.HIDDEN + Mods.HARDROCK),
count_100=14,
count_50=1,
count_miss=1,
combo=2000,
),
Score(
"307618",
mods=int(Mods.DOUBLETIME + Mods.HIDDEN + Mods.HARDROCK),
),
]
assert calc.calculate_score_batch(scores) == [
Calculation(
difficulty_values={"total": 5.919765949249268},
performance_values={"total": 298.1595153808594},
),
Calculation(
difficulty_values={"total": 6.20743465423584},
performance_values={"total": 476.4307861328125},
),
Calculation(
difficulty_values={"total": 6.20743465423584},
performance_values={"total": 630.419677734375},
),
]

@pytest.fixture
def calc(self):
calc = OppaiDifficultyCalculator()
calc.set_beatmap("307618")
calc.calculate()
return calc

def test_difficulty_total(self, calc: OppaiDifficultyCalculator):
assert calc.difficulty_total == 4.200401306152344

def test_performance_total(self, calc: OppaiDifficultyCalculator):
assert calc.performance_total == 126.96746063232422

def test_close(self, calc: OppaiDifficultyCalculator):
calc.close()
with pytest.raises(CalculatorClosedException):
calc.calculate()

def test_set_accuracy(self, calc: OppaiDifficultyCalculator):
calc.set_accuracy(14, 1)
calc.calculate()
assert calc.difficulty_total == 4.200401306152344
assert calc.performance_total == 118.77462005615234

def test_set_misses(self, calc: OppaiDifficultyCalculator):
calc.set_misses(1)
calc.calculate()
assert calc.difficulty_total == 4.200401306152344
assert calc.performance_total == 123.09051513671875

def test_set_combo(self, calc: OppaiDifficultyCalculator):
calc.set_combo(2000)
calc.calculate()
assert calc.difficulty_total == 4.200401306152344
assert calc.performance_total == 106.42977142333984

def test_set_mods(self, calc: OppaiDifficultyCalculator):
calc.set_mods(Mods.DOUBLETIME + Mods.HIDDEN)
calc.calculate()
assert calc.difficulty_total == 5.919765949249268
assert calc.performance_total == 397.2521057128906


class TestDifficalcyDifficultyCalculator:
def test_context_manager(self):
with DifficalcyOsuDifficultyCalculator() as calc:
Expand Down
1 change: 0 additions & 1 deletion osuchan/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,6 @@ class EnvSettings(BaseSettings):
# Difficulty calculation

DIFFICULTY_CALCULATOR_CLASSES = {
"oppai": "common.osu.difficultycalculator.OppaiDifficultyCalculator",
"difficalcy-osu": "common.osu.difficultycalculator.DifficalcyOsuDifficultyCalculator",
"difficalcy-taiko": "common.osu.difficultycalculator.DifficalcyTaikoDifficultyCalculator",
"difficalcy-catch": "common.osu.difficultycalculator.DifficalcyCatchDifficultyCalculator",
Expand Down
56 changes: 1 addition & 55 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions profiles/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_refresh_user_from_api(self):
PerformanceCalculation.objects.filter(
score__user_stats_id=user_stats.id
).count()
== 18 # 6 scores (5 real, 1 nochoke mutation) * 3 calculators
== 12 # 6 scores (5 real, 1 nochoke mutation) * 2 calculators
)
assert user_stats.score_style_accuracy == 98.23948233070678
assert user_stats.score_style_bpm == 211.857710839314
Expand All @@ -55,7 +55,7 @@ def test_fetch_scores(self):
PerformanceCalculation.objects.filter(
score__user_stats_id=user_stats.id
).count()
== 54 # 18 scores (16 real, 2 nochoke mutation) * 3 calculators
== 36 # 18 scores (16 real, 2 nochoke mutation) * 2 calculators
)
assert user_stats.score_style_accuracy == 98.09793775692623
assert user_stats.score_style_bpm == 211.85490142989167
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ djangorestframework = "^3.12.4"
django-debug-toolbar = "^4.1.0"
gunicorn = "^22.0.0"
httpx = "^0.27.0"
oppaipy = "^1.0.4"
psycopg2-binary = "^2.9.1"
requests = "^2.25.1"
tqdm = "^4.64.0"
Expand Down

0 comments on commit 1f7b330

Please sign in to comment.