From 26c45d18db49a79d4a59e42d76ebbae4dd00792c Mon Sep 17 00:00:00 2001 From: Lulox Date: Sat, 13 Jan 2024 09:28:19 -0300 Subject: [PATCH] Game is playable, still lacks withdraw mechanism --- packages/nextjs/.env.example | 1 + .../components/tictactoe/TicTacToeBoard.tsx | 60 ++++---- .../nextjs/contracts/deployedContracts.ts | 2 +- packages/nextjs/pages/index.tsx | 138 ++++-------------- packages/nextjs/types/TicTacToeTypes.ts | 6 +- 5 files changed, 60 insertions(+), 147 deletions(-) diff --git a/packages/nextjs/.env.example b/packages/nextjs/.env.example index c8d03d7..7a2909b 100644 --- a/packages/nextjs/.env.example +++ b/packages/nextjs/.env.example @@ -11,3 +11,4 @@ # More info: https://nextjs.org/docs/pages/building-your-application/configuring/environment-variables NEXT_PUBLIC_ALCHEMY_API_KEY= NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID= +NEXT_PUBLIC_DEPLOY_BLOCK= diff --git a/packages/nextjs/components/tictactoe/TicTacToeBoard.tsx b/packages/nextjs/components/tictactoe/TicTacToeBoard.tsx index 06ac6e6..5b76349 100755 --- a/packages/nextjs/components/tictactoe/TicTacToeBoard.tsx +++ b/packages/nextjs/components/tictactoe/TicTacToeBoard.tsx @@ -5,8 +5,13 @@ import { ethers } from "ethers"; import { useScaffoldContractRead, useScaffoldContractWrite } from "~~/hooks/scaffold-eth"; import { TicTacToeBoardProps } from "~~/types/TicTacToeTypes"; -const TicTacToeBoard: React.FC = ({ game, isGameAccepted, isGameFinished }) => { - const [position, setPosition] = useState(0); +const TicTacToeBoard: React.FC = ({ + game, + isGameAccepted, + isGameFinished, + currentPlayer, + // movesMade, +}) => { const [board, setBoard] = useState(Array(9).fill(0)); // Initialize an empty board const { data: boardFromContract } = useScaffoldContractRead({ @@ -15,16 +20,7 @@ const TicTacToeBoard: React.FC = ({ game, isGameAccepted, i args: [BigInt(game.gameId)], }); - // const { data: numberOfMoves } = useScaffoldContractRead({ - // contractName: "TicTacToe", - // functionName: "getNumberOfMoves", - // args: [BigInt(game.gameId)], - // }); - - console.log("boardFromContract: ", boardFromContract); - useEffect(() => { - // Update the local board based on the latest data from the contract if (boardFromContract) { setBoard(boardFromContract.map(Number)); } @@ -33,7 +29,7 @@ const TicTacToeBoard: React.FC = ({ game, isGameAccepted, i const { writeAsync: makeMove } = useScaffoldContractWrite({ contractName: "TicTacToe", functionName: "makeMove", - args: [BigInt(game.gameId), position], + args: [BigInt(game.gameId), 0], }); const { writeAsync: acceptGame } = useScaffoldContractWrite({ @@ -43,14 +39,6 @@ const TicTacToeBoard: React.FC = ({ game, isGameAccepted, i value: BigInt(game.bet), }); - const handleMakeMove = async () => { - try { - await makeMove(); - } catch (error) { - console.error("Error making move:", error); - } - }; - return ( @@ -60,8 +48,11 @@ const TicTacToeBoard: React.FC = ({ game, isGameAccepted, i -
{isGameAccepted ? "is playing against" : "has challenged"}{" "} -
+
{" "} + {isGameAccepted ? (isGameFinished ? "played against" : "is playing against") : "challenged"} + <> +
+ {isGameAccepted ? ( "" @@ -75,12 +66,17 @@ const TicTacToeBoard: React.FC = ({ game, isGameAccepted, i paddingBottom={3} > - Each player bets:
{parseFloat(ethers.formatEther(game.bet.toString())).toFixed(4)} ETH + Each player bets:
{" "} + {parseFloat(ethers.formatEther(game.bet.toString())).toFixed(4)} ETH
- + {game.player2 === currentPlayer ? ( + + ) : ( + "" + )} )} @@ -94,12 +90,13 @@ const TicTacToeBoard: React.FC = ({ game, isGameAccepted, i paddingBottom={3} > - Each player betted:
{parseFloat(ethers.formatEther(game.bet.toString())).toFixed(4)} ETH + Each player betted:
{" "} + {parseFloat(ethers.formatEther(game.bet.toString())).toFixed(4)} ETH
Game state:
- {isGameFinished ? "Finished" : "Not finished"} + {isGameFinished ? Finished : Not finished}
) : ( @@ -107,7 +104,7 @@ const TicTacToeBoard: React.FC = ({ game, isGameAccepted, i )} {/* Render the Tic Tac Toe board here */} {isGameAccepted ? ( - + {board.map((cell, index) => (