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);
// 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 = () => {
setConnetionState("connected");
};
@ -89,6 +93,7 @@ const Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => {
setSearchingOpponent(false);
setGame(mancalaGame);
pitAnimator.setNewGame(mancalaGame);
setHasOngoingAction(false);
});
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);
setGame(mancalaGame);
pitAnimator.setUpdatedGame(mancalaGame);
setHasOngoingAction(false);
});
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) => {
const userKeyWhoLeave = message;
setUserKeyWhoLeave(userKeyWhoLeave);
setHasOngoingAction(false);
});
};
@ -121,14 +128,17 @@ const Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => {
setGame(undefined);
setCrashMessage(undefined);
setUserKeyWhoLeave(undefined);
setHasOngoingAction(false);
};
const getBoardIndex = (index: number) => {
if(!game) return -1;
if (!game) return -1;
if (userKey === game.player2Id) return index + game.board.pits.length / 2;
return index;
};
const checkHasAnOngoingAction = () => hasOngoingAction;
React.useEffect(() => {
setTheme(context.themeManager.theme);
const pitAnimator = new PitAnimator(context, updateBoardViewModel);
@ -158,7 +168,11 @@ const Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => {
};
const onPitSelect = (index: number, pit: Pit) => {
if(!boardViewModel) return;
if(checkHasAnOngoingAction()) {
return;
}
setHasOngoingAction(true);
if (!boardViewModel) return;
//TODO : stoneCount comes from view model!
if (pit.stoneCount === 0) {
//TODO : warn user
@ -201,19 +215,19 @@ const Home: FunctionComponent<{ initial?: number }> = ({ initial = 0 }) => {
<span style={{ color: textColorOnBoard }}>{connectionStateText()}</span>
</FloatingPanel>
<HeaderBar color={theme?.appBarBgColor}>
<Row>
<HeaderbarIcon />
<HeaderbarTitle title={context.texts.Mancala} color={textColorOnAppBar} />
</Row>
<Row>
<ThemeSwitchMenu context={context} textColor={textColorOnAppBar} />
<Button
context={context}
color={context.themeManager.theme.pitColor}
text={renderNewGameBtn ? context.texts.NewGame : context.texts.Leave}
onClick={renderNewGameBtn ? onNewGameClick : onLeaveGameClick} />
</Row>
</HeaderBar>
<Row>
<HeaderbarIcon />
<HeaderbarTitle title={context.texts.Mancala} color={textColorOnAppBar} />
</Row>
<Row>
<ThemeSwitchMenu context={context} textColor={textColorOnAppBar} />
<Button
context={context}
color={context.themeManager.theme.pitColor}
text={renderNewGameBtn ? context.texts.NewGame : context.texts.Leave}
onClick={renderNewGameBtn ? onNewGameClick : onLeaveGameClick} />
</Row>
</HeaderBar>
<InfoPanel
context={context}
game={game}