diff --git a/backend/api/controllers/result.js b/backend/api/controllers/result.js index 809055f81..0a1a88a62 100644 --- a/backend/api/controllers/result.js +++ b/backend/api/controllers/result.js @@ -13,6 +13,7 @@ import { validateResult, validateKeys, } from "../../anticheat/index"; +import MonkeyStatusCodes from "../../constants/monkey-status-codes"; const objecthash = node_object_hash().hash; @@ -61,7 +62,8 @@ class ResultController { const { result } = req.body; result.uid = uid; if (result.wpm === result.raw && result.acc !== 100) { - throw new MonkeyError(400, "Bad input"); + const status = MonkeyStatusCodes.RESULT_DATA_INVALID; + throw new MonkeyError(status.code, "Bad input"); // todo move this } if ( (result.mode === "time" && result.mode2 < 15 && result.mode2 > 0) || @@ -88,7 +90,8 @@ class ResultController { result.customText.isTimeRandom && result.customText.time < 15) ) { - throw new MonkeyError(400, "Test too short"); + const status = MonkeyStatusCodes.TEST_TOO_SHORT; + throw new MonkeyError(status.code, status.message); } let resulthash = result.hash; @@ -109,13 +112,15 @@ class ResultController { }, uid ); - throw new MonkeyError(400, "Incorrect result hash"); + const status = MonkeyStatusCodes.RESULT_HASH_INVALID; + throw new MonkeyError(status.code, "Incorrect result hash"); } } if (anticheatImplemented()) { if (!validateResult(result)) { - throw new MonkeyError(400, "Result data doesn't make sense"); + const status = MonkeyStatusCodes.RESULT_DATA_INVALID; + throw new MonkeyError(status.code, "Result data doesn't make sense"); } } else { if (process.env.MODE === "dev") { @@ -180,7 +185,8 @@ class ResultController { }, uid ); - throw new MonkeyError(400, "Invalid result spacing"); + const status = MonkeyStatusCodes.RESULT_SPACING_INVALID; + throw new MonkeyError(status.code, "Invalid result spacing"); } try { @@ -221,7 +227,8 @@ class ResultController { ) { if (anticheatImplemented()) { if (!validateKeys(result, uid)) { - throw new MonkeyError(400, "Possible bot detected"); + const status = MonkeyStatusCodes.BOT_DETECTED; + throw new MonkeyError(status.code, "Possible bot detected"); } } else { if (process.env.MODE === "dev") { @@ -233,7 +240,8 @@ class ResultController { } } } else { - throw new MonkeyError(400, "Missing key data"); + const status = MonkeyStatusCodes.MISSING_KEY_DATA; + throw new MonkeyError(status.code, "Missing key data"); } } } diff --git a/backend/constants/monkey-status-codes.ts b/backend/constants/monkey-status-codes.ts new file mode 100644 index 000000000..9f64f0c38 --- /dev/null +++ b/backend/constants/monkey-status-codes.ts @@ -0,0 +1,51 @@ +type Status = { + code: number; + message: string; +}; + +type Statuses = { + TEST_TOO_SHORT: Status; + RESULT_HASH_INVALID: Status; + RESULT_DATA_INVALID: Status; + RESULT_SPACING_INVALID: Status; + MISSING_KEY_DATA: Status; + BOT_DETECTED: Status; + GIT_GUD: Status; +}; + +export function getCodesRangeStart(): number { + return 460; +} + +const statuses: Statuses = { + TEST_TOO_SHORT: { + code: 460, + message: "Test too short", + }, + RESULT_HASH_INVALID: { + code: 461, + message: "Result hash invalid", + }, + RESULT_SPACING_INVALID: { + code: 462, + message: "Result spacing invalid", + }, + RESULT_DATA_INVALID: { + code: 463, + message: "Result data invalid", + }, + MISSING_KEY_DATA: { + code: 464, + message: "Missing key data", + }, + BOT_DETECTED: { + code: 465, + message: "Bot detected", + }, + GIT_GUD: { + code: 469, + message: "Git gud scrub", + }, +}; + +export default statuses; diff --git a/backend/handlers/monkey-response.js b/backend/handlers/monkey-response.js index 44d794e8b..3535649ed 100644 --- a/backend/handlers/monkey-response.js +++ b/backend/handlers/monkey-response.js @@ -1,3 +1,5 @@ +import { getCodesRangeStart } from "../constants/monkey-status-codes"; + export class MonkeyResponse { constructor(message, data, status = 200) { this.message = message; @@ -14,6 +16,8 @@ export function handleMonkeyResponse(handlerData, res) { const { message, data, status } = monkeyResponse; res.status(status); + if (status >= getCodesRangeStart()) res.statusMessage = message; + res.monkeyMessage = message; // so that we can see message in swagger stats if ([301, 302].includes(status)) { return res.redirect(data);