From 6a01bc8d472b0db3dfd37d845297649c57763eae Mon Sep 17 00:00:00 2001 From: DTieman Date: Sat, 22 Apr 2023 12:42:23 +0200 Subject: [PATCH] fix --- src/layout/components/Game.tsx | 46 ++++++++++++++++++++++ src/layout/pages/Room.tsx | 72 ++++++++++++++++++++-------------- 2 files changed, 89 insertions(+), 29 deletions(-) create mode 100644 src/layout/components/Game.tsx diff --git a/src/layout/components/Game.tsx b/src/layout/components/Game.tsx new file mode 100644 index 0000000..7822622 --- /dev/null +++ b/src/layout/components/Game.tsx @@ -0,0 +1,46 @@ +import React, {FunctionComponent} from "react"; +import Deck from "./Deck"; +import Hand from "./Hand"; + +interface GameState { + PlayerName: string; + Hand: string[]; + CurrentCard: string; + CurrentPlayer: string; + Players: string[]; +} + +interface Props { + gameState: GameState + handleCardSend: (cardString: string) => void; + handleDraw: () => void; +} + +const Game:FunctionComponent = ({gameState, handleCardSend, handleDraw}) => { + return ( +
+ { + gameState.CurrentCard && + + } + { + gameState.Hand && + + } +
    + { + gameState.Players && + gameState.Players.map((player, index) => { + const isCurrentPlayer = player === gameState.CurrentPlayer; + const isMe = player === gameState.PlayerName; + return
  • + {player} {isMe && '(You)'} +
  • + }) + } +
+
+ ) +} + +export default Game; \ No newline at end of file diff --git a/src/layout/pages/Room.tsx b/src/layout/pages/Room.tsx index fa1e076..2f530cb 100644 --- a/src/layout/pages/Room.tsx +++ b/src/layout/pages/Room.tsx @@ -5,6 +5,7 @@ import {GHButton} from "../components/Button"; import useTitle from "../../utils/hooks/TitleHook"; import Hand from "../components/Hand"; import Deck from "../components/Deck"; +import Game from "../components/Game"; interface GameState { PlayerName: string; @@ -14,6 +15,16 @@ interface GameState { Players: string[]; } +interface ChatMessage { + PlayerName: string; + Message: string; +} + +interface SocketMessage { + Type: string; + Payload: any; +} + const Room = () => { useTitle('Mau!'); @@ -31,6 +42,7 @@ const Room = () => { CurrentPlayer: '', Players: [] }); + const [chatMessages, setChatMessages] = React.useState([]); const websocket = useWebSocket(WS_URL, { onOpen: () => { @@ -38,7 +50,9 @@ const Room = () => { }, onMessage: (event) => { const data = JSON.parse(event.data); - setGameState(data); + const payload = JSON.parse(data.Payload); + if (data.Type === 'GAME') setGameState(payload); + if (data.Type === 'CHAT') setChatMessages(prev => [...prev, payload]); } }); @@ -48,45 +62,45 @@ const Room = () => { navigateTo('/'); } + const handleSend = (message: SocketMessage) => { + websocket.sendMessage(JSON.stringify(message)); + } + const handleCardSend = (card: string) => { - const formattedAction = JSON.stringify({ - Action: "PLAYCARD", - Data: JSON.stringify({CardType: card.split(' ')[0], CardValue: card.split(' ')[1]}) - }); - websocket.sendMessage(formattedAction); + handleSend({ + Type: "GAME", + Payload: { + Action: "PLAYCARD", + Data: JSON.stringify({ + CardType: card.split(' ')[0], + CardValue: card.split(' ')[1] + }) + } + }) } const handleDraw = () => { - const formattedAction = JSON.stringify({ - Action: "DRAW", - Data: "" + handleSend({ + Type: "GAME", + Payload: { + Action: "DRAW", + Data: "" + } + }); + } + + const handleChat = (Message: string) => { + handleSend({ + Type: "CHAT", + Payload: { Message } }); - websocket.sendMessage(formattedAction); } return (

Room {roomId}

Leave Room - { - gameState.CurrentCard && - - } - { - gameState.Hand && - - } -
    - { - gameState.Players.map((player, index) => { - const isCurrentPlayer = player === gameState.CurrentPlayer; - const isMe = player === gameState.PlayerName; - return
  • - {player} {isMe && '(You)'} -
  • - }) - } -
+
) }