Skip to content

Commit 1e2d96b

Browse files
authored
Merge pull request #213 from daithihearn/autoplay-4
Autoplay 4
2 parents 3439a6b + 88c3142 commit 1e2d96b

File tree

7 files changed

+42
-59
lines changed

7 files changed

+42
-59
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "frontend",
3-
"version": "8.0.4",
3+
"version": "8.1.0",
44
"description": "React frontend for the Cards 110",
55
"author": "Daithi Hearn",
66
"license": "MIT",

public/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"short_name": "Cards 110",
33
"name": "Cards 110",
4-
"version": "8.0.4",
4+
"version": "8.1.0",
55
"icons": [
66
{
77
"src": "./assets/favicon.png",

src/caches/PlayCardSlice.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/caches/caches.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ import {
66
} from "@reduxjs/toolkit"
77

88
import { gameSlice } from "./GameSlice"
9-
import { playCardSlice } from "./PlayCardSlice"
109

1110
const combinedReducer = combineReducers({
1211
game: gameSlice.reducer,
13-
playCard: playCardSlice.reducer,
1412
})
1513

1614
export type RootState = ReturnType<typeof combinedReducer>

src/components/Game/MyCards.tsx

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
1-
import React, { useCallback, useMemo } from "react"
1+
import React, { useCallback, useEffect, useMemo, useState } from "react"
22
import {
33
DragDropContext,
44
Draggable,
55
Droppable,
66
DropResult,
77
} from "react-beautiful-dnd"
8-
import { EMPTY, Card } from "model/Cards"
8+
import { EMPTY, Card, CardName } from "model/Cards"
99
import { RoundStatus } from "model/Round"
1010
import { useAppDispatch, useAppSelector } from "caches/hooks"
11-
import {
12-
getCardToPlay,
13-
togglePlayCard,
14-
clearAutoPlay,
15-
} from "caches/PlayCardSlice"
1611
import { CardContent, CardMedia, useTheme } from "@mui/material"
1712
import {
1813
clearSelectedCards,
1914
getCardsFull,
15+
getGameId,
2016
getIamGoer,
17+
getIsMyGo,
2118
getIsRoundCalled,
2219
getRound,
2320
replaceMyCards,
2421
toggleSelect,
2522
toggleUniqueSelect,
2623
} from "caches/GameSlice"
24+
import { useGameActions } from "components/Hooks/useGameActions"
2725

2826
const EMPTY_HAND = [
2927
{ ...EMPTY, selected: false },
@@ -36,12 +34,15 @@ const EMPTY_HAND = [
3634
const MyCards = () => {
3735
const dispatch = useAppDispatch()
3836
const theme = useTheme()
37+
const { playCard } = useGameActions()
3938

39+
const gameId = useAppSelector(getGameId)
4040
const round = useAppSelector(getRound)
4141
const isRoundCalled = useAppSelector(getIsRoundCalled)
4242
const myCards = useAppSelector(getCardsFull)
43-
const autoPlayCard = useAppSelector(getCardToPlay)
43+
const [autoPlayCard, setAutoPlayCard] = useState<CardName>(CardName.EMPTY)
4444
const iamGoer = useAppSelector(getIamGoer)
45+
const isMyGo = useAppSelector(getIsMyGo)
4546

4647
const cardsSelectable = useMemo(
4748
() =>
@@ -59,22 +60,34 @@ const MyCards = () => {
5960
[isRoundCalled, iamGoer],
6061
)
6162

63+
useEffect(() => {
64+
if (
65+
autoPlayCard !== CardName.EMPTY &&
66+
round &&
67+
round.status === RoundStatus.PLAYING &&
68+
isMyGo
69+
) {
70+
playCard({ gameId: gameId!, card: autoPlayCard })
71+
setAutoPlayCard(CardName.EMPTY)
72+
}
73+
}, [autoPlayCard, round, gameId, isMyGo])
74+
6275
const handleSelectCard = useCallback(
6376
(card: Card, event: React.MouseEvent<HTMLImageElement, MouseEvent>) => {
6477
if (!cardsSelectable || card.name === EMPTY.name) {
6578
return
6679
}
6780

6881
// If the round status is PLAYING then only allow one card to be selected
69-
if (round && round.status === RoundStatus.PLAYING) {
82+
if (gameId && round && round.status === RoundStatus.PLAYING) {
7083
if (autoPlayCard === card.name) {
71-
dispatch(clearAutoPlay())
84+
setAutoPlayCard(CardName.EMPTY)
7285
dispatch(clearSelectedCards())
7386
} else if (event.detail === 2) {
74-
dispatch(togglePlayCard(card))
87+
setAutoPlayCard(card.name)
7588
} else {
7689
dispatch(toggleUniqueSelect(card))
77-
dispatch(clearAutoPlay())
90+
setAutoPlayCard(CardName.EMPTY)
7891
}
7992
} else {
8093
dispatch(toggleSelect(card))

src/components/Hooks/useGameActions.tsx

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ export const useGameActions = () => {
3434
)
3535
return response.data
3636
},
37-
onSuccess: data => {
37+
onSuccess: (data: GameStateResponse) => {
3838
queryClient.setQueryData(["gameState"], data)
3939
dispatch(updateGame(data))
4040
},
41-
onError: e => enqueueSnackbar(parseError(e), { variant: "error" }),
41+
onError: (e: any) =>
42+
enqueueSnackbar(parseError(e), { variant: "error" }),
4243
})
4344

4445
const buyCards = useMutation({
@@ -59,11 +60,12 @@ export const useGameActions = () => {
5960
)
6061
return response.data
6162
},
62-
onSuccess: data => {
63+
onSuccess: (data: GameStateResponse) => {
6364
queryClient.setQueryData(["gameState"], data)
6465
dispatch(updateGame(data))
6566
},
66-
onError: e => enqueueSnackbar(parseError(e), { variant: "error" }),
67+
onError: (e: any) =>
68+
enqueueSnackbar(parseError(e), { variant: "error" }),
6769
})
6870

6971
const selectSuit = useMutation({
@@ -89,11 +91,12 @@ export const useGameActions = () => {
8991
)
9092
return response.data
9193
},
92-
onSuccess: data => {
94+
onSuccess: (data: GameStateResponse) => {
9395
queryClient.setQueryData(["gameState"], data)
9496
dispatch(updateGame(data))
9597
},
96-
onError: e => enqueueSnackbar(parseError(e), { variant: "error" }),
98+
onError: (e: any) =>
99+
enqueueSnackbar(parseError(e), { variant: "error" }),
97100
})
98101

99102
const playCard = useMutation({
@@ -114,11 +117,12 @@ export const useGameActions = () => {
114117
)
115118
return response.data
116119
},
117-
onSuccess: data => {
120+
onSuccess: (data: GameStateResponse) => {
118121
queryClient.setQueryData(["gameState"], data)
119122
dispatch(updateGame(data))
120123
},
121-
onError: e => enqueueSnackbar(parseError(e), { variant: "error" }),
124+
onError: (e: any) =>
125+
enqueueSnackbar(parseError(e), { variant: "error" }),
122126
})
123127

124128
const deleteGame = useMutation({
@@ -134,7 +138,8 @@ export const useGameActions = () => {
134138
queryClient.invalidateQueries({
135139
queryKey: ["myGames", "gameState"],
136140
}),
137-
onError: e => enqueueSnackbar(parseError(e), { variant: "error" }),
141+
onError: (e: any) =>
142+
enqueueSnackbar(parseError(e), { variant: "error" }),
138143
})
139144

140145
return {

src/components/Hooks/useGameState.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const useGameState = (gameId?: string) => {
3232
}
3333
return res.data
3434
},
35-
// Refetch the data every 2 seconds
35+
// Refetch the data every 5 seconds
3636
refetchInterval: 5_000,
3737
enabled: !!accessToken && !!gameId,
3838
})

0 commit comments

Comments
 (0)