import { useState, useEffect } from "react"; import Web3 from "web3"; import { Button } from "./Button"; interface RevealProps { account: string; contract: any; config: Config | null; web3: Web3 | null; setStatus: (status: string) => void; selectedMove: string | null; secret: string; } type MoveName = "Rock" | "Paper" | "Scissors"; const MOVES: Record = { "1": { name: "Rock", icon: "✊" }, "2": { name: "Paper", icon: "✋" }, "3": { name: "Scissors", icon: "✌️" }, }; const OUTCOMES: Record = { 0: { name: "None", emoji: "❓", color: "gray" }, 1: { name: "You Won!", emoji: "🏆", color: "green" }, 2: { name: "You Lost", emoji: "😢", color: "red" }, 3: { name: "Draw", emoji: "🤝", color: "yellow" }, }; export default function Reveal({ account, contract, config, web3, setStatus, selectedMove, secret, }: Readonly) { const [loading, setLoading] = useState(false); const [bothRevealed, setBothRevealed] = useState(false); const [playerARevealed, setPlayerARevealed] = useState(false); const [playerBRevealed, setPlayerBRevealed] = useState(false); const [revealTimeLeft, setRevealTimeLeft] = useState(0); const [outcome, setOutcome] = useState(0); const [revealed, setRevealed] = useState(false); const clearMove = selectedMove && secret ? `${selectedMove}-${secret}` : ""; // Check game status on mount useEffect(() => { const checkStatus = async () => { if (!contract) return; try { const [br, par, pbr, rtl, out] = await Promise.all([ await contract.methods.bothRevealed().call({ from : account}), await contract.methods.playerARevealed().call({ from : account}), await contract.methods.playerBRevealed().call({ from : account}), await contract.methods.revealTimeLeft().call({ from : account}), await contract.methods.getLastWinner().call({ from : account}), ]); console.log("Status:", { br, par, pbr, rtl, out }); setBothRevealed(br); setPlayerARevealed(par); setPlayerBRevealed(pbr); setRevealTimeLeft(Number(rtl)); setOutcome(Number(out)); } catch (err: any) { console.error("Failed to check status:", err); } }; const interval = setInterval(checkStatus, 3000); checkStatus(); return () => clearInterval(interval); }, [contract, account]); const handleReveal = async () => { if (!contract || !web3 || !account || !clearMove) return; setLoading(true); setStatus(""); try { const tx = contract.methods.reveal(clearMove); const gas = await tx.estimateGas({ from: account }); const result = await (globalThis as any).ethereum.request({ method: "eth_sendTransaction", params: [ { from: account, to: config?.GAME_CONTRACT_ADDRESS, data: tx.encodeABI(), gas: web3.utils.toHex(gas), chainId: web3.utils.toHex(await web3.eth.net.getId()), }, ], }); setStatus("✅ Reveal tx sent: " + result); setRevealed(true); } catch (err: any) { setStatus("❌ Reveal failed: " + err.message); } finally { setLoading(false); } }; const handleGetOutcome = async () => { if (!contract || !web3 || !account) return; setLoading(true); setStatus(""); try { const tx = contract.methods.getOutcome(); const gas = await tx.estimateGas({ from: account }); const result = await (globalThis as any).ethereum.request({ method: "eth_sendTransaction", params: [ { from: account, to: config?.GAME_CONTRACT_ADDRESS, data: tx.encodeABI(), gas: web3.utils.toHex(gas), chainId: web3.utils.toHex(await web3.eth.net.getId()), }, ], }); setStatus("✅ Claim tx sent: " + result); } catch (err: any) { setStatus("❌ Claim failed: " + err.message); } finally { setLoading(false); } }; const outcomeData = OUTCOMES[outcome] || OUTCOMES[0]; return (
{/* Your Move Section */}

Your Move

{selectedMove ? (
{MOVES[selectedMove].icon} {MOVES[selectedMove].name}

Clear Move:

{clearMove}
) : (

No move selected yet

)}
{/* Game Status Section */}

{playerARevealed ? "✅" : "⏳"}

Player A

{playerARevealed ? "Revealed" : "Waiting"}

{playerBRevealed ? "✅" : "⏳"}

Player B

{playerBRevealed ? "Revealed" : "Waiting"}

⏱️

Time Left

{revealTimeLeft > 0 ? `${revealTimeLeft}s` : "Expired"}

{/* Reveal Section */}

Reveal Your Move

Submit your clear move and secret to the blockchain. This proves you didn't cheat!

{/* Winner Section - Only show if both revealed */} {bothRevealed && (

{outcomeData.emoji}

{outcomeData.name}

)} {/* Status Messages */} {!bothRevealed && !revealed && (

⏳ Waiting for both players to reveal...

)}
); }