Kopie van https://gitlab.com/studieverenigingvia/ict/centurion met een paar aanpassingen
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
centurion/backend/src/state.js

60 lines
1.2 KiB

const Lobby = require("./lobby.js");
const User = require("./user.js");
const {getRandomInt} = require("./util.js");
class State {
/**
* @type {Object.<string, Lobby>}
*/
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();