Separate client and server codes. Keep everything in one git repo for simplicity
[vchess.git] / client / client_OLD / javascripts / shared / challengeCheck.js
1 // 'vname' for 'variant name' is defined when run on client side
2 function checkChallenge(c, vname)
3 {
4 const vid = parseInt(c.vid);
5 if (isNaN(vid) || vid <= 0)
6 return "Please select a variant";
7
8 const mainTime = parseInt(c.mainTime);
9 const increment = parseInt(c.increment);
10 if (isNaN(mainTime) || mainTime <= 0)
11 return "Main time should be strictly positive";
12 if (isNaN(increment) || increment < 0)
13 return "Increment must be positive";
14
15 // Basic alphanumeric check for players names
16 let playerCount = 0;
17 for (p of c.players)
18 {
19 if (p.name.length > 0)
20 {
21 if (!p.name.match(/^[\w]+$/))
22 return "Wrong characters in players names";
23 playerCount++;
24 }
25 }
26
27 if (playerCount > 0 && playerCount != c.nbPlayers-1)
28 return "None, or all of the opponent names must be filled"
29
30 if (typeof document !== "undefined") //client side
31 {
32 const V = eval(vname + "Rules");
33 // Allow custom FEN (and check it) only for individual challenges
34 if (c.fen.length > 0 && playerCount > 0)
35 {
36 if (!V.IsGoodFen(c.fen))
37 return "Bad FEN string";
38 }
39 else
40 {
41 // Generate a FEN
42 c.fen = V.GenRandInitFen();
43 }
44 }
45 else
46 {
47 // Just characters check on server:
48 if (!c.fen.match(/^[a-zA-Z0-9, /-]*$/))
49 return "Bad FEN string";
50 }
51 }
52
53 try { module.exports = checkChallenge; } catch(e) { } //for server