Skip to content

Commit

Permalink
Started migrating with some errors
Browse files Browse the repository at this point in the history
  • Loading branch information
luloxi committed Jan 6, 2024
1 parent 4177484 commit 6fc3c5e
Show file tree
Hide file tree
Showing 8 changed files with 1,944 additions and 73 deletions.
82 changes: 82 additions & 0 deletions packages/nextjs/components/tictactoe/CreateChallengeBox.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import React from "react";
import { useState } from "react";
import { Button, Card, CardBody, Heading, Stack, Text } from "@chakra-ui/react";
// import { ethers } from "ethers";
import { AddressInput, EtherInput } from "~~/components/scaffold-eth";
import { useScaffoldContractWrite } from "~~/hooks/scaffold-eth";

const CreateChallengeBox = ({}) => {
const [player2Address, setPlayer2Address] = useState<string | undefined>("");
const [betAmount, setBetAmount] = useState<string>("");

const { writeAsync: createGame } = useScaffoldContractWrite({
contractName: "TicTacToe",
functionName: "createGame",
args: [player2Address],
// value: betAmount ? betAmount.toString() : undefined,
});

console.log("Bet amount: ", betAmount);

return (
<Card
direction={{ base: "column", sm: "row" }}
maxWidth={"md"}
overflow="hidden"
variant="solid"
textColor={"white"}
marginRight={4}
backgroundColor={"gray.900"}
textAlign={"center"}
>
<Stack>
<CardBody>
<Heading size="xl">⭕ Play and bet on a Tic Tac Toe game! ❌</Heading>
<Text fontWeight={"bold"} marginBottom={0}>
⚔️ Who do you want to challenge? ⚔️
</Text>
<AddressInput
placeholder="Enter address for team 2"
onChange={setPlayer2Address}
value={player2Address ?? ""}
/>
<br />
<Text fontWeight={"bold"} marginBottom={0} marginTop={0}>
💰 (optional) Bet ETH on the match outcome 💰
</Text>
<EtherInput
placeholder="Enter your bet amount in ETH or USD"
onChange={newValue => {
if (newValue) {
setBetAmount(newValue);
} else {
setBetAmount("");
}
}}
value={betAmount}
/>
Your oponent will have to pay the same to accept
<br />
<Text fontWeight={"bold"} mt={0} mb={0}>
Winner gets all, Tie gives back the ETH
</Text>
<Button
variant="solid"
onClick={event => {
event.preventDefault();
createGame();
}}
backgroundColor={"red.500"}
textColor={"white"}
colorScheme="orange"
marginTop={4}
>
Create challenge
</Button>
</CardBody>
</Stack>
</Card>
);
};

export default CreateChallengeBox;
41 changes: 41 additions & 0 deletions packages/nextjs/components/tictactoe/TicTacToeBoard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React from /* , { useState } */
"react";
import { Button, Flex, Text } from "@chakra-ui/react";

// import { ethers } from "ethers";

interface TicTacToeBoardProps {
gameId: number;
currentPlayer: number;
board: number[];
makeMove: (position: number) => void;
}

const TicTacToeBoard: React.FC<TicTacToeBoardProps> = ({ gameId, makeMove }) => {
const renderSquare = (i: number) => (
<Button
key={i}
size="md"
fontSize="xl"
fontWeight="bold"
colorScheme="teal"
// disabled={board[i] !== 0 || currentPlayer !== 1}
onClick={() => makeMove(i)}
>
{/* {board[i] === 1 ? "X" : board[i] === 2 ? "O" : ""} */}
</Button>
);

return (
<Flex direction="column" alignItems="center" justifyContent="center" marginTop={4}>
<Text fontSize="xl" fontWeight="bold" marginBottom={2}>
Tic Tac Toe Game #{gameId}
</Text>
<Flex direction="row" flexWrap="wrap">
{[0, 1, 2, 3, 4, 5, 6, 7, 8].map(i => renderSquare(i))}
</Flex>
</Flex>
);
};

