mirror of
https://github.com/smogon/pokemon-showdown.git
synced 2026-05-05 21:17:43 -05:00
Specifically, Users.users, Users.connections, and Users.pastUsers are now ES6 Maps. In theory, this should be a minor performance upgrade, but we still need to profile to make sure.
37 lines
1017 B
JavaScript
37 lines
1017 B
JavaScript
'use strict';
|
|
|
|
let EventEmitter = require('events').EventEmitter;
|
|
|
|
function createWorker() {
|
|
let fakeWorker = new EventEmitter();
|
|
fakeWorker.send = function () {};
|
|
Sockets.workers[fakeWorker.id] = fakeWorker;
|
|
return fakeWorker;
|
|
}
|
|
|
|
function createConnection(ip, workerid, socketid) {
|
|
if (!workerid || !socketid) {
|
|
workerid = Object.keys(Sockets.workers)[0];
|
|
if (!workerid) workerid = createWorker().id;
|
|
socketid = 1;
|
|
while (Users.connections.has(workerid + '-' + socketid)) {
|
|
socketid++;
|
|
}
|
|
}
|
|
let connectionid = workerid + '-' + socketid;
|
|
let connection = new Users.Connection(connectionid, Sockets.workers[workerid], socketid, null, ip || '127.0.0.1');
|
|
Users.connections.set(connectionid, connection);
|
|
return connection;
|
|
}
|
|
|
|
function createUser(connection) {
|
|
if (!connection) connection = createConnection();
|
|
let user = new Users.User(connection);
|
|
connection.user = user;
|
|
user.joinRoom('global', connection);
|
|
return user;
|
|
}
|
|
|
|
exports.Connection = createConnection;
|
|
exports.User = createUser;
|