459396af6fa1ae1290cc2de8994a5d23a985cf15
[vchess.git] / server / data / challengeCheck.js
1 function checkChallenge(c)
2 {
3 const vid = parseInt(c.vid);
4 if (isNaN(vid) || vid <= 0)
5 return "Please select a variant";
6
7 const mainTime = parseInt(c.mainTime);
8 const increment = parseInt(c.increment);
9 if (isNaN(mainTime) || mainTime <= 0)
10 return "Main time should be strictly positive";
11 if (isNaN(increment) || increment < 0)
12 return "Increment must be positive";
13
14 // Basic alphanumeric check for players names
15 let playerCount = 0;
16 for (p of c.players)
17 {
18 if (p.name.length > 0)
19 {
20 if (!p.name.match(/^[\w]+$/))
21 return "Wrong characters in players names";
22 playerCount++;
23 }
24 }
25
26 if (playerCount > 0 && playerCount != c.nbPlayers-1)
27 return "None, or all of the opponent names must be filled"
28
29 // Just characters check on server:
30 if (!c.fen.match(/^[a-zA-Z0-9, /-]*$/))
31 return "Bad FEN string";
32 }
33
34 module.exports = checkChallenge;