This commit is contained in:
pb-coding 2023-09-24 01:03:38 +02:00
parent 85c508d02d
commit d743b4693c
4 changed files with 68 additions and 22 deletions

View file

@ -5,7 +5,6 @@ import GameCanvas from "./components/GameCanvas";
import { Footer } from "./components/Footer"; import { Footer } from "./components/Footer";
import { JoinSession } from "./components/JoinSession"; import { JoinSession } from "./components/JoinSession";
import { Game } from "./types/gameTypes"; import { Game } from "./types/gameTypes";
import { extractCurrentPlayer } from "./helpers";
import MessageDisplay from "./components/MessageDisplay"; import MessageDisplay from "./components/MessageDisplay";
export default function App() { export default function App() {
@ -19,8 +18,6 @@ export default function App() {
const showStartGameButton = session !== "" && clientsInRoom >= 2; const showStartGameButton = session !== "" && clientsInRoom >= 2;
const showNextGameButton = gameData?.phase === "new round"; const showNextGameButton = gameData?.phase === "new round";
const currentPlayerData = extractCurrentPlayer(gameData);
function setTempMessage(message: string) { function setTempMessage(message: string) {
setMessageDisplay(message); setMessageDisplay(message);
setTimeout(() => { setTimeout(() => {
@ -82,7 +79,7 @@ export default function App() {
isConnected={isConnected} isConnected={isConnected}
session={session} session={session}
clientsInRoom={clientsInRoom} clientsInRoom={clientsInRoom}
currentPlayerData={currentPlayerData} playerData={gameData?.players ?? []}
showNextGameButton={showNextGameButton} showNextGameButton={showNextGameButton}
/> />
)} )}

View file

@ -9,32 +9,66 @@ type Footer = {
isConnected: boolean; isConnected: boolean;
session: string; session: string;
clientsInRoom: number; clientsInRoom: number;
currentPlayerData: Player | undefined; playerData: Player[];
showNextGameButton: boolean; showNextGameButton: boolean;
}; };
// TODO: use clients in room to validate player count
export const Footer: FC<Footer> = ({ export const Footer: FC<Footer> = ({
isConnected, isConnected,
session, session,
clientsInRoom, playerData,
currentPlayerData,
showNextGameButton, showNextGameButton,
}) => { }) => {
function nextGame() { function nextGame() {
socket.emit("next-round", { sessionId: session }); socket.emit("next-round", { sessionId: session });
} }
const ConnectedIndicator: FC = () => (
<span className="ml-2 flex w-2.5 h-2.5 bg-green-500 rounded-full"></span>
);
const DisconnectedIndicator: FC = () => (
<span className="ml-2 flex w-2 h-2 bg-red-500 rounded-full"></span>
);
return ( return (
<div> <div
<Text>Player Name: {currentPlayerData?.name}</Text> style={{
<Text>Round Points: {currentPlayerData?.roundPoints}</Text> backgroundImage: "linear-gradient(to bottom, #4B5563,#1F2937)",
<Text>Total Points: {currentPlayerData?.totalPoints}</Text> }}
<br /> className="p-4 rounded-lg shadow-lg flex flex-col justify-between text-white"
<Text>State: {"" + isConnected}</Text> >
<Text>SocketId: {socket.id}</Text> <div className="flex justify-between">
<div className="flex items-center">
<Text>Session: {session} </Text> <Text>Session: {session} </Text>
<Text>Players: {clientsInRoom}</Text> {isConnected ? <ConnectedIndicator /> : <DisconnectedIndicator />}
</div>
<div className="flex justify-between items-center">
{showNextGameButton && <Button onClick={nextGame}>Next Game</Button>} {showNextGameButton && <Button onClick={nextGame}>Next Game</Button>}
<Button
variant="secondary"
onClick={() => socket.emit("leave-session")}
>
Leave
</Button>
</div>
</div>
{playerData.map((player) => (
<div className="mb-3 pt-2 mt-2 border-t border-gray-600">
<div className="flex justify-between items-center">
<Text>
{player?.name} {player.socketId == socket.id && "(You)"}
</Text>
<p></p>
</div>
<div className="flex justify-between">
<Text>Round Points: {player?.roundPoints}</Text>
<Text>Total Points: {player?.totalPoints}</Text>
</div>
</div>
))}
</div> </div>
); );
}; };

View file

@ -1,10 +1,21 @@
import { FC, ReactNode, ButtonHTMLAttributes } from "react"; import { FC, ReactNode, ButtonHTMLAttributes } from "react";
type ButtonProps = ButtonHTMLAttributes<HTMLButtonElement> & { type ButtonProps = ButtonHTMLAttributes<HTMLButtonElement> & {
variant?: "primary" | "secondary";
children: ReactNode; children: ReactNode;
}; };
const Button: FC<ButtonProps> = ({ children, ...rest }) => { const Button: FC<ButtonProps> = ({ variant, children, ...rest }) => {
if (variant === "secondary") {
return (
<button
className="text-white focus:outline-none focus:ring-4 font-medium rounded-lg text-sm px-3 py-2 mr-2 bg-gray-800 hover:bg-gray-700 focus:ring-gray-700 border-gray-700"
{...rest}
>
{children}
</button>
);
}
return ( return (
<button <button
className="text-white focus:ring-4 font-medium rounded-lg text-sm px-5 py-2.5 mr-2 bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-blue-800" className="text-white focus:ring-4 font-medium rounded-lg text-sm px-5 py-2.5 mr-2 bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-blue-800"

View file

@ -1,11 +1,15 @@
import { FC, ReactNode } from "react"; import { FC, ReactNode, HTMLProps } from "react";
type TextNormalProps = { type TextNormalProps = HTMLProps<HTMLParagraphElement> & {
children: ReactNode; children: ReactNode;
}; };
const Text: FC<TextNormalProps> = ({ children }) => { const Text: FC<TextNormalProps> = ({ children, ...rest }) => {
return <p className="mb-2 text-sm font-medium text-white">{children}</p>; return (
<p className="text-sm font-medium text-white" {...rest}>
{children}
</p>
);
}; };
export default Text; export default Text;