prettier
This commit is contained in:
103
lib/GameStore.ts
103
lib/GameStore.ts
@@ -1,75 +1,80 @@
|
||||
import Deck from './TarokkaDeck'
|
||||
import Deck from './TarokkaDeck';
|
||||
|
||||
import { GameState, GameUpdate, TarokkaGameCard } from '../types'
|
||||
import { GameState, GameUpdate, TarokkaGameCard } from '../types';
|
||||
|
||||
const deck = new Deck();
|
||||
|
||||
export default class GameStore {
|
||||
private games: Map<string, GameState>;
|
||||
private games: Map<string, GameState>;
|
||||
|
||||
constructor() {
|
||||
this.games = new Map();
|
||||
}
|
||||
constructor() {
|
||||
this.games = new Map();
|
||||
}
|
||||
|
||||
createGame(id: string): GameState {
|
||||
if (this.games.has(id)) throw new Error(`Game ${id} already exists`);
|
||||
createGame(id: string): GameState {
|
||||
if (this.games.has(id)) throw new Error(`Game ${id} already exists`);
|
||||
|
||||
const newGame: GameState = {
|
||||
id,
|
||||
players: new Set(),
|
||||
cards: deck.select(5).map(card => ({ ...card, flipped: false })),
|
||||
lastUpdated: Date.now(),
|
||||
};
|
||||
const newGame: GameState = {
|
||||
id,
|
||||
players: new Set(),
|
||||
cards: deck.select(5).map((card) => ({ ...card, flipped: false })),
|
||||
lastUpdated: Date.now(),
|
||||
};
|
||||
|
||||
this.games.set(id, newGame);
|
||||
this.games.set(id, newGame);
|
||||
|
||||
return newGame;
|
||||
}
|
||||
return newGame;
|
||||
}
|
||||
|
||||
joinGame(gameID: string, playerID: string): GameUpdate {
|
||||
const game = this.games.get(gameID) || this.createGame(gameID);
|
||||
joinGame(gameID: string, playerID: string): GameUpdate {
|
||||
const game = this.games.get(gameID) || this.createGame(gameID);
|
||||
|
||||
game.players.add(playerID);
|
||||
game.lastUpdated = Date.now();
|
||||
game.players.add(playerID);
|
||||
game.lastUpdated = Date.now();
|
||||
|
||||
return this.gameUpdate(game);
|
||||
}
|
||||
return this.gameUpdate(game);
|
||||
}
|
||||
|
||||
leaveGame(gameID: string, playerID: string): GameState {
|
||||
const game = this.getGame(gameID);
|
||||
leaveGame(gameID: string, playerID: string): GameState {
|
||||
const game = this.getGame(gameID);
|
||||
|
||||
game.players.delete(playerID);
|
||||
game.lastUpdated = Date.now();
|
||||
game.players.delete(playerID);
|
||||
game.lastUpdated = Date.now();
|
||||
|
||||
return game;
|
||||
}
|
||||
return game;
|
||||
}
|
||||
|
||||
flipCard(gameID: string, cardIndex: number): GameUpdate {
|
||||
const game = this.getGame(gameID);
|
||||
const card = game.cards[cardIndex];
|
||||
flipCard(gameID: string, cardIndex: number): GameUpdate {
|
||||
const game = this.getGame(gameID);
|
||||
const card = game.cards[cardIndex];
|
||||
|
||||
if (!card) throw new Error(`Card ${cardIndex} not found`);
|
||||
if (!card) throw new Error(`Card ${cardIndex} not found`);
|
||||
|
||||
card.flipped = !card.flipped;
|
||||
game.lastUpdated = Date.now();
|
||||
card.flipped = !card.flipped;
|
||||
game.lastUpdated = Date.now();
|
||||
|
||||
return this.gameUpdate(game);
|
||||
}
|
||||
return this.gameUpdate(game);
|
||||
}
|
||||
|
||||
getGame(gameID: string): GameState {
|
||||
const game = this.games.get(gameID);
|
||||
getGame(gameID: string): GameState {
|
||||
const game = this.games.get(gameID);
|
||||
|
||||
if (!game) throw new Error(`Game ${gameID} not found`);
|
||||
if (!game) throw new Error(`Game ${gameID} not found`);
|
||||
|
||||
return game;
|
||||
}
|
||||
return game;
|
||||
}
|
||||
|
||||
gameUpdate(game: GameState): GameUpdate {
|
||||
const { id, cards } = game;
|
||||
return { id, cards: (cards as TarokkaGameCard[]).map((card: TarokkaGameCard) => card.flipped ? card : { ...deck.getBack(), flipped: false }) };
|
||||
}
|
||||
gameUpdate(game: GameState): GameUpdate {
|
||||
const { id, cards } = game;
|
||||
return {
|
||||
id,
|
||||
cards: (cards as TarokkaGameCard[]).map((card: TarokkaGameCard) =>
|
||||
card.flipped ? card : { ...deck.getBack(), flipped: false },
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
deleteGame(gameID: string): void {
|
||||
this.games.delete(gameID);
|
||||
}
|
||||
deleteGame(gameID: string): void {
|
||||
this.games.delete(gameID);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,45 +3,45 @@ import cards from '../constants/standardCards';
|
||||
import type { StandardCard } from '../types';
|
||||
|
||||
export interface Options {
|
||||
back: number;
|
||||
jokers: boolean;
|
||||
back: number;
|
||||
jokers: boolean;
|
||||
}
|
||||
|
||||
export interface OptionProps {
|
||||
back?: number;
|
||||
jokers?: boolean;
|
||||
back?: number;
|
||||
jokers?: boolean;
|
||||
}
|
||||
|
||||
const DEFAULT_OPTIONS = {
|
||||
jokers: false,
|
||||
back: 1,
|
||||
jokers: false,
|
||||
back: 1,
|
||||
};
|
||||
|
||||
export default class Cards {
|
||||
private options: Options;
|
||||
private options: Options;
|
||||
|
||||
private deck: StandardCard[] = [];
|
||||
private backs: StandardCard[] = [];
|
||||
private jokers: StandardCard[] = [];
|
||||
private deck: StandardCard[] = [];
|
||||
private backs: StandardCard[] = [];
|
||||
private jokers: StandardCard[] = [];
|
||||
|
||||
constructor(options: OptionProps = {}) {
|
||||
this.options = { ...DEFAULT_OPTIONS, ...options };
|
||||
constructor(options: OptionProps = {}) {
|
||||
this.options = { ...DEFAULT_OPTIONS, ...options };
|
||||
|
||||
this.deck = cards.filter(card => !card.back && (this.options.jokers || !card.joker));
|
||||
this.backs = cards.filter(card => card.back);
|
||||
this.jokers = cards.filter(card => card.joker);
|
||||
}
|
||||
this.deck = cards.filter((card) => !card.back && (this.options.jokers || !card.joker));
|
||||
this.backs = cards.filter((card) => card.back);
|
||||
this.jokers = cards.filter((card) => card.joker);
|
||||
}
|
||||
|
||||
select(count: number): StandardCard[] {
|
||||
return getRandomItems(this.deck, count);
|
||||
}
|
||||
select(count: number): StandardCard[] {
|
||||
return getRandomItems(this.deck, count);
|
||||
}
|
||||
|
||||
getBack(style: number): StandardCard {
|
||||
style = style || this.options.back;
|
||||
return this.backs.find(card => card.id.startsWith(String(style))) || this.backs[0];
|
||||
}
|
||||
getBack(style: number): StandardCard {
|
||||
style = style || this.options.back;
|
||||
return this.backs.find((card) => card.id.startsWith(String(style))) || this.backs[0];
|
||||
}
|
||||
|
||||
getJokers(): StandardCard[] {
|
||||
return this.jokers;
|
||||
}
|
||||
getJokers(): StandardCard[] {
|
||||
return this.jokers;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,19 +3,19 @@ import cards from '../constants/tarokkaCards';
|
||||
import type { TarokkaCard } from '../types';
|
||||
|
||||
export default class TarokkaDeck {
|
||||
private deck: TarokkaCard[] = [];
|
||||
private backs: TarokkaCard[] = [];
|
||||
private deck: TarokkaCard[] = [];
|
||||
private backs: TarokkaCard[] = [];
|
||||
|
||||
constructor() {
|
||||
this.deck = cards.filter(card => !card.back);
|
||||
this.backs = cards.filter(card => card.back);
|
||||
}
|
||||
constructor() {
|
||||
this.deck = cards.filter((card) => !card.back);
|
||||
this.backs = cards.filter((card) => card.back);
|
||||
}
|
||||
|
||||
select(count: number): TarokkaCard[] {
|
||||
return getRandomItems(this.deck, count);
|
||||
}
|
||||
select(count: number): TarokkaCard[] {
|
||||
return getRandomItems(this.deck, count);
|
||||
}
|
||||
|
||||
getBack(): TarokkaCard {
|
||||
return this.backs[0];
|
||||
}
|
||||
getBack(): TarokkaCard {
|
||||
return this.backs[0];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user