UNPKG

webxash3d-mserver

Version:
60 lines (59 loc) 1.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MServer = void 0; const connection_1 = require("./connection"); const server_1 = require("./server"); class MServer { constructor() { this.connections = new Map; this.servers = new Map; } onClient() { const connection = new connection_1.Connection(); this.connections.set(connection.id, connection); return connection; } onClientDisconnect(id) { this.unregister(id); this.connections.delete(id); } fetchServers(params = {}) { const { game = 'valve', offset = 0, limit = 10 } = params; const allServers = this.servers.get(game) ?? []; const servers = allServers.slice(offset, offset + limit); return { servers, offset, limit, total: allServers.length, }; } unregister(connectionID) { const connection = this.connections.get(connectionID); if (!connection?.server) return; const game = connection.server.params.game; const servers = this.servers.get(game); if (!servers) return; const newServers = servers.filter(s => s.connectionID !== connectionID); if (newServers.length) { this.servers.set(game, newServers); } else { this.servers.delete(game); } delete connection.server; } register(id, params) { const connection = this.connections.get(id); if (!connection) return; this.unregister(id); connection.server = new server_1.Server(id, params); const servers = this.servers.get(params.game) ?? []; servers.push(connection.server); this.servers.set(params.game, servers); } } exports.MServer = MServer;