diff --git a/server/scripts/bpi-poyashi-data/update-bpi-poyashi.ts b/server/scripts/bpi-poyashi-data/update-bpi-poyashi.ts deleted file mode 100644 index a380dcc42..000000000 --- a/server/scripts/bpi-poyashi-data/update-bpi-poyashi.ts +++ /dev/null @@ -1,118 +0,0 @@ -/* eslint-disable no-await-in-loop */ -import { Command } from "commander"; -import db from "external/mongo/db"; -import fs from "fs"; -import CreateLogCtx from "lib/logger/logger"; -import fetch from "node-fetch"; -import path from "path"; -import { ChartDocument, Difficulties, IIDXBPIData } from "tachi-common"; -import { FindChartWithPTDFVersion } from "utils/queries/charts"; -import { FindSongOnTitle } from "utils/queries/songs"; -import { RecalcAllScores } from "../state-sync/recalc-all-scores"; - -const program = new Command(); - -const logger = CreateLogCtx(__filename); - -program.option("-f, --fetch", "Fetch the latest data from the poyashi repo."); - -program.parse(process.argv); -const options = program.opts(); - -const dataLoc = path.join(__dirname, "./output.json"); - -const difficultyResolve: Record = { - 3: ["SP", "HYPER"], - 4: ["SP", "ANOTHER"], - 8: ["DP", "HYPER"], - 9: ["DP", "ANOTHER"], - 10: ["SP", "LEGGENDARIA"], - 11: ["DP", "LEGGENDARIA"], -}; - -async function UpdatePoyashiData() { - let data; - if (options.fetch) { - // lol! - logger.info("Fetching data from github..."); - const rj = await fetch("https://proxy.poyashi.me/?type=bpi").then((r) => r.json()); - - logger.info("Fetched data."); - fs.writeFileSync(dataLoc, JSON.stringify(rj)); - logger.info("Saved data to output.json."); - - data = rj; - } else { - logger.info("Reading data from output.json..."); - data = JSON.parse(fs.readFileSync(dataLoc, "utf-8")); - } - - const realData: IIDXBPIData[] = []; - for (const d of data.body) { - const res = difficultyResolve[d.difficulty]; - - if (!res) { - throw new Error(`Unknown difficulty ${d.difficulty}`); - } - - const [playtype, diff] = res; - - const tachiSong = await FindSongOnTitle("iidx", d.title); - - if (!tachiSong) { - logger.warn(`Cannot find song ${d.title}?`); - continue; - } - - const tachiChart = (await FindChartWithPTDFVersion( - "iidx", - tachiSong.id, - playtype as "SP" | "DP", - diff as Difficulties["iidx:DP" | "iidx:SP"], - "27" - )) as ChartDocument<"iidx:SP" | "iidx:DP">; - - if (!tachiChart) { - logger.warn( - `Cannot find chart ${tachiSong.title} (${tachiSong.id}) ${playtype}, ${diff}?` - ); - continue; - } - - const kavg = Number(d.avg); - - if (kavg < 0) { - logger.warn( - `${tachiSong.title} (${playtype} ${diff}). Invalid kavg ${d.avg}, Skipping.` - ); - continue; - } - - if (d.removed) { - logger.info(`Skipping removed chart ${tachiSong.title}.`); - continue; - } - - realData.push({ - coef: d.coef === -1 ? null : d.coef, - kavg: Number(d.avg), - wr: Number(d.wr), - chartID: tachiChart.chartID, - }); - } - - await db["iidx-bpi-data"].remove({}); - await db["iidx-bpi-data"].insert(realData); - - logger.info(`Removed all, and inserted ${realData.length} documents.`); - - logger.info(`Triggering IIDX Recalc.`); - - await RecalcAllScores({ game: "iidx" }); - - logger.info(`Done.`); -} - -if (require.main === module) { - UpdatePoyashiData(); -} diff --git a/server/scripts/sync-database/sync-database.ts b/server/scripts/sync-database/sync-database.ts index 36d74944a..d830beced 100644 --- a/server/scripts/sync-database/sync-database.ts +++ b/server/scripts/sync-database/sync-database.ts @@ -19,6 +19,7 @@ import { TableDocument, BMSCourseDocument, } from "tachi-common"; +import { RecalcAllScores } from "utils/calculations/recalc-scores"; import { InitaliseFolderChartLookup } from "utils/folder"; interface SyncInstructions { @@ -63,6 +64,8 @@ async function GenericUpsert( map.set(doc[field], doc); } + const changedFields = []; + let i = 0; for (const document of documents) { i++; @@ -87,6 +90,8 @@ async function GenericUpsert( replacement: document, }, }); + + changedFields.push(field); } // free some memory. @@ -106,14 +111,16 @@ async function GenericUpsert( upsertedCount, modifiedCount, }); - await InitaliseFolderChartLookup(); } if (remove) { await RemoveNotPresent(documents, collection, field, logger); } - return bwriteOps.length; + return { + thingsChanged: bwriteOps.length, + changedFields, + }; } const syncInstructions: SyncInstructions[] = [ @@ -135,7 +142,7 @@ const syncInstructions: SyncInstructions[] = [ const r = await GenericUpsert(charts, collection, "chartID", logger, false); - if (r) { + if (r.thingsChanged) { await InitaliseFolderChartLookup(); await UpdateIsPrimaryStatus(); } @@ -150,15 +157,27 @@ const syncInstructions: SyncInstructions[] = [ ) => { const r = await GenericUpsert(charts, collection, "chartID", logger, true); - if (r) { + if (r.thingsChanged) { await InitaliseFolderChartLookup(); + await UpdateIsPrimaryStatus(); + + await RecalcAllScores({ + chartID: { $in: r.changedFields }, + }); } }, }, { pattern: /^songs-/u, - handler: (songs: SongDocument[], collection: ICollection, logger) => - GenericUpsert(songs, collection, "id", logger), + handler: async (songs: SongDocument[], collection: ICollection, logger) => { + const r = await GenericUpsert(songs, collection, "id", logger); + + if (r.thingsChanged) { + await RecalcAllScores({ + songID: { $in: r.changedFields }, + }); + } + }, }, { pattern: /^folders$/u,