import {Game} from "./game.mjs" import * as log from "./log.mjs" import * as util from "./util.mjs" import {IllegalAction} from "./server/illegalAction.mjs" class RoomManager { constructor(){ this.games = []; } getGameByID(id) { const game = this.games.filter(obj => obj.id == id)[0] if (game === undefined) { return false } return game } addGame(id, options) { const game = new Game(id, options); this.games.push(game); return game } removeGameByID(id) { this.games = this.games.filter(obj => obj.id != id); log.log("removed game - " + id) } getAllPlayerIDs() { let ids = [] for (let i = 0; i < this.games.length; i++) { for (let j = 0; j < this.games[i].players.length; j++) { ids.push(this.games[i].players[j].id) } } return ids } getAllGameIDs() { let ids = [] for (let i = 0; i < this.games.length; i++) { ids.push(this.games[i].id) } return ids } playerJoinGame(roomCode, playerName, playerID) { // See if the client is already in a game console.log(roomCode, playerName) if (this.getAllPlayerIDs().includes(playerID)) { return new IllegalAction(id, 22) } if (!this.getAllGameIDs().includes(roomCode)) { return new IllegalAction(id, 30) } const game = this.getGameByID(roomCode); const player = game.addPlayer(playerID, playerName); // See if player name is taken already if (player === false) { this.removeGame(game) return new IllegalAction(id, 20) } return [game, player] } playerCreateGame(options, playerName, playerColor, playerID) { if (this.getAllPlayerIDs().includes(playerID)) { return new IllegalAction(id, 22) } console.log(options) const id = util.createCode() const game = this.addGame(id, options) const player = game.addPlayer(playerID, playerName, playerColor); return [game, player] } } export var roomManager = new RoomManager();