Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: disallow scores in the future #1027

Merged
merged 2 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { ValidateScore } from "./validate-score";
import { ONE_DAY } from "lib/constants/time";
import t from "tap";
import { Testing511SPA, TestingIIDXSPScore } from "test-utils/test-data";

t.test("#ValidateScore", (t) => {
t.test("Should reject scores in the future", (t) => {
t.throws(
() => {
ValidateScore(
{ ...TestingIIDXSPScore, timeAchieved: Date.now() + ONE_DAY * 2 },
Testing511SPA
);
},
{
message: "Invalid timestamp: score happens in the future.",
}
);

t.end();
});

t.end();
});
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
import { InvalidScoreFailure } from "../common/converter-failures";
import { GPT_SERVER_IMPLEMENTATIONS } from "game-implementations/game-implementations";
import { RunValidators } from "game-implementations/games/_common";
import { ONE_HOUR } from "lib/constants/time";
import { GetGPTConfig, GetGPTString } from "tachi-common";
import type { ChartDocument, GPTString, ScoreDocument } from "tachi-common";
import type { ConfScoreMetric } from "tachi-common/types/metrics";

/**
* Checks if a score passes all of its validation checks. This tests gpt specific
* things, like whether this passes the validators defined in gptConfig, or whether
* it passes the chart-dependent validators defined in the gpt server implementation.
* Checks if a score passes all of its validation checks.
*
* @returns nothing. This will throw an InvalidScoreFailure on error.
*/
export function ValidateScore(score: ScoreDocument, chart: ChartDocument): void {
const leniency = ONE_HOUR * 24;

if (score.timeAchieved !== null && score.timeAchieved > Date.now() + leniency) {
throw new InvalidScoreFailure("Invalid timestamp: score happens in the future.");
}

ValidateScoreGameSpecific(score, chart);
}

function ValidateScoreGameSpecific(score: ScoreDocument, chart: ChartDocument): void {
const gptString = GetGPTString(score.game, score.playtype);
const gptConfig = GetGPTConfig(gptString);
const gptImpl = GPT_SERVER_IMPLEMENTATIONS[gptString];
Expand Down
Loading