Commit | Line | Data |
---|---|---|
74ea2e8d BA |
1 | // 'vname' for 'variant name' is defined when run on client side |
2 | function checkChallenge(c, vname) | |
ab4f4bf2 BA |
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 | { | |
74ea2e8d | 19 | if (p.name.length > 0) |
ab4f4bf2 | 20 | { |
74ea2e8d | 21 | if (!p.name.match(/^[\w]+$/)) |
ab4f4bf2 BA |
22 | return "Wrong characters in players names"; |
23 | playerCount++; | |
24 | } | |
25 | } | |
26 | ||
74ea2e8d | 27 | if (playerCount > 0 && playerCount != c.nbPlayers-1) |
ab4f4bf2 BA |
28 | return "None, or all of the opponent names must be filled" |
29 | ||
74ea2e8d | 30 | if (typeof document !== "undefined") //client side |
ab4f4bf2 | 31 | { |
74ea2e8d BA |
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) | |
ab4f4bf2 | 35 | { |
74ea2e8d BA |
36 | if (!V.IsGoodFen(c.fen)) |
37 | return "Bad FEN string"; | |
38 | } | |
39 | else | |
40 | { | |
41 | // Generate a FEN | |
42 | c.fen = V.GenRandInitFen(); | |
ab4f4bf2 BA |
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 |