Separate timeControl logic into utils/timeControl
[vchess.git] / client / src / data / challengeCheck.js
CommitLineData
dd75774d 1import { extractTime } from "@/utils/timeControl";
8ef2edfa 2
8ef2edfa
BA
3export 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";
dd75774d
BA
12 // Less than 3 days ==> live game (TODO: heuristic... 40 moves also)
13 c.liveGame = (tc.mainTime + 40 * tc.increment < 3*24*60*60);
ab4f4bf2
BA
14
15 // Basic alphanumeric check for players names
16 let playerCount = 0;
6faa92f2 17 for (const pname of c.to)
ab4f4bf2 18 {
6faa92f2 19 if (pname.length > 0)
ab4f4bf2 20 {
9d58ef95 21 // TODO: slightly redundant (see data/userCheck.js)
6faa92f2 22 if (!pname.match(/^[\w]+$/))
ab4f4bf2
BA
23 return "Wrong characters in players names";
24 playerCount++;
25 }
26 }
27
74ea2e8d 28 if (playerCount > 0 && playerCount != c.nbPlayers-1)
ab4f4bf2
BA
29 return "None, or all of the opponent names must be filled"
30
9d58ef95
BA
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 }
dd75774d
BA
37 else
38 c.fen = "";
ab4f4bf2 39}