diff --git a/packages/hardhat/contracts/TicTacToe.sol b/packages/hardhat/contracts/TicTacToe.sol
index 56f4e2a..34b99b8 100644
--- a/packages/hardhat/contracts/TicTacToe.sol
+++ b/packages/hardhat/contracts/TicTacToe.sol
@@ -307,6 +307,10 @@ contract TicTacToe {
return games[_gameId].moves % 2 == 0 ? 1 : 2;
}
+ function getNumberOfMoves(uint256 _gameId) public view returns (uint8) {
+ return games[_gameId].moves;
+ }
+
function getBoard(uint256 _gameId) external view returns (uint8[9] memory) {
return games[_gameId].board;
}
diff --git a/packages/nextjs/components/Header.tsx b/packages/nextjs/components/Header.tsx
index 11d9754..1bfe22c 100644
--- a/packages/nextjs/components/Header.tsx
+++ b/packages/nextjs/components/Header.tsx
@@ -92,12 +92,7 @@ export const Header = () => {
TicTacToe
-
- Made with{" "}
-
- Scaffold-ETH 2
-
-
+ Made with Scaffold-ETH 2
diff --git a/packages/nextjs/components/tictactoe/TicTacToeBoard.tsx b/packages/nextjs/components/tictactoe/TicTacToeBoard.tsx
index 28db690..2cdbdd0 100755
--- a/packages/nextjs/components/tictactoe/TicTacToeBoard.tsx
+++ b/packages/nextjs/components/tictactoe/TicTacToeBoard.tsx
@@ -1,28 +1,35 @@
import React, { useEffect, useState } from "react";
import { Address } from "../scaffold-eth";
-import { Button, Flex, Grid } from "@chakra-ui/react";
+import { Box, Button, Flex, Grid } from "@chakra-ui/react";
import { ethers } from "ethers";
import { useScaffoldContractRead, useScaffoldContractWrite } from "~~/hooks/scaffold-eth";
import { TicTacToeBoardProps } from "~~/types/TicTacToeTypes";
-const TicTacToeBoard: React.FC = ({ game, isGameAccepted, movesAmount, isGameFinished }) => {
+const TicTacToeBoard: React.FC = ({ game, isGameAccepted, isGameFinished }) => {
const [position, setPosition] = useState(0);
const [board, setBoard] = useState(Array(9).fill(0)); // Initialize an empty board
- const { data: getBoard } = useScaffoldContractRead({
+ const { data: boardFromContract } = useScaffoldContractRead({
contractName: "TicTacToe",
functionName: "getBoard",
args: [BigInt(game.gameId)],
});
- console.log("getBoard reads: ", getBoard);
+ const { data: numberOfMoves } = useScaffoldContractRead({
+ contractName: "TicTacToe",
+ functionName: "getNumberOfMoves",
+ args: [BigInt(game.gameId)],
+ });
+
+ console.log("boardFromContract: ", boardFromContract);
+ console.log("numberOfMoves: ", numberOfMoves);
useEffect(() => {
// Update the local board based on the latest data from the contract
- if (getBoard) {
- setBoard(getBoard.map(Number));
+ if (boardFromContract) {
+ setBoard(boardFromContract.map(Number));
}
- }, [getBoard]);
+ }, [boardFromContract]);
const { writeAsync: makeMove } = useScaffoldContractWrite({
contractName: "TicTacToe",
@@ -46,68 +53,94 @@ const TicTacToeBoard: React.FC = ({ game, isGameAccepted, m
};
return (
-
-
- GameId: {game.gameId}
+
+
+ Game Id #
+
+ {game.gameId}
+
-
-
+
+
Player 1:
-
-
+
+
Player 2:
-
-
- Each player bets:
{parseFloat(ethers.formatEther(game.bet.toString())).toFixed(4)} ETH
-
+
-
-
- Is game accepted?:
- {isGameAccepted ? (
- "Yes"
- ) : (
-
-
- # of moves made:
- {movesAmount}
-
-
- Is game finished?:
- {isGameFinished ? "Yes" : "No"}
-
-
-
+
+
+ Each player bets:
{parseFloat(ethers.formatEther(game.bet.toString())).toFixed(4)} ETH
+
+
+ )}
+ {isGameAccepted ? (
+
+
+ Each player betted:
{parseFloat(ethers.formatEther(game.bet.toString())).toFixed(4)} ETH
+
+
+ # of moves made:
+ {numberOfMoves}
+
+
+ Game state:
+ {isGameFinished ? "Finished" : "Not finished"}
+
+
+ ) : (
+ ""
+ )}
{/* Render the Tic Tac Toe board here */}
-
- {board.map((cell, index) => (
- {
- if (!isGameFinished) {
- setPosition(index);
- handleMakeMove();
- }
- }}
- >
- {cell === 1 ? "✖️" : cell === 2 ? "⭕" : ""}
-
- ))}
-
-
-
-
+ {isGameAccepted ? (
+
+ {board.map((cell, index) => (
+ {
+ if (!isGameFinished) {
+ setPosition(index);
+ handleMakeMove();
+ }
+ }}
+ >
+ {cell === 1 ? "✖️" : cell === 2 ? "⭕" : ""}
+
+ ))}
+
+ ) : (
+ ""
+ )}
+
);
};
diff --git a/packages/nextjs/contracts/deployedContracts.ts b/packages/nextjs/contracts/deployedContracts.ts
index 0aeebd6..6aaaf0f 100644
--- a/packages/nextjs/contracts/deployedContracts.ts
+++ b/packages/nextjs/contracts/deployedContracts.ts
@@ -7,7 +7,7 @@ import { GenericContractsDeclaration } from "~~/utils/scaffold-eth/contract";
const deployedContracts = {
31337: {
TicTacToe: {
- address: "0x5FC8d32690cc91D4c39d9d3abcBD16989F875707",
+ address: "0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0",
abi: [
{
anonymous: false,
@@ -241,6 +241,25 @@ const deployedContracts = {
stateMutability: "view",
type: "function",
},
+ {
+ inputs: [
+ {
+ internalType: "uint256",
+ name: "_gameId",
+ type: "uint256",
+ },
+ ],
+ name: "getNumberOfMoves",
+ outputs: [
+ {
+ internalType: "uint8",
+ name: "",
+ type: "uint8",
+ },
+ ],
+ stateMutability: "view",
+ type: "function",
+ },
{
inputs: [
{
diff --git a/packages/nextjs/pages/index.tsx b/packages/nextjs/pages/index.tsx
index 7285eeb..72a2887 100644
--- a/packages/nextjs/pages/index.tsx
+++ b/packages/nextjs/pages/index.tsx
@@ -1,18 +1,25 @@
import { useEffect, useState } from "react";
-import { GameAcceptedProps, GameCreatedProps, GameFinishedProps, MoveMadeProps } from "../types/TicTacToeTypes";
+import { GameAcceptedProps, GameCreatedProps, GameFinishedProps } from "../types/TicTacToeTypes";
import { Card, CardBody, Flex, Heading } from "@chakra-ui/react";
import type { NextPage } from "next";
import { MetaHeader } from "~~/components/MetaHeader";
import CreateChallengeBox from "~~/components/tictactoe/CreateChallengeBox";
import TicTacToeBoard from "~~/components/tictactoe/TicTacToeBoard";
-import { useScaffoldEventHistory, useScaffoldEventSubscriber } from "~~/hooks/scaffold-eth";
+import { useScaffoldContractRead, useScaffoldEventHistory, useScaffoldEventSubscriber } from "~~/hooks/scaffold-eth";
const Home: NextPage = () => {
const [gameHistory, setGameHistory] = useState([]);
const [gameAcceptedHistory, setGameAcceptedHistory] = useState([]);
- const [moveMadeHistory, setMoveMadeHistory] = useState([]);
const [gameFinishedHistory, setGameFinishedHistory] = useState([]);
+ const { data: gameIdCounter } = useScaffoldContractRead({
+ contractName: "TicTacToe",
+ functionName: "gameIdCounter",
+ args: undefined,
+ });
+
+ console.log("gameIdCounter: ", gameIdCounter);
+
// Event history hooks
const { data: GameCreatedHistory } = useScaffoldEventHistory({
contractName: "TicTacToe",
@@ -28,13 +35,6 @@ const Home: NextPage = () => {
blockData: false,
});
- const { data: MoveMadeHistory } = useScaffoldEventHistory({
- contractName: "TicTacToe",
- eventName: "MoveMade",
- fromBlock: BigInt(process.env.NEXT_PUBLIC_DEPLOY_BLOCK || "0"),
- blockData: false,
- });
-
const { data: GameFinishedHistory } = useScaffoldEventHistory({
contractName: "TicTacToe",
eventName: "GameFinished",
@@ -62,15 +62,6 @@ const Home: NextPage = () => {
setGameAcceptedHistory(mappedHistory);
}, [GameAcceptedHistory]);
- useEffect(() => {
- const mappedHistory = MoveMadeHistory?.map(event => ({
- gameId: parseInt(event.args[0].toString()),
- player: event.args[1],
- position: parseInt(event.args[2].toString()),
- })) as MoveMadeProps[];
- setMoveMadeHistory(mappedHistory);
- }, [MoveMadeHistory]);
-
useEffect(() => {
const mappedHistory = GameFinishedHistory?.map(event => ({
gameId: parseInt(event.args[0].toString()),
@@ -122,21 +113,6 @@ const Home: NextPage = () => {
},
});
- useScaffoldEventSubscriber({
- contractName: "TicTacToe",
- eventName: "MoveMade",
- listener: (logs: any[]) => {
- setMoveMadeHistory(indexedHistory => {
- const newMoveMade: MoveMadeProps = {
- gameId: parseInt(logs[0].args[0].toString()),
- player: logs[0].args[1],
- position: parseInt(logs[0].args[2].toString()),
- };
- return [newMoveMade, ...indexedHistory];
- });
- },
- });
-
useScaffoldEventSubscriber({
contractName: "TicTacToe",
eventName: "GameFinished",
@@ -154,11 +130,9 @@ const Home: NextPage = () => {
const gameCards = gameHistory?.map(game => {
const isGameAccepted = gameAcceptedHistory.some(acceptedGame => acceptedGame.gameId === game.gameId);
- const movesList = moveMadeHistory.filter(move => move.gameId === game.gameId);
- const movesAmount = movesList.length;
const isGameFinished = gameFinishedHistory.some(finishedGame => finishedGame.gameId === game.gameId);
- return { game, isGameAccepted, movesList, movesAmount, isGameFinished };
+ return { game, isGameAccepted, isGameFinished };
});
return (
@@ -184,14 +158,14 @@ const Home: NextPage = () => {
filter: "brightness(0.3)",
}}
/>
-
+
{
⭕ See your active challenges! ❌
- {gameCards?.map(({ game, isGameAccepted, movesList, movesAmount, isGameFinished }) => (
+ {gameCards?.map(({ game, isGameAccepted, isGameFinished }) => (
))}
diff --git a/packages/nextjs/types/TicTacToeTypes.ts b/packages/nextjs/types/TicTacToeTypes.ts
index 7f61436..8543c0b 100644
--- a/packages/nextjs/types/TicTacToeTypes.ts
+++ b/packages/nextjs/types/TicTacToeTypes.ts
@@ -26,7 +26,5 @@ export type GameFinishedProps = {
export type TicTacToeBoardProps = {
game: GameCreatedProps;
isGameAccepted: boolean;
- movesList: MoveMadeProps[];
- movesAmount: number;
isGameFinished: boolean;
};