Separate timeControl logic into utils/timeControl
[vchess.git] / client / src / data / challengeCheck.js
1 import { extractTime } from "@/utils/timeControl";
2
3 export function checkChallenge(c)
4 {
5 const vid = parseInt(c.vid);
6 if (isNaN(vid) || vid <= 0)
7 return "Please select a variant";
8
9 const tc = extractTime(c.timeControl);
10 if (!tc)
11 return "Wrong time control";
12 // Less than 3 days ==> live game (TODO: heuristic... 40 moves also)
13 c.liveGame = (tc.mainTime + 40 * tc.increment < 3*24*60*60);
14
15 // Basic alphanumeric check for players names
16 let playerCount = 0;
17 for (const pname of c.to)
18 {
19 if (pname.length > 0)
20 {
21 // TODO: slightly redundant (see data/userCheck.js)
22 if (!pname.match(/^[\w]+$/))
23 return "Wrong characters in players names";
24 playerCount++;
25 }
26 }
27
28 if (playerCount > 0 && playerCount != c.nbPlayers-1)
29 return "None, or all of the opponent names must be filled"
30
31 // Allow custom FEN (and check it) only for individual challenges
32 if (c.fen.length > 0 && playerCount > 0)
33 {
34 if (!V.IsGoodFen(c.fen))
35 return "Bad FEN string";
36 }
37 else
38 c.fen = "";
39 }