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';
|
2024-06-17 17:25:40 +03:00
|
|
|
import { RTMTWS, ConnectionState } from '@mancala/core';
|
2022-07-30 12:01:50 +03:00
|
|
|
import { getColorByBrightness } from './util/ColorUtil';
|
2024-06-17 23:50:13 +03:00
|
|
|
import { Theme } from '@mancala/core';
|
2022-07-30 14:17:16 +03:00
|
|
|
import LobyPage from './routes/LobyPage';
|
2024-04-04 13:40:57 +03:00
|
|
|
import PrivacyPage from './routes/PrivacyPage';
|
2022-09-03 23:27:13 +03:00
|
|
|
import swal from 'sweetalert';
|
2022-09-05 08:25:12 +03:00
|
|
|
import Util from './util/Util';
|
2024-06-17 17:25:40 +03:00
|
|
|
import { server } from './const/config';
|
2024-06-17 23:50:13 +03:00
|
|
|
import { useTranslation } from 'react-i18next';
|
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
|
|
|
|
2024-06-17 23:50:13 +03:00
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
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)
|
2024-06-17 17:25:40 +03:00
|
|
|
const url = server.wsServerAdress + "?userKey=" + userKey;
|
|
|
|
|
rtmt.connectWebSocket(url, userKey)
|
2022-09-05 08:25:12 +03:00
|
|
|
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!
|
2024-06-17 23:50:13 +03:00
|
|
|
swal(t("Error") + "!", t("ErrorWhenRetrievingInformation"), "error");
|
2022-09-03 23:27:13 +03:00
|
|
|
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>
|
2024-04-04 13:40:57 +03:00
|
|
|
<Route path="privacy" element={<PrivacyPage context={context} theme={theme} userKey={userKey} />}>
|
|
|
|
|
</Route>
|
2022-07-30 12:01:50 +03:00
|
|
|
</Route>
|
|
|
|
|
</Routes>
|
|
|
|
|
</BrowserRouter>
|
2022-09-05 08:25:12 +03:00
|
|
|
<FloatingPanel context={context} color={context.themeManager.theme.boardColor} visible={connectionState != "connected"}>
|
2024-06-17 23:50:13 +03:00
|
|
|
<span style={{ color: textColorOnBoard, transition: 'color 0.5s' }}>{Util.getTextByConnectionState(context, connectionState, t)}</span>
|
2022-07-30 12:01:50 +03:00
|
|
|
</FloatingPanel>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default MancalaApp;
|