added Searching Opponent message

This commit is contained in:
jhalitaksoy 2021-07-04 01:23:10 +03:00
parent b325e621be
commit 272b39915c
3 changed files with 66 additions and 42 deletions

View File

@ -28,6 +28,8 @@ const Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => {
const [connectionState, setConnetionState] = useState<ConnectionState>("connecting") const [connectionState, setConnetionState] = useState<ConnectionState>("connecting")
const [searchingOpponent, setSearchingOpponent] = useState<boolean>(false)
const [game, setGame] = useState<Game>(undefined) const [game, setGame] = useState<Game>(undefined)
const [crashMessage, setCrashMessage] = useState<string>(undefined) const [crashMessage, setCrashMessage] = useState<string>(undefined)
@ -78,6 +80,7 @@ const Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => {
const newGame: Game = JSON.parse(decodeText(message)) const newGame: Game = JSON.parse(decodeText(message))
console.log("on_game_start"); console.log("on_game_start");
console.log(newGame); console.log(newGame);
setSearchingOpponent(false)
setGame(new Game(newGame.player1, newGame.player2, newGame.board, newGame.turn, newGame.state)) setGame(new Game(newGame.player1, newGame.player2, newGame.board, newGame.turn, newGame.state))
}) })
@ -109,6 +112,7 @@ const Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => {
const newGameClick = () => { const newGameClick = () => {
resetGameState() resetGameState()
setSearchingOpponent(true)
context.rtmt.sendMessage("new_game", new Uint8Array()) context.rtmt.sendMessage("new_game", new Uint8Array())
} }
@ -182,17 +186,18 @@ 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 && !userKeyWhoLeave && < Button color="white" text={context.texts.Leave} onClick={leaveGame} />} {game && !userKeyWhoLeave && !crashMessage && < Button color="white" text={context.texts.Leave} onClick={leaveGame} />}
</div> </div>
</div> </div>
<InfoPanel game={game} crashMessage={crashMessage} userKey={userKey} userKeyWhoLeave={userKeyWhoLeave} /> <InfoPanel
game={game}
crashMessage={crashMessage}
userKey={userKey}
userKeyWhoLeave={userKeyWhoLeave}
searchingOpponent={searchingOpponent} />
{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

@ -3,46 +3,59 @@ 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, userKeyWhoLeave: string }> = ({ const InfoPanel: FunctionComponent<{
game, crashMessage, userKey, userKeyWhoLeave }) => { game: Game,
crashMessage: string,
if (crashMessage) { userKey: string,
return ( userKeyWhoLeave: string,
<h4>{ searchingOpponent: boolean
context.texts.GameCrashed + " " + crashMessage }> = ({
}</h4> game, crashMessage, userKey, userKeyWhoLeave, searchingOpponent }) => {
) if (searchingOpponent) {
}
if (userKeyWhoLeave) {
let message = context.texts.OpponentLeavesTheGame
if (userKeyWhoLeave == userKey) {
message = context.texts.YouLeftTheGame
}
return (
<h4>
{message}
</h4>
)
}
if (game) {
if (game.state == "ended") {
const whoWon = game.getWonPlayer() == userKey ? context.texts.YouWon : context.texts.YouLost
return ( return (
<h4>{ <h4>{
context.texts.GameEnded + " " + whoWon context.texts.SearchingOpponent + " " + context.texts.PleaseWait
}</h4> }</h4>
) )
} else { }
if (crashMessage) {
return ( return (
<h4>{game.checkGameTurn(userKey) ? context.texts.YourTurn : context.texts.OpponentTurn}</h4> <h4>{
context.texts.GameCrashed + " " + crashMessage
}</h4>
) )
} }
if (userKeyWhoLeave) {
let message = context.texts.OpponentLeavesTheGame
if (userKeyWhoLeave == userKey) {
message = context.texts.YouLeftTheGame
}
return (
<h4>
{message}
</h4>
)
}
if (game) {
if (game.state == "ended") {
const whoWon = game.getWonPlayer() == userKey ? context.texts.YouWon : context.texts.YouLost
return (
<h4>{
context.texts.GameEnded + " " + whoWon
}</h4>
)
} else {
return (
<h4>{game.checkGameTurn(userKey) ? context.texts.YourTurn : context.texts.OpponentTurn}</h4>
)
}
}
return <h4></h4>
} }
return <h4></h4>
}
export default InfoPanel export default InfoPanel

View File

@ -18,6 +18,8 @@ export type Texts = {
SearchingOpponet: string, SearchingOpponet: string,
OpponentLeavesTheGame: string, OpponentLeavesTheGame: string,
YouLeftTheGame: string, YouLeftTheGame: string,
SearchingOpponent: string,
PleaseWait : string
} }
export const EnUs: Texts = { export const EnUs: Texts = {
@ -38,7 +40,9 @@ export const EnUs: Texts = {
ServerError: "Server Error", ServerError: "Server Error",
SearchingOpponet: "Searching Opponet", SearchingOpponet: "Searching Opponet",
OpponentLeavesTheGame: "Opponent Leaves The Game", OpponentLeavesTheGame: "Opponent Leaves The Game",
YouLeftTheGame: "You Left The Game" YouLeftTheGame: "You Left The Game",
SearchingOpponent: "Searching Opponent",
PleaseWait : "Please Wait"
} }
export const TrTr: Texts = { export const TrTr: Texts = {
@ -59,5 +63,7 @@ export const TrTr: Texts = {
ServerError: "Sunucu Hatası", ServerError: "Sunucu Hatası",
SearchingOpponet: "Rakip Aranıyor", SearchingOpponet: "Rakip Aranıyor",
OpponentLeavesTheGame: "Rakip Oyundan Ayrıldı", OpponentLeavesTheGame: "Rakip Oyundan Ayrıldı",
YouLeftTheGame: "Sen Oyundan Ayrıldın" YouLeftTheGame: "Sen Oyundan Ayrıldın",
SearchingOpponent: "Rakip Aranıyor",
PleaseWait: "Lütfen Bekleyin"
} }