29 lines
854 B
TypeScript
29 lines
854 B
TypeScript
import { Injectable, Logger } from '@nestjs/common';
|
|
import { Game } from './game';
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
import { Player } from 'src/players/player';
|
|
import { GameNotFoundException } from 'src/exceptions';
|
|
|
|
@Injectable()
|
|
export class GameService {
|
|
private readonly logger = new Logger(GameService.name);
|
|
private readonly games: Map<string, Game> = new Map();
|
|
|
|
createLobby(player: Player): Game {
|
|
const gameId = uuidv4();
|
|
const gameCode = uuidv4().slice(-5).toUpperCase();
|
|
const game: Game = new Game(gameId, gameCode, player);
|
|
this.games.set(gameCode, game);
|
|
return game;
|
|
}
|
|
|
|
joinGame(player: Player, gameCode: string): Game {
|
|
const game = this.games.get(gameCode);
|
|
if (game === undefined) {
|
|
throw new GameNotFoundException(gameCode);
|
|
}
|
|
game.players.push(player);
|
|
return game;
|
|
}
|
|
}
|