X-Git-Url: https://git.auder.net/?p=vchess.git;a=blobdiff_plain;f=client%2Fsrc%2Fdata%2FchallengeCheck.js;h=e0aa978cda51ba36f589eb2d53da883fa2cb96b6;hp=c533b11d6bebc8342103389566dbcea6b554a64a;hb=dd75774d31a140cabf80790bdade9a40048c38d5;hpb=03608482744c433ad284f4b61e85b2e8cff4f5f3 diff --git a/client/src/data/challengeCheck.js b/client/src/data/challengeCheck.js index c533b11d..e0aa978c 100644 --- a/client/src/data/challengeCheck.js +++ b/client/src/data/challengeCheck.js @@ -1,26 +1,4 @@ - - -function timeUnitToSeconds(value, unit) -{ - let seconds = value; - switch (unit) - { - case 'd': - seconds *= 24; - case 'h': - seconds *= 60; - case 'm': - seconds *= 60; - } - return seconds; -} - -function isLargerUnit(unit1, unit2) -{ - return (unit1 == 'd' && unit2 != 'd') - || (unit1 == 'h' && ['s','m'].includes(unit2)) - || (unit1 == 'm' && unit2 == 's'); -} +import { extractTime } from "@/utils/timeControl"; export function checkChallenge(c) { @@ -28,39 +6,20 @@ export function checkChallenge(c) if (isNaN(vid) || vid <= 0) return "Please select a variant"; - const tcParts = c.timeControl.replace(/ /g,"").split('+'); - const mainTime = tcParts[0].match(/([0-9]+)([smhd])/); - if (!mainTime) + const tc = extractTime(c.timeControl); + if (!tc) return "Wrong time control"; - const mainTimeValue = parseInt(mainTime[1]); - const mainTimeUnit = mainTime[2]; - if (isNaN(mainTimeValue) || mainTimeValue <= 0) - return "Main time should be strictly positive"; - c.mainTime = timeUnitToSeconds(mainTimeValue, mainTimeUnit); - if (tcParts.length >= 2) - { - const increment = tcParts[1].match(/([0-9]+)([smhd])/); - if (!increment) - return "Wrong time control"; - const incrementValue = parseInt(increment[1]); - const incrementUnit = increment[2]; - if (isLargerUnit(incrementUnit, mainTimeUnit)) - return "Increment unit cannot be larger than main unit"; - if (isNaN(incrementValue) || incrementValue < 0) - return "Increment must be positive"; - c.increment = timeUnitToSeconds(incrementValue, incrementUnit); - } - else - c.increment = 0; + // Less than 3 days ==> live game (TODO: heuristic... 40 moves also) + c.liveGame = (tc.mainTime + 40 * tc.increment < 3*24*60*60); // Basic alphanumeric check for players names let playerCount = 0; - for (const p of c.to) + for (const pname of c.to) { - if (p.name.length > 0) + if (pname.length > 0) { // TODO: slightly redundant (see data/userCheck.js) - if (!p.name.match(/^[\w]+$/)) + if (!pname.match(/^[\w]+$/)) return "Wrong characters in players names"; playerCount++; } @@ -75,6 +34,6 @@ export function checkChallenge(c) if (!V.IsGoodFen(c.fen)) return "Bad FEN string"; } - else //generate a FEN - c.fen = V.GenRandInitFen(); + else + c.fen = ""; }