From 8ef2edfa55bb960cfdebe99c99f781173d8da913 Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Tue, 12 Feb 2019 17:52:50 +0100
Subject: [PATCH] Small changes in Hall + data/challengeCheck

---
 client/src/data/challengeCheck.js | 52 ++++++++++++++++---------------
 client/src/views/Hall.vue         | 27 +++++++++-------
 2 files changed, 43 insertions(+), 36 deletions(-)

diff --git a/client/src/data/challengeCheck.js b/client/src/data/challengeCheck.js
index 17cf296f..f35eeb57 100644
--- a/client/src/data/challengeCheck.js
+++ b/client/src/data/challengeCheck.js
@@ -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();
 }
diff --git a/client/src/views/Hall.vue b/client/src/views/Hall.vue
index 08867a71..822c90b2 100644
--- a/client/src/views/Hall.vue
+++ b/client/src/views/Hall.vue
@@ -63,6 +63,7 @@ import { NbPlayers } from "@/data/nbPlayers";
 import { checkChallenge } from "@/data/challengeCheck";
 import { ArrayFun } from "@/utils/array";
 import { ajax } from "@/utils/ajax";
+import { genRandString } from "@/utils/alea";
 import GameList from "@/components/GameList.vue";
 import ChallengeList from "@/components/ChallengeList.vue";
 export default {
@@ -284,6 +285,14 @@ export default {
 //      if (this.settings.sound >= 1)
 //        new Audio("/sounds/newgame.mp3").play().catch(err => {});
 //    },
+    // Load a variant file (TODO: should probably be global)
+    loadVariant: async function(vid, variantArray) {
+      const idxInVariants = variantArray.findIndex(v => v.id == vid);
+      const vname = variantArray[idxInVariants].name;
+      const vModule = await import("@/variants/" + vname + ".js");
+      window.V = vModule.VariantRules;
+      return vname;
+    },
     // Send new challenge (corr or live, cf. time control), with button or click on player
     newChallenge: async function() {
       if (this.challenges.some(c => c.from.sid == this.st.user.sid))
@@ -291,16 +300,16 @@ export default {
         document.getElementById("modalNewgame").checked = false;
         return alert("You already have a pending challenge");
       }
-      const idxInVariants =
-        this.st.variants.findIndex(v => v.id == this.newchallenge.vid);
-      const vname = this.st.variants[idxInVariants].name;
-      const vModule = await import("@/variants/" + vname + ".js");
-      window.V = vModule.VariantRules;
+      // TODO: put this "load variant" block elsewhere
+      const vname = this.loadVariant(this.newchallenge.vid, this.st.variants);
       // checkChallenge side-effect = set FEN, and mainTime + increment in seconds
       // TODO: should not be a side-effect but set here ; for received server challenges we do not have mainTime+increment
       const error = checkChallenge(this.newchallenge);
       if (!!error)
         return alert(error);
+// TODO: set FEN, set mainTime and increment ?!
+else //generate a FEN
+    c.fen = V.GenRandInitFen();
       // Less than 3 days ==> live game (TODO: heuristic... 40 moves also)
       const liveGame =
         this.newchallenge.mainTime + 40 * this.newchallenge.increment < 3*24*60*60;
@@ -337,7 +346,8 @@ export default {
           p.sid = this.players[pIdx].sid;
         }
       }
-      const finishAddChallenge = () => {
+      const finishAddChallenge = (cid) => {
+        chall.id = cid || "c" + genRandString();
         this.challenges.push(chall);
         // Send challenge to peers
         let challSock =
@@ -368,11 +378,6 @@ export default {
         }
         document.getElementById("modalNewgame").checked = false;
       };
-      
-      
-      // TODO: challenges all have IDs: "c" + genRandString()
-      
-      
       if (liveGame)
       {
         // Live challenges have cid = 0
-- 
2.44.0