From 161d82bee9f9e65680516a9cfd392e0cf297eadf Mon Sep 17 00:00:00 2001
From: nsfisis <nsfisis@gmail.com>
Date: Mon, 29 Jul 2024 02:58:54 +0900
Subject: feat: handle code and execresult messages

---
 frontend/app/components/GolfPlayApp.tsx | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

(limited to 'frontend/app/components/GolfPlayApp.tsx')

diff --git a/frontend/app/components/GolfPlayApp.tsx b/frontend/app/components/GolfPlayApp.tsx
index 31d1c44..c6c20d4 100644
--- a/frontend/app/components/GolfPlayApp.tsx
+++ b/frontend/app/components/GolfPlayApp.tsx
@@ -69,8 +69,11 @@ export default function GolfPlayApp({ game }: { game: Game }) {
   void setCurrentScore;
 
   const onCodeChange = useDebouncedCallback((code: string) => {
-    void code;
-    // sendJsonMessage({});
+    console.log("player:c2s:code");
+    sendJsonMessage({
+      type: "player:c2s:code",
+      data: { code },
+    });
   }, 1000);
 
   if (readyState === ReadyState.UNINSTANTIATED) {
@@ -104,6 +107,14 @@ export default function GolfPlayApp({ game }: { game: Game }) {
             setTimeLeftSeconds(start_at - nowSec);
             setGameState("starting");
           }
+        } else if (lastJsonMessage.type === "player:s2c:execresult") {
+          const { score } = lastJsonMessage.data;
+          if (
+            score !== null &&
+            (currentScore === null || score < currentScore)
+          ) {
+            setCurrentScore(score);
+          }
         }
       } else {
         setGameState("waiting");
@@ -111,7 +122,7 @@ export default function GolfPlayApp({ game }: { game: Game }) {
         sendJsonMessage({ type: "player:c2s:entry" });
       }
     }
-  }, [sendJsonMessage, lastJsonMessage, readyState, gameState]);
+  }, [sendJsonMessage, lastJsonMessage, readyState, gameState, currentScore]);
 
   if (gameState === "connecting") {
     return <GolfPlayAppConnecting />;
-- 
cgit v1.3.1

