1 // TODO: rename file "timeControl.js" in utils/
2 function timeUnitToSeconds(value
, unit
)
17 function isLargerUnit(unit1
, unit2
)
19 return (unit1
== 'd' && unit2
!= 'd')
20 || (unit1
== 'h' && ['s','m'].includes(unit2
))
21 || (unit1
== 'm' && unit2
== 's');
24 export function extractTime(timeControl
)
26 const tcParts
= timeControl
.replace(/ /g
,"").split('+');
27 const mainTimeArray
= tcParts
[0].match(/([0-9]+)([smhd])/);
30 const mainTimeValue
= parseInt(mainTimeArray
[1]);
31 const mainTimeUnit
= mainTimeArray
[2];
32 const mainTime
= timeUnitToSeconds(mainTimeValue
, mainTimeUnit
);
34 if (tcParts
.length
>= 2)
36 const increment
= tcParts
[1].match(/([0-9]+)([smhd])/);
39 const incrementValue
= parseInt(increment
[1]);
40 const incrementUnit
= increment
[2];
41 // Increment unit cannot be larger than main unit:
42 if (isLargerUnit(incrementUnit
, mainTimeUnit
))
44 increment
= timeUnitToSeconds(incrementValue
, incrementUnit
);
46 return {mainTime:mainTime
, increment:increment
};
49 // TODO: put this in Hall.vue
50 export function checkChallenge(c
)
52 const vid
= parseInt(c
.vid
);
53 if (isNaN(vid
) || vid
<= 0)
54 return "Please select a variant";
56 const tc
= extractTime(c
.timeControl
);
58 return "Wrong time control";
60 // Basic alphanumeric check for players names
62 for (const pname
of c
.to
)
66 // TODO: slightly redundant (see data/userCheck.js)
67 if (!pname
.match(/^[\w]+$/))
68 return "Wrong characters in players names";
73 if (playerCount
> 0 && playerCount
!= c
.nbPlayers
-1)
74 return "None, or all of the opponent names must be filled"
76 // Allow custom FEN (and check it) only for individual challenges
77 if (c
.fen
.length
> 0 && playerCount
> 0)
79 if (!V
.IsGoodFen(c
.fen
))
80 return "Bad FEN string";