now printing message when user leaves the game

This commit is contained in:
jhalitaksoy 2021-07-04 01:04:59 +03:00
parent 5dc7d80d9a
commit b325e621be
4 changed files with 105 additions and 59 deletions

View File

@ -7,7 +7,7 @@ import { Game } from './mancala';
import { decodeText, encodeText } from './rtmt/byte_util'; import { decodeText, encodeText } from './rtmt/byte_util';
import { Bytes } from './rtmt/rtmt'; import { Bytes } from './rtmt/rtmt';
import { RTMTWS } from './rtmt/rtmt_websocket'; 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 Button from './components/Button';
import InfoPanel from './components/InfoPanel'; import InfoPanel from './components/InfoPanel';
@ -26,12 +26,14 @@ type ConnectionState = "connecting" | "error" | "connected" | "reconnecting"
const Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => { const Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => {
const [userKey, setUserKey] = useState(undefined); const [userKey, setUserKey] = useState(undefined);
const [game, setGame] = useState<Game>(undefined)
const [connectionState, setConnetionState] = useState<ConnectionState>("connecting") const [connectionState, setConnetionState] = useState<ConnectionState>("connecting")
const [game, setGame] = useState<Game>(undefined)
const [crashMessage, setCrashMessage] = useState<string>(undefined) const [crashMessage, setCrashMessage] = useState<string>(undefined)
const [userKeyWhoLeave, setUserKeyWhoLeave] = useState<string>(undefined)
const onConnectionDone = () => { const onConnectionDone = () => {
setConnetionState("connected") setConnetionState("connected")
} }
@ -85,6 +87,13 @@ const Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => {
console.log(newCrashMessage); console.log(newCrashMessage);
setCrashMessage(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(() => { React.useEffect(() => {
@ -92,7 +101,14 @@ const Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => {
connectToServer("connecting") connectToServer("connecting")
}, []) }, [])
const resetGameState = () => {
setGame(undefined)
setCrashMessage(undefined)
setUserKeyWhoLeave(undefined)
}
const newGameClick = () => { const newGameClick = () => {
resetGameState()
context.rtmt.sendMessage("new_game", new Uint8Array()) context.rtmt.sendMessage("new_game", new Uint8Array())
} }
@ -120,6 +136,9 @@ const Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => {
const renderNewGameButton = () => { const renderNewGameButton = () => {
const newGame = <Button text={context.texts.NewGame} color="white" onClick={newGameClick} /> const newGame = <Button text={context.texts.NewGame} color="white" onClick={newGameClick} />
if (userKeyWhoLeave) {
return newGame
}
if (crashMessage) { if (crashMessage) {
return newGame return newGame
} }
@ -163,13 +182,17 @@ const Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => {
<h1 style={{ margin: "10px 0px" }}>{context.texts.Mancala}</h1> <h1 style={{ margin: "10px 0px" }}>{context.texts.Mancala}</h1>
<div> <div>
{renderNewGameButton()} {renderNewGameButton()}
{game && <Button color="white" text={context.texts.Leave} onClick={leaveGame} />} {game && !userKeyWhoLeave && < Button color="white" text={context.texts.Leave} onClick={leaveGame} />}
</div> </div>
</div> </div>
<InfoPanel game={game} crashMessage={crashMessage} userKey={userKey} /> <InfoPanel game={game} crashMessage={crashMessage} userKey={userKey} userKeyWhoLeave={userKeyWhoLeave} />
{game && <BoardView userKey={userKey} game={game} onHoleSelect={onHoleSelect} />} {game && <BoardView userKey={userKey} game={game} onHoleSelect={onHoleSelect} />}
</div > </div >
} }
export default Home export default Home
function channel_game_user_leave(channel_game_user_leave: any, arg1: (message: Uint8Array) => void) {
throw new Error('Function not implemented.');
}

View File

@ -4,3 +4,5 @@ export const channel_game_move = "game_move"
export const channel_on_game_update = "on_game_update" export const channel_on_game_update = "on_game_update"
export const channel_leave_game = "leave_game" export const channel_leave_game = "leave_game"
export const channel_on_game_end = "on_game_end" export const channel_on_game_end = "on_game_end"
export const channel_on_game_crashed = "on_game_crashed"
export const channel_on_game_user_leave = "on_game_user_leave"

View File

@ -3,7 +3,9 @@ import { FunctionComponent } from "react"
import { context } from '../context'; import { context } from '../context';
import { Game } from '../mancala'; import { Game } from '../mancala';
const InfoPanel: FunctionComponent<{ game: Game, crashMessage: string, userKey: string }> = ({ game, crashMessage, userKey }) => { const InfoPanel: FunctionComponent<{ game: Game, crashMessage: string, userKey: string, userKeyWhoLeave: string }> = ({
game, crashMessage, userKey, userKeyWhoLeave }) => {
if (crashMessage) { if (crashMessage) {
return ( return (
<h4>{ <h4>{
@ -12,6 +14,19 @@ const InfoPanel: FunctionComponent<{ game: Game, crashMessage: string, userKey:
) )
} }
if (userKeyWhoLeave) {
let message = context.texts.OpponentLeavesTheGame
if (userKeyWhoLeave == userKey) {
message = context.texts.YouLeftTheGame
}
return (
<h4>
{message}
</h4>
)
}
if (game) { if (game) {
if (game.state == "ended") { if (game.state == "ended") {
const whoWon = game.getWonPlayer() == userKey ? context.texts.YouWon : context.texts.YouLost const whoWon = game.getWonPlayer() == userKey ? context.texts.YouWon : context.texts.YouLost

View File

@ -16,6 +16,8 @@ export type Texts = {
ConnectingAgain: string, ConnectingAgain: string,
ServerError: string, ServerError: string,
SearchingOpponet: string, SearchingOpponet: string,
OpponentLeavesTheGame: string,
YouLeftTheGame: string,
} }
export const EnUs: Texts = { export const EnUs: Texts = {
@ -35,6 +37,8 @@ export const EnUs : Texts = {
ConnectingAgain: "Connecting Again", ConnectingAgain: "Connecting Again",
ServerError: "Server Error", ServerError: "Server Error",
SearchingOpponet: "Searching Opponet", SearchingOpponet: "Searching Opponet",
OpponentLeavesTheGame: "Opponent Leaves The Game",
YouLeftTheGame: "You Left The Game"
} }
export const TrTr: Texts = { export const TrTr: Texts = {
@ -54,4 +58,6 @@ export const TrTr : Texts = {
ConnectingAgain: "Tekrar Bağlanılıyor", ConnectingAgain: "Tekrar Bağlanılıyor",
ServerError: "Sunucu Hatası", ServerError: "Sunucu Hatası",
SearchingOpponet: "Rakip Aranıyor", SearchingOpponet: "Rakip Aranıyor",
OpponentLeavesTheGame: "Rakip Oyundan Ayrıldı",
YouLeftTheGame: "Sen Oyundan Ayrıldın"
} }