pokemon-showdown/dev-tools/users-utils.js
Juanma Serrano c4ac8d6e2f Use strict mode and let and const instead of var
This commit also fixes some duplicated variable declarations.
2015-11-06 21:56:52 -05:00

36 lines
997 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[workerid + '-' + socketid]) {
socketid++;
}
}
let connectionid = workerid + '-' + socketid;
let connection = Users.connections[connectionid] = new Users.Connection(connectionid, Sockets.workers[workerid], socketid, null, ip || '127.0.0.1');
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;