diff --git a/src/caches/GameSlice.ts b/src/caches/GameSlice.ts index 2d18fe5..73c516a 100644 --- a/src/caches/GameSlice.ts +++ b/src/caches/GameSlice.ts @@ -92,16 +92,23 @@ export const getIsDoublesGame = createSelector( export const getIsMyGo = createSelector(getGame, game => game.isMyGo) export const getIamGoer = createSelector(getGame, game => game.iamGoer) +export const getIHavePlayed = createSelector(getGame, game => { + if (game.iamGoer) return false + + const myPosition = game.players.findIndex(p => p.id === game.me?.id) + const currentPlayerPosition = game.players.findIndex( + p => p.id === game.round?.currentHand.currentPlayerId, + ) + return myPosition < currentPlayerPosition +}) export const getIamSpectator = createSelector( getGame, game => game.iamSpectator, ) -export const getCanBuyCards = createSelector( - getIsMyGo, - getIamGoer, +export const haveBoughtCards = createSelector( getIsRoundBuying, - (isMyGo, iamGoer, isRoundBuying) => isMyGo && iamGoer && isRoundBuying, + isRoundBuying => isRoundBuying, ) export const getIsInBunker = createSelector( diff --git a/src/caches/MyCardsSlice.ts b/src/caches/MyCardsSlice.ts index 159508e..5bc9966 100644 --- a/src/caches/MyCardsSlice.ts +++ b/src/caches/MyCardsSlice.ts @@ -41,6 +41,11 @@ export const myCardsSlice = createSlice({ if (c.name === action.payload.name) c.selected = !c.selected else c.selected = false }), + selectAll: state => { + state.cards.forEach(c => { + if (c.name !== BLANK_CARD.name) c.selected = true + }) + }, clearSelectedCards: state => { state.cards.forEach(c => { c.selected = false @@ -55,6 +60,7 @@ export const { replaceMyCards, removeCard, clearSelectedCards, + selectAll, toggleSelect, toggleUniqueSelect, clearMyCards, diff --git a/src/components/Game/AutoActionManager.tsx b/src/components/Game/AutoActionManager.tsx index e4889b6..81ffee9 100644 --- a/src/components/Game/AutoActionManager.tsx +++ b/src/components/Game/AutoActionManager.tsx @@ -3,7 +3,6 @@ import GameService from "../../services/GameService" import { useAppDispatch, useAppSelector } from "../../caches/hooks" import { - getCanBuyCards, getCards, getGameId, getIsInBunker, @@ -26,7 +25,6 @@ const AutoActionManager = () => { const autoPlayCard = useAppSelector(getAutoPlayCard) - const canBuyCards = useAppSelector(getCanBuyCards) const isMyGo = useAppSelector(getIsMyGo) const isInBunker = useAppSelector(getIsInBunker) @@ -40,9 +38,6 @@ const AutoActionManager = () => { const call = (id: string, callAmount: number) => dispatch(GameService.call(id, callAmount)).catch(console.error) - const buyCards = (gameId: string, cardsToBuy: string[]) => - dispatch(GameService.buyCards(gameId, cardsToBuy)).catch(console.error) - // If in the bunker, Pass useEffect(() => { if (gameId && isInBunker) call(gameId, 0) @@ -65,11 +60,6 @@ const AutoActionManager = () => { } }, [gameId, round, isMyGo, cards, autoPlayCard]) - // Buy cards in if you are the goer - useEffect(() => { - if (gameId && canBuyCards && cards.length <= 5) buyCards(gameId, cards) - }, [gameId, cards, canBuyCards]) - return null } diff --git a/src/components/Game/Buying.tsx b/src/components/Game/Buying.tsx index b7cafd2..3a0decf 100644 --- a/src/components/Game/Buying.tsx +++ b/src/components/Game/Buying.tsx @@ -1,6 +1,6 @@ -import { Button, ButtonGroup, Form, CardBody } from "reactstrap" +import { Button, ButtonGroup, CardBody } from "reactstrap" -import { useCallback, useState } from "react" +import { useCallback, useEffect, useState } from "react" import GameService from "../../services/GameService" import { useAppDispatch, useAppSelector } from "../../caches/hooks" @@ -8,11 +8,13 @@ import { useSnackbar } from "notistack" import { getMyCardsWithoutBlanks, getSelectedCards, + selectAll, } from "../../caches/MyCardsSlice" import { getGameId, + getIamGoer, + getIHavePlayed, getIsMyGo, - getIsRoundBuying, getSuit, } from "../../caches/GameSlice" import { riskOfMistakeBuyingCards } from "../../utils/GameUtils" @@ -26,24 +28,18 @@ const Buying = () => { const gameId = useAppSelector(getGameId) const suit = useAppSelector(getSuit) const myCards = useAppSelector(getMyCardsWithoutBlanks) - const isBuying = useAppSelector(getIsRoundBuying) + const [readyToBuy, setReadyToBuy] = useState(false) + const iHavePlayed = useAppSelector(getIHavePlayed) const isMyGo = useAppSelector(getIsMyGo) + const iamGoer = useAppSelector(getIamGoer) const [deleteCardsDialog, updateDeleteCardsDialog] = useState(false) const selectedCards = useAppSelector(getSelectedCards) - const buyCardsFormSubmit = useCallback( - (e?: React.FormEvent) => { - if (e) e.preventDefault() - if (!gameId) throw Error("No game id set") - - if (riskOfMistakeBuyingCards(suit!, selectedCards, myCards)) - showCancelDeleteCardsDialog() - else buyCards(gameId, selectedCards) - }, - [gameId, suit, selectedCards, myCards], - ) + const toggleReadyToBuy = useCallback(() => { + setReadyToBuy(!readyToBuy) + }, [readyToBuy]) const buyCards = (id: string, sel: SelectableCard[]) => { dispatch(GameService.buyCards(id, sel)).catch(e => @@ -55,32 +51,51 @@ const Buying = () => { updateDeleteCardsDialog(false) }, []) - const showCancelDeleteCardsDialog = () => { - updateDeleteCardsDialog(true) - } + useEffect(() => { + if (iamGoer) { + dispatch(selectAll()) + setReadyToBuy(true) + } + }, [iamGoer]) + + useEffect(() => { + if (isMyGo && readyToBuy) { + if (!gameId) throw Error("No game id set") + + if (riskOfMistakeBuyingCards(suit!, selectedCards, myCards)) { + setReadyToBuy(false) + updateDeleteCardsDialog(true) + } else buyCards(gameId, selectedCards) + } + }, [gameId, suit, selectedCards, myCards, isMyGo, readyToBuy]) return (
- {isBuying ? ( - -
- - {isMyGo ? ( - - ) : null} - -
+ + + {!iHavePlayed ? ( + + ) : null} + - - - ) : null} + +
) } diff --git a/src/components/Game/Calling.tsx b/src/components/Game/Calling.tsx index 9030ac0..bec11f2 100644 --- a/src/components/Game/Calling.tsx +++ b/src/components/Game/Calling.tsx @@ -3,7 +3,6 @@ import { Button, ButtonGroup, CardBody } from "reactstrap" import GameService from "../../services/GameService" import { useAppDispatch, useAppSelector } from "../../caches/hooks" import { getGame, getGameId } from "../../caches/GameSlice" -import { RoundStatus } from "../../model/Round" import { useCallback } from "react" import { useSnackbar } from "notistack" import parseError from "../../utils/ErrorUtils" @@ -36,67 +35,65 @@ const Calling = () => { } return (
- {!!game.round && game.round.status === RoundStatus.CALLING ? ( - - + + + + {game.players.length === 6 && + ((game.iamDealer && game.maxCall! <= 10) || + game.maxCall! < 10) ? ( - {game.players.length === 6 && - ((game.iamDealer && game.maxCall! <= 10) || - game.maxCall! < 10) ? ( - - ) : null} - {(game.iamDealer && game.maxCall! <= 15) || - game.maxCall! < 15 ? ( - - ) : null} - {(game.iamDealer && game.maxCall! <= 20) || - game.maxCall! < 20 ? ( - - ) : null} - {(game.iamDealer && game.maxCall! <= 25) || - game.maxCall! < 25 ? ( - - ) : null} + ) : null} + {(game.iamDealer && game.maxCall! <= 15) || + game.maxCall! < 15 ? ( - - - ) : null} + ) : null} + {(game.iamDealer && game.maxCall! <= 20) || + game.maxCall! < 20 ? ( + + ) : null} + {(game.iamDealer && game.maxCall! <= 25) || + game.maxCall! < 25 ? ( + + ) : null} + + +
) } diff --git a/src/components/Game/GameWrapper.tsx b/src/components/Game/GameWrapper.tsx index cac3b9e..9fb8ed2 100644 --- a/src/components/Game/GameWrapper.tsx +++ b/src/components/Game/GameWrapper.tsx @@ -8,10 +8,16 @@ import SelectSuit from "./SelectSuit" import WebsocketManager from "./WebsocketManager" import { useAppSelector } from "../../caches/hooks" -import { getIamSpectator } from "../../caches/GameSlice" +import { + getIamSpectator, + getIsRoundBuying, + getIsRoundCalling, +} from "../../caches/GameSlice" const GameWrapper = () => { const iamSpectator = useAppSelector(getIamSpectator) + const isBuying = useAppSelector(getIsRoundBuying) + const isCalling = useAppSelector(getIsRoundCalling) return ( @@ -21,8 +27,8 @@ const GameWrapper = () => { {!iamSpectator ? : null} - {!iamSpectator ? : null} - {!iamSpectator ? : null} + {!iamSpectator && isCalling ? : null} + {!iamSpectator && isBuying ? : null} {!iamSpectator ? : null}