Merge pull request #109 from zkldi/zkldi/issue105

Migrate Fervidex-Static dan updates to classHandler
This commit is contained in:
zkldi
2021-05-21 08:19:35 +01:00
committed by GitHub
5 changed files with 87 additions and 53 deletions
+22 -1
View File
@@ -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();
});
+1 -48
View File
@@ -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.
@@ -10,8 +10,9 @@ export interface ClassHandler {
game: Game,
playtype: Playtypes[Game],
userID: integer,
customRatings: Record<string, number>
): Promise<Record<string, string>> | Record<string, string>;
customRatings: Record<string, number>,
logger: KtLogger
): Promise<Record<string, string>> | Record<string, string> | 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);
}
@@ -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<string, unknown>): 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,
};
};
}
@@ -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),
};
}