refactor: smartrte b30 csv exporter

This commit is contained in:
283375 2024-09-28 18:36:27 +08:00
parent caced6eaec
commit eab2a3e520
Signed by: 283375
SSH Key Fingerprint: SHA256:UcX0qg6ZOSDOeieKPGokA5h7soykG61nz2uxuQgVLSk
3 changed files with 75 additions and 67 deletions

View File

@ -0,0 +1,75 @@
from typing import List, Tuple
from sqlalchemy import func
from sqlalchemy.orm import Session
from arcaea_offline.constants.enums.arcaea import ArcaeaRatingClass
from arcaea_offline.database.models.v5 import (
ChartInfo,
Difficulty,
PlayResultBest,
Song,
)
from arcaea_offline.utils.formatters.rating_class import RatingClassFormatter
class SmartRteBest30CsvExporter:
CSV_ROWS = [
"SongName",
"SongId",
"Difficulty",
"Score",
"Perfect",
"Perfect+",
"Far",
"Lost",
"Constant",
"PlayRating",
]
@classmethod
def rows(cls, session: Session) -> List:
results: List[
Tuple[str, str, ArcaeaRatingClass, int, int, int, int, int, float]
] = (
session.query(
func.coalesce(Difficulty.title, Song.title),
PlayResultBest.song_id,
PlayResultBest.rating_class,
PlayResultBest.score,
PlayResultBest.pure,
PlayResultBest.shiny_pure,
PlayResultBest.far,
PlayResultBest.lost,
ChartInfo.constant,
PlayResultBest.potential,
)
.join(
ChartInfo,
(ChartInfo.song_id == PlayResultBest.song_id)
& (ChartInfo.rating_class == PlayResultBest.rating_class),
)
.join(Song, (Song.id == PlayResultBest.song_id))
.join(
Difficulty,
(Difficulty.song_id == PlayResultBest.song_id)
& (Difficulty.rating_class == PlayResultBest.rating_class),
)
.all()
)
csv_rows = []
csv_rows.append(cls.CSV_ROWS.copy())
for _result in results:
result = list(_result)
# replace the comma in song title because the target project
# cannot handle quoted string
result[0] = result[0].replace(",", "") # type: ignore
result[2] = RatingClassFormatter.name(result[2]) # type: ignore
result[-2] = result[-2] / 10 # type: ignore
result[-1] = round(result[-1], 5) # type: ignore
csv_rows.append(result)
return csv_rows

View File

@ -1,3 +0,0 @@
from .b30_csv import SmartRteB30CsvConverter
__all__ = ["SmartRteB30CsvConverter"]

View File

@ -1,64 +0,0 @@
from sqlalchemy.orm import Session
from ...models import Chart, ScoreBest
from ...utils.rating import rating_class_to_text
class SmartRteB30CsvConverter:
CSV_ROWS = [
"songname",
"songId",
"Difficulty",
"score",
"Perfect",
"criticalPerfect",
"Far",
"Lost",
"Constant",
"singlePTT",
]
def __init__(
self,
session: Session,
):
self.session = session
def rows(self) -> list:
csv_rows = [self.CSV_ROWS.copy()]
with self.session as session:
results = (
session.query(
Chart.title,
ScoreBest.song_id,
ScoreBest.rating_class,
ScoreBest.score,
ScoreBest.pure,
ScoreBest.shiny_pure,
ScoreBest.far,
ScoreBest.lost,
Chart.constant,
ScoreBest.potential,
)
.join(
Chart,
(Chart.song_id == ScoreBest.song_id)
& (Chart.rating_class == ScoreBest.rating_class),
)
.all()
)
for result in results:
# replace the comma in song title because the target project
# cannot handle quoted string
result = list(result)
result[0] = result[0].replace(",", "")
result[2] = rating_class_to_text(result[2])
# divide constant to float
result[-2] = result[-2] / 10
# round potential
result[-1] = round(result[-1], 5)
csv_rows.append(result)
return csv_rows