From e319f88da87f11a8d37c775e205e6e6e7e238ff5 Mon Sep 17 00:00:00 2001 From: zkldi Date: Tue, 27 Apr 2021 17:01:38 +0100 Subject: [PATCH] refactor charts to do versioning instead of songs --- server/scripts/save-testdata-from-ktblack.ts | 4 ++-- server/src/internal-api/import/import.test.ts | 1 + .../database-lookup/chart-ptdf.ts | 22 +++++++++++++++++++ .../file/csv-eamusement-iidx/converter.ts | 12 +++++----- 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/server/scripts/save-testdata-from-ktblack.ts b/server/scripts/save-testdata-from-ktblack.ts index 8453e211d..5cd41c62e 100644 --- a/server/scripts/save-testdata-from-ktblack.ts +++ b/server/scripts/save-testdata-from-ktblack.ts @@ -2,7 +2,7 @@ import monk from "monk"; import fs from "fs"; import path from "path"; import { Command } from "commander"; -import CreateLogCtx from "../logger"; +import CreateLogCtx from "../src/logger"; const program = new Command(); @@ -24,7 +24,7 @@ const ktBlackDB = monk(`${process.env.MONGO_BASE_URL}/ktblackdb`); logger.info(`Fetched ${data.length} documents, Writing...`); fs.writeFileSync( - path.join(__dirname, `../test-utils/test-data/kamaitachi/ktblack-${collection}.json`), + path.join(__dirname, `../src/test-utils/test-data/kamaitachi/ktblack-${collection}.json`), JSON.stringify(data) ); diff --git a/server/src/internal-api/import/import.test.ts b/server/src/internal-api/import/import.test.ts index e8c4cee25..6e128bf7c 100644 --- a/server/src/internal-api/import/import.test.ts +++ b/server/src/internal-api/import/import.test.ts @@ -21,6 +21,7 @@ async function LoadKTBlackIIDXData() { await db.charts.iidx.remove({}); await db.charts.iidx.insert(charts); } + // reset DB handles the post-stuff t.test("POST /internal-api/import/file", async (t) => { diff --git a/server/src/score-import/database-lookup/chart-ptdf.ts b/server/src/score-import/database-lookup/chart-ptdf.ts index aba57627c..5697799a1 100644 --- a/server/src/score-import/database-lookup/chart-ptdf.ts +++ b/server/src/score-import/database-lookup/chart-ptdf.ts @@ -3,6 +3,10 @@ import { Difficulties, Game, integer, Playtypes, IDStrings } from "kamaitachi-common"; import db from "../../db/db"; +/** + * Find chart with PlaytypeDifficulty. This only finds charts that have `isPrimary` set to true. + * If you want to find charts that are not primary, you need to use PTDFVersion. + */ export function FindChartWithPTDF< G extends Game = Game, P extends Playtypes[G] = Playtypes[G], @@ -12,5 +16,23 @@ export function FindChartWithPTDF< songID: songID, playtype: playtype, difficulty: difficulty, + isPrimary: true, + }); +} + +/** + * Find chart with Playtype, Difficulty and a given version. This does not necessarily return a chart that has + * `isPrimary` set. + */ +export function FindChartWithPTDFVersion< + G extends Game = Game, + P extends Playtypes[G] = Playtypes[G], + I extends IDStrings = IDStrings +>(game: G, songID: integer, playtype: P, difficulty: Difficulties[I], version: string) { + return db.charts[game].findOne({ + songID: songID, + playtype: playtype, + difficulty: difficulty, + versions: version, }); } diff --git a/server/src/score-import/import-types/file/csv-eamusement-iidx/converter.ts b/server/src/score-import/import-types/file/csv-eamusement-iidx/converter.ts index b56f49a70..c9a940b77 100644 --- a/server/src/score-import/import-types/file/csv-eamusement-iidx/converter.ts +++ b/server/src/score-import/import-types/file/csv-eamusement-iidx/converter.ts @@ -1,7 +1,8 @@ import { config, ESDCore, Lamps, AnySongDocument, ChartDocument } from "kamaitachi-common"; import { Logger } from "winston"; import { DryScore, ConverterFunction, ConverterFnReturn } from "../../../../types"; -import { FindChartWithPTDF } from "../../../database-lookup/chart-ptdf"; +import { FindChartWithPTDFVersion } from "../../../database-lookup/chart-ptdf"; +import { FindSongOnTitle } from "../../../database-lookup/song-title"; import { KTDataNotFoundFailure, InternalFailure, @@ -35,7 +36,7 @@ async function EamScoreConverter( isLegacyLeggendaria: boolean, logger: Logger ) { - const HUMANISED_CHART_TITLE = `${ktchiSong.title} (${context.playtype} ${eamScore.difficulty})`; + const HUMANISED_CHART_TITLE = `${ktchiSong.title} (${context.playtype} ${eamScore.difficulty} [v${context.importVersion}])`; if (!eamScore.level) { // charts that dont exist in the format have a level of 0 return null; @@ -51,11 +52,12 @@ async function EamScoreConverter( return null; } - let ktchiChart = (await FindChartWithPTDF( + let ktchiChart = (await FindChartWithPTDFVersion( "iidx", ktchiSong.id, context.playtype, - eamScore.difficulty + eamScore.difficulty, + context.importVersion )) as ChartDocument<"iidx:SP" | "iidx:DP">; if (!ktchiChart) { @@ -234,7 +236,7 @@ const ConverterFn: ConverterFunction