2022-07-30 12:01:50 +03:00
|
|
|
import * as React from 'react';
|
|
|
|
|
import { FunctionComponent, useState } from 'react';
|
|
|
|
|
import {
|
|
|
|
|
BrowserRouter,
|
|
|
|
|
Routes,
|
|
|
|
|
Route,
|
|
|
|
|
} from "react-router-dom";
|
|
|
|
|
import FloatingPanel from './components/FloatingPanel';
|
|
|
|
|
import GamePage from './routes/GamePage';
|
|
|
|
|
import Home from './routes/Home';
|
|
|
|
|
|
|
|
|
|
import { initContext } from './context/context';
|
|
|
|
|
import { RTMTWS } from './rtmt/rtmt_websocket';
|
|
|
|
|
import { getColorByBrightness } from './util/ColorUtil';
|
|
|
|
|
import { ConnectionState } from './models/ConnectionState';
|
2022-07-30 14:17:16 +03:00
|
|
|
import { Theme } from './theme/Theme';
|
|
|
|
|
import LobyPage from './routes/LobyPage';
|
2022-07-30 12:01:50 +03:00
|
|
|
const context = initContext();
|
|
|
|
|
|
|
|
|
|
const MancalaApp: FunctionComponent = () => {
|
2022-07-30 14:17:16 +03:00
|
|
|
|
2022-07-30 12:01:50 +03:00
|
|
|
const [userKey, setUserKey] = useState<string | undefined>(undefined);
|
|
|
|
|
|
|
|
|
|
const [connectionState, setConnetionState] = useState<ConnectionState>("connecting");
|
|
|
|
|
|
2022-07-30 14:17:16 +03:00
|
|
|
const [theme, setTheme] = useState<Theme>(context.themeManager.theme);
|
|
|
|
|
|
2022-07-30 12:01:50 +03:00
|
|
|
const onConnectionDone = () => {
|
|
|
|
|
setConnetionState("connected");
|
|
|
|
|
};
|
|
|
|
|
const onConnectionLost = () => {
|
|
|
|
|
connectToServer("reconnecting");
|
|
|
|
|
};
|
|
|
|
|
const onConnectionError = (event: Event) => {
|
|
|
|
|
setConnetionState("error");
|
|
|
|
|
};
|
2022-07-30 16:32:13 +03:00
|
|
|
const connectToServer = async (connectionState: ConnectionState) => {
|
2022-07-30 12:01:50 +03:00
|
|
|
setConnetionState(connectionState);
|
2022-07-30 16:32:13 +03:00
|
|
|
const userKey = await context.userKeyStore.getUserKey();
|
|
|
|
|
setUserKey(userKey);
|
|
|
|
|
const rtmtws = context.rtmt as RTMTWS;
|
|
|
|
|
if (rtmtws) {
|
|
|
|
|
rtmtws.initWebSocket(
|
|
|
|
|
userKey,
|
|
|
|
|
onConnectionDone,
|
|
|
|
|
onConnectionLost,
|
|
|
|
|
onConnectionError
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
console.error("context.rtmt is not RTMTWS");
|
|
|
|
|
}
|
2022-07-30 12:01:50 +03:00
|
|
|
};
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
connectToServer("connecting");
|
2022-07-30 14:17:16 +03:00
|
|
|
context.themeManager.onThemeChange = (theme: Theme) => {
|
|
|
|
|
setTheme(theme);
|
|
|
|
|
}
|
2022-07-30 12:01:50 +03:00
|
|
|
return () => {
|
|
|
|
|
// todo: dispose rtmt.dispose
|
|
|
|
|
//context.rtmt.dispose();
|
|
|
|
|
};
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const showConnectionState = connectionState != "connected";
|
|
|
|
|
const floatingPanelColor = context.themeManager.theme.boardColor;
|
|
|
|
|
const connectionStateText = () => {
|
|
|
|
|
const map: { [key: string]: string } = {
|
|
|
|
|
connecting: context.texts.Connecting,
|
|
|
|
|
connected: context.texts.Connected,
|
|
|
|
|
error: context.texts.CannotConnect,
|
|
|
|
|
reconnecting: context.texts.ConnectingAgain,
|
|
|
|
|
};
|
|
|
|
|
return map[connectionState];
|
|
|
|
|
};
|
|
|
|
|
const textColorOnBoard = getColorByBrightness(
|
|
|
|
|
context.themeManager.theme.boardColor,
|
|
|
|
|
context.themeManager.theme.textColor,
|
|
|
|
|
context.themeManager.theme.textLightColor
|
|
|
|
|
);
|
2022-09-02 00:10:41 +03:00
|
|
|
if(!userKey) return <></>;
|
2022-07-30 12:01:50 +03:00
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<BrowserRouter>
|
|
|
|
|
<Routes>
|
2022-07-30 14:17:16 +03:00
|
|
|
<Route index element={<Home context={context} theme={theme} userKey={userKey} />} />
|
2022-07-30 12:01:50 +03:00
|
|
|
<Route path="/" >
|
|
|
|
|
<Route path="game" >
|
2022-07-30 16:32:13 +03:00
|
|
|
<Route path=":gameId" element={<GamePage context={context} theme={theme} userKey={userKey} connectionState={connectionState} />} ></Route>
|
2022-07-30 14:17:16 +03:00
|
|
|
</Route>
|
|
|
|
|
<Route path="loby" element={<LobyPage context={context} theme={theme} userKey={userKey} />}>
|
2022-07-30 12:01:50 +03:00
|
|
|
</Route>
|
|
|
|
|
</Route>
|
|
|
|
|
</Routes>
|
|
|
|
|
</BrowserRouter>
|
|
|
|
|
<FloatingPanel context={context} color={floatingPanelColor} visible={showConnectionState}>
|
|
|
|
|
<span style={{ color: textColorOnBoard }}>{connectionStateText()}</span>
|
|
|
|
|
</FloatingPanel>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default MancalaApp;
|