MultiplayerMinesweeper/dist/roomManager.js

61 lines
1.9 KiB
JavaScript

import { Game } from "./game.js";
import * as util from "./util.js";
class RoomManager {
constructor() {
this.games = [];
}
getGameByID(id) {
const game = this.games.filter(obj => obj.id == id)[0];
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);
}
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, playerColor, playerID) {
// See if the client is already in a game
if (this.getAllPlayerIDs().includes(playerID)) {
}
if (!this.getAllGameIDs().includes(roomCode)) {
}
const game = this.getGameByID(roomCode);
const player = game.addPlayer(playerID, playerName, playerColor);
// See if player name is taken already
// if (player === false) {
// this.removeGameByID(game.id)
// }
return [game, player];
}
playerCreateGame(options, playerName, playerColor, playerID) {
if (this.getAllPlayerIDs().includes(playerID)) {
// return new IllegalAction(playerID, 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();
//# sourceMappingURL=roomManager.js.map