developmaunt -> mauster #3

Merged
DTieman merged 7 commits from developmaunt into mauster 2024-04-24 18:05:41 +00:00
2 changed files with 50 additions and 20 deletions
Showing only changes of commit d5df884ae8 - Show all commits

View File

@@ -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<Props> = ({winner, handleLobbyAction}) => {
const onStartClick = () => {
console.log('Start Game');
handleLobbyAction("START");
}
// @ts-ignore
return (
<div>
<h1>Lobby</h1>
{
winner &&
<h2>{winner} has won the game!</h2>
}
<GHButton onClick={onStartClick}>Start Game</GHButton>
</div>
);
}

View File

@@ -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<GameState>({
Me: {Name: playerName, Id: '', CardsLeft: 0},
CurrentState: '',
Hand: [],
CurrentCard: '',
CurrentPlayer: {Name: "", Id: '', CardsLeft: 0},
Players: []
});
const [gameState, setGameState] = React.useState<GameState | undefined>(undefined);
const [lobbyState, setLobbyState] = React.useState<string>("");
const [winner, setWinner] = React.useState<string | undefined>(undefined);
const [chatScroll, setChatScroll] = React.useState<boolean>(true);
const chatRef = React.useRef<HTMLOListElement>(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 = () => {
</aside>
<main className={"room-main"}>
{
isLobby &&
<Lobby/>
}
{
isGame &&
<Game gameState={gameState} handleGameAction={handleGameAction}/>
gameState ?
<Game gameState={gameState} handleGameAction={handleGameAction}/>
:
<Lobby winner={winner} handleLobbyAction={handleLobbyAction}/>
}
</main>
</div>