Skip to content

Commit

Permalink
Merge pull request #176 from daithihearn/bugfix-buying-cards-when-goer
Browse files Browse the repository at this point in the history
Fixing bug where it is possible to lose cards when buying as the goer
  • Loading branch information
daithihearn authored Jul 6, 2023
2 parents 115eb0a + ef83b4d commit 3c9b4ee
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 47 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "frontend",
"version": "7.2.3",
"version": "7.2.4",
"description": "React frontend for the Cards 110",
"author": "Daithi Hearn",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion public/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"short_name": "Cards 110",
"name": "Cards 110",
"version": "7.2.3",
"version": "7.2.4",
"icons": [
{
"src": "./assets/favicon.png",
Expand Down
34 changes: 25 additions & 9 deletions src/components/Game/Actions/Buying.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { useCallback, useEffect, useState } from "react"

import GameService from "services/GameService"
import { useAppDispatch, useAppSelector } from "caches/hooks"
import { useSnackbar } from "notistack"
import {
getMyCardsWithoutBlanks,
getSelectedCards,
Expand All @@ -19,7 +18,6 @@ import {
import { pickBestCards, riskOfMistakeBuyingCards } from "utils/GameUtils"
import ThrowCardsWarningModal from "./ThrowCardsWarningModal"
import { SelectableCard } from "model/Cards"
import parseError from "utils/ErrorUtils"
import { Button } from "@mui/material"
import { getSettings } from "caches/SettingsSlice"

Expand All @@ -31,8 +29,8 @@ const WaitingForRoundToStart = () => (

const Buying = () => {
const dispatch = useAppDispatch()
const { enqueueSnackbar } = useSnackbar()
const settings = useAppSelector(getSettings)
const numPlayers = useAppSelector(getNumPlayers)
const gameId = useAppSelector(getGameId)
const suit = useAppSelector(getSuit)
const myCards = useAppSelector(getMyCardsWithoutBlanks)
Expand All @@ -52,9 +50,7 @@ const Buying = () => {
const buyCards = useCallback(
(sel: SelectableCard[]) => {
if (!gameId) return
dispatch(GameService.buyCards(gameId, sel)).catch((e: Error) =>
enqueueSnackbar(parseError(e), { variant: "error" }),
)
dispatch(GameService.buyCards(gameId, sel)).catch(console.error)
},
[gameId],
)
Expand All @@ -72,15 +68,35 @@ const Buying = () => {
}, [iamGoer])

useEffect(() => {
// 1. If it is not my go then do nothing
if (!isMyGo || !suit) return
if (readyToBuy) {
// 2. If I am the goer and it is my go then keep all cards
else if (iamGoer) {
buyCards(myCards)
}
// 3. If I am not the goer and it is my go and I have enabled auto buy cards then keep best cards
else if (!iamGoer && settings.autoBuyCards) {
buyCards(pickBestCards(myCards, suit, numPlayers))
}
// 4. If I am not the goer and it is my go and I am ready to buy then keep selected cards
else if (!iamGoer && readyToBuy) {
if (riskOfMistakeBuyingCards(suit, selectedCards, myCards)) {
updateDeleteCardsDialog(true)
} else buyCards(selectedCards)
}
}, [iamGoer, suit, selectedCards, myCards, isMyGo, readyToBuy])
}, [
settings,
iamGoer,
suit,
selectedCards,
myCards,
isMyGo,
numPlayers,
readyToBuy,
])

if (iHavePlayed || settings.autoBuyCards) return <WaitingForRoundToStart />
if (iHavePlayed || settings.autoBuyCards || iamGoer)
return <WaitingForRoundToStart />
return (
<>
<Button type="button" onClick={toggleReadyToBuy} color="primary">
Expand Down
38 changes: 2 additions & 36 deletions src/components/Game/AutoActionManager.tsx
Original file line number Diff line number Diff line change
@@ -1,38 +1,27 @@
import { useCallback, useEffect } from "react"
import { useEffect } from "react"
import GameService from "services/GameService"

import { useAppDispatch, useAppSelector } from "caches/hooks"
import {
getCards,
getGameId,
getIamGoer,
getIsInBunker,
getIsMyGo,
getNumPlayers,
getRound,
getSuit,
} from "caches/GameSlice"
import { RoundStatus } from "model/Round"
import { getAutoPlayCard } from "caches/AutoPlaySlice"
import { bestCardLead, getWorstCard, pickBestCards } from "utils/GameUtils"
import { bestCardLead, getWorstCard } from "utils/GameUtils"
import { useSnackbar } from "notistack"
import parseError from "utils/ErrorUtils"
import { getSettings } from "caches/SettingsSlice"
import { SelectableCard } from "model/Cards"
import { getMyCardsWithoutBlanks } from "caches/MyCardsSlice"

const AutoActionManager = () => {
const dispatch = useAppDispatch()
const { enqueueSnackbar } = useSnackbar()

const settings = useAppSelector(getSettings)
const numPlayers = useAppSelector(getNumPlayers)
const suit = useAppSelector(getSuit)
const myCards = useAppSelector(getMyCardsWithoutBlanks)
const gameId = useAppSelector(getGameId)
const round = useAppSelector(getRound)
const cards = useAppSelector(getCards)
const iamGoer = useAppSelector(getIamGoer)

const autoPlayCard = useAppSelector(getAutoPlayCard)

Expand All @@ -46,16 +35,6 @@ const AutoActionManager = () => {
})
})

const buyCards = useCallback(
(sel: SelectableCard[]) => {
if (!gameId) return
dispatch(GameService.buyCards(gameId, sel)).catch((e: Error) =>
enqueueSnackbar(parseError(e), { variant: "error" }),
)
},
[gameId],
)

const call = (id: string, callAmount: number) =>
dispatch(GameService.call(id, callAmount)).catch(console.error)

Expand All @@ -81,19 +60,6 @@ const AutoActionManager = () => {
}
}, [gameId, round, isMyGo, cards, autoPlayCard])

// Auto buy cards
useEffect(() => {
if (
settings.autoBuyCards &&
suit &&
isMyGo &&
round?.status === RoundStatus.BUYING &&
!iamGoer
) {
buyCards(pickBestCards(myCards, suit, numPlayers))
}
}, [settings, isMyGo, iamGoer, round, myCards, suit, numPlayers])

return null
}

Expand Down

0 comments on commit 3c9b4ee

Please sign in to comment.