From d5df884ae80790dd5b71ebc2c84386411ef3c0f7 Mon Sep 17 00:00:00 2001 From: DTieman Date: Mon, 22 Apr 2024 21:52:12 +0200 Subject: [PATCH] Better lobby system --- src/layout/components/Lobby.tsx | 22 ++++++++++++++- src/layout/pages/Room.tsx | 48 ++++++++++++++++++++------------- 2 files changed, 50 insertions(+), 20 deletions(-) diff --git a/src/layout/components/Lobby.tsx b/src/layout/components/Lobby.tsx index e35cb13..3dfd788 100644 --- a/src/layout/components/Lobby.tsx +++ b/src/layout/components/Lobby.tsx @@ -1,7 +1,27 @@ -const Lobby = () => { +import {FunctionComponent} from "react"; +import {GHButton} from "./Button"; + +interface Props { + winner: string | undefined; + handleLobbyAction: (action: string) => void; +} + +const Lobby: FunctionComponent = ({winner, handleLobbyAction}) => { + + const onStartClick = () => { + console.log('Start Game'); + handleLobbyAction("START"); + } + + // @ts-ignore return (

Lobby

+ { + winner && +

{winner} has won the game!

+ } + Start Game
); } diff --git a/src/layout/pages/Room.tsx b/src/layout/pages/Room.tsx index bb72bdc..3806e83 100644 --- a/src/layout/pages/Room.tsx +++ b/src/layout/pages/Room.tsx @@ -34,14 +34,9 @@ const Room = () => { const playerName = localStorage.getItem('playerName') || Math.random().toString(36).substring(7); const WS_URL = `${process.env.REACT_APP_WEBSOCKET_URL}/room/${roomId}/${playerName}`; - const [gameState, setGameState] = React.useState({ - Me: {Name: playerName, Id: '', CardsLeft: 0}, - CurrentState: '', - Hand: [], - CurrentCard: '', - CurrentPlayer: {Name: "", Id: '', CardsLeft: 0}, - Players: [] - }); + const [gameState, setGameState] = React.useState(undefined); + const [lobbyState, setLobbyState] = React.useState(""); + const [winner, setWinner] = React.useState(undefined); const [chatScroll, setChatScroll] = React.useState(true); const chatRef = React.useRef(null); @@ -68,8 +63,24 @@ const Room = () => { onMessage: (event) => { const data = JSON.parse(event.data); const payload = JSON.parse(data.Payload); - if (data.Type === 'GAME') setGameState(payload); - if (data.Type === 'CHAT') addChatMessage({Sender: payload.Sender, Message: payload.Message}) + switch (data.Type) { + case 'LOBBY': + setLobbyState(data.Type); + break; + case 'GAME': + setLobbyState(data.Type); + setGameState(payload); + break; + case 'CHAT': + addChatMessage(payload); + break; + case "END": + setWinner(payload.Name) + setGameState(undefined); + break; + default: + console.log('Unknown message type:', data.Type); + } } }); @@ -85,6 +96,10 @@ const Room = () => { handleSend({Type: "GAME", Payload: JSON.stringify(action)}); } + const handleLobbyAction = (action: string) => { + handleSend({Type: "LOBBY", Payload: JSON.stringify(action)}); + } + const handleChatMessage = (chatMessage: string) => { handleSend({ Type: "CHAT", @@ -92,9 +107,6 @@ const Room = () => { }); } - const isLobby = false; - const isGame = true; - useEffect(() => { return () => { const socket = websocket.getWebSocket(); @@ -118,12 +130,10 @@ const Room = () => {
{ - isLobby && - - } - { - isGame && - + gameState ? + + : + }