1
0
Fork 0
polaris-task-force/src/tools/seed/backfillOpordBestTimes.ts

86 lines
2.6 KiB
TypeScript

import "dotenv/config";
import { getPayload } from "payload";
import config from "@payload-config";
import { OPORD_DIFFICULTIES } from "../../lib/minigames/opord-recall";
// Usage: `bun run src/tools/seed/backfillOpordBestTimes.ts`.
// Backfills OPORD Recall best times introduced after the speed-bonus scoring
// change: any profile with a nonzero per-tier best score but no recorded time
// gets the tier's par time as the neutral placeholder, so pre-change players
// are accounted for on the leaderboard's Time column. Idempotent.
type Tier = "recruit" | "veteran" | "elite";
const TIERS: readonly Tier[] = ["recruit", "veteran", "elite"];
export const backfillOpordBestTimes = async () => {
const payload = await getPayload({ config });
payload.logger.info("Backfilling OPORD Recall best times...");
let updated = 0;
let page = 1;
const limit = 100;
for (;;) {
const res = await payload.find({
collection: "profiles",
page,
limit,
depth: 0,
overrideAccess: true,
});
for (const profile of res.docs) {
const progression = (profile.progression ?? {}) as {
minigames?: {
opordRecall?: Record<
| "recruitBestScore"
| "veteranBestScore"
| "eliteBestScore"
| "recruitBestTimeMs"
| "veteranBestTimeMs"
| "eliteBestTimeMs",
number | null | undefined
>;
};
};
const stats = progression.minigames?.opordRecall;
if (!stats) continue;
const patch: Partial<Record<"recruitBestTimeMs" | "veteranBestTimeMs" | "eliteBestTimeMs", number>> = {};
for (const tier of TIERS) {
const score = stats[`${tier}BestScore`] ?? 0;
const time = stats[`${tier}BestTimeMs`];
if (score > 0 && time == null) {
patch[`${tier}BestTimeMs`] = OPORD_DIFFICULTIES[tier].parTimeMs;
}
}
if (Object.keys(patch).length === 0) continue;
await payload.update({
collection: "profiles",
id: profile.id,
data: {
progression: {
...progression,
minigames: {
...(progression.minigames ?? {}),
opordRecall: {
...stats,
...patch,
},
},
},
},
overrideAccess: true,
depth: 0,
});
updated += 1;
}
if (page >= res.totalPages) break;
page += 1;
}
payload.logger.info("--- backfill summary ---");
payload.logger.info(`Profiles updated: ${updated}`);
};
await backfillOpordBestTimes();
process.exit(0);