X-Git-Url: https://git.auder.net/?p=vchess.git;a=blobdiff_plain;f=client%2Fsrc%2Fdata%2FchallengeCheck.js;h=db205ac7b8953cd31bd908d868f160175f39e17e;hp=85d8571d1bee65d6e71b9df28c18d392405ad643;hb=4f298adbee00942323fc7ec517117552aeb5a08a;hpb=8d61fc4ab7373b4a576f3f9108cdf7768ae27096 diff --git a/client/src/data/challengeCheck.js b/client/src/data/challengeCheck.js index 85d8571d..db205ac7 100644 --- a/client/src/data/challengeCheck.js +++ b/client/src/data/challengeCheck.js @@ -1,53 +1,29 @@ -// 'vname' for 'variant name' is defined when run on client side -function checkChallenge(c, vname) -{ - const vid = parseInt(c.vid); - if (isNaN(vid) || vid <= 0) - return "Please select a variant"; +import { extractTime } from "@/utils/timeControl"; - const mainTime = parseInt(c.mainTime); - const increment = parseInt(c.increment); - if (isNaN(mainTime) || mainTime <= 0) - return "Main time should be strictly positive"; - if (isNaN(increment) || increment < 0) - return "Increment must be positive"; +export function checkChallenge(c) +{ + const vid = parseInt(c.vid); + if (isNaN(vid) || vid <= 0) + return "Please select a variant"; - // Basic alphanumeric check for players names - let playerCount = 0; - for (p of c.players) - { - if (p.name.length > 0) - { - if (!p.name.match(/^[\w]+$/)) - return "Wrong characters in players names"; - playerCount++; - } - } + const tc = extractTime(c.timeControl); + if (!tc) + return "Wrong time control"; - if (playerCount > 0 && playerCount != c.nbPlayers-1) - return "None, or all of the opponent names must be filled" + // Basic alphanumeric check for opponent name + if (!!c.to) + { + // NOTE: slightly redundant (see data/userCheck.js) + if (!c.to.match(/^[\w]+$/)) + return "Wrong characters in opponent name"; + } - 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 && !!c.to) + { + if (!V.IsGoodFen(c.fen)) + return "Bad FEN string"; + } + else + c.fen = ""; } - -try { module.exports = checkChallenge; } catch(e) { } //for server