check has an ongoing action on pit select

This commit is contained in:
Halit Aksoy 2022-07-15 18:50:05 +03:00
parent 7fc7abd995
commit 88669e8ded

View File

@ -52,6 +52,10 @@ const Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => {
const [theme, setTheme] = useState<Theme | undefined>(undefined); const [theme, setTheme] = useState<Theme | undefined>(undefined);
// It is a flag for ongoing action such as send game move.
// We have to block future actions if there is an ongoing action.
const [hasOngoingAction, setHasOngoingAction] = useState<boolean>(false);
const onConnectionDone = () => { const onConnectionDone = () => {
setConnetionState("connected"); setConnetionState("connected");
}; };
@ -89,6 +93,7 @@ const Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => {
setSearchingOpponent(false); setSearchingOpponent(false);
setGame(mancalaGame); setGame(mancalaGame);
pitAnimator.setNewGame(mancalaGame); pitAnimator.setNewGame(mancalaGame);
setHasOngoingAction(false);
}); });
context.rtmt.listenMessage(channel_on_game_update, (message: Object) => { context.rtmt.listenMessage(channel_on_game_update, (message: Object) => {
@ -96,6 +101,7 @@ const Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => {
const mancalaGame = MancalaGame.createFromMancalaGame(newGame); const mancalaGame = MancalaGame.createFromMancalaGame(newGame);
setGame(mancalaGame); setGame(mancalaGame);
pitAnimator.setUpdatedGame(mancalaGame); pitAnimator.setUpdatedGame(mancalaGame);
setHasOngoingAction(false);
}); });
context.rtmt.listenMessage(channel_on_game_crashed, (message: any) => { context.rtmt.listenMessage(channel_on_game_crashed, (message: any) => {
@ -108,6 +114,7 @@ const Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => {
context.rtmt.listenMessage(channel_on_game_user_leave, (message: any) => { context.rtmt.listenMessage(channel_on_game_user_leave, (message: any) => {
const userKeyWhoLeave = message; const userKeyWhoLeave = message;
setUserKeyWhoLeave(userKeyWhoLeave); setUserKeyWhoLeave(userKeyWhoLeave);
setHasOngoingAction(false);
}); });
}; };
@ -121,14 +128,17 @@ const Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => {
setGame(undefined); setGame(undefined);
setCrashMessage(undefined); setCrashMessage(undefined);
setUserKeyWhoLeave(undefined); setUserKeyWhoLeave(undefined);
setHasOngoingAction(false);
}; };
const getBoardIndex = (index: number) => { const getBoardIndex = (index: number) => {
if(!game) return -1; if (!game) return -1;
if (userKey === game.player2Id) return index + game.board.pits.length / 2; if (userKey === game.player2Id) return index + game.board.pits.length / 2;
return index; return index;
}; };
const checkHasAnOngoingAction = () => hasOngoingAction;
React.useEffect(() => { React.useEffect(() => {
setTheme(context.themeManager.theme); setTheme(context.themeManager.theme);
const pitAnimator = new PitAnimator(context, updateBoardViewModel); const pitAnimator = new PitAnimator(context, updateBoardViewModel);
@ -158,7 +168,11 @@ const Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => {
}; };
const onPitSelect = (index: number, pit: Pit) => { const onPitSelect = (index: number, pit: Pit) => {
if(!boardViewModel) return; if(checkHasAnOngoingAction()) {
return;
}
setHasOngoingAction(true);
if (!boardViewModel) return;
//TODO : stoneCount comes from view model! //TODO : stoneCount comes from view model!
if (pit.stoneCount === 0) { if (pit.stoneCount === 0) {
//TODO : warn user //TODO : warn user