From b325e621be073cd68a3e7ad7527d941981b94725 Mon Sep 17 00:00:00 2001 From: jhalitaksoy Date: Sun, 4 Jul 2021 01:04:59 +0300 Subject: [PATCH] now printing message when user leaves the game --- src/Home.tsx | 35 +++++++++-- src/channel_names.ts | 2 + src/components/InfoPanel.tsx | 17 +++++- src/const/texts.ts | 110 ++++++++++++++++++----------------- 4 files changed, 105 insertions(+), 59 deletions(-) diff --git a/src/Home.tsx b/src/Home.tsx index c50d840..2ef234e 100644 --- a/src/Home.tsx +++ b/src/Home.tsx @@ -7,7 +7,7 @@ import { Game } from './mancala'; import { decodeText, encodeText } from './rtmt/byte_util'; import { Bytes } from './rtmt/rtmt'; import { RTMTWS } from './rtmt/rtmt_websocket'; -import { channel_game_move, channel_leave_game, channel_on_game_update } from './channel_names'; +import { channel_game_move, channel_leave_game, channel_on_game_update, channel_on_game_user_leave } from './channel_names'; import Button from './components/Button'; import InfoPanel from './components/InfoPanel'; @@ -26,12 +26,14 @@ type ConnectionState = "connecting" | "error" | "connected" | "reconnecting" const Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => { const [userKey, setUserKey] = useState(undefined); - const [game, setGame] = useState(undefined) - const [connectionState, setConnetionState] = useState("connecting") + const [game, setGame] = useState(undefined) + const [crashMessage, setCrashMessage] = useState(undefined) + const [userKeyWhoLeave, setUserKeyWhoLeave] = useState(undefined) + const onConnectionDone = () => { setConnetionState("connected") } @@ -85,6 +87,13 @@ const Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => { console.log(newCrashMessage); setCrashMessage(newCrashMessage) }) + + context.rtmt.listenMessage(channel_on_game_user_leave, (message) => { + const userKeyWhoLeave = decodeText(message) + console.log("on_game_user_leave"); + console.log(channel_on_game_user_leave); + setUserKeyWhoLeave(userKeyWhoLeave) + }) } React.useEffect(() => { @@ -92,7 +101,14 @@ const Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => { connectToServer("connecting") }, []) + const resetGameState = () => { + setGame(undefined) + setCrashMessage(undefined) + setUserKeyWhoLeave(undefined) + } + const newGameClick = () => { + resetGameState() context.rtmt.sendMessage("new_game", new Uint8Array()) } @@ -120,6 +136,9 @@ const Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => { const renderNewGameButton = () => { const newGame =