adds leave buttons & more

This commit is contained in:
pb-coding 2023-09-25 12:39:40 +02:00
parent 99effc1644
commit 2b87fb7d28
4 changed files with 34 additions and 11 deletions

4
.env
View file

@ -1,5 +1,5 @@
VITE_ENVIRONMENT=dev VITE_ENVIRONMENT=dev
VITE_BACKEND_URL=https://skyjo-backend.voltvector.org VITE_BACKEND_URL=https://skyjo-backend.voltvector.org
#VITE_ENVIRONMENT=local VITE_ENVIRONMENT=local
#VITE_BACKEND_URL=http://localhost:3001 VITE_BACKEND_URL=http://localhost:3001

View file

@ -3,7 +3,7 @@ import { socket } from "./socket";
import GameCanvas from "./components/GameCanvas"; import GameCanvas from "./components/GameCanvas";
import { Footer } from "./components/Footer"; import { Footer } from "./components/Footer";
import { JoinSession } from "./components/JoinSession"; import { SessionManager } from "./components/SessionManager";
import { Game } from "./types/gameTypes"; import { Game } from "./types/gameTypes";
import MessageDisplay from "./components/MessageDisplay"; import MessageDisplay from "./components/MessageDisplay";
@ -72,8 +72,9 @@ export default function App() {
return ( return (
<div className="bg-gray-900 w-screen h-screen"> <div className="bg-gray-900 w-screen h-screen">
{!gameData && ( {!gameData && (
<JoinSession <SessionManager
clientsInRoom={clientsInRoom} clientsInRoom={clientsInRoom}
setClientsInRoom={setClientsInRoom}
session={session} session={session}
setSession={setSession} setSession={setSession}
showStartGameButton={showStartGameButton} showStartGameButton={showStartGameButton}
@ -88,6 +89,8 @@ export default function App() {
clientsInRoom={clientsInRoom} clientsInRoom={clientsInRoom}
playerData={gameData?.players ?? []} playerData={gameData?.players ?? []}
showNextGameButton={showNextGameButton} showNextGameButton={showNextGameButton}
setClientsInRoom={setClientsInRoom}
setSession={setSession}
/> />
)} )}
</div> </div>

View file

@ -1,4 +1,4 @@
import { FC } from "react"; import { Dispatch, FC, SetStateAction } from "react";
import { socket } from "../socket"; import { socket } from "../socket";
import Text from "../global/Text"; import Text from "../global/Text";
@ -11,6 +11,8 @@ type Footer = {
clientsInRoom: number; clientsInRoom: number;
playerData: Player[]; playerData: Player[];
showNextGameButton: boolean; showNextGameButton: boolean;
setClientsInRoom: Dispatch<SetStateAction<number>>;
setSession: Dispatch<SetStateAction<string>>;
}; };
// TODO: use clients in room to validate player count // TODO: use clients in room to validate player count
@ -20,11 +22,19 @@ export const Footer: FC<Footer> = ({
session, session,
playerData, playerData,
showNextGameButton, showNextGameButton,
setClientsInRoom,
setSession,
}) => { }) => {
function nextGame() { function nextGame() {
socket.emit("next-round", { sessionId: session }); socket.emit("next-round", { sessionId: session });
} }
function leaveSession(sessionName: string) {
socket.emit("leave-session", sessionName);
setClientsInRoom(0);
setSession("");
}
const ConnectedIndicator: FC = () => ( const ConnectedIndicator: FC = () => (
<span className="ml-2 flex w-2.5 h-2.5 bg-green-500 rounded-full"></span> <span className="ml-2 flex w-2.5 h-2.5 bg-green-500 rounded-full"></span>
); );
@ -47,10 +57,7 @@ export const Footer: FC<Footer> = ({
</div> </div>
<div className="flex justify-between items-center"> <div className="flex justify-between items-center">
{showNextGameButton && <Button onClick={nextGame}>Next Game</Button>} {showNextGameButton && <Button onClick={nextGame}>Next Game</Button>}
<Button <Button variant="secondary" onClick={() => leaveSession(session)}>
variant="secondary"
onClick={() => socket.emit("leave-session")}
>
Leave Leave
</Button> </Button>
</div> </div>

View file

@ -3,15 +3,17 @@ import { socket } from "../socket";
import Button from "../global/Button"; import Button from "../global/Button";
type JoinSessionProps = { type SessionManagerProps = {
clientsInRoom: number; clientsInRoom: number;
setClientsInRoom: Dispatch<SetStateAction<number>>;
session: string; session: string;
setSession: Dispatch<SetStateAction<string>>; setSession: Dispatch<SetStateAction<string>>;
showStartGameButton: boolean; showStartGameButton: boolean;
}; };
export const JoinSession: FC<JoinSessionProps> = ({ export const SessionManager: FC<SessionManagerProps> = ({
clientsInRoom, clientsInRoom,
setClientsInRoom,
session, session,
setSession, setSession,
showStartGameButton, showStartGameButton,
@ -25,6 +27,12 @@ export const JoinSession: FC<JoinSessionProps> = ({
setSessionField(""); setSessionField("");
} }
function leaveSession(sessionName: string) {
socket.emit("leave-session", sessionName);
setClientsInRoom(0);
setSession("");
}
function startGame() { function startGame() {
socket.emit("new-game", { sessionId: session }); socket.emit("new-game", { sessionId: session });
} }
@ -76,6 +84,11 @@ export const JoinSession: FC<JoinSessionProps> = ({
{showStartGameButton && ( {showStartGameButton && (
<Button onClick={startGame}>Start Game</Button> <Button onClick={startGame}>Start Game</Button>
)} )}
{isActiveSession && (
<Button variant="secondary" onClick={() => leaveSession(session)}>
Leave Session
</Button>
)}
</div> </div>
</div> </div>
</section> </section>