Skip to content

Commit

Permalink
Remove codes from api/stats (#954)
Browse files Browse the repository at this point in the history
  • Loading branch information
sspenst authored Jul 25, 2023
1 parent b78f89f commit 95cab66
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 782 deletions.
5 changes: 0 additions & 5 deletions helpers/validateSolution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ export default function validateSolution(directions: Direction[], level: Level)

const direction = directions[i];

// account for bad user input from the API
if (!(direction in Direction)) {
return false;
}

// validate and update position with direction
pos = pos.add(directionToPosition(direction));

Expand Down
23 changes: 11 additions & 12 deletions pages/api/stats/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import AchievementInfo from '@root/constants/achievementInfo';
import Direction, { getDirectionFromCode } from '@root/constants/direction';
import Direction from '@root/constants/direction';
import getDifficultyEstimate from '@root/helpers/getDifficultyEstimate';
import User from '@root/models/db/user';
import { AttemptContext } from '@root/models/schemas/playAttemptSchema';
Expand Down Expand Up @@ -35,8 +35,7 @@ export default withAuth({
GET: {},
PUT: {
body: {
codes: ValidArray(false),
directions: ValidArray(false),
directions: ValidArray(),
levelId: ValidObjectId(),
matchId: ValidType('string', false),
}
Expand All @@ -47,16 +46,16 @@ export default withAuth({

return res.status(200).json(stats);
} else if (req.method === 'PUT') {
const { codes, directions, levelId, matchId } = req.body;
const { directions, levelId, matchId } = req.body;

if (!directions && !codes) {
return res.status(400).json({
error: 'No directions or codes provided',
});
for (const direction of directions) {
if (!(direction in Direction)) {
return res.status(400).json({
error: `Invalid direction provided: ${direction}`,
});
}
}

const dirs: Direction[] = directions ?? codes.map((code: string) => getDirectionFromCode(code));

const ts = TimerUtil.getTs();
const session = await mongoose.startSession();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -78,13 +77,13 @@ export default withAuth({
throw new Error(resTrack.json.error);
}

if (!validateSolution(dirs, level)) {
if (!validateSolution(directions, level)) {
resTrack.status = 400;
resTrack.json.error = `Invalid solution provided for level ${levelId}`;
throw new Error(resTrack.json.error);
}

const moves = dirs.length;
const moves = directions.length;

// ensure no stats are saved for draft levels
if (level.isDraft || level.leastMoves === 0) {
Expand Down
Loading

0 comments on commit 95cab66

Please sign in to comment.