refactor: calculate.py

This commit is contained in:
283375 2023-08-24 23:16:34 +08:00
parent 2d4135fdfe
commit a58f97f61b
Signed by: 283375
SSH Key Fingerprint: SHA256:UcX0qg6ZOSDOeieKPGokA5h7soykG61nz2uxuQgVLSk

View File

@ -2,11 +2,11 @@ from decimal import Decimal
from math import floor
from typing import Dict, List
from .models import Calculated, Chart, Score
from .models.scores import Calculated
def calculate_score_range(chart: Chart, pure: int, far: int):
single_note_score = 10000000 / Decimal(chart.note)
def calculate_score_range(notes: int, pure: int, far: int):
single_note_score = 10000000 / Decimal(notes)
actual_score = floor(
single_note_score * pure + single_note_score * Decimal(0.5) * far
@ -14,41 +14,20 @@ def calculate_score_range(chart: Chart, pure: int, far: int):
return (actual_score, actual_score + pure)
def calculate_score(chart: Chart, score: Score) -> Calculated:
assert chart.song_id == score.song_id
assert chart.rating_class == score.rating_class
single_note_score = 10000000 / Decimal(chart.note)
actual_score = (
single_note_score * score.pure + single_note_score * Decimal(0.5) * score.far
)
pure_small = score.score - floor(actual_score)
constant = Decimal(chart.rating) / 10
if score.score >= 10000000:
potential = constant + 2
elif score.score >= 9800000:
potential = constant + 1 + (Decimal(score.score - 9800000) / 200000)
def calculate_potential(_constant: float, score: int) -> Decimal:
constant = Decimal(_constant)
if score >= 10000000:
return constant + 2
elif score >= 9800000:
return constant + 1 + (Decimal(score - 9800000) / 200000)
else:
potential = max(
Decimal(0), constant + (Decimal(score.score - 9500000) / 300000)
)
assert isinstance(potential, Decimal)
return max(Decimal(0), constant + (Decimal(score - 9500000) / 300000))
return Calculated(
id=score.id,
song_id=chart.song_id,
rating_class=chart.rating_class,
score=score.score,
pure=score.pure,
far=score.far,
lost=score.lost,
time=score.time,
rating=chart.rating,
note=chart.note,
pure_small=pure_small,
potential=float(potential),
)
def calculate_shiny_pure(notes: int, score: int, pure: int, far: int) -> int:
single_note_score = 10000000 / Decimal(notes)
actual_score = single_note_score * pure + single_note_score * Decimal(0.5) * far
return score - floor(actual_score)
def get_b30_calculated_list(calculated_list: List[Calculated]) -> List[Calculated]:
@ -67,33 +46,3 @@ def calculate_b30(calculated_list: List[Calculated]) -> Decimal:
ptt_list = [Decimal(c.potential) for c in get_b30_calculated_list(calculated_list)]
sum_ptt_list = sum(ptt_list)
return (sum_ptt_list / len(ptt_list)) if sum_ptt_list else Decimal("0.0")
# def get_r10_calculated_list(calculated_list: List[Calculated]) -> List[Calculated]:
# recent_scores: Dict[str, Calculated] = {}
# for calculated in calculated_list:
# key = f"{calculated.song_id}_{calculated.rating_class}"
# stored = recent_scores.get(key)
# if stored is None or stored.time < calculated.time:
# recent_scores[key] = calculated
# ret_list = list(recent_scores.values())
# ret_list = sorted(ret_list, key=lambda c: c.time, reverse=True)[:10]
# return ret_list
# def calculate_r10(calculated_list: List[Calculated]) -> Decimal:
# ptt_list = [Decimal(c.potential) for c in get_r10_calculated_list(calculated_list)]
# sum_ptt_list = sum(ptt_list)
# return (sum_ptt_list / len(ptt_list)) if sum_ptt_list else Decimal("0.0")
# def calculate_potential(calculated_list: List[Calculated]) -> Decimal:
# b30_ptt_list = [
# Decimal(c.potential) for c in get_b30_calculated_list(calculated_list)
# ]
# r10_ptt_list = [
# Decimal(c.potential) for c in get_r10_calculated_list(calculated_list)
# ]
# b30_sum = sum(b30_ptt_list) or Decimal("0.0")
# r10_sum = sum(r10_ptt_list) or Decimal("0.0")
# return (b30_sum + r10_sum) / (len(b30_ptt_list) + len(r10_ptt_list))