typescript on both client and server with communication via socket.io
This commit is contained in:
48
lib/Cards.ts
Normal file
48
lib/Cards.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import getRandomItems from '../tools/getRandomItems';
|
||||
import cards from '../const/cards';
|
||||
import type { CardImage } from '@/types';
|
||||
|
||||
export interface Options {
|
||||
back: number;
|
||||
jokers: boolean;
|
||||
}
|
||||
|
||||
export interface OptionProps {
|
||||
back?: number;
|
||||
jokers?: boolean;
|
||||
}
|
||||
|
||||
const DEFAULT_OPTIONS = {
|
||||
jokers: false,
|
||||
back: 1,
|
||||
};
|
||||
|
||||
export default class Cards {
|
||||
private options: Options;
|
||||
|
||||
private deck: CardImage[] = [];
|
||||
private backs: CardImage[] = [];
|
||||
private jokers: CardImage[] = [];
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
select(count: number): CardImage[] {
|
||||
return getRandomItems(this.deck, count);
|
||||
}
|
||||
|
||||
getBack(style: number): CardImage {
|
||||
style = style || this.options.back;
|
||||
return this.backs.find(card => card.id.startsWith(String(style))) || this.backs[0];
|
||||
}
|
||||
|
||||
getJokers(): CardImage[] {
|
||||
return this.jokers;
|
||||
}
|
||||
}
|
||||
70
lib/GameStore.ts
Normal file
70
lib/GameStore.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import Cards from './Cards'
|
||||
|
||||
import { GameState } from '../types'
|
||||
|
||||
const deck = new Cards();
|
||||
|
||||
export default class GameStore {
|
||||
private games: Map<string, GameState>;
|
||||
|
||||
constructor() {
|
||||
this.games = new Map();
|
||||
}
|
||||
|
||||
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(),
|
||||
};
|
||||
|
||||
this.games.set(id, newGame);
|
||||
|
||||
return newGame;
|
||||
}
|
||||
|
||||
joinGame(gameID: string, playerID: string): GameState {
|
||||
const game = this.games.get(gameID) || this.createGame(gameID);
|
||||
|
||||
game.players.add(playerID);
|
||||
game.lastUpdated = Date.now();
|
||||
|
||||
return game;
|
||||
}
|
||||
|
||||
leaveGame(gameID: string, playerID: string): GameState {
|
||||
const game = this.getGame(gameID);
|
||||
|
||||
game.players.delete(playerID);
|
||||
game.lastUpdated = Date.now();
|
||||
|
||||
return game;
|
||||
}
|
||||
|
||||
flipCard(gameID: string, cardID: string): GameState {
|
||||
const game = this.getGame(gameID);
|
||||
const card = game.cards.find(c => c.id === cardID);
|
||||
|
||||
if (!card) throw new Error(`Card ${cardID} not found`);
|
||||
|
||||
card.flipped = !card.flipped;
|
||||
game.lastUpdated = Date.now();
|
||||
|
||||
return game;
|
||||
}
|
||||
|
||||
getGame(gameID: string): GameState {
|
||||
const game = this.games.get(gameID);
|
||||
|
||||
if (!game) throw new Error(`Game ${gameID} not found`);
|
||||
|
||||
return game;
|
||||
}
|
||||
|
||||
deleteGame(gameID: string): void {
|
||||
this.games.delete(gameID);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user