Browse Source

fix gun game timeout logic

Kirk Trombley 3 years ago
parent
commit
7e1d79a6b8
1 changed files with 49 additions and 16 deletions
  1. 49 16
      client/src/components/screens/GamePanel/GuessPane/GuessPane.jsx

+ 49 - 16
client/src/components/screens/GamePanel/GuessPane/GuessPane.jsx

@@ -35,27 +35,56 @@ const GuessPane = () => {
   const targetPoint = useTargetPoint();
   const roundNum = useCurrentRound();
 
-  const handleSubmitGuess = async () => {
+  const handleSubmitGuess = useCallback(async () => {
     setSubmitted(true);
     if (!submitted) {
       await dispatch.submitGuess(selectedPoint);
     }
-  };
+  }, [selectedPoint, submitted]);
 
-  const handleGateKeeping = async ({ target }) => {
-    const { score } = await checkScore(
-      selectedPoint,
-      targetPoint,
-      scoreMethod,
-      roundNum
-    );
-    if (score < 4000) {
-      target.blur();
-      setGunGameBlock(true);
-    } else {
-      await handleSubmitGuess();
+  const handleGateKeeping = useCallback(
+    async ({ target }) => {
+      const { score } = await checkScore(
+        selectedPoint,
+        targetPoint,
+        scoreMethod,
+        roundNum
+      );
+      if (score < 4000) {
+        target.blur();
+        setGunGameBlock(true);
+      } else {
+        await handleSubmitGuess();
+      }
+    },
+    [handleSubmitGuess, roundNum, selectedPoint, targetPoint, scoreMethod]
+  );
+
+  const handleGateKeptTimeout = useCallback(async () => {
+    if (selectedPoint) {
+      const { score } = await checkScore(
+        selectedPoint,
+        targetPoint,
+        scoreMethod,
+        roundNum
+      );
+      if (score >= 4000) {
+        await handleSubmitGuess();
+        return;
+      }
+    }
+    setSubmitted(true);
+    if (!submitted) {
+      await dispatch.submitGuess(null);
     }
-  };
+  }, [
+    handleSubmitGuess,
+    submitted,
+    roundNum,
+    selectedPoint,
+    targetPoint,
+    scoreMethod,
+  ]);
 
   return (
     <>
@@ -72,7 +101,11 @@ const GuessPane = () => {
           Submit Guess
         </button>
         <ClickMarkerMap onMarkerMoved={setSelectedPoint} />
-        <RoundTimer onTimeout={handleSubmitGuess} />
+        <RoundTimer
+          onTimeout={
+            gameMode === GUN_GAME ? handleGateKeptTimeout : handleSubmitGuess
+          }
+        />
         <div
           className={styles.resize}
           onClick={toggleBig}