export default TicTacToeBoard;
5 changes: 5 additions & 0 deletions packages/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@
"vercel:yolo": "vercel --build-env NEXT_PUBLIC_IGNORE_BUILD_ERROR=true"
},
"dependencies": {
"@chakra-ui/react": "^2.8.2",
"@emotion/react": "^11.11.3",
"@emotion/styled": "^11.11.0",
"@ethersproject/providers": "~5.7.2",
"@heroicons/react": "~2.0.11",
"@rainbow-me/rainbowkit": "1.3.0",
"@uniswap/sdk-core": "~4.0.1",
"@uniswap/v2-sdk": "~3.0.1",
"blo": "~1.0.1",
"daisyui": "~4.4.19",
"ethers": "^6.9.2",
"framer-motion": "^10.17.9",
"next": "~13.1.6",
"nextjs-progressbar": "~0.0.16",
"qrcode.react": "~3.1.0",
Expand Down
19 changes: 11 additions & 8 deletions packages/nextjs/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useEffect, useState } from "react";
import type { AppProps } from "next/app";
import { ChakraProvider } from "@chakra-ui/react";
import { RainbowKitProvider, darkTheme, lightTheme } from "@rainbow-me/rainbowkit";
import "@rainbow-me/rainbowkit/styles.css";
import NextNProgress from "nextjs-progressbar";
Expand Down Expand Up @@ -27,14 +28,16 @@ const ScaffoldEthApp = ({ Component, pageProps }: AppProps) => {

return (
<>
<div className="flex flex-col min-h-screen">
<Header />
<main className="relative flex flex-col flex-1">
<Component {...pageProps} />
</main>
<Footer />
</div>
<Toaster />
<ChakraProvider>
<div className="flex flex-col min-h-screen">
<Header />
<main className="relative flex flex-col flex-1">
<Component {...pageProps} />
</main>
<Footer />
</div>
<Toaster />
</ChakraProvider>
</>
);
};
Expand Down
154 changes: 104 additions & 50 deletions packages/nextjs/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,60 +1,114 @@
import Link from "next/link";
import { useEffect, useState } from "react";
// import Link from "next/link";
import { NewGameProps } from "../types/TicTacToeTypes";
import { Card, CardBody, Flex, Heading } from "@chakra-ui/react";
// import { ethers } from "ethers";
import type { NextPage } from "next";
import { BugAntIcon, MagnifyingGlassIcon } from "@heroicons/react/24/outline";
// import { BugAntIcon, MagnifyingGlassIcon } from "@heroicons/react/24/outline";
import { MetaHeader } from "~~/components/MetaHeader";
import { Address } from "~~/components/scaffold-eth";
import CreateChallengeBox from "~~/components/tictactoe/CreateChallengeBox";
// import TicTacToeBoard from "~~/components/tictactoe/TicTacToeBoard";
import { useScaffoldEventHistory, useScaffoldEventSubscriber } from "~~/hooks/scaffold-eth";

