Merge pull request #587 from TNG-dev:zkldi/issue-586

Add support for the rest of LR2Hook.
This commit is contained in:
zkldi
2021-12-11 23:57:15 +00:00
committed by GitHub
5 changed files with 69 additions and 2 deletions
@@ -1,6 +1,7 @@
import { Lamps } from "tachi-common";
import { Lamps, ScoreDocument } from "tachi-common";
import {
InternalFailure,
InvalidScoreFailure,
KTDataNotFoundFailure,
} from "lib/score-import/framework/common/converter-failures";
import { GenericGetGradeAndPercent } from "lib/score-import/framework/common/score-utils";
@@ -61,6 +62,7 @@ export const ConverterLR2Hook: ConverterFunction<LR2HookScore, LR2HookContext> =
},
},
scoreMeta: {
random: chart.playtype === "7K" ? ConvertRandom(data.playerData.random) : null,
client: "LR2",
},
};
@@ -68,6 +70,21 @@ export const ConverterLR2Hook: ConverterFunction<LR2HookScore, LR2HookContext> =
return { song, chart, dryScore };
};
function ConvertRandom(
random: LR2HookScore["playerData"]["random"]
): ScoreDocument<"bms:7K">["scoreMeta"]["random"] {
switch (random) {
case "NONRAN":
return "NONRAN";
case "MIRROR":
return "MIRROR";
case "RAN":
return "RANDOM";
case "S-RAN":
return "S-RANDOM";
}
}
function ConvertLamp(lamp: LR2HookScore["scoreData"]["lamp"]): Lamps["bms:7K" | "bms:14K"] {
switch (lamp) {
case "EASY":
@@ -46,6 +46,23 @@ t.test("#ParseLR2Hook", (t) => {
dm({ scoreData: { unexpectedField: "foo" } }),
"Should allow excess keys inside scoreData that we do not recognise."
);
assertSuccess(
dm({ playerData: { unexpectedField: "foo" } }),
"Should allow excess keys inside playerData that we do not recognise."
);
assertFail(
dm({ playerData: { autoScr: true } }),
"Should reject scores where autoScr is set to true."
);
assertFail(
dm({ playerData: { random: "H-RAN" } }),
"Should reject scores where random is set to H-RAN."
);
assertFail(
dm({ playerData: { random: "ALLSCR" } }),
"Should reject scores where random is set to ALLSCR"
);
assertFail({}, "Should reject an empty object");
@@ -5,8 +5,18 @@ import p, { PrudenceSchema } from "prudence";
import ScoreImportFatalError from "lib/score-import/framework/score-importing/score-import-error";
import { FormatPrError } from "utils/prudence";
const SUPPORTED_RANDOMS = ["NONRAN", "MIRROR", "RAN", "S-RAN"];
const PR_LR2Hook: PrudenceSchema = {
md5: "string",
playerData: {
autoScr: p.is(false),
// don't really need this, but it's
// { "ALL", "SINGLE", "7K", "5K", "DOUBLE", "14K", "10K", "9K" };
gameMode: p.any,
// ALLSCR and H-RAN may also be sent, but we don't support them.
random: p.isIn(SUPPORTED_RANDOMS),
},
scoreData: {
pgreat: p.isPositiveInteger,
great: p.isPositiveInteger,
@@ -40,7 +50,20 @@ export function ParseLR2Hook(
logger: KtLogger
): ParserFunctionReturns<LR2HookScore, LR2HookContext> {
// Ignore excess keys, as lr2hook is likely to add more features in the future.
const err = p(body, PR_LR2Hook, undefined, { allowExcessKeys: true });
const err = p(
body,
PR_LR2Hook,
{
playerData: {
autoScr:
"Auto Scratch cannot be turned on, as it is treated as an EASY CLEAR by LR2.",
random: `Expected any of ${SUPPORTED_RANDOMS.join(
", "
)}. Note that ALLSCR and H-RAN are not supported!`,
},
},
{ allowExcessKeys: true }
);
if (err) {
throw new ScoreImportFatalError(400, FormatPrError(err));
@@ -2,6 +2,11 @@ import { integer } from "tachi-common";
export interface LR2HookScore {
md5: string;
playerData: {
autoScr: boolean;
gameMode: unknown;
random: "NONRAN" | "MIRROR" | "RAN" | "S-RAN";
};
scoreData: {
pgreat: integer;
great: integer;
+5
View File
@@ -45,6 +45,11 @@ export const TestingKsHookSV3CScore = GetKTDataJSON("./kshook-sv3c/base.json") a
export const TestingLR2HookScore: LR2HookScore = {
md5: "38616b85332037cc12924f2ae2840262",
playerData: {
autoScr: false,
gameMode: "7K",
random: "RAN",
},
scoreData: {
pgreat: 1000,
great: 500,