aded Tarokka deck
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import Cards from './Cards'
|
||||
import Deck from './TarokkaDeck'
|
||||
|
||||
import { GameState, GameUpdate } from '../types'
|
||||
|
||||
const deck = new Cards();
|
||||
const deck = new Deck();
|
||||
|
||||
export default class GameStore {
|
||||
private games: Map<string, GameState>;
|
||||
@@ -44,11 +44,11 @@ export default class GameStore {
|
||||
return game;
|
||||
}
|
||||
|
||||
flipCard(gameID: string, cardID: string): GameUpdate {
|
||||
flipCard(gameID: string, cardIndex: number): GameUpdate {
|
||||
const game = this.getGame(gameID);
|
||||
const card = game.cards.find(c => c.id === cardID);
|
||||
const card = game.cards[cardIndex];
|
||||
|
||||
if (!card) throw new Error(`Card ${cardID} not found`);
|
||||
if (!card) throw new Error(`Card ${cardIndex} not found`);
|
||||
|
||||
card.flipped = !card.flipped;
|
||||
game.lastUpdated = Date.now();
|
||||
@@ -66,7 +66,7 @@ export default class GameStore {
|
||||
|
||||
gameUpdate(game: GameState): GameUpdate {
|
||||
const { id, cards } = game;
|
||||
return { id, cards };
|
||||
return { id, cards: cards.map(card => card.flipped ? card : deck.getBack()) };
|
||||
}
|
||||
|
||||
deleteGame(gameID: string): void {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import getRandomItems from '../tools/getRandomItems';
|
||||
import cards from '../const/cards';
|
||||
import type { CardImage } from '@/types';
|
||||
import cards from '../constants/standardCards';
|
||||
import type { StandardCard } from '../types';
|
||||
|
||||
export interface Options {
|
||||
back: number;
|
||||
@@ -20,10 +20,9 @@ const DEFAULT_OPTIONS = {
|
||||
export default class Cards {
|
||||
private options: Options;
|
||||
|
||||
private deck: CardImage[] = [];
|
||||
private backs: CardImage[] = [];
|
||||
private jokers: CardImage[] = [];
|
||||
|
||||
private deck: StandardCard[] = [];
|
||||
private backs: StandardCard[] = [];
|
||||
private jokers: StandardCard[] = [];
|
||||
|
||||
constructor(options: OptionProps = {}) {
|
||||
this.options = { ...DEFAULT_OPTIONS, ...options };
|
||||
@@ -33,16 +32,16 @@ export default class Cards {
|
||||
this.jokers = cards.filter(card => card.joker);
|
||||
}
|
||||
|
||||
select(count: number): CardImage[] {
|
||||
select(count: number): StandardCard[] {
|
||||
return getRandomItems(this.deck, count);
|
||||
}
|
||||
|
||||
getBack(style: number): CardImage {
|
||||
getBack(style: number): StandardCard {
|
||||
style = style || this.options.back;
|
||||
return this.backs.find(card => card.id.startsWith(String(style))) || this.backs[0];
|
||||
}
|
||||
|
||||
getJokers(): CardImage[] {
|
||||
getJokers(): StandardCard[] {
|
||||
return this.jokers;
|
||||
}
|
||||
}
|
||||
21
lib/TarokkaDeck.ts
Normal file
21
lib/TarokkaDeck.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import getRandomItems from '../tools/getRandomItems';
|
||||
import cards from '../constants/tarokkaCards';
|
||||
import type { TarokkaCard } from '../types';
|
||||
|
||||
export default class TarokkaDeck {
|
||||
private deck: TarokkaCard[] = [];
|
||||
private backs: TarokkaCard[] = [];
|
||||
|
||||
constructor() {
|
||||
this.deck = cards.filter(card => !card.back);
|
||||
this.backs = cards.filter(card => card.back);
|
||||
}
|
||||
|
||||
select(count: number): TarokkaCard[] {
|
||||
return getRandomItems(this.deck, count);
|
||||
}
|
||||
|
||||
getBack(): TarokkaCard {
|
||||
return this.backs[0];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user