mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-03-10 13:44:27 +08:00
Added monkey status codes (#2613)
* test code * added some monkey status codes * adding status message to response * added more codes * using all new codes
This commit is contained in:
parent
d792844473
commit
226de70e36
3 changed files with 70 additions and 7 deletions
|
@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
51
backend/constants/monkey-status-codes.ts
Normal file
51
backend/constants/monkey-status-codes.ts
Normal file
|
@ -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;
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue