Skip to content

Commit

Permalink
Merge pull request #45 from Syriiin/use-batch-calculations
Browse files Browse the repository at this point in the history
Use diffcalc to use batch calculations and return all values
  • Loading branch information
Syriiin authored May 8, 2024
2 parents ba1b488 + 5e80d8d commit 4228fde
Show file tree
Hide file tree
Showing 8 changed files with 500 additions and 299 deletions.
19 changes: 10 additions & 9 deletions common/osu/difficultycalculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class Score(NamedTuple):


class Calculation(NamedTuple):
difficulty: float
performance: float
difficulty_values: dict[str, float]
performance_values: dict[str, float]


class DifficultyCalculatorException(Exception):
Expand Down Expand Up @@ -90,7 +90,8 @@ def calculate_score(self, score: Score) -> Calculation:

self.calculate()
return Calculation(
difficulty=self.difficulty_total, performance=self.performance_total
difficulty_values={"total": self.difficulty_total},
performance_values={"total": self.performance_total},
)

def calculate_score_batch(self, scores: Iterable[Score]) -> list[Calculation]:
Expand Down Expand Up @@ -328,12 +329,12 @@ def calculate_score(self, score: Score) -> Calculation:
data = response.json()
except httpx.HTTPStatusError as e:
raise CalculationException(
f"An error occured in calculating the beatmap {score.beatmap_id}"
f"An error occured in calculating the beatmap {score.beatmap_id}: {e.response.text}"
) from e

return Calculation(
difficulty=data["difficulty"]["total"],
performance=data["performance"]["total"],
difficulty_values=data["difficulty"],
performance_values=data["performance"],
)

def calculate_score_batch(self, scores: Iterable[Score]) -> list[Calculation]:
Expand All @@ -346,13 +347,13 @@ def calculate_score_batch(self, scores: Iterable[Score]) -> list[Calculation]:
data = response.json()
except httpx.HTTPStatusError as e:
raise CalculationException(
f"An error occured in calculating the beatmaps"
f"An error occured in calculating the beatmaps: {e.response.text}"
) from e

return [
Calculation(
difficulty=calculation_data["difficulty"]["total"],
performance=calculation_data["performance"]["total"],
difficulty_values=calculation_data["difficulty"],
performance_values=calculation_data["performance"],
)
for calculation_data in data
]
Expand Down
112 changes: 99 additions & 13 deletions common/osu/test_difficultycalculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def test_calculate_score(self):
combo=2000,
)
assert calc.calculate_score(score) == Calculation(
difficulty=5.919765949249268, performance=298.1595153808594
difficulty_values={"total": 5.919765949249268},
performance_values={"total": 298.1595153808594},
)

def test_calculate_score_batch(self):
Expand Down Expand Up @@ -71,9 +72,18 @@ def test_calculate_score_batch(self):
),
]
assert calc.calculate_score_batch(scores) == [
Calculation(difficulty=5.919765949249268, performance=298.1595153808594),
Calculation(difficulty=6.20743465423584, performance=476.4307861328125),
Calculation(difficulty=6.20743465423584, performance=630.419677734375),
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
Expand Down Expand Up @@ -148,7 +158,8 @@ def test_calculate_score(self):
combo=2000,
)
assert calc.calculate_score(score) == Calculation(
difficulty=6.264344677869616, performance=312.43705315450256
difficulty_values={"total": 6.264344677869616},
performance_values={"total": 312.43705315450256},
)

def test_calculate_score_batch(self):
Expand Down Expand Up @@ -176,9 +187,18 @@ def test_calculate_score_batch(self):
),
]
assert calc.calculate_score_batch(scores) == [
Calculation(difficulty=6.264344677869616, performance=312.43705315450256),
Calculation(difficulty=6.531051472171891, performance=487.5904861756349),
Calculation(difficulty=6.531051472171891, performance=655.9388807525456),
Calculation(
difficulty_values={"total": 6.264344677869616},
performance_values={"total": 312.43705315450256},
),
Calculation(
difficulty_values={"total": 6.531051472171891},
performance_values={"total": 487.5904861756349},
),
Calculation(
difficulty_values={"total": 6.531051472171891},
performance_values={"total": 655.9388807525456},
),
]

@pytest.fixture
Expand Down Expand Up @@ -234,7 +254,19 @@ def test_version(self):
def test_context_manager(self):
with DifficalcyOsuDifficultyCalculator() as calc:
assert calc.calculate_score(Score("307618")) == Calculation(
difficulty=4.4569433791337945, performance=135.0040504515237
difficulty_values={
"aim": 2.08629357857818,
"speed": 2.1778593015565684,
"flashlight": 0,
"total": 4.4569433791337945,
},
performance_values={
"aim": 44.12278272319251,
"speed": 50.54174287197802,
"accuracy": 36.07670429437059,
"flashlight": 0,
"total": 135.0040504515237,
},
)

def test_invalid_beatmap(self):
Expand All @@ -253,7 +285,19 @@ def test_calculate_score(self):
combo=2000,
)
assert calc.calculate_score(score) == Calculation(
difficulty=6.263707394408435, performance=312.36671287580185
difficulty_values={
"aim": 2.892063051954271,
"speed": 3.0958487396004704,
"flashlight": 0,
"total": 6.263707394408435,
},
performance_values={
"aim": 98.6032935956297,
"speed": 118.92511309917593,
"accuracy": 84.96884392557897,
"flashlight": 0,
"total": 312.36671287580185,
},
)

def test_calculate_score_batch(self):
Expand Down Expand Up @@ -281,7 +325,49 @@ def test_calculate_score_batch(self):
),
]
assert calc.calculate_score_batch(scores) == [
Calculation(difficulty=6.263707394408435, performance=312.36671287580185),
Calculation(difficulty=6.530286188377548, performance=487.4810004992573),
Calculation(difficulty=6.530286188377548, performance=655.7872855036575),
Calculation(
difficulty_values={
"aim": 2.892063051954271,
"speed": 3.0958487396004704,
"flashlight": 0,
"total": 6.263707394408435,
},
performance_values={
"aim": 98.6032935956297,
"speed": 118.92511309917593,
"accuracy": 84.96884392557897,
"flashlight": 0,
"total": 312.36671287580185,
},
),
Calculation(
difficulty_values={
"aim": 3.1381340530266333,
"speed": 3.1129549941521066,
"flashlight": 0,
"total": 6.530286188377548,
},
performance_values={
"aim": 153.058022351103,
"speed": 153.10941688245896,
"accuracy": 166.32370945374015,
"flashlight": 0,
"total": 487.4810004992573,
},
),
Calculation(
difficulty_values={
"aim": 3.1381340530266333,
"speed": 3.1129549941521066,
"flashlight": 0,
"total": 6.530286188377548,
},
performance_values={
"aim": 207.5808620241847,
"speed": 215.2746980112218,
"accuracy": 212.8087296294707,
"flashlight": 0,
"total": 655.7872855036575,
},
),
]
Loading

0 comments on commit 4228fde

Please sign in to comment.