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';
|
2022-07-30 14:17:16 +03:00
|
|
|
import { Theme } from './theme/Theme';
|
|
|
|
|
import LobyPage from './routes/LobyPage';
|
2022-09-03 23:27:13 +03:00
|
|
|
import swal from 'sweetalert';
|
2022-09-04 01:06:26 +03:00
|
|
|
import { ConnectionState } from './rtmt/rtmt';
|
2022-09-05 08:25:12 +03:00
|
|
|
import Util from './util/Util';
|
2022-09-03 23:27:13 +03:00
|
|
|
|
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-09-05 08:25:12 +03:00
|
|
|
const onConnectionError = (event: Event) => console.error("(RTMT) Connection Error: ", event);
|
|
|
|
|
|
|
|
|
|
const onConnectionChange = (_connectionState: ConnectionState) => setConnetionState(_connectionState);
|
|
|
|
|
|
|
|
|
|
const onThemeChange = (theme: Theme) => setTheme(theme);
|
|
|
|
|
|
|
|
|
|
const connectRTMT = (userKey: string) => {
|
|
|
|
|
const rtmt = context.rtmt as RTMTWS;
|
|
|
|
|
rtmt.on("error", onConnectionError);
|
|
|
|
|
rtmt.on("connectionchange", onConnectionChange)
|
|
|
|
|
rtmt.connectWebSocket(userKey)
|
|
|
|
|
return rtmt;
|
2022-09-02 00:03:37 +03:00
|
|
|
}
|
2022-09-05 08:25:12 +03:00
|
|
|
|
|
|
|
|
const loadUserKeyAndConnectServer = () => {
|
|
|
|
|
context.userKeyStore.getUserKey().then((userKey: string) => {
|
2022-09-03 23:27:13 +03:00
|
|
|
setUserKey(userKey);
|
2022-09-05 08:25:12 +03:00
|
|
|
connectRTMT(userKey);
|
|
|
|
|
}).catch((error) => {
|
2022-09-03 23:27:13 +03:00
|
|
|
//TODO: check if it is network error!
|
|
|
|
|
swal(context.texts.Error + "!", context.texts.ErrorWhenRetrievingInformation, "error");
|
|
|
|
|
console.error(error);
|
2022-09-05 08:25:12 +03:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const disposeApp = () => {
|
|
|
|
|
context.rtmt?.dispose();
|
|
|
|
|
context.themeManager.off("themechange", onThemeChange);
|
|
|
|
|
}
|
2022-09-02 00:03:37 +03:00
|
|
|
|
2022-07-30 12:01:50 +03:00
|
|
|
React.useEffect(() => {
|
2022-09-05 08:25:12 +03:00
|
|
|
loadUserKeyAndConnectServer();
|
2022-09-02 00:03:37 +03:00
|
|
|
context.themeManager.on("themechange", onThemeChange);
|
2022-09-05 08:25:12 +03:00
|
|
|
return () => disposeApp();
|
2022-07-30 12:01:50 +03:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const textColorOnBoard = getColorByBrightness(
|
|
|
|
|
context.themeManager.theme.boardColor,
|
|
|
|
|
context.themeManager.theme.textColor,
|
|
|
|
|
context.themeManager.theme.textLightColor
|
|
|
|
|
);
|
|
|
|
|
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-09-04 01:06:26 +03:00
|
|
|
<Route path=":gameId" element={<GamePage context={context} theme={theme} userKey={userKey} />} ></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>
|
2022-09-05 08:25:12 +03:00
|
|
|
<FloatingPanel context={context} color={context.themeManager.theme.boardColor} visible={connectionState != "connected"}>
|
|
|
|
|
<span style={{ color: textColorOnBoard, transition: 'color 0.5s' }}>{Util.getTextByConnectionState(context, connectionState)}</span>
|
2022-07-30 12:01:50 +03:00
|
|
|
</FloatingPanel>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default MancalaApp;
|