mancala/src/routes/Home.tsx

238 lines
7.9 KiB
TypeScript
Raw Normal View History

2022-05-12 23:41:24 +03:00
import * as React from "react";
import { FunctionComponent, useEffect, useState } from "react";
2022-07-14 13:38:00 +03:00
import BoardView from "../components/board/BoardView";
import { context } from "../context/context";
import { RTMTWS } from "../rtmt/rtmt_websocket";
2022-05-12 23:41:24 +03:00
import {
channel_game_move,
channel_leave_game,
channel_on_game_crashed,
channel_on_game_start,
2022-05-12 23:41:24 +03:00
channel_on_game_update,
channel_on_game_user_leave,
2022-07-14 13:38:00 +03:00
} from "../const/channel_names";
import InfoPanel from "../components/InfoPanel";
2022-05-12 23:41:24 +03:00
import { CommonMancalaGame, MancalaGame, Pit } from "mancala.js";
2022-07-14 13:38:00 +03:00
import { GameMove } from "../models/GameMove";
import PitAnimator from "../animation/PitAnimator";
import BoardViewModel from "../viewmodel/BoardViewModel";
2022-05-22 17:57:33 +03:00
import { v4 } from "uuid";
2022-07-14 13:38:00 +03:00
import { getColorByBrightness } from "../util/ColorUtil";
import { Theme } from "../theme/Theme";
import HeaderBar from "../components/HeaderBar";
import FloatingPanel from "../components/FloatingPanel";
import PageContainer from "../components/PageContainer";
2022-07-15 18:11:09 +03:00
import Row from "../components/Row";
import HeaderbarIcon from "../components/headerbar/HeaderbarIcon";
import HeaderbarTitle from "../components/headerbar/HeaderbarTitle";
import ThemeSwitchMenu from "../components/headerbar/ThemeSwitchMenu";
import Button from "../components/Button";
2022-05-12 23:41:24 +03:00
type ConnectionState = "connecting" | "error" | "connected" | "reconnecting";
2021-07-02 23:13:38 +03:00
2021-06-29 03:29:46 +03:00
const Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => {
2022-07-14 13:23:15 +03:00
const [userKey, setUserKey] = useState<string | undefined>(undefined);
2021-06-29 03:29:46 +03:00
2022-05-12 23:41:24 +03:00
const [connectionState, setConnetionState] =
useState<ConnectionState>("connecting");
2021-06-30 19:41:53 +03:00
2022-05-12 23:41:24 +03:00
const [searchingOpponent, setSearchingOpponent] = useState<boolean>(false);
2021-07-04 01:23:10 +03:00
2022-07-14 13:23:15 +03:00
const [game, setGame] = useState<CommonMancalaGame | undefined>(undefined);
2022-07-14 13:23:15 +03:00
const [crashMessage, setCrashMessage] = useState<string | undefined>(undefined);
2021-07-04 00:45:00 +03:00
2022-07-14 13:23:15 +03:00
const [userKeyWhoLeave, setUserKeyWhoLeave] = useState<string | undefined>(undefined);
2022-05-12 23:41:24 +03:00
2022-07-14 13:23:15 +03:00
const [boardViewModel, setBoardViewModel] = useState<BoardViewModel | undefined>(undefined);
2022-05-12 23:41:24 +03:00
2022-07-14 13:23:15 +03:00
const [boardId, setBoardId] = useState<string>("-1");
2022-05-12 23:41:24 +03:00
2022-07-14 13:23:15 +03:00
const [pitAnimator, setPitAnimator] = useState<PitAnimator | undefined>(undefined);
const [theme, setTheme] = useState<Theme | undefined>(undefined);
2021-07-02 23:13:38 +03:00
const onConnectionDone = () => {
2022-05-12 23:41:24 +03:00
setConnetionState("connected");
};
2021-07-02 23:13:38 +03:00
const onConnectionLost = () => {
2022-05-12 23:41:24 +03:00
connectToServer("reconnecting");
};
2021-06-30 19:41:53 +03:00
2021-07-02 23:13:38 +03:00
const onConnectionError = (event: Event) => {
2022-05-12 23:41:24 +03:00
setConnetionState("error");
};
2021-07-02 23:13:38 +03:00
const connectToServer = (connectionState: ConnectionState) => {
2022-05-12 23:41:24 +03:00
setConnetionState(connectionState);
2021-06-29 03:29:46 +03:00
context.userKeyStore.getUserKey((userKey: string) => {
2022-05-12 23:41:24 +03:00
setUserKey(userKey);
const rtmtws = context.rtmt as RTMTWS;
2021-06-29 03:29:46 +03:00
if (rtmtws) {
2022-05-12 23:41:24 +03:00
rtmtws.initWebSocket(
userKey,
onConnectionDone,
onConnectionLost,
onConnectionError
);
2021-06-29 03:29:46 +03:00
} else {
2022-05-04 23:41:04 +03:00
console.error("context.rtmt is not RTMTWS");
2021-06-29 03:29:46 +03:00
}
2022-05-12 23:41:24 +03:00
});
};
2021-06-29 03:29:46 +03:00
const listenMessages = (pitAnimator: PitAnimator) => {
context.rtmt.listenMessage(channel_on_game_start, (message: Object) => {
2022-05-04 23:41:04 +03:00
const newGame: CommonMancalaGame = message as CommonMancalaGame;
2022-05-12 23:41:24 +03:00
const mancalaGame = MancalaGame.createFromMancalaGame(newGame);
setSearchingOpponent(false);
setGame(mancalaGame);
pitAnimator.setNewGame(mancalaGame);
2022-05-12 23:41:24 +03:00
});
2021-06-27 19:28:55 +03:00
context.rtmt.listenMessage(channel_on_game_update, (message: Object) => {
2022-05-04 23:41:04 +03:00
const newGame: CommonMancalaGame = message as CommonMancalaGame;
const mancalaGame = MancalaGame.createFromMancalaGame(newGame);
setGame(mancalaGame);
pitAnimator.setUpdatedGame(mancalaGame);
2022-05-12 23:41:24 +03:00
});
2021-07-04 00:45:00 +03:00
context.rtmt.listenMessage(channel_on_game_crashed, (message: any) => {
2022-05-12 23:41:24 +03:00
const newCrashMessage = message as string;
2022-05-04 23:41:04 +03:00
console.error("on_game_crash");
console.error(newCrashMessage);
2022-05-12 23:41:24 +03:00
setCrashMessage(newCrashMessage);
});
2022-05-12 23:41:24 +03:00
context.rtmt.listenMessage(channel_on_game_user_leave, (message: any) => {
2022-04-23 12:53:08 +03:00
const userKeyWhoLeave = message;
2022-05-12 23:41:24 +03:00
setUserKeyWhoLeave(userKeyWhoLeave);
});
};
2021-06-27 19:28:55 +03:00
const updateBoardViewModel = (boardViewModel: BoardViewModel) => {
2022-05-22 17:57:33 +03:00
boardViewModel.id = v4();
setBoardId(boardViewModel.id);
setBoardViewModel(boardViewModel);
};
2022-07-13 22:39:01 +03:00
const resetGameState = () => {
setGame(undefined);
setCrashMessage(undefined);
setUserKeyWhoLeave(undefined);
};
const getBoardIndex = (index: number) => {
2022-07-14 13:23:15 +03:00
if(!game) return -1;
2022-07-13 22:39:01 +03:00
if (userKey === game.player2Id) return index + game.board.pits.length / 2;
return index;
};
2021-07-02 23:13:38 +03:00
React.useEffect(() => {
setTheme(context.themeManager.theme);
const pitAnimator = new PitAnimator(context, updateBoardViewModel);
setPitAnimator(pitAnimator);
listenMessages(pitAnimator);
2022-05-12 23:41:24 +03:00
connectToServer("connecting");
return () => {
pitAnimator.dispose();
};
2022-05-12 23:41:24 +03:00
}, []);
2021-07-02 23:13:38 +03:00
2022-06-04 20:54:19 +03:00
React.useEffect(() => {
context.themeManager.onThemeChange = (theme) => {
setTheme(theme);
2022-07-14 13:23:15 +03:00
pitAnimator && game && updateBoardViewModel(pitAnimator.getBoardViewModelFromGame(game));
2022-06-04 20:54:19 +03:00
};
}, [boardViewModel]);
2022-07-13 22:39:01 +03:00
const onNewGameClick = () => {
2022-05-12 23:41:24 +03:00
resetGameState();
setSearchingOpponent(true);
context.rtmt.sendMessage("new_game", {});
};
2021-07-02 23:13:38 +03:00
2022-07-13 22:39:01 +03:00
const onLeaveGameClick = () => {
2022-05-12 23:41:24 +03:00
context.rtmt.sendMessage(channel_leave_game, {});
};
2021-06-29 03:29:46 +03:00
2022-07-13 22:45:06 +03:00
const onPitSelect = (index: number, pit: Pit) => {
2022-07-14 13:23:15 +03:00
if(!boardViewModel) return;
//TODO : stoneCount comes from view model!
if (pit.stoneCount === 0) {
//TODO : warn user
return;
}
2022-05-22 17:58:11 +03:00
boardViewModel.pits[getBoardIndex(index)].pitColor =
context.themeManager.theme.pitSelectedColor;
updateBoardViewModel(boardViewModel);
2022-05-12 23:41:24 +03:00
const gameMove: GameMove = { index: index };
context.rtmt.sendMessage(channel_game_move, gameMove);
};
2021-06-29 03:29:46 +03:00
2022-05-12 23:41:24 +03:00
const showConnectionState = connectionState != "connected";
2021-07-02 23:13:38 +03:00
const connectionStateText = () => {
let map: { [key: string]: string } = {
2022-05-12 23:41:24 +03:00
connecting: context.texts.Connecting,
connected: context.texts.Connected,
error: context.texts.CannotConnect,
reconnecting: context.texts.ConnectingAgain,
2021-07-02 23:13:38 +03:00
};
2022-05-12 23:41:24 +03:00
return map[connectionState];
};
2021-07-02 23:13:38 +03:00
2022-07-13 22:39:01 +03:00
const textColorOnBoard = getColorByBrightness(
context.themeManager.theme.boardColor,
2022-07-13 15:21:30 +03:00
context.themeManager.theme.textColor,
context.themeManager.theme.textLightColor
2022-06-04 20:54:19 +03:00
);
2022-07-15 18:11:09 +03:00
const textColorOnAppBar = getColorByBrightness(
context.themeManager.theme.appBarBgColor,
context.themeManager.theme.textColor,
context.themeManager.theme.textLightColor
);
const renderNewGameBtn = userKeyWhoLeave || !game || (game && game.state == "ended");
2022-05-12 23:41:24 +03:00
return (
2022-07-13 22:39:01 +03:00
<PageContainer theme={theme!}>
<FloatingPanel context={context} color={context.themeManager.theme.boardColor} visible={showConnectionState}>
<span style={{ color: textColorOnBoard }}>{connectionStateText()}</span>
</FloatingPanel>
2022-07-15 18:11:09 +03:00
<HeaderBar color={theme?.appBarBgColor}>
<Row>
<HeaderbarIcon />
<HeaderbarTitle title={context.texts.Mancala} color={textColorOnAppBar} />
</Row>
<Row>
<ThemeSwitchMenu context={context} textColor={textColorOnAppBar} />
<Button
context={context}
color={context.themeManager.theme.pitColor}
text={renderNewGameBtn ? context.texts.NewGame : context.texts.Leave}
onClick={renderNewGameBtn ? onNewGameClick : onLeaveGameClick} />
</Row>
</HeaderBar>
2022-05-12 23:41:24 +03:00
<InfoPanel
2022-06-04 14:49:34 +03:00
context={context}
2022-05-12 23:41:24 +03:00
game={game}
crashMessage={crashMessage}
userKey={userKey}
userKeyWhoLeave={userKeyWhoLeave}
2022-07-14 13:23:15 +03:00
searchingOpponent={searchingOpponent} />
{game && boardViewModel && userKey && (
2022-05-12 23:41:24 +03:00
<BoardView
userKey={userKey}
game={game}
boardId={boardId}
boardViewModel={boardViewModel}
context={context}
2022-07-14 13:23:15 +03:00
onPitSelect={onPitSelect} />
2022-05-12 23:41:24 +03:00
)}
2022-07-13 22:39:01 +03:00
</PageContainer>
2022-05-12 23:41:24 +03:00
);
};
export default Home;