proper card layout

This commit is contained in:
Gavin McDonald
2025-04-12 16:43:58 -04:00
parent 7b9d1a1c2c
commit 55409766b3
4 changed files with 36 additions and 18 deletions

View File

@@ -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 ? (
<main className="min-h-screen flex flex-col items-center justify-center gap-4 bg-[url('/img/table3.png')] bg-cover bg-center">
<div className="grid grid-cols-3 grid-rows-3 gap-8 w-fit mx-auto">
{Array.from({ length: 9 }).map((_, i) => {
const cardIndex = [1, 3, 4, 5, 7].indexOf(i);
return (
<div key={i} className="aspect-[2/3]}">
{cardIndex !== -1 && (
<Card card={cards[cardIndex]} flipAction={() => flipCard(cardIndex)} />
)}
{Array.from({ length: 9 })
.map(arrangeCards)
.map((card, index) => (
<div key={index} className="aspect-[2/3]}">
{card && <Card card={card} flipAction={() => flipCard(cardMap[index])} />}
</div>
);
})}
))}
</div>
</main>
) : null;

View File

@@ -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(),
};

View File

@@ -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 {

View File

@@ -30,13 +30,13 @@ export interface TarokkaGameCard extends TarokkaCard {
export interface GameState {
id: string;
players: Set<string>;
cards: StandardGameCard[] | TarokkaGameCard[];
cards: TarokkaGameCard[];
lastUpdated: number;
}
export interface GameUpdate {
id: string;
cards: StandardGameCard[] | TarokkaGameCard[];
cards: TarokkaGameCard[];
}
export interface ClientUpdate {