1 function checkChallenge(c
)
3 const vid
= parseInt(c
.vid
);
4 if (isNaN(vid
) || vid
<= 0)
5 return "Please select a variant";
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";
14 // Basic alphanumeric check for players names
20 if (!p
.match(/^[\w]+$/))
21 return "Wrong characters in players names";
26 if (playerCount
> 0 && playerCount
!= c
.nbPlayers
)
27 return "None, or all of the opponent names must be filled"
29 if (!!document
) //client side
31 const idxInVariants
= variantArray
.findIndex(v
=> v
.id
== c
.vid
);
32 const vname
= variantArray
[idxInVariants
].name
;
33 const scriptId
= vname
+ "RulesScript";
34 const afterRulesAreLoaded
= () => {
35 const V
= eval(vname
+ "Rules");
36 // Allow custom FEN (and check it) only for individual challenges
37 if (c
.fen
.length
> 0 && playerCount
> 0)
39 if (!V
.IsGoodFen(c
.fen
))
40 return "Bad FEN string";
45 c
.fen
= V
.GenRandInitFen();
48 if (!document
.getElementById(scriptId
))
50 // Load variant rules (only once)
51 var script
= document
.createElement("script");
53 script
.src
= "/javascripts/variants/" + vname
+ ".js";
54 document
.body
.appendChild(script
);
55 script
.onload
= afterRulesAreLoaded
;
60 // Just characters check on server:
61 if (!c
.fen
.match(/^[a-zA-Z0-9, /-]*$/))
62 return "Bad FEN string";
66 try { module
.exports
= checkChallenge
; } catch(e
) { } //for server