import { extractTime } from "@/utils/timeControl";
-export function checkChallenge(c)
-{
- const vid = parseInt(c.vid);
- if (isNaN(vid) || vid <= 0)
- return "Please select a variant";
+export function checkChallenge(c) {
+ const vid = parseInt(c.vid, 10);
+ if (isNaN(vid) || vid <= 0) return "Please select a variant";
- const tc = extractTime(c.timeControl);
- if (!tc)
- return "Wrong time control";
- // Less than 3 days ==> live game (TODO: heuristic... 40 moves also)
- c.liveGame = (tc.mainTime + 40 * tc.increment < 3*24*60*60);
+ const tc = extractTime(c.cadence);
+ if (!tc) return "Wrong time control";
- // Basic alphanumeric check for players names
- let playerCount = 0;
- for (const pname of c.to)
- {
- if (pname.length > 0)
- {
- // TODO: slightly redundant (see data/userCheck.js)
- if (!pname.match(/^[\w]+$/))
- return "Wrong characters in players names";
- playerCount++;
- }
- }
-
- if (playerCount > 0 && playerCount != c.nbPlayers-1)
- return "None, or all of the opponent names must be filled"
+ // Basic alphanumeric check for opponent name
+ if (c.to) {
+ // NOTE: slightly redundant (see data/userCheck.js)
+ if (!c.to.match(/^[\w]+$/)) return "Name: alphanumerics and underscore";
+ }
// Allow custom FEN (and check it) only for individual challenges
- if (c.fen.length > 0 && playerCount > 0)
- {
- if (!V.IsGoodFen(c.fen))
- return "Bad FEN string";
+ if (c.fen.length > 0 && !!c.to) {
+ if (!V.IsGoodFen(c.fen)) return "Errors in FEN";
}
- else
- c.fen = "";
+ else c.fen = "";
+
+ return "";
}