From 4780e73b99190163a0fac0022b6c4ce179b94edf Mon Sep 17 00:00:00 2001 From: zkldi Date: Fri, 21 May 2021 08:16:17 +0100 Subject: [PATCH] Migrate Fervidex-Static dan updates to classHandler Fixes #105 --- server/src/api/ir/fervidex/fervidex.test.ts | 23 +++++++- server/src/api/ir/fervidex/fervidex.ts | 49 +--------------- .../framework/user-game-stats/classes.ts | 8 ++- .../ir/fervidex-static/class-handler.ts | 57 +++++++++++++++++++ .../import-types/ir/fervidex-static/parser.ts | 3 +- 5 files changed, 87 insertions(+), 53 deletions(-) create mode 100644 server/src/score-import/import-types/ir/fervidex-static/class-handler.ts diff --git a/server/src/api/ir/fervidex/fervidex.test.ts b/server/src/api/ir/fervidex/fervidex.test.ts index b945a827f..2ba247a62 100644 --- a/server/src/api/ir/fervidex/fervidex.test.ts +++ b/server/src/api/ir/fervidex/fervidex.test.ts @@ -105,7 +105,7 @@ function TestHeaders(url: string, cookie: string[], data: any) { t.test("POST /api/ir/fervidex/class/submit", async (t) => { const cookie = await CreateFakeAuthCookie(mockApi); - // @todo + // @todo #108 TestHeaders("/api/ir/fervidex/class/submit", cookie, {}); t.beforeEach(ResetDBState); @@ -205,12 +205,33 @@ t.test("POST /api/ir/fervidex/profile/submit", async (t) => { t.equal(res.body.body.errors.length, 0, "Should have 0 failed scores."); + t.strictSame( + res.body.body.classDeltas, + [ + { + set: "dan", + playtype: "SP", + old: null, + new: "9", + }, + ], + "Should return updated dan deltas." + ); + const scores = await db.scores.count({ service: "Fervidex Static", }); t.equal(scores, 3, "Should import 3 scores."); + const ugs = await db["game-stats"].findOne({ + userID: 1, + game: "iidx", + playtype: "SP", + }); + + t.equal(ugs!.classes.dan, "9", "Should successfully update dan to 9th."); + t.end(); }); diff --git a/server/src/api/ir/fervidex/fervidex.ts b/server/src/api/ir/fervidex/fervidex.ts index 4fb80b5be..1fc887591 100644 --- a/server/src/api/ir/fervidex/fervidex.ts +++ b/server/src/api/ir/fervidex/fervidex.ts @@ -9,6 +9,7 @@ import { ParseFervidexStatic } from "../../../score-import/import-types/ir/fervi import { ParseFervidexSingle } from "../../../score-import/import-types/ir/fervidex/parser"; import { Playtypes } from "kamaitachi-common"; import CreateLogCtx from "../../../common/logger"; +import { FERVIDEX_COURSE_LOOKUP } from "../../../score-import/import-types/ir/fervidex-static/class-handler"; const logger = CreateLogCtx("fervidex.ts"); @@ -168,32 +169,6 @@ router.post( (logger) => ParseFervidexStatic(req.body, headers, logger) ); - if (req.body.sp_dan || req.body.sp_dan === 0) { - const classVal = FERVIDEX_COURSE_LOOKUP[req.body.sp_dan]; - - if (!classVal) { - return res.status(400).json({ - success: false, - description: `Invalid courseID of ${req.body.sp_dan}.`, - }); - } - - await UpdateClassIfGreater(req.session.ktchi!.userID, "iidx", "SP", "dan", classVal); - } - - if (req.body.dp_dan || req.body.dp_dan === 0) { - const classVal = FERVIDEX_COURSE_LOOKUP[req.body.dp_dan]; - - if (!classVal) { - return res.status(400).json({ - success: false, - description: `Invalid courseID of ${req.body.dp_dan}.`, - }); - } - - await UpdateClassIfGreater(req.session.ktchi!.userID, "iidx", "DP", "dan", classVal); - } - return res.status(responseData.statusCode).json(responseData.body); } ); @@ -237,28 +212,6 @@ router.post( } ); -const FERVIDEX_COURSE_LOOKUP = [ - "7kyu", - "6kyu", - "5kyu", - "4kyu", - "3kyu", - "2kyu", - "1kyu", - "1", - "2", - "3", - "4", - "5", - "6", - "7", - "8", - "9", - "10", - "chuuden", - "kaiden", -]; - /** * Submits the result of a class to Kamaitachi. This contains the dan played * and whether it was achieved. diff --git a/server/src/score-import/framework/user-game-stats/classes.ts b/server/src/score-import/framework/user-game-stats/classes.ts index 5150a24da..52252f4e5 100644 --- a/server/src/score-import/framework/user-game-stats/classes.ts +++ b/server/src/score-import/framework/user-game-stats/classes.ts @@ -10,8 +10,9 @@ export interface ClassHandler { game: Game, playtype: Playtypes[Game], userID: integer, - customRatings: Record - ): Promise> | Record; + customRatings: Record, + logger: KtLogger + ): Promise> | Record | undefined; } type ClassHandlerMap = { @@ -94,7 +95,8 @@ export async function UpdateUGSClasses( if (ClassHandler) { logger.debug(`Calling custom class handler.`); - const customClasses = await ClassHandler(game, playtype, userID, customRatings); + const customClasses = + (await ClassHandler(game, playtype, userID, customRatings, logger)) ?? {}; classes = deepmerge(customClasses, classes); } diff --git a/server/src/score-import/import-types/ir/fervidex-static/class-handler.ts b/server/src/score-import/import-types/ir/fervidex-static/class-handler.ts new file mode 100644 index 000000000..962b00e2b --- /dev/null +++ b/server/src/score-import/import-types/ir/fervidex-static/class-handler.ts @@ -0,0 +1,57 @@ +import { ClassHandler } from "../../../framework/user-game-stats/classes"; +import { integer } from "kamaitachi-common"; + +export const FERVIDEX_COURSE_LOOKUP = [ + "7kyu", + "6kyu", + "5kyu", + "4kyu", + "3kyu", + "2kyu", + "1kyu", + "1", + "2", + "3", + "4", + "5", + "6", + "7", + "8", + "9", + "10", + "chuuden", + "kaiden", +]; + +export function FerStaticClassHandler(body: Record): ClassHandler { + return (game, playtype, userID, customRatings, logger) => { + let index; + + if (playtype === "SP") { + index = body.sp_dan; + } else if (playtype === "DP") { + index = body.dp_dan; + } else { + logger.error( + `Invalid playtype ${playtype} passed to FerStaticClassHandler. Attempting to continue.` + ); + return; + } + + if (!Number.isInteger(index)) { + logger.info(`Recieved invalid fer-static class of ${index}.`); + return; + } + + const classVal = FERVIDEX_COURSE_LOOKUP[index as integer]; + + if (!classVal) { + logger.info(`Recieved invalid fer-static class of ${index}.`); + return; + } + + return { + dan: classVal, + }; + }; +} diff --git a/server/src/score-import/import-types/ir/fervidex-static/parser.ts b/server/src/score-import/import-types/ir/fervidex-static/parser.ts index b7555cbb1..2dd4921a8 100644 --- a/server/src/score-import/import-types/ir/fervidex-static/parser.ts +++ b/server/src/score-import/import-types/ir/fervidex-static/parser.ts @@ -6,6 +6,7 @@ import { ConverterIRFervidexStatic } from "./converter"; import { FervidexStaticContext, FervidexStaticScore } from "./types"; import { FerHeaders, SoftwareIDToVersion } from "../fervidex/parser"; import { AssertStrAsPositiveInt } from "../../../framework/common/string-asserts"; +import { FerStaticClassHandler } from "./class-handler"; const PR_FervidexStatic: PrudenceSchema = { ex_score: p.isPositiveInteger, @@ -78,6 +79,6 @@ export function ParseFervidexStatic( game: "iidx", iterable: scores, ConverterFunction: ConverterIRFervidexStatic, - classHandler: null, //@todo #105 + classHandler: FerStaticClassHandler(body), }; }