馃悰 Fix hard crash when encountering users using reserved JS keywords

parent d23d4ccb
......@@ -88,9 +88,9 @@ class BanchoClient extends EventEmitter {
this.connectCallback = null;
this.joinCallbacks = [];
this.partCallbacks = [];
this.users = {};
this.usersById = {};
this.channels = {};
this.users = new Map();
this.usersById = new Map();
this.channels = new Map();
// Register our own error listener so there are no uncaught exceptions
// for exceptions WE throw for the end-user that don't need to be caught (such as timeouts)
......@@ -202,10 +202,9 @@ class BanchoClient extends EventEmitter {
*/
onClose(err) {
// Every currently joined channel should be considered left.
for(const channelName in this.channels)
if(this.channels.hasOwnProperty(channelName))
if(this.channels[channelName].joined)
IrcCommands.PART.emit(this, this.getSelf(), this.channels[channelName]);
for(const channel of this.channels)
if(channel[1].joined)
IrcCommands.PART.emit(this, this.getSelf(), channel[1]);
if(this.connectState == ConnectStates.Disconnected)
return;
......@@ -256,10 +255,13 @@ class BanchoClient extends EventEmitter {
*/
getUser(username) {
username = username.replace(/ /g, "_").split("\n")[0].substring(0, 28);
if(this.users[username.toLowerCase()])
return this.users[username.toLowerCase()];
else
return (this.users[username.toLowerCase()] = new BanchoUser(this, username));
if(this.users.has(username.toLowerCase()))
return this.users.get(username.toLowerCase());
else {
const user = new BanchoUser(this, username);
this.users.set(username.toLowerCase(), user);
return user;
}
}
/**
......@@ -280,8 +282,8 @@ class BanchoClient extends EventEmitter {
return new Promise((resolve, reject) => {
if(isNaN(userid))
reject(new Error("id needs to be a number!"));
if(this.usersById[userid])
resolve(this.usersById[userid]);
if(this.usersById.has(userid))
resolve(this.usersById.get(userid));
else
this.osuApi.user.get(userid, Nodesu.LookupType.id).then((apiUser) => {
const user = this.getUser(apiUser.username);
......@@ -300,12 +302,13 @@ class BanchoClient extends EventEmitter {
getChannel(channelName) {
if(channelName.indexOf("#") != 0 || channelName.indexOf(",") != -1 || channelName.indexOf("") != -1)
throw new Error("Invalid channel name!");
if(this.channels[channelName])
return this.channels[channelName];
else if(channelName.indexOf("#mp_") == 0 && this.osuApi != null)
return (this.channels[channelName] = new BanchoMultiplayerChannel(this, channelName));
else
return (this.channels[channelName] = new BanchoChannel(this, channelName));
if(this.channels.has(channelName))
return this.channels.get(channelName);
else {
const channel = (channelName.indexOf("#mp_") == 0 && this.osuApi != null) ? new BanchoMultiplayerChannel(this, channelName) : new BanchoChannel(this, channelName);
this.channels.set(channelName, channel);
return channel;
}
}
/**
......
......@@ -225,8 +225,8 @@ class BanchoUser extends EventEmitter {
set id(newId) {
if(isNaN(newId))
throw new Error("id needs to be a number!");
if(this._id != null)
this.banchojs.usersById[this._id] = null;
if(this._id != null && this.banchojs.usersById.has(this._id))
this.banchojs.usersById.delete(this._id);
this._id = newId;
this.banchojs.usersById[this._id] = this;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment