add checkConnectionAndMaybeAlert

This commit is contained in:
Halit Aksoy 2022-09-04 01:06:26 +03:00
parent 472044577f
commit 639cb942ef
3 changed files with 11 additions and 7 deletions

View File

@ -12,10 +12,10 @@ import Home from './routes/Home';
import { initContext } from './context/context'; import { initContext } from './context/context';
import { RTMTWS } from './rtmt/rtmt_websocket'; import { RTMTWS } from './rtmt/rtmt_websocket';
import { getColorByBrightness } from './util/ColorUtil'; import { getColorByBrightness } from './util/ColorUtil';
import { ConnectionState } from './models/ConnectionState';
import { Theme } from './theme/Theme'; import { Theme } from './theme/Theme';
import LobyPage from './routes/LobyPage'; import LobyPage from './routes/LobyPage';
import swal from 'sweetalert'; import swal from 'sweetalert';
import { ConnectionState } from './rtmt/rtmt';
const context = initContext(); const context = initContext();
@ -89,7 +89,7 @@ const MancalaApp: FunctionComponent = () => {
<Route index element={<Home context={context} theme={theme} userKey={userKey} />} /> <Route index element={<Home context={context} theme={theme} userKey={userKey} />} />
<Route path="/" > <Route path="/" >
<Route path="game" > <Route path="game" >
<Route path=":gameId" element={<GamePage context={context} theme={theme} userKey={userKey} connectionState={connectionState} />} ></Route> <Route path=":gameId" element={<GamePage context={context} theme={theme} userKey={userKey} />} ></Route>
</Route> </Route>
<Route path="loby" element={<LobyPage context={context} theme={theme} userKey={userKey} />}> <Route path="loby" element={<LobyPage context={context} theme={theme} userKey={userKey} />}>
</Route> </Route>

View File

@ -20,7 +20,6 @@ import UserStatus from '../components/UserStatus';
import { channel_on_game_update, channel_on_game_crashed, channel_on_game_user_leave, channel_on_user_connection_change, channel_leave_game, channel_game_move, channel_listen_game_events, channel_unlisten_game_events } from '../const/channel_names'; import { channel_on_game_update, channel_on_game_crashed, channel_on_game_user_leave, channel_on_user_connection_change, channel_leave_game, channel_game_move, channel_listen_game_events, channel_unlisten_game_events } from '../const/channel_names';
import { Context } from '../context/context'; import { Context } from '../context/context';
import useWindowDimensions from '../hooks/useWindowDimensions'; import useWindowDimensions from '../hooks/useWindowDimensions';
import { ConnectionState } from '../models/ConnectionState';
import { GameMove } from '../models/GameMove'; import { GameMove } from '../models/GameMove';
import { LoadingState } from '../models/LoadingState'; import { LoadingState } from '../models/LoadingState';
import { Theme } from '../theme/Theme'; import { Theme } from '../theme/Theme';
@ -30,13 +29,13 @@ import Center from '../components/Center';
import { Game, GameUsersConnectionInfo } from '../models/Game'; import { Game, GameUsersConnectionInfo } from '../models/Game';
import notyf from '../util/Notyf'; import notyf from '../util/Notyf';
import swal from 'sweetalert'; import swal from 'sweetalert';
import Util from '../util/Util';
const GamePage: FunctionComponent<{ const GamePage: FunctionComponent<{
context: Context, context: Context,
userKey?: string, userKey?: string,
theme: Theme, theme: Theme,
connectionState: ConnectionState }> = ({ context, userKey, theme }) => {
}> = ({ context, userKey, theme, connectionState }) => {
let params = useParams<{ gameId: string }>(); let params = useParams<{ gameId: string }>();
const [game, setGame] = useState<Game | undefined>(undefined); const [game, setGame] = useState<Game | undefined>(undefined);
@ -139,6 +138,7 @@ const GamePage: FunctionComponent<{
const checkHasAnOngoingAction = () => hasOngoingAction; const checkHasAnOngoingAction = () => hasOngoingAction;
const onLeaveGameClick = () => { const onLeaveGameClick = () => {
if(Util.checkConnectionAndMaybeAlert(context)) return;
swal({ swal({
title: context.texts.AreYouSureToLeaveGame, title: context.texts.AreYouSureToLeaveGame,
icon: "warning", icon: "warning",
@ -153,6 +153,7 @@ const GamePage: FunctionComponent<{
}; };
const onNewGameClick = () => { const onNewGameClick = () => {
if(Util.checkConnectionAndMaybeAlert(context)) return;
navigate("/loby") navigate("/loby")
}; };
@ -173,6 +174,7 @@ const GamePage: FunctionComponent<{
notyf.error(context.texts.UMustWaitUntilCurrentMoveComplete); notyf.error(context.texts.UMustWaitUntilCurrentMoveComplete);
return; return;
} }
if(Util.checkConnectionAndMaybeAlert(context)) return;
if (!boardViewModel) return; if (!boardViewModel) return;
//TODO: this check should be in mancala.js //TODO: this check should be in mancala.js
if (pit.stoneCount === 0) { if (pit.stoneCount === 0) {
@ -228,13 +230,13 @@ const GamePage: FunctionComponent<{
const bottomLocatedUser = { const bottomLocatedUser = {
id: bottomLocatedUserId, id: bottomLocatedUserId,
name: "Anonymous", name: "Anonymous",
isOnline: isSpectator ? isUserOnline(bottomLocatedUserId) : connectionState === "connected", isOnline: isSpectator ? isUserOnline(bottomLocatedUserId) : context.rtmt.connectionState === "connected",
isAnonymous: true isAnonymous: true
}; };
const currentUser = isSpectator ? { const currentUser = isSpectator ? {
id: "2", id: "2",
name: "Anonymous", name: "Anonymous",
isOnline: connectionState === "connected", isOnline: context.rtmt.connectionState === "connected",
isAnonymous: true isAnonymous: true
} : bottomLocatedUser; } : bottomLocatedUser;
const leftPlayer = userKeyWhoLeave ? (userKeyWhoLeave === topLocatedUser.id ? topLocatedUser : bottomLocatedUser) : undefined; const leftPlayer = userKeyWhoLeave ? (userKeyWhoLeave === topLocatedUser.id ? topLocatedUser : bottomLocatedUser) : undefined;

View File

@ -11,6 +11,7 @@ import ThemeSwitchMenu from "../components/headerbar/ThemeSwitchMenu";
import Button from "../components/Button"; import Button from "../components/Button";
import { Context } from "../context/context"; import { Context } from "../context/context";
import { Link, useNavigate } from "react-router-dom"; import { Link, useNavigate } from "react-router-dom";
import Util from "../util/Util";
const Home: FunctionComponent<{ const Home: FunctionComponent<{
context: Context, context: Context,
@ -21,6 +22,7 @@ const Home: FunctionComponent<{
const navigate = useNavigate(); const navigate = useNavigate();
const onNewGameClick = () => { const onNewGameClick = () => {
if(Util.checkConnectionAndMaybeAlert(context)) return;
navigate("/loby") navigate("/loby")
}; };