X-Git-Url: https://git.auder.net/?p=vchess.git;a=blobdiff_plain;f=client%2Fsrc%2Fdata%2FchallengeCheck.js;h=e0aa978cda51ba36f589eb2d53da883fa2cb96b6;hp=f35eeb574d33411c1e7f4d29342bc74451e35897;hb=dd75774d31a140cabf80790bdade9a40048c38d5;hpb=81d9ce72c32c32e62a8b43ca8e94fcb974f4a685 diff --git a/client/src/data/challengeCheck.js b/client/src/data/challengeCheck.js index f35eeb57..e0aa978c 100644 --- a/client/src/data/challengeCheck.js +++ b/client/src/data/challengeCheck.js @@ -1,52 +1,5 @@ -// TODO: rename file "timeControl.js" in utils/ -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 extractTime(timeControl) -{ - const tcParts = timeControl.replace(/ /g,"").split('+'); - const mainTimeArray = tcParts[0].match(/([0-9]+)([smhd])/); - if (!mainTimeArray) - return null; - const mainTimeValue = parseInt(mainTimeArray[1]); - const mainTimeUnit = mainTimeArray[2]; - const mainTime = timeUnitToSeconds(mainTimeValue, mainTimeUnit); - let increment = 0; - if (tcParts.length >= 2) - { - const increment = tcParts[1].match(/([0-9]+)([smhd])/); - if (!increment) - return null; - const incrementValue = parseInt(increment[1]); - const incrementUnit = increment[2]; - // Increment unit cannot be larger than main unit: - if (isLargerUnit(incrementUnit, mainTimeUnit)) - return null; - increment = timeUnitToSeconds(incrementValue, incrementUnit); - } - return {mainTime:mainTime, increment:increment}; -} +import { extractTime } from "@/utils/timeControl"; -// TODO: put this in Hall.vue export function checkChallenge(c) { const vid = parseInt(c.vid); @@ -56,6 +9,8 @@ export function checkChallenge(c) const tc = extractTime(c.timeControl); if (!tc) return "Wrong time control"; + // 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; @@ -79,4 +34,6 @@ export function checkChallenge(c) if (!V.IsGoodFen(c.fen)) return "Bad FEN string"; } + else + c.fen = ""; }