From 55409766b3376376447f1a486d8d0cd28dd314e2 Mon Sep 17 00:00:00 2001 From: Gavin McDonald Date: Sat, 12 Apr 2025 16:43:58 -0400 Subject: [PATCH] proper card layout --- app/[gameID]/page.tsx | 34 ++++++++++++++++++++++++---------- lib/GameStore.ts | 2 +- lib/TarokkaDeck.ts | 14 +++++++++----- types/index.ts | 4 ++-- 4 files changed, 36 insertions(+), 18 deletions(-) diff --git a/app/[gameID]/page.tsx b/app/[gameID]/page.tsx index e9914cd..f105de1 100644 --- a/app/[gameID]/page.tsx +++ b/app/[gameID]/page.tsx @@ -8,6 +8,19 @@ import Card from '@/components/Card'; import type { GameUpdate, ClientUpdate, StandardGameCard, TarokkaGameCard } from '@/types'; +// map our five cards to their appropriate +// locations in a 3x3 grid +// █1█ +// 042 +// █3█ +const cardMap = { + 3: 0, + 1: 1, + 5: 2, + 7: 3, + 4: 4, +}; + export default function GamePage() { const { gameID: gameIDParam } = useParams(); @@ -52,20 +65,21 @@ export default function GamePage() { socket.emit('flip-card', flip); }; + // map our five Tarokka cards to their proper locations in a 3x3 grid + // common deck cards: left, top, and right + // high deck cards: bottom and center + const arrangeCards = (_cell: any, index: number) => cards[cardMap[index]]; + return cards.length ? (
- {Array.from({ length: 9 }).map((_, i) => { - const cardIndex = [1, 3, 4, 5, 7].indexOf(i); - - return ( -
- {cardIndex !== -1 && ( - flipCard(cardIndex)} /> - )} + {Array.from({ length: 9 }) + .map(arrangeCards) + .map((card, index) => ( +
+ {card && flipCard(cardMap[index])} />}
- ); - })} + ))}
) : null; diff --git a/lib/GameStore.ts b/lib/GameStore.ts index 07e2f53..7357550 100644 --- a/lib/GameStore.ts +++ b/lib/GameStore.ts @@ -17,7 +17,7 @@ export default class GameStore { const newGame: GameState = { id, players: new Set(), - cards: deck.select(5).map((card) => ({ ...card, flipped: false })), + cards: deck.getHand(), lastUpdated: Date.now(), }; diff --git a/lib/TarokkaDeck.ts b/lib/TarokkaDeck.ts index f67cfec..95c76b8 100644 --- a/lib/TarokkaDeck.ts +++ b/lib/TarokkaDeck.ts @@ -1,18 +1,22 @@ import getRandomItems from '../tools/getRandomItems'; import cards from '../constants/tarokkaCards'; -import type { TarokkaCard } from '../types'; +import type { TarokkaCard, TarokkaGameCard } from '../types'; export default class TarokkaDeck { - private deck: TarokkaCard[] = []; + private highDeck: TarokkaCard[] = []; + private commonDeck: TarokkaCard[] = []; private backs: TarokkaCard[] = []; constructor() { - this.deck = cards.filter((card) => !card.back); + this.highDeck = cards.filter((card) => !card.back && card.suit === 'High Deck'); + this.commonDeck = cards.filter((card) => !card.back && card.suit !== 'High Deck'); this.backs = cards.filter((card) => card.back); } - select(count: number): TarokkaCard[] { - return getRandomItems(this.deck, count); + getHand(): TarokkaGameCard[] { + return [...getRandomItems(this.commonDeck, 3), ...getRandomItems(this.highDeck, 2)].map( + (card) => ({ ...card, flipped: false }), + ); } getBack(): TarokkaCard { diff --git a/types/index.ts b/types/index.ts index 38dd744..5f628a8 100644 --- a/types/index.ts +++ b/types/index.ts @@ -30,13 +30,13 @@ export interface TarokkaGameCard extends TarokkaCard { export interface GameState { id: string; players: Set; - cards: StandardGameCard[] | TarokkaGameCard[]; + cards: TarokkaGameCard[]; lastUpdated: number; } export interface GameUpdate { id: string; - cards: StandardGameCard[] | TarokkaGameCard[]; + cards: TarokkaGameCard[]; } export interface ClientUpdate {