Skip to content

Commit

Permalink
card flash fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
naueramant committed Jul 28, 2024
1 parent 71f659f commit a586337
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 17 deletions.
19 changes: 10 additions & 9 deletions src/components/CardFlash/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -34,14 +35,9 @@ export const CardFlashProvider: FunctionComponent<CardFlashProviderProps> = ({
const [card, setCard] = useState<Card>();
const [_, setTimeoutRef] = useState<ReturnType<typeof setInterval>>();

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(() => {
Expand All @@ -52,11 +48,16 @@ export const CardFlashProvider: FunctionComponent<CardFlashProviderProps> = ({
setShow(true);
};

const hide = () => {
setShow(false);
};

return (
<CardFlashContext.Provider
value={{
show,
flashCard,
flash,
hide,
}}
>
{card && <CardFlashDialog open={show} card={card} />}
Expand Down
9 changes: 5 additions & 4 deletions src/stores/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ interface GameActions {
StartChug: () => number;
StopChug: () => number;

DrawCard: () => Card;
DrawCard: () => [Card, number];

Exit: (options?: { dnf: boolean; description?: string }) => void;

Expand Down Expand Up @@ -218,7 +218,8 @@ const useGame = create<GameState & GameActions>()(
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!");
}
Expand Down Expand Up @@ -254,7 +255,7 @@ const useGame = create<GameState & GameActions>()(
set(update);

if (state.offline) {
return card;
return [card, cardsLeft - 1];
}

try {
Expand All @@ -268,7 +269,7 @@ const useGame = create<GameState & GameActions>()(
} catch (error) {
console.error("[Game]", "Failed to update game state", error);
} finally {
return card;
return [card, cardsLeft - 1];
}
},

Expand Down
23 changes: 22 additions & 1 deletion src/views/Game/components/CardInventory.tsx
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -12,6 +13,8 @@ const CardInventory: FunctionComponent<CardInventoryProps> = () => {
DrawCard: state.DrawCard,
}));

const cardFlasher = useCardFlash();

const gameMetrics = useGameMetrics();

const cardsLeftOfValue = (value: number) => {
Expand All @@ -21,6 +24,24 @@ const CardInventory: FunctionComponent<CardInventoryProps> = () => {
);
};

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 (
<Stack
direction="row"
Expand All @@ -37,7 +58,7 @@ const CardInventory: FunctionComponent<CardInventoryProps> = () => {
key={i}
kind={valueToSymbol(i + 2)}
value={empty ? 0 : cardsLeftOfValue(i + 2)}
onClick={game.DrawCard}
onClick={drawCard}
/>
);
})}
Expand Down
18 changes: 15 additions & 3 deletions src/views/Game/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import GameTable from "./components/Table";
const GameView: FunctionComponent = () => {
const [showTerminal, setShowTerminal] = useState<boolean>(false);

const flashCard = useCardFlash();
const cardFlasher = useCardFlash();

const game = useGame((state) => ({
DrawCard: state.DrawCard,
Expand Down Expand Up @@ -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 (
Expand Down

0 comments on commit a586337

Please sign in to comment.