Small changes in Hall + data/challengeCheck
[vchess.git] / client / src / data / challengeCheck.js
index 17cf296..f35eeb5 100644 (file)
@@ -1,5 +1,4 @@
-
-
+// TODO: rename file "timeControl.js" in utils/
 function timeUnitToSeconds(value, unit)
 {
   let seconds = value;
@@ -22,36 +21,41 @@ function isLargerUnit(unit1, unit2)
     || (unit1 == 'm' && unit2 == 's');
 }
 
-export function checkChallenge(c)
+export function extractTime(timeControl)
 {
-       const vid = parseInt(c.vid);
-       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)
-    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);
+  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 "Wrong time control";
+      return null;
     const incrementValue = parseInt(increment[1]);
     const incrementUnit = increment[2];
+    // Increment unit cannot be larger than main unit:
     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);
+      return null;
+    increment = timeUnitToSeconds(incrementValue, incrementUnit);
   }
-  else
-    c.increment = 0;
+  return {mainTime:mainTime, increment:increment};
+}
+
+// TODO: put this in Hall.vue
+export function checkChallenge(c)
+{
+       const vid = parseInt(c.vid);
+       if (isNaN(vid) || vid <= 0)
+               return "Please select a variant";
+
+  const tc = extractTime(c.timeControl);
+  if (!tc)
+    return "Wrong time control";
 
        // Basic alphanumeric check for players names
        let playerCount = 0;
@@ -75,6 +79,4 @@ export function checkChallenge(c)
     if (!V.IsGoodFen(c.fen))
       return "Bad FEN string";
   }
-  else //generate a FEN
-    c.fen = V.GenRandInitFen();
 }