From af1422a065cb1e191bf518b9d7ceb7000db42261 Mon Sep 17 00:00:00 2001 From: zkldi <20380519+zkldi@users.noreply.github.com> Date: Wed, 8 Dec 2021 23:51:28 +0000 Subject: [PATCH] Create a script for updating stale iidx percents. --- .../single-use/recalc-iidx-percents.ts | 51 +++++++++++++++++++ server/src/lib/score-mutation/update-score.ts | 2 - 2 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 server/scripts/single-use/recalc-iidx-percents.ts diff --git a/server/scripts/single-use/recalc-iidx-percents.ts b/server/scripts/single-use/recalc-iidx-percents.ts new file mode 100644 index 000000000..c31a6ac8b --- /dev/null +++ b/server/scripts/single-use/recalc-iidx-percents.ts @@ -0,0 +1,51 @@ +import db from "external/mongo/db"; +import CreateLogCtx from "lib/logger/logger"; +import { GetGradeFromPercent } from "lib/score-import/framework/common/score-utils"; +import UpdateScore from "lib/score-mutation/update-score"; +import { ChartDocument, GetGamePTConfig } from "tachi-common"; + +const logger = CreateLogCtx(__filename); + +if (require.main === module) { + (async () => { + let i = 0; + db.scores + .find({ game: "iidx" }) + // @ts-expect-error dumb + .each(async (oldScore, { pause, resume }) => { + pause(); + + const chart = (await db.charts.iidx.findOne({ + chartID: oldScore.chartID, + })) as ChartDocument<"iidx:SP">; + + if (!chart) { + logger.severe(`Chart ${oldScore.chartID} has no chart?`); + return resume(); + } + + const percent = (100 * oldScore.scoreData.score) / (chart.data.notecount * 2); + if (Math.abs(percent - oldScore.scoreData.percent) > 0.00001) { + const newScore = JSON.parse(JSON.stringify(oldScore)); + + newScore.scoreData.percent = percent; + newScore.scoreData.grade = GetGradeFromPercent( + "iidx", + oldScore.playtype, + percent + ); + newScore.scoreData.gradeIndex = GetGamePTConfig( + "iidx", + oldScore.playtype + ).grades.indexOf(newScore.scoreData.grade); + + await UpdateScore(oldScore, newScore); + + i++; + } + + resume(); + }) + .then(() => logger.info(`Fixed ${i} bad scores.`)); + })(); +} diff --git a/server/src/lib/score-mutation/update-score.ts b/server/src/lib/score-mutation/update-score.ts index 544a11ce6..d9a5f398d 100644 --- a/server/src/lib/score-mutation/update-score.ts +++ b/server/src/lib/score-mutation/update-score.ts @@ -54,8 +54,6 @@ export default async function UpdateScore(oldScore: ScoreDocument, newScore: Sco const logger = rootLogger.child({ context: ["Update Score", oldScore.scoreID, newScore.scoreID, FormatUserDoc(user)], - oldScore, - newScore, }) as KtLogger; logger.info("Received Update Score request.");