2021-06-27 19:28:55 +03:00
|
|
|
import * as React from 'react';
|
2021-06-29 03:29:46 +03:00
|
|
|
import { FunctionComponent, useState } from 'react';
|
|
|
|
|
import BoardView from './components/BoardView';
|
|
|
|
|
import { context } from './context'
|
|
|
|
|
import { Board, GameMove, Hole, Store } from './mancala';
|
|
|
|
|
import { Game } from './mancala';
|
|
|
|
|
import { decodeText, encodeText } from './rtmt/byte_util';
|
|
|
|
|
import { Bytes } from './rtmt/rtmt';
|
2021-06-27 19:28:55 +03:00
|
|
|
import { RTMTWS } from './rtmt/rtmt_websocket';
|
2021-07-04 01:04:59 +03:00
|
|
|
import { channel_game_move, channel_leave_game, channel_on_game_update, channel_on_game_user_leave } from './channel_names';
|
2021-07-02 23:13:38 +03:00
|
|
|
import Button from './components/Button';
|
2021-07-04 00:45:00 +03:00
|
|
|
import InfoPanel from './components/InfoPanel';
|
2021-06-27 19:28:55 +03:00
|
|
|
|
2021-06-29 03:29:46 +03:00
|
|
|
const testBoard = (): Board => {
|
|
|
|
|
const board = new Board(
|
|
|
|
|
[new Hole(0), new Hole(4), new Hole(4), new Hole(3), new Hole(2), new Hole(1)],
|
|
|
|
|
[new Hole(4), new Hole(4), new Hole(4), new Hole(4), new Hole(4), new Hole(10)],
|
|
|
|
|
new Store(0), new Store(0))
|
|
|
|
|
return board
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-30 19:41:53 +03:00
|
|
|
const testGame = new Game("0", "1", testBoard(), "player2", "playing")
|
2021-06-29 03:29:46 +03:00
|
|
|
|
2021-07-02 23:13:38 +03:00
|
|
|
type ConnectionState = "connecting" | "error" | "connected" | "reconnecting"
|
|
|
|
|
|
2021-06-29 03:29:46 +03:00
|
|
|
const Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => {
|
|
|
|
|
const [userKey, setUserKey] = useState(undefined);
|
|
|
|
|
|
2021-07-02 23:13:38 +03:00
|
|
|
const [connectionState, setConnetionState] = useState<ConnectionState>("connecting")
|
2021-06-30 19:41:53 +03:00
|
|
|
|
2021-07-04 01:23:10 +03:00
|
|
|
const [searchingOpponent, setSearchingOpponent] = useState<boolean>(false)
|
|
|
|
|
|
2021-07-04 01:04:59 +03:00
|
|
|
const [game, setGame] = useState<Game>(undefined)
|
|
|
|
|
|
2021-07-04 00:45:00 +03:00
|
|
|
const [crashMessage, setCrashMessage] = useState<string>(undefined)
|
|
|
|
|
|
2021-07-04 01:04:59 +03:00
|
|
|
const [userKeyWhoLeave, setUserKeyWhoLeave] = useState<string>(undefined)
|
|
|
|
|
|
2021-07-02 23:13:38 +03:00
|
|
|
const onConnectionDone = () => {
|
|
|
|
|
setConnetionState("connected")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const onConnectionLost = () => {
|
|
|
|
|
connectToServer("reconnecting")
|
|
|
|
|
}
|
2021-06-30 19:41:53 +03:00
|
|
|
|
2021-07-02 23:13:38 +03:00
|
|
|
const onConnectionError = (event: Event) => {
|
|
|
|
|
setConnetionState("error")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const connectToServer = (connectionState: ConnectionState) => {
|
|
|
|
|
setConnetionState(connectionState)
|
2021-06-29 03:29:46 +03:00
|
|
|
context.userKeyStore.getUserKey((userKey: string) => {
|
|
|
|
|
setUserKey(userKey)
|
|
|
|
|
const rtmtws = context.rtmt as RTMTWS
|
|
|
|
|
if (rtmtws) {
|
2021-07-02 23:13:38 +03:00
|
|
|
rtmtws.initWebSocket(userKey, onConnectionDone, onConnectionLost, onConnectionError)
|
2021-06-29 03:29:46 +03:00
|
|
|
} else {
|
|
|
|
|
console.log("context.rtmt is not RTMTWS");
|
|
|
|
|
}
|
|
|
|
|
})
|
2021-07-02 23:13:38 +03:00
|
|
|
}
|
2021-06-29 03:29:46 +03:00
|
|
|
|
2021-07-02 23:13:38 +03:00
|
|
|
const listenMessages = () => {
|
2022-04-23 12:53:08 +03:00
|
|
|
context.rtmt.listenMessage(channel_on_game_update, (message: Object) => {
|
|
|
|
|
const newGame: Game = message as Game;
|
2021-06-29 03:29:46 +03:00
|
|
|
console.log("on game update");
|
|
|
|
|
console.log(newGame);
|
2021-06-30 19:41:53 +03:00
|
|
|
setGame(new Game(newGame.player1, newGame.player2, newGame.board, newGame.turn, newGame.state))
|
2021-06-29 03:29:46 +03:00
|
|
|
})
|
2021-06-27 19:28:55 +03:00
|
|
|
|
2022-04-23 12:53:08 +03:00
|
|
|
context.rtmt.listenMessage(channel_on_game_update, (message: Object) => {
|
|
|
|
|
const newGame: Game = message as Game;
|
2021-06-29 03:29:46 +03:00
|
|
|
console.log("on game update");
|
|
|
|
|
console.log(newGame);
|
2021-06-30 19:41:53 +03:00
|
|
|
setGame(new Game(newGame.player1, newGame.player2, newGame.board, newGame.turn, newGame.state))
|
2021-06-29 03:29:46 +03:00
|
|
|
})
|
2021-06-27 19:28:55 +03:00
|
|
|
|
2022-04-23 12:53:08 +03:00
|
|
|
context.rtmt.listenMessage("on_game_start", (message: Object) => {
|
|
|
|
|
const newGame: Game = message as Game;
|
2021-06-29 03:29:46 +03:00
|
|
|
console.log("on_game_start");
|
|
|
|
|
console.log(newGame);
|
2021-07-04 01:23:10 +03:00
|
|
|
setSearchingOpponent(false)
|
2021-06-30 19:41:53 +03:00
|
|
|
setGame(new Game(newGame.player1, newGame.player2, newGame.board, newGame.turn, newGame.state))
|
2021-06-27 19:28:55 +03:00
|
|
|
})
|
2021-07-04 00:45:00 +03:00
|
|
|
|
2022-04-23 12:53:08 +03:00
|
|
|
context.rtmt.listenMessage("on_game_crashed", (message : any) => {
|
|
|
|
|
const newCrashMessage = message as string
|
2021-07-04 00:45:00 +03:00
|
|
|
console.log("on_game_crash");
|
|
|
|
|
console.log(newCrashMessage);
|
|
|
|
|
setCrashMessage(newCrashMessage)
|
|
|
|
|
})
|
2021-07-04 01:04:59 +03:00
|
|
|
|
2022-04-23 12:53:08 +03:00
|
|
|
context.rtmt.listenMessage(channel_on_game_user_leave, (message : any) => {
|
|
|
|
|
const userKeyWhoLeave = message;
|
2021-07-04 01:04:59 +03:00
|
|
|
console.log("on_game_user_leave");
|
|
|
|
|
console.log(channel_on_game_user_leave);
|
|
|
|
|
setUserKeyWhoLeave(userKeyWhoLeave)
|
|
|
|
|
})
|
2021-06-27 19:28:55 +03:00
|
|
|
}
|
|
|
|
|
|
2021-07-02 23:13:38 +03:00
|
|
|
React.useEffect(() => {
|
|
|
|
|
listenMessages()
|
|
|
|
|
connectToServer("connecting")
|
|
|
|
|
}, [])
|
|
|
|
|
|
2021-07-04 01:04:59 +03:00
|
|
|
const resetGameState = () => {
|
|
|
|
|
setGame(undefined)
|
|
|
|
|
setCrashMessage(undefined)
|
|
|
|
|
setUserKeyWhoLeave(undefined)
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-02 23:13:38 +03:00
|
|
|
const newGameClick = () => {
|
2021-07-04 01:04:59 +03:00
|
|
|
resetGameState()
|
2021-07-04 01:23:10 +03:00
|
|
|
setSearchingOpponent(true)
|
2022-04-23 12:53:08 +03:00
|
|
|
context.rtmt.sendMessage("new_game", {})
|
2021-07-02 23:13:38 +03:00
|
|
|
}
|
|
|
|
|
|
2021-06-29 03:29:46 +03:00
|
|
|
const leaveGame = () => {
|
2022-04-23 12:53:08 +03:00
|
|
|
context.rtmt.sendMessage(channel_leave_game, {})
|
2021-06-29 03:29:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const onHoleSelect = (index: number, hole: Hole) => {
|
|
|
|
|
const gameMove: GameMove = { index: index }
|
2022-04-23 12:53:08 +03:00
|
|
|
context.rtmt.sendMessage(channel_game_move, gameMove)
|
2021-06-29 03:29:46 +03:00
|
|
|
}
|
|
|
|
|
|
2021-07-02 23:13:38 +03:00
|
|
|
const showConnectionState = connectionState != "connected"
|
|
|
|
|
|
|
|
|
|
const connectionStateText = () => {
|
|
|
|
|
let map: { [key: string]: string } = {
|
|
|
|
|
"connecting": context.texts.Connecting,
|
|
|
|
|
"connected": context.texts.Connected,
|
|
|
|
|
"error": context.texts.CannotConnect,
|
|
|
|
|
"reconnecting": context.texts.ConnectingAgain
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return map[connectionState]
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-04 00:45:00 +03:00
|
|
|
const renderNewGameButton = () => {
|
2021-07-04 01:35:08 +03:00
|
|
|
const newGame = <Button text={context.texts.NewGame} color="#005f73" onClick={newGameClick} />
|
2021-07-04 01:04:59 +03:00
|
|
|
if (userKeyWhoLeave) {
|
|
|
|
|
return newGame
|
|
|
|
|
}
|
2021-07-04 00:45:00 +03:00
|
|
|
if (crashMessage) {
|
|
|
|
|
return newGame
|
|
|
|
|
}
|
|
|
|
|
if (!game) {
|
|
|
|
|
return newGame
|
|
|
|
|
} else if (game.state == "ended") {
|
|
|
|
|
return newGame
|
|
|
|
|
}
|
|
|
|
|
return <></>
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-29 03:29:46 +03:00
|
|
|
return <div style={{
|
|
|
|
|
display: "flex",
|
|
|
|
|
flexDirection: "column",
|
2021-06-30 16:11:22 +03:00
|
|
|
alignItems: "center",
|
|
|
|
|
background: "#EEEEEE",
|
|
|
|
|
flex: "1",
|
2021-06-29 03:29:46 +03:00
|
|
|
}}>
|
2021-07-04 00:45:00 +03:00
|
|
|
{showConnectionState &&
|
|
|
|
|
<div style={{
|
2021-07-02 23:13:38 +03:00
|
|
|
position: "absolute",
|
|
|
|
|
bottom: "0px",
|
|
|
|
|
left: "0px",
|
2021-07-04 01:35:08 +03:00
|
|
|
padding: "15px ",
|
2021-07-02 23:13:38 +03:00
|
|
|
borderTopRightRadius: "1vw",
|
|
|
|
|
minWidth: "10vw",
|
|
|
|
|
minHeight: "1vw",
|
2021-07-04 01:35:08 +03:00
|
|
|
background: "#2F2504",
|
2021-07-04 01:51:39 +03:00
|
|
|
color: "white",
|
2021-07-02 23:13:38 +03:00
|
|
|
}}>
|
|
|
|
|
{connectionStateText()}
|
|
|
|
|
</div>}
|
2021-06-30 16:11:22 +03:00
|
|
|
<div style={{
|
|
|
|
|
padding: "0px 50px",
|
2021-06-30 19:41:53 +03:00
|
|
|
background: "rgb(228, 228, 228)",
|
2021-06-30 16:11:22 +03:00
|
|
|
display: 'flex',
|
|
|
|
|
flexDirection: "row",
|
|
|
|
|
alignItems: "center",
|
|
|
|
|
justifyContent: "space-between",
|
|
|
|
|
alignSelf: "stretch"
|
|
|
|
|
}}>
|
2021-07-04 00:45:00 +03:00
|
|
|
<h1 style={{ margin: "10px 0px" }}>{context.texts.Mancala}</h1>
|
|
|
|
|
<div>
|
|
|
|
|
{renderNewGameButton()}
|
2021-07-04 01:51:39 +03:00
|
|
|
{game && !userKeyWhoLeave && !crashMessage && game?.state == "playing" && < Button color="#005f73" text={context.texts.Leave} onClick={leaveGame} />}
|
2021-07-04 00:45:00 +03:00
|
|
|
</div>
|
2021-06-30 16:11:22 +03:00
|
|
|
|
2021-07-04 00:45:00 +03:00
|
|
|
</div>
|
2021-07-04 01:23:10 +03:00
|
|
|
<InfoPanel
|
|
|
|
|
game={game}
|
|
|
|
|
crashMessage={crashMessage}
|
|
|
|
|
userKey={userKey}
|
|
|
|
|
userKeyWhoLeave={userKeyWhoLeave}
|
|
|
|
|
searchingOpponent={searchingOpponent} />
|
2021-07-04 00:45:00 +03:00
|
|
|
{game && <BoardView userKey={userKey} game={game} onHoleSelect={onHoleSelect} />}
|
|
|
|
|
</div >
|
2021-06-27 19:28:55 +03:00
|
|
|
}
|
|
|
|
|
|
2021-07-04 01:23:10 +03:00
|
|
|
export default Home
|