const Lobby = require("./Lobby.js"); const {getRandomInt} = require("./util.js"); class State { /** * @type {Object.} */ lobbies = {}; constructor() { } /** * @returns {Lobby} */ createRandomLobby() { let lobby = undefined; while (!lobby) { const lobbyCount = Object.keys(this.lobbies).length; const id = getRandomInt(100, Math.max(1000, lobbyCount * 2)); lobby = this.createLobby(id); } return lobby; } /** * * @param lobbyId * @returns {Lobby|undefined} */ getLobby(lobbyId) { if (!lobbyId || !this.lobbies.hasOwnProperty(lobbyId)) { return undefined; } return this.lobbies[lobbyId]; } /** * Returns undefined when the lobby already exists. * @param {number} lobbyId * @returns {Lobby|undefined} */ createLobby(lobbyId) { if (this.lobbies.hasOwnProperty(lobbyId)) { return undefined; } return new Lobby(lobbyId); } removeLobby(lobbyId) { delete this.lobbies[lobbyId]; } } module.exports = new State();