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 math import floor
from typing import Dict, List 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): def calculate_score_range(notes: int, pure: int, far: int):
single_note_score = 10000000 / Decimal(chart.note) single_note_score = 10000000 / Decimal(notes)
actual_score = floor( actual_score = floor(
single_note_score * pure + single_note_score * Decimal(0.5) * far 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) return (actual_score, actual_score + pure)
def calculate_score(chart: Chart, score: Score) -> Calculated: def calculate_potential(_constant: float, score: int) -> Decimal:
assert chart.song_id == score.song_id constant = Decimal(_constant)
assert chart.rating_class == score.rating_class if score >= 10000000:
return constant + 2
single_note_score = 10000000 / Decimal(chart.note) elif score >= 9800000:
actual_score = ( return constant + 1 + (Decimal(score - 9800000) / 200000)
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)
else: else:
potential = max( return max(Decimal(0), constant + (Decimal(score - 9500000) / 300000))
Decimal(0), constant + (Decimal(score.score - 9500000) / 300000)
)
assert isinstance(potential, Decimal)
return Calculated(
id=score.id, def calculate_shiny_pure(notes: int, score: int, pure: int, far: int) -> int:
song_id=chart.song_id, single_note_score = 10000000 / Decimal(notes)
rating_class=chart.rating_class, actual_score = single_note_score * pure + single_note_score * Decimal(0.5) * far
score=score.score, return score - floor(actual_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 get_b30_calculated_list(calculated_list: List[Calculated]) -> List[Calculated]: 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)] ptt_list = [Decimal(c.potential) for c in get_b30_calculated_list(calculated_list)]
sum_ptt_list = sum(ptt_list) sum_ptt_list = sum(ptt_list)
return (sum_ptt_list / len(ptt_list)) if sum_ptt_list else Decimal("0.0") 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))