mirror of
https://github.com/zkldi/Tachi.git
synced 2026-09-30 19:07:58 +03:00
Change constants array to an enum
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { Router, RequestHandler } from "express";
|
||||
import p from "prudence";
|
||||
import { FindChartOnSHA256 } from "utils/queries/charts";
|
||||
import { SYMBOL_TachiAPIAuth, SYMBOL_TachiData } from "lib/constants/tachi";
|
||||
import db from "external/mongo/db";
|
||||
@@ -21,20 +22,23 @@ import { CDNStore } from "lib/cdn/cdn";
|
||||
import { ONE_MEGABYTE } from "lib/constants/filesize";
|
||||
import { RequirePermissions } from "server/middleware/auth";
|
||||
import { GetUSCIRReplayURL } from "lib/cdn/url-format";
|
||||
import { FormatPrError } from "utils/prudence";
|
||||
import { USCClientChart } from "./types";
|
||||
|
||||
const logger = CreateLogCtx(__filename);
|
||||
|
||||
const router: Router = Router({ mergeParams: true });
|
||||
|
||||
const STATUS_CODES = {
|
||||
UNAUTH: 41,
|
||||
CHART_REFUSE: 42,
|
||||
FORBIDDEN: 43,
|
||||
NOT_FOUND: 44,
|
||||
SERVER_ERROR: 50,
|
||||
SUCCESS: 20,
|
||||
BAD_REQ: 40,
|
||||
};
|
||||
enum STATUS_CODES {
|
||||
UNAUTH = 41,
|
||||
CHART_REFUSE = 42,
|
||||
FORBIDDEN = 43,
|
||||
NOT_FOUND = 44,
|
||||
SERVER_ERROR = 50,
|
||||
SUCCESS = 20,
|
||||
ACCEPTED = 22,
|
||||
BAD_REQ = 40,
|
||||
}
|
||||
|
||||
const ValidateUSCRequest: RequestHandler = async (req, res, next) => {
|
||||
const token = req.header("Authorization");
|
||||
@@ -230,29 +234,45 @@ router.get("/charts/:chartHash/leaderboard", RetrieveChart, async (req, res) =>
|
||||
});
|
||||
});
|
||||
|
||||
const PR_USCIRChartDoc = {
|
||||
chartHash: "string",
|
||||
artist: "string",
|
||||
title: "string",
|
||||
level: p.isBoundedInteger(1, 20),
|
||||
difficulty: p.isBoundedInteger(0, 3),
|
||||
effector: "string",
|
||||
illustrator: "string",
|
||||
bpm: "string",
|
||||
};
|
||||
|
||||
/**
|
||||
* Sends a score to the server.
|
||||
* https://uscir.readthedocs.io/en/latest/endpoints/score-submit.html
|
||||
* @name POST /ir/usc/scores
|
||||
*/
|
||||
router.post("/scores", RequirePermissions("submit_score"), async (req, res) => {
|
||||
if (typeof req.body.chart !== "object" || req.body.chart === null) {
|
||||
const chartErr = p(
|
||||
req.body.chart,
|
||||
PR_USCIRChartDoc,
|
||||
{},
|
||||
{
|
||||
throwOnNonObject: false,
|
||||
allowExcessKeys: true,
|
||||
}
|
||||
);
|
||||
|
||||
if (chartErr) {
|
||||
return res.status(200).json({
|
||||
statusCode: STATUS_CODES.BAD_REQ,
|
||||
description: "Invalid chart provided.",
|
||||
description: FormatPrError(chartErr, "Invalid chart."),
|
||||
});
|
||||
}
|
||||
|
||||
if (typeof req.body.chart.chartHash !== "string") {
|
||||
return res.status(200).json({
|
||||
statusCode: STATUS_CODES.BAD_REQ,
|
||||
description: "Invalid chart provided.",
|
||||
});
|
||||
}
|
||||
const uscChart = req.body.chart as USCClientChart;
|
||||
|
||||
const chartDoc = (await FindChartOnSHA256(
|
||||
"usc",
|
||||
req.body.chart.chartHash
|
||||
uscChart.chartHash
|
||||
)) as ChartDocument<"usc:Single"> | null;
|
||||
|
||||
if (!chartDoc) {
|
||||
|
||||
Reference in New Issue
Block a user