From 570c7bfa33f68bb2b4567f8935ca2b6909c58853 Mon Sep 17 00:00:00 2001 From: zkldi Date: Tue, 8 Jun 2021 17:34:51 +0100 Subject: [PATCH] factor out SUPPORTED_GAMES --- .../scripts/__KT_DATABASE_MIGRATION/scores.ts | 2 +- server/scripts/set-indexes.ts | 6 ++--- server/scripts/validate-database.ts | 9 ++++---- server/src/external/mongo/schemas.ts | 4 ++-- .../common/batch-manual/parser.ts | 9 ++++---- server/src/lib/setup/config.ts | 23 +++++++++++-------- 6 files changed, 29 insertions(+), 24 deletions(-) diff --git a/server/scripts/__KT_DATABASE_MIGRATION/scores.ts b/server/scripts/__KT_DATABASE_MIGRATION/scores.ts index 739d6188c..b9f844093 100644 --- a/server/scripts/__KT_DATABASE_MIGRATION/scores.ts +++ b/server/scripts/__KT_DATABASE_MIGRATION/scores.ts @@ -1,7 +1,7 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ import { PrivateUserDocument, ScoreDocument } from "tachi-common"; -import { grades, lamps, supportedGames, validPlaytypes } from "tachi-common/js/config"; +import { grades, lamps, validPlaytypes } from "tachi-common/js/config"; import db from "../../src/db/db"; import MigrateRecords from "./migrate"; import CreateLogCtx from "../../src/common/logger"; diff --git a/server/scripts/set-indexes.ts b/server/scripts/set-indexes.ts index a95931524..7e38ac71f 100644 --- a/server/scripts/set-indexes.ts +++ b/server/scripts/set-indexes.ts @@ -3,7 +3,7 @@ import { Command } from "commander"; import CreateLogCtx from "../src/lib/logger/logger"; import { IndexOptions } from "mongodb"; import { ValidDatabases } from "tachi-common"; -import { supportedGames } from "tachi-common/js/config"; +import { CONF_INFO } from "../src/lib/setup/config"; const logger = CreateLogCtx(__filename); @@ -87,7 +87,7 @@ const staticIndexes: Partial> = { const indexes: Partial> = staticIndexes; -for (const game of supportedGames) { +for (const game of CONF_INFO.SUPPORTED_GAMES) { if (indexes[`charts-${game}` as "charts-iidx"]) { indexes[`charts-${game}` as "charts-iidx"]!.push( index({ chartID: 1 }, UNIQUE), @@ -138,7 +138,7 @@ export async function SetIndexes(dbst: string) { logger.info("Done."); } -// if calling this as a script +// if calling this as a script -- similar to pythons if __name__ == "__main__" if (require.main === module) { SetIndexes(options.db ?? "ktblackdb").then(process.exit(0)); } diff --git a/server/scripts/validate-database.ts b/server/scripts/validate-database.ts index 1ff84721f..778b41149 100644 --- a/server/scripts/validate-database.ts +++ b/server/scripts/validate-database.ts @@ -1,11 +1,12 @@ import Prudence, { PrudenceSchema } from "prudence"; import db from "../src/db/db"; -import { PRUDENCE_CHART_SCHEMAS, PRUDENCE_SCORE_SCHEMAS, STATIC_SCHEMAS } from "../src/db/schemas"; +import { PRUDENCE_SCORE_SCHEMAS, STATIC_SCHEMAS } from "../src/db/schemas"; import CreateLogCtx from "../src/common/logger"; import fs from "fs"; import path from "path"; import { PrudenceError } from "prudence/js/error"; -import { supportedGames, validPlaytypes } from "tachi-common/js/config"; +import { validPlaytypes } from "tachi-common/js/config"; +import { CONF_INFO } from "../src/lib/setup/config"; const BASE_DIR = path.join(__dirname, "./validate-database-errs"); @@ -62,7 +63,7 @@ async function ValidateScores(): Promise { let globalTotal = 0; let globalSuccess = 0; let globalFail = 0; - for (const game of supportedGames) { + for (const game of CONF_INFO.SUPPORTED_GAMES) { for (const playtype of validPlaytypes[game]) { // @ts-expect-error shut up const schema = PRUDENCE_SCORE_SCHEMAS[game][playtype]; @@ -134,7 +135,7 @@ async function ValidateChartsOrSongs(type: "songs" | "charts"): Promise { let globalTotal = 0; let globalSuccess = 0; let globalFail = 0; - for (const game of supportedGames) { + for (const game of CONF_INFO.SUPPORTED_GAMES) { const schema = STATIC_SCHEMAS[type]![game]; let successCount = 0; diff --git a/server/src/external/mongo/schemas.ts b/server/src/external/mongo/schemas.ts index a7fc2daf5..e78b6781c 100644 --- a/server/src/external/mongo/schemas.ts +++ b/server/src/external/mongo/schemas.ts @@ -7,9 +7,9 @@ import { validHitData, validDifficulties, validPlaytypes, - supportedGames, } from "tachi-common/js/config"; import p, { PrudenceSchema, ValidSchemaValue } from "prudence"; +import { CONF_INFO } from "../../lib/setup/config"; const LAZY_EMAIL_REGEX = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/u; @@ -408,7 +408,7 @@ const PRUDENCE_FOLDER_CHART_LOOKUP: PrudenceSchema = { const PRUDENCE_FOLDER: PrudenceSchema = { title: "string", - game: p.isIn(supportedGames), + game: p.isIn(CONF_INFO.SUPPORTED_GAMES), playtype: (self, parent) => p.isIn(validPlaytypes[parent.game as Game])(self), folderID: "string", table: "string", diff --git a/server/src/lib/score-import/import-types/common/batch-manual/parser.ts b/server/src/lib/score-import/import-types/common/batch-manual/parser.ts index addfeaefd..fa4de2158 100644 --- a/server/src/lib/score-import/import-types/common/batch-manual/parser.ts +++ b/server/src/lib/score-import/import-types/common/batch-manual/parser.ts @@ -2,12 +2,13 @@ import { KtLogger } from "../../../../logger/logger"; import ScoreImportFatalError from "../../../framework/score-importing/score-import-error"; import { BatchManual, BatchManualContext, BatchManualScore } from "./types"; import p, { PrudenceSchema, ValidSchemaValue } from "prudence"; -import { lamps, supportedGames, validHitData, validPlaytypes } from "tachi-common/js/config"; +import { lamps, validHitData, validPlaytypes } from "tachi-common/js/config"; import { Game, ImportTypes } from "tachi-common"; import { ConverterBatchManual } from "./converter"; import deepmerge from "deepmerge"; import { FormatPrError } from "../../../../../utils/prudence"; import { ParserFunctionReturnsSync } from "../types"; +import { CONF_INFO } from "../../../../setup/config"; const optNull = (v: ValidSchemaValue) => p.optional(p.nullable(v)); @@ -101,7 +102,7 @@ const PR_BatchManualScore = (game: Game): PrudenceSchema => ({ const PR_BatchManual = (game: Game): PrudenceSchema => ({ head: { service: p.isBoundedString(3, 15), - game: p.isIn(supportedGames), + game: p.isIn(CONF_INFO.SUPPORTED_GAMES), version: "*?string", }, body: [PR_BatchManualScore(game)], @@ -140,10 +141,10 @@ export function ParseBatchManualFromObject( ); } - if (!supportedGames.includes(possiblyGame)) { + if (!CONF_INFO.SUPPORTED_GAMES.includes(possiblyGame)) { throw new ScoreImportFatalError( 400, - `Invalid game ${possiblyGame} - expected any of ${supportedGames.join(", ")}` + `Invalid game ${possiblyGame} - expected any of ${CONF_INFO.SUPPORTED_GAMES.join(", ")}` ); } diff --git a/server/src/lib/setup/config.ts b/server/src/lib/setup/config.ts index 62967b1f7..3b97db403 100644 --- a/server/src/lib/setup/config.ts +++ b/server/src/lib/setup/config.ts @@ -4,8 +4,7 @@ import JSON5 from "json5"; import fs from "fs"; import p from "prudence"; import { FormatPrError } from "../../utils/prudence"; -import { integer } from "tachi-common"; - +import { integer, Game } from "tachi-common"; dotenv.config(); // imports things like NODE_ENV from a local .env file if one is present. // reads from $pwd/conf.json5 @@ -38,8 +37,9 @@ export interface TachiConfig { CDN_ROOT: string; TYPE: "ktchi" | "btchi" | "omni"; PORT: integer; - INFO: { + TYPE_INFO: { NAME: string; + SUPPORTED_GAMES: Game[]; }; } @@ -64,15 +64,17 @@ if (err) { /** * KTCHI | Kamaitachi is the arcade build of Tachi. */ -const KTCHI_INFO: TachiConfig["INFO"] = { +const KTCHI_INFO: TachiConfig["TYPE_INFO"] = { NAME: "Kamaitachi", + SUPPORTED_GAMES: ["iidx", "gitadora", "chunithm", "maimai", "museca", "sdvx"], }; /** * BTCHI | Bokutachi is the home-simulator build of Tachi. */ -const BTCHI_INFO: TachiConfig["INFO"] = { +const BTCHI_INFO: TachiConfig["TYPE_INFO"] = { NAME: "Bokutachi", + SUPPORTED_GAMES: ["usc", "bms"], }; /** @@ -81,16 +83,17 @@ const BTCHI_INFO: TachiConfig["INFO"] = { * If you're one of those nerds who is reading this to figure out how to steal and rerun * this codebase this is what you're looking for. */ -const OMNI_INFO: TachiConfig["INFO"] = { +const OMNI_INFO: TachiConfig["TYPE_INFO"] = { NAME: "Omnitachi", + SUPPORTED_GAMES: [...KTCHI_INFO.SUPPORTED_GAMES, ...BTCHI_INFO.SUPPORTED_GAMES], }; if (config.TYPE === "ktchi") { - config.INFO = KTCHI_INFO; + config.TYPE_INFO = KTCHI_INFO; } else if (config.TYPE === "btchi") { - config.INFO = BTCHI_INFO; + config.TYPE_INFO = BTCHI_INFO; } else if (config.type === "omni") { - config.INFO = OMNI_INFO; + config.TYPE_INFO = OMNI_INFO; } const tachiConfig = config as TachiConfig; @@ -104,6 +107,6 @@ export const EAG_API_URL = tachiConfig.EAG_API_URL; export const ARC_API_URL = tachiConfig.ARC_API_URL; export const ARC_AUTH_TOKEN = tachiConfig.ARC_AUTH_TOKEN; export const KTCDN_ROOT = tachiConfig.CDN_ROOT; -export const CONF_INFO = tachiConfig.INFO; +export const CONF_INFO = tachiConfig.TYPE_INFO; export const PORT = tachiConfig.PORT; export const CONFIG = tachiConfig;