From a586337e1a417dfe2b84035b12eaf721b2aed1fd Mon Sep 17 00:00:00 2001 From: Jonas Tranberg Date: Sun, 28 Jul 2024 15:01:03 +0200 Subject: [PATCH] card flash fixes --- src/components/CardFlash/provider.tsx | 19 +++++++++-------- src/stores/game.ts | 9 ++++---- src/views/Game/components/CardInventory.tsx | 23 ++++++++++++++++++++- src/views/Game/index.tsx | 18 +++++++++++++--- 4 files changed, 52 insertions(+), 17 deletions(-) diff --git a/src/components/CardFlash/provider.tsx b/src/components/CardFlash/provider.tsx index 550476a..759826c 100644 --- a/src/components/CardFlash/provider.tsx +++ b/src/components/CardFlash/provider.tsx @@ -10,11 +10,12 @@ import { CardFlashDialog } from "./dialog"; const CardFlashContext = createContext({ show: false, - flashCard: (card: Card, options?: flashCardOptions) => {}, + flash: (card: Card, options?: flashCardOptions) => {}, + hide: () => {}, }); export const useCardFlash = () => { - return useContext(CardFlashContext).flashCard; + return useContext(CardFlashContext); }; export interface CardFlashProviderProps { @@ -34,14 +35,9 @@ export const CardFlashProvider: FunctionComponent = ({ const [card, setCard] = useState(); const [_, setTimeoutRef] = useState>(); - const flashCard = (card: Card, options?: flashCardOptions) => { + const flash = (card: Card, options?: flashCardOptions) => { setCard(card); - if (card.value === 14) { - setShow(false); - return; - } - setTimeoutRef((prev) => { prev && clearTimeout(prev); return setTimeout(() => { @@ -52,11 +48,16 @@ export const CardFlashProvider: FunctionComponent = ({ setShow(true); }; + const hide = () => { + setShow(false); + }; + return ( {card && } diff --git a/src/stores/game.ts b/src/stores/game.ts index c06b1aa..325278b 100644 --- a/src/stores/game.ts +++ b/src/stores/game.ts @@ -50,7 +50,7 @@ interface GameActions { StartChug: () => number; StopChug: () => number; - DrawCard: () => Card; + DrawCard: () => [Card, number]; Exit: (options?: { dnf: boolean; description?: string }) => void; @@ -218,7 +218,8 @@ const useGame = create()( throw new Error("Cannot draw a new card while chugging"); } - const cardsLeft = (CardValues.length - 1) * state.players.length; + const cardsLeft: number = + CardValues.length * state.players.length - state.draws.length; if (cardsLeft <= 0) { throw new Error("Cannot draw from an empty deck!"); } @@ -254,7 +255,7 @@ const useGame = create()( set(update); if (state.offline) { - return card; + return [card, cardsLeft - 1]; } try { @@ -268,7 +269,7 @@ const useGame = create()( } catch (error) { console.error("[Game]", "Failed to update game state", error); } finally { - return card; + return [card, cardsLeft - 1]; } }, diff --git a/src/views/Game/components/CardInventory.tsx b/src/views/Game/components/CardInventory.tsx index 7ec496e..90add16 100644 --- a/src/views/Game/components/CardInventory.tsx +++ b/src/views/Game/components/CardInventory.tsx @@ -1,5 +1,6 @@ import { Box, Card, darken, Stack, Typography } from "@mui/material"; import { FunctionComponent, memo } from "react"; +import { useCardFlash } from "../../../components/CardFlash"; import useGame from "../../../stores/game"; import { useGameMetrics } from "../../../stores/metrics"; @@ -12,6 +13,8 @@ const CardInventory: FunctionComponent = () => { DrawCard: state.DrawCard, })); + const cardFlasher = useCardFlash(); + const gameMetrics = useGameMetrics(); const cardsLeftOfValue = (value: number) => { @@ -21,6 +24,24 @@ const CardInventory: FunctionComponent = () => { ); }; + const drawCard = () => { + const [card, cardsLeft] = game.DrawCard(); + + // If chug card, don't flash it + if (card.value === 14) { + cardFlasher.hide(); + return; + } + + // If last card, don't flash it + if (cardsLeft === 0) { + cardFlasher.hide(); + return; + } + + cardFlasher.flash(card); + }; + return ( = () => { key={i} kind={valueToSymbol(i + 2)} value={empty ? 0 : cardsLeftOfValue(i + 2)} - onClick={game.DrawCard} + onClick={drawCard} /> ); })} diff --git a/src/views/Game/index.tsx b/src/views/Game/index.tsx index 36ba264..d98242f 100644 --- a/src/views/Game/index.tsx +++ b/src/views/Game/index.tsx @@ -19,7 +19,7 @@ import GameTable from "./components/Table"; const GameView: FunctionComponent = () => { const [showTerminal, setShowTerminal] = useState(false); - const flashCard = useCardFlash(); + const cardFlasher = useCardFlash(); const game = useGame((state) => ({ DrawCard: state.DrawCard, @@ -139,9 +139,21 @@ const GameView: FunctionComponent = () => { }; const drawCard = () => { - const card = game.DrawCard(); + const [card, cardsLeft] = game.DrawCard(); - flashCard(card); + // If chug card, don't flash it + if (card.value === 14) { + cardFlasher.hide(); + return; + } + + // If last card, don't flash it + if (cardsLeft === 0) { + cardFlasher.hide(); + return; + } + + cardFlasher.flash(card); }; return (