From 1f4cb6d27b172353a777b18bc0301cb6f5c6ebd2 Mon Sep 17 00:00:00 2001 From: tower1229 Date: Thu, 1 Feb 2024 19:44:13 +0800 Subject: [PATCH] feat: user select --- src/pages/game/_components/GameOver.tsx | 161 ++++++++++++++---------- 1 file changed, 93 insertions(+), 68 deletions(-) diff --git a/src/pages/game/_components/GameOver.tsx b/src/pages/game/_components/GameOver.tsx index 296f4d2..5738df3 100644 --- a/src/pages/game/_components/GameOver.tsx +++ b/src/pages/game/_components/GameOver.tsx @@ -7,7 +7,7 @@ import { useBlockNumber, useAccount, } from "wagmi"; -import { useState, useEffect } from "react"; +import { useState, useEffect, useRef } from "react"; import FileSaver from "file-saver"; import { generatePublicInput, gameState } from "../_utils"; import { @@ -31,52 +31,7 @@ const canisterId = import.meta.env.VITE_APP_CANISTERID; const actor = Actor.createActor(idlFactory, { agent, canisterId }); const ContractAddress = import.meta.env.VITE_APP_CONTRACT_ADDRESS; -{ - /* 2000000000000000n */ -} -/** - * - - * **/ export const GameOver = ({ onRefresh, onExit, @@ -84,15 +39,21 @@ export const GameOver = ({ onRefresh: () => void; onExit: () => void; }) => { + const balanceData = useRef(0n); const { address } = useAccount(); const { data: blockNumber } = useBlockNumber({ watch: true }); - const { data: balanceData, queryKey } = useBalance({ + const { data: balance, queryKey } = useBalance({ address, }); const queryClient = useQueryClient(); useEffect(() => { void queryClient.invalidateQueries({ queryKey }); }, [blockNumber, queryClient, queryKey]); + useEffect(() => { + if (balance?.value) { + balanceData.current = balance.value; + } + }, [balance]); const { path } = gameState; const [step, setStep] = useState(0); @@ -121,6 +82,10 @@ export const GameOver = ({ } }, [contractResult, onRefresh]); + const userSelect = useRef(); + + const hiddenStepIndex = useRef([]); + const SettlementProgress = [ { prefix: "$", @@ -191,24 +156,82 @@ export const GameOver = ({ prefix: "$", content: ( <> - Go next? + Verify result to Arbitrum Sepolia blockchain? + {userSelect.current !== false && ( + + )} + {userSelect.current !== true && ( + + )} + + ), + class: "", + run: () => { + return new Promise((resolve, reject) => { + const timer = setInterval(() => { + if (userSelect.current === true) { + clearInterval(timer); + resolve(true); + } else if (userSelect.current === false) { + clearInterval(timer); + reject(`User Cancel!`); + } + }, 200); + }); + }, + }, + { + prefix: "$", + content: ( + <> + Minimum 0.002 ETH required. ), - class: "text-warning", + class: "text-error", run: () => { - return new Promise((resolve) => resolve(true)); + return new Promise((resolve) => { + const timer = setInterval(() => { + console.log("balance=", balanceData.current); + if (balanceData.current > 2000000000000000n) { + hiddenStepIndex.current = [4]; + clearInterval(timer); + resolve(true); + } + }, 1000); + }); }, }, { @@ -331,18 +354,20 @@ export const GameOver = ({ return (
- {SettlementProgress.slice(0, step + 1).map((log, index) => ( -
-            {step === index && !errorMsg && (
-              
-            )}
-            {log.content}
-          
- ))} + {SettlementProgress.slice(0, step + 1).map((log, index) => { + return hiddenStepIndex.current.includes(index) ? null : ( +
+              {step === index && !errorMsg && (
+                
+              )}
+              {log.content}
+            
+ ); + })} {SettlementOver && (