0a3a9917e3a4eda28101c93e62984efffdad958e
3 function timeUnitToSeconds(value
, unit
)
18 function isLargerUnit(unit1
, unit2
)
20 return (unit1
== 'd' && unit2
!= 'd')
21 || (unit1
== 'h' && ['s','m'].includes(unit2
))
22 || (unit1
== 'm' && unit2
== 's');
25 export function checkChallenge(c
)
27 const vid
= parseInt(c
.vid
);
28 if (isNaN(vid
) || vid
<= 0)
29 return "Please select a variant";
31 const tcParts
= c
.timeControl
.replace(/ /g
,"").split('+');
32 const mainTime
= tcParts
[0].match(/([0-9]+)([smhd])/);
34 return "Wrong time control";
35 const mainTimeValue
= parseInt(mainTime
[1]);
36 const mainTimeUnit
= mainTime
[2];
37 if (isNaN(mainTimeValue
) || mainTimeValue
<= 0)
38 return "Main time should be strictly positive";
39 c
.mainTime
= timeUnitToSeconds(mainTimeValue
, mainTimeUnit
);
40 if (tcParts
.length
>= 2)
42 const increment
= tcParts
[1].match(/([0-9]+)([smhd])/);
44 return "Wrong time control";
45 const incrementValue
= parseInt(increment
[1]);
46 const incrementUnit
= increment
[2];
47 if (isLargerUnit(incrementUnit
, mainTimeUnit
))
48 return "Increment unit cannot be larger than main unit";
49 if (isNaN(incrementValue
) || incrementValue
< 0)
50 return "Increment must be positive";
51 c
.increment
= timeUnitToSeconds(incrementValue
, incrementUnit
);
54 // Basic alphanumeric check for players names
58 if (p
.name
.length
> 0)
60 // TODO: slightly redundant (see data/userCheck.js)
61 if (!p
.name
.match(/^[\w]+$/))
62 return "Wrong characters in players names";
67 if (playerCount
> 0 && playerCount
!= c
.nbPlayers
-1)
68 return "None, or all of the opponent names must be filled"
70 // Allow custom FEN (and check it) only for individual challenges
71 if (c
.fen
.length
> 0 && playerCount
> 0)
73 if (!V
.IsGoodFen(c
.fen
))
74 return "Bad FEN string";
77 c
.fen
= V
.GenRandInitFen();