const Home: NextPage = () => {
const [gameHistory, setGameHistory] = useState<NewGameProps[]>([]);

// Event history hooks
const { data: GameCreatedHistory } = useScaffoldEventHistory({
contractName: "TicTacToe",
eventName: "GameCreated",
fromBlock: BigInt(process.env.NEXT_PUBLIC_DEPLOY_BLOCK || "0"),
blockData: false,
});

// useEffect for dynamic updating? (not sure if needed)
useEffect(() => {
const mappedHistory = GameCreatedHistory?.map(event => ({
gameId: parseInt(event.args[0].toString()),
player1: event.args[1],
player2: event.args[2],
bet: parseInt(event.args[3].toString()),
})) as NewGameProps[];
setGameHistory(mappedHistory);
}, [GameCreatedHistory]);

// Event subscription hooks
useScaffoldEventSubscriber({
contractName: "TicTacToe",
eventName: "GameCreated",
listener: (logs: any[]) => {
setGameHistory(indexedHistory => {
const newGameCreated: NewGameProps = {
gameId: parseInt(logs[0].args[0].toString()),
player1: logs[0].args[1],
player2: logs[0].args[2],
bet: parseInt(logs[0].args[3].toString()),
};
return [newGameCreated, ...indexedHistory];
});
},
});

const gameCards = gameHistory?.map(game => {
// This is where all events relevant to a single game will be stored and sorted which is a palindrome
return game;
});

return (
<>
<MetaHeader />
<div className="flex items-center flex-col flex-grow pt-10">
<div className="px-5">
<h1 className="text-center mb-8">
<span className="block text-2xl mb-2">Welcome to</span>
<span className="block text-4xl font-bold">Scaffold-ETH 2</span>
</h1>
<p className="text-center text-lg">
Get started by editing{" "}
<code className="italic bg-base-300 text-base font-bold max-w-full break-words break-all inline-block">
packages/nextjs/pages/index.tsx
</code>
</p>
<p className="text-center text-lg">
Edit your smart contract{" "}
<code className="italic bg-base-300 text-base font-bold max-w-full break-words break-all inline-block">
YourContract.sol
</code>{" "}
in{" "}
<code className="italic bg-base-300 text-base font-bold max-w-full break-words break-all inline-block">
packages/hardhat/contracts
</code>
</p>
</div>

<div className="flex-grow bg-base-300 w-full mt-16 px-8 py-12">
<div className="flex justify-center items-center gap-12 flex-col sm:flex-row">
<div className="flex flex-col bg-base-100 px-10 py-10 text-center items-center max-w-xs rounded-3xl">
<BugAntIcon className="h-8 w-8 fill-secondary" />
<p>
Tinker with your smart contract using the{" "}
<Link href="/debug" passHref className="link">
Debug Contract
</Link>{" "}
tab.
</p>
</div>
<div className="flex flex-col bg-base-100 px-10 py-10 text-center items-center max-w-xs rounded-3xl">
<MagnifyingGlassIcon className="h-8 w-8 fill-secondary" />
<p>
Explore your local transactions with the{" "}
<Link href="/blockexplorer" passHref className="link">
Block Explorer
</Link>{" "}
tab.
</p>
</div>
</div>
</div>
<div
style={{
position: "relative",
minHeight: "100vh",
}}
>
<div
style={{
position: "absolute",
top: 0,
left: 0,
width: "100%",
height: "100%",
backgroundImage: "url('background.jpg')",
backgroundSize: "cover",
backgroundRepeat: "no-repeat",
backgroundPosition: "center",
filter: "brightness(0.3)",
}}
/>
<Flex direction={{ base: "column", md: "row" }} justify="center" gap={10} align="center" marginTop={4}>
<CreateChallengeBox />
<Card
direction={{ base: "column", sm: "row" }}
width="container.sm"
maxWidth={{ base: "container.sm", sm: "container.sm", md: "container.md" }}
variant="solid"
maxHeight={{ base: "container.sm", sm: "container.sm", md: "480" }}
overflow={"auto"}
textColor={"white"}
backgroundColor={"gray.900"}
>
<CardBody>
<Heading size="xl">⭕ See your active challenges! ❌</Heading>
<Flex direction="column" alignItems="center" justifyContent="center">
{gameCards?.map(({ gameId, player1, player2 /* , bet */ }) => (
<>
<p>GameId: {gameId}</p>
<p>
Player 1: <Address address={player1} />
</p>
<p>
Player 2: <Address address={player2} />
</p>
{/* <p>Bet: {ethers.utils.formatEther(bet.toString())} ETH</p> */}
</>
))}
</Flex>
</CardBody>
</Card>
</Flex>
</div>
</>
);
Expand Down
Binary file added packages/nextjs/public/background.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions packages/nextjs/types/TicTacToeTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type NewGameProps = {
gameId: number;
player1: string;
player2: string;
bet: number;
};
Loading

0 comments on commit 6fc3c5e

Please sign in to comment.