Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can start a game #25

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 40 additions & 10 deletions apps/web-app/src/app/game/[gameId]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"use client";

import { Stack, HStack, VStack, Text } from "@chakra-ui/react";
import { Stack, HStack, VStack, Text, UnorderedList, ListItem, Button } from "@chakra-ui/react";

import { Box } from "@chakra-ui/react";
import { useConfig } from "wagmi";
import { readContracts } from "@wagmi/core";
import { useEffect, useState, useMemo } from "react";
import { readContracts, writeContract } from "@wagmi/core";
import { useEffect, useState, useMemo, useCallback } from "react";

import { formatter } from "@/utils";
import { gameArtifact, GameState, gameEventTypes } from "@/helpers";
import { gameArtifact, GameState, gameEventTypes, MIN_PLAYERS_TO_START } from "@/helpers";

export default function GamePage({ params }: { params: { gameId: number } }) {
const { gameId } = params;
Expand All @@ -31,29 +31,59 @@ export default function GamePage({ params }: { params: { gameId: number } }) {
];

const results = await readContracts(wagmiConfig, { contracts });
setGame(results[0].result);
setState && setGame(results[0].result);
}

getGame(gameId);

return () => {
setState = false;
};
}, [contractCfg, gameId, wagmiConfig]);

return (
game && (
// can you use async function inside useCallback?
const startGameHandler = useCallback(async () => {
const result = await writeContract(wagmiConfig, {
...contractCfg,
functionName: "startGame",
args: [gameId],
});
}, [contractCfg, gameId, wagmiConfig]);

if (!game) {
return <></>;
} else {
const canStartGame = game.players.length >= MIN_PLAYERS_TO_START;
return (
<VStack spacing={3}>
<Text>
Game ID: <strong>{gameId}</strong>
</Text>
<Text>Players: {game.players.length}</Text>
<UnorderedList styleType="- ">
{game.players.map((p) => (
<ListItem key={`game-${gameId}-${p}`} fontSize={14}>
{p}
</ListItem>
))}
</UnorderedList>
<Text>
State: <strong>{GameState[game.state]}</strong>
</Text>
<Text>Created: {formatter.dateTime(game.startTime)}</Text>
<Text>Last Updated: {formatter.dateTime(game.lastUpdate)}</Text>
<HStack>
{game.state === GameState.GameInitiated && (
<Button
onClick={startGameHandler}
variant="outline"
colorScheme="blue"
isDisabled={!canStartGame}
>
{canStartGame ? "Start Game" : "Waiting for more players to join"}
</Button>
)}
</HStack>
</VStack>
)
);
);
}
}
4 changes: 4 additions & 0 deletions apps/web-app/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ function GameCard({ gameId, game }) {
{userJoinedGame ? "Already Joined" : "Join Game"}
</Button>
)}
{game.state >= GameState.RoundCommit && game.state <= GameState.RoundEnd && (
<Text>Game in Progress...</Text>
)}
{game.state >= GameState.GameEnd && <Text>Game Ended</Text>}
</CardFooter>
</Card>
);
Expand Down
2 changes: 2 additions & 0 deletions apps/web-app/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export enum GameState {
GameEnd,
}

export const MIN_PLAYERS_TO_START = 3;

export const gameEventTypes = {
newGame: "NewGame",
} as const;
Expand Down