From 09becfcc3aedc51ed6ef51cd9c86155f3736473e Mon Sep 17 00:00:00 2001 From: zkldi <20380519+zkldi@users.noreply.github.com> Date: Wed, 12 Oct 2022 23:38:40 +0100 Subject: [PATCH] feat: add bms displayTables to gameSpecific settings, refactor duplicate implementation out --- common/src/lib/schemas.ts | 35 +++++++++++++++---- .../games/_game/_playtype/settings/router.ts | 19 ++-------- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/common/src/lib/schemas.ts b/common/src/lib/schemas.ts index 6720ac491..3b681d8a8 100644 --- a/common/src/lib/schemas.ts +++ b/common/src/lib/schemas.ts @@ -36,6 +36,33 @@ function prSchemaFnWrap(schema: PrudenceSchema) { }; } +export const PR_GAMESPECIFIC_SETTINGS = (game: Game): PrudenceSchema => { + switch (game) { + case "iidx": + return { + display2DXTra: p.optional("boolean"), + bpiTarget: p.optional(p.isBoundedInteger(0, 100)), + }; + case "sdvx": + case "usc": + return { + vf6Target: p.optional(p.isBetween(0, 0.5)), + }; + case "bms": + return { + displayTables: ["string"], + }; + case "jubeat": + return { + // 165.1 is the max jubility. + jubilityTarget: p.optional(p.isBetween(0, 165.1)), + }; + + default: + return {}; + } +}; + const PR_GAME_STATS = (game: Game, playtype: Playtypes[Game], gptConfig: GamePTConfig) => ({ userID: p.isPositiveNonZeroInteger, game: p.is(game), @@ -926,12 +953,8 @@ const PRE_SCHEMAS = { (self) => Array.isArray(self) && self.length <= 6 ), scoreBucket: p.isIn(null, "grade", "lamp"), - gameSpecific: - game === "iidx" - ? { - display2DXTra: "boolean", - } - : {}, + + gameSpecific: PR_GAMESPECIFIC_SETTINGS(game), }, })(s); }, diff --git a/server/src/server/router/api/v1/users/_userID/games/_game/_playtype/settings/router.ts b/server/src/server/router/api/v1/users/_userID/games/_game/_playtype/settings/router.ts index 06b18214e..5be946033 100644 --- a/server/src/server/router/api/v1/users/_userID/games/_game/_playtype/settings/router.ts +++ b/server/src/server/router/api/v1/users/_userID/games/_game/_playtype/settings/router.ts @@ -5,6 +5,7 @@ import CreateLogCtx from "lib/logger/logger"; import p from "prudence"; import { RequirePermissions } from "server/middleware/auth"; import { GetGamePTConfig } from "tachi-common"; +import { PR_GAMESPECIFIC_SETTINGS } from "tachi-common/lib/schemas"; import { FormatPrError, optNull } from "utils/prudence"; import { GetUGPT } from "utils/req-tachi-data"; import { FormatUserDoc } from "utils/user"; @@ -30,23 +31,7 @@ router.patch( const gptConfig = GetGamePTConfig(game, playtype); - let gameSpecificSchema = {}; - - if (game === "iidx") { - gameSpecificSchema = { - display2DXTra: p.optional("boolean"), - bpiTarget: p.optional(p.isBoundedInteger(0, 100)), - }; - } else if (game === "sdvx" || game === "usc") { - gameSpecificSchema = { - vf6Target: p.optional(p.isBetween(0, 0.5)), - }; - } else if (game === "jubeat") { - gameSpecificSchema = { - // 165.1 is the max jubility. - jubilityTarget: p.optional(p.isBetween(0, 165.1)), - }; - } + const gameSpecificSchema = PR_GAMESPECIFIC_SETTINGS(game); const err = p(req.safeBody, { preferredScoreAlg: p.optional(p.nullable(p.isIn(gptConfig.scoreRatingAlgs))),