Skip to content

Commit

Permalink
Auto select trump cards
Browse files Browse the repository at this point in the history
  • Loading branch information
daithihearn committed May 21, 2023
1 parent 6768fa2 commit 552671a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/caches/MyCardsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ export const myCardsSlice = createSlice({
const idx = state.cards.findIndex(c => c.name === action.payload)
if (idx > 0) state.cards[idx] = { ...BLANK_CARD, selected: false }
},
selectCard: (state, action: PayloadAction<SelectableCard>) => {
state.cards.forEach(c => {
if (c.name === action.payload.name) c.selected = true
})
},
toggleSelect: (state, action: PayloadAction<SelectableCard>) =>
state.cards.forEach(c => {
if (c.name === action.payload.name) c.selected = !c.selected
Expand Down Expand Up @@ -61,6 +66,7 @@ export const {
removeCard,
clearSelectedCards,
selectAll,
selectCard,
toggleSelect,
toggleUniqueSelect,
clearMyCards,
Expand Down
36 changes: 34 additions & 2 deletions src/components/Game/MyCards.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useMemo } from "react"
import React, { useCallback, useEffect, useMemo, useRef } from "react"
import {
DragDropContext,
Draggable,
Expand All @@ -13,6 +13,7 @@ import {
clearSelectedCards,
getMyCards,
replaceMyCards,
selectCard,
toggleSelect,
toggleUniqueSelect,
} from "caches/MyCardsSlice"
Expand All @@ -21,7 +22,8 @@ import {
toggleAutoPlay,
clearAutoPlay,
} from "caches/AutoPlaySlice"
import { CardContent, CardMedia, useTheme } from "@mui/material"
import { Card, CardContent, CardMedia, useTheme } from "@mui/material"
import { CARDS } from "model/Cards"

const EMPTY_HAND = [
{ ...BLANK_CARD, selected: false },
Expand All @@ -31,6 +33,14 @@ const EMPTY_HAND = [
{ ...BLANK_CARD, selected: false },
]

const usePrevious = (value: any) => {
const ref = useRef()
useEffect(() => {
ref.current = value
})
return ref.current
}

const MyCards: React.FC = () => {
const theme = useTheme()
const dispatch = useAppDispatch()
Expand All @@ -39,6 +49,28 @@ const MyCards: React.FC = () => {
const myCards = useAppSelector(getMyCards)
const autoPlayCard = useAppSelector(getAutoPlayCard)
const iamGoer = useAppSelector(getIamGoer)
const prevRoundStatus = usePrevious(round?.status)

useEffect(() => {
if (
round?.status === RoundStatus.BUYING &&
prevRoundStatus !== RoundStatus.BUYING &&
!iamGoer &&
round.suit
) {
// Auto select all cards of a specific suit when the round status is BUYING

const cardsOfSuit = myCards.filter(
card =>
card.suit === round.suit ||
card.name === "JOKER" ||
card.name === "ACE_HEARTS",
)
cardsOfSuit.forEach(card => {
dispatch(selectCard(card))
})
}
}, [round, myCards, prevRoundStatus, iamGoer])

const cardsSelectable = useMemo(
() =>
Expand Down

0 comments on commit 552671a

Please sign in to comment.