From 89b36bfc22b2f9d999fa66d305278035cda8e200 Mon Sep 17 00:00:00 2001 From: zkldi Date: Tue, 7 Sep 2021 12:47:19 +0100 Subject: [PATCH] Add quickinsert and destroy scripts --- server/.gitignore | 4 +- server/scripts/quick-insert/quick-insert.ts | 21 +++++ .../destroy-versionbleed-scores/destroy.ts | 76 +++++++++++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 server/scripts/quick-insert/quick-insert.ts create mode 100644 server/scripts/single-use/destroy-versionbleed-scores/destroy.ts diff --git a/server/.gitignore b/server/.gitignore index fbffad7ab..3cfeefdeb 100644 --- a/server/.gitignore +++ b/server/.gitignore @@ -30,4 +30,6 @@ local-cdn test-cdn # Weird Redis Dumps -dump.rdb \ No newline at end of file +dump.rdb + +scripts/quick-insert/insert.ts \ No newline at end of file diff --git a/server/scripts/quick-insert/quick-insert.ts b/server/scripts/quick-insert/quick-insert.ts new file mode 100644 index 000000000..ec6b7c994 --- /dev/null +++ b/server/scripts/quick-insert/quick-insert.ts @@ -0,0 +1,21 @@ +import deepmerge from "deepmerge"; +import { ICollection } from "monk"; +import { ChartDocument, IDStrings } from "tachi-common"; +import { Random20Hex } from "utils/misc"; + +export async function QuickInsert( + base: ChartDocument, + content: Partial>[], + coll: ICollection +) { + const docs = []; + + for (const c of content) { + const doc = deepmerge(base, c); + + doc.chartID = Random20Hex(); + docs.push(doc); + } + + await coll.insert(docs); +} diff --git a/server/scripts/single-use/destroy-versionbleed-scores/destroy.ts b/server/scripts/single-use/destroy-versionbleed-scores/destroy.ts new file mode 100644 index 000000000..352a9153b --- /dev/null +++ b/server/scripts/single-use/destroy-versionbleed-scores/destroy.ts @@ -0,0 +1,76 @@ +/* eslint-disable no-await-in-loop */ +import db from "external/mongo/db"; +import CreateLogCtx from "lib/logger/logger"; +import { CreatePBDoc, UpdateChartRanking } from "lib/score-import/framework/pb/create-pb-doc"; + +const logger = CreateLogCtx(__filename); + +(async () => { + // in an alternate timeline this would be 2020 10 02, + // but i have to account for *those* idiots. + // who now mean i'm shredding other people scores. + // - not zkldi + const hvSt = new Date(2020, 9, 1); + + const charts = await db.charts.iidx.find({ + songID: 167, // 761 -> rising in the sun; 167 -> sometime. + isPrimary: false, + }); + + const chartIDs = charts.map((e) => e.chartID); + + const affectedUsers = await db.scores.find({ + chartID: { $in: chartIDs }, + timeAchieved: { + $gt: hvSt.getTime(), + }, + }); + + const userIDs = affectedUsers.map((e) => e.userID); + + const r = await db.scores.remove({ + chartID: { $in: chartIDs }, + timeAchieved: { + $gt: hvSt.getTime(), + }, + }); + + logger.info(`Destroyed scores.`, { r }); + + for (const userID of userIDs) { + logger.info(`Reprocessing PBs for ${userID}`); + + for (const chartID of chartIDs) { + const scores = await db.scores.find({ + chartID, + userID, + }); + + if (scores.length === 0) { + await db["personal-bests"].remove({ + chartID, + userID, + }); + } else { + const pbDoc = await CreatePBDoc(userID, chartID, logger); + + await db["personal-bests"].update( + { + chartID, + userID, + }, + { + $set: pbDoc, + }, + { + upsert: true, + } + ); + + await UpdateChartRanking(chartID); + } + } + } + + logger.info(`Done`); +})();