From 2ccd06cf8d6d523f0cb41ad489a1fa72e6ffbaac Mon Sep 17 00:00:00 2001 From: zkldi <20380519+zkldi@users.noreply.github.com> Date: Tue, 30 Nov 2021 16:57:28 +0000 Subject: [PATCH] Make sure the poyashi script recalcs iidx profiles. --- server/src/lib/jobs/update-bpi-poyashi.ts | 3 ++ .../calculations/recalc-game-profiles.ts | 37 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 server/src/utils/calculations/recalc-game-profiles.ts diff --git a/server/src/lib/jobs/update-bpi-poyashi.ts b/server/src/lib/jobs/update-bpi-poyashi.ts index a232fdbc0..fbdc37151 100644 --- a/server/src/lib/jobs/update-bpi-poyashi.ts +++ b/server/src/lib/jobs/update-bpi-poyashi.ts @@ -3,6 +3,7 @@ import db from "external/mongo/db"; import CreateLogCtx from "lib/logger/logger"; import fetch from "node-fetch"; import { ChartDocument, Difficulties, IIDXBPIData } from "tachi-common"; +import { RecalcGameProfiles } from "utils/calculations/recalc-game-profiles"; import { RecalcAllScores } from "utils/calculations/recalc-scores"; import { RecalcSessions } from "utils/calculations/recalc-sessions"; import { FindChartWithPTDFVersion } from "utils/queries/charts"; @@ -128,6 +129,8 @@ async function UpdatePoyashiData() { // @optimisable -- can be filtered to just scores that were recalced. await RecalcSessions({ game: "iidx" }); + await RecalcGameProfiles({ game: "iidx" }); + logger.info(`Done.`); } diff --git a/server/src/utils/calculations/recalc-game-profiles.ts b/server/src/utils/calculations/recalc-game-profiles.ts new file mode 100644 index 000000000..6f10b4177 --- /dev/null +++ b/server/src/utils/calculations/recalc-game-profiles.ts @@ -0,0 +1,37 @@ +/* eslint-disable no-await-in-loop */ + +import db from "external/mongo/db"; +import CreateLogCtx from "lib/logger/logger"; +import { CreateSessionCalcData } from "lib/score-import/framework/sessions/calculated-data"; +import { UpdateUsersGamePlaytypeStats } from "lib/score-import/framework/user-game-stats/update-ugs"; +import { FormatUserDoc } from "utils/user"; + +const logger = CreateLogCtx(__filename); + +export async function RecalcGameProfiles(filter = {}) { + const profiles = await db["game-stats"].find(filter); + + for (const profile of profiles) { + const user = await db.users.findOne({ + id: profile.userID, + }); + + if (!user) { + logger.severe(`User ${profile.userID} does not exist?`); + throw new Error(`User ${profile.userID} does not exist.`); + } + + logger.verbose( + `Recalcing ${FormatUserDoc(user)}'s ${profile.game} ${profile.playtype} stats.` + ); + await UpdateUsersGamePlaytypeStats( + profile.game, + profile.playtype, + profile.userID, + null, + logger + ); + } + + logger.info(`Done.`); +}