X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Fdata%2FchallengeCheck.js;h=0a3a9917e3a4eda28101c93e62984efffdad958e;hb=9d58ef95e3affd799571838164f7c5bbfda11f64;hp=85d8571d1bee65d6e71b9df28c18d392405ad643;hpb=5d3e317ac0e2bc7a6575768ddb972b35e8680bc8;p=vchess.git diff --git a/client/src/data/challengeCheck.js b/client/src/data/challengeCheck.js index 85d8571d..0a3a9917 100644 --- a/client/src/data/challengeCheck.js +++ b/client/src/data/challengeCheck.js @@ -1,16 +1,55 @@ -// 'vname' for 'variant name' is defined when run on client side -function checkChallenge(c, vname) + + +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'); +} + +export function checkChallenge(c) { const vid = parseInt(c.vid); if (isNaN(vid) || vid <= 0) return "Please select a variant"; - const mainTime = parseInt(c.mainTime); - const increment = parseInt(c.increment); - if (isNaN(mainTime) || mainTime <= 0) + const tcParts = c.timeControl.replace(/ /g,"").split('+'); + const mainTime = tcParts[0].match(/([0-9]+)([smhd])/); + if (!mainTime) + 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"; - if (isNaN(increment) || increment < 0) - return "Increment must be 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); + } // Basic alphanumeric check for players names let playerCount = 0; @@ -18,6 +57,7 @@ function checkChallenge(c, vname) { if (p.name.length > 0) { + // TODO: slightly redundant (see data/userCheck.js) if (!p.name.match(/^[\w]+$/)) return "Wrong characters in players names"; playerCount++; @@ -27,27 +67,12 @@ function checkChallenge(c, vname) if (playerCount > 0 && playerCount != c.nbPlayers-1) return "None, or all of the opponent names must be filled" - if (typeof document !== "undefined") //client side - { - const V = eval(vname + "Rules"); - // Allow custom FEN (and check it) only for individual challenges - if (c.fen.length > 0 && playerCount > 0) - { - if (!V.IsGoodFen(c.fen)) - return "Bad FEN string"; - } - else - { - // Generate a FEN - c.fen = V.GenRandInitFen(); - } - } - else - { - // Just characters check on server: - if (!c.fen.match(/^[a-zA-Z0-9, /-]*$/)) - return "Bad FEN string"; - } + // Allow custom FEN (and check it) only for individual challenges + if (c.fen.length > 0 && playerCount > 0) + { + if (!V.IsGoodFen(c.fen)) + return "Bad FEN string"; + } + else //generate a FEN + c.fen = V.GenRandInitFen(); } - -try { module.exports = checkChallenge; } catch(e) { } //for server