Merge pull request #6 from Charsy89/master

Use newer isLobby, Fix lobby creation bug
This commit is contained in:
BopItFreak 2019-11-20 18:46:59 -05:00 committed by GitHub
commit 51c175691b
3 changed files with 54 additions and 38 deletions

View File

@ -5,6 +5,16 @@ global.fs = require('fs');
global.createKeccakHash = require('keccak'); global.createKeccakHash = require('keccak');
const AsyncConsole = require('asyncconsole') const AsyncConsole = require('asyncconsole')
global.isString = function(a){
return typeof a === 'string';
}
global.isBool = function(a){
return typeof a === 'boolean';
}
global.isObj = function(a){
return typeof a === "object" && !Array.isArray(a) && a !== null;
}
let Server = require("./src/Server.js"); let Server = require("./src/Server.js");
let config = require('./src/db/config.json'); let config = require('./src/db/config.json');
global.SERVER = new Server(config); global.SERVER = new Server(config);

View File

@ -55,15 +55,8 @@ module.exports = (cl) => {
cl.on("chset", msg => { cl.on("chset", msg => {
if (!(cl.channel && cl.participantId)) return; if (!(cl.channel && cl.participantId)) return;
if (!(cl.user._id == cl.channel.crown.userId)) return; if (!(cl.user._id == cl.channel.crown.userId)) return;
if (!msg.hasOwnProperty("set") || !msg.set) msg.set = {}; if (!msg.hasOwnProperty("set") || !msg.set) msg.set = cl.channel.verifySet(cl.channel._id,{});
let settings = {}; cl.channel.settings = msg.set;
settings.lobby = cl.channel.isLobby(cl.channel._id);
settings.visible = !!msg.set.visible;
settings.crownsolo = !!msg.set.crownsolo;
settings.chat = !!msg.set.chat;
settings.color = cl.channel.verifyColor(msg.set.color) || cl.channel.settings.color;
settings.color2 = cl.channel.verifyColor(msg.set.color2) || cl.channel.settings.color2;
cl.channel.settings = settings;
cl.channel.updateCh(); cl.channel.updateCh();
}) })
cl.on("a", msg => { cl.on("a", msg => {

View File

@ -11,14 +11,7 @@ class Room extends EventEmitter {
this.server = server; this.server = server;
this.crown = null; this.crown = null;
this.crowndropped = false; this.crowndropped = false;
this.settings = { this.settings = this.verifySet(this._id,{set:settings});
lobby: this.isLobby(_id),
visible: settings.hasOwnProperty('visible') ? settings.visible : true,
crownsolo: settings.crownsolo || false,
chat: settings.chat || true,
color: this.verifyColor(settings.color) || this.getColor(_id),
color2: this.verifyColor(settings.color) || this.getColor2(_id)
}
this.chatmsgs = []; this.chatmsgs = [];
this.ppl = new Map(); this.ppl = new Map();
this.connections = []; this.connections = [];
@ -109,7 +102,6 @@ class Room extends EventEmitter {
options.name ? this.ppl.get(pid).user.name = options.name : {}; options.name ? this.ppl.get(pid).user.name = options.name : {};
options._id ? this.ppl.get(pid).user._id = options._id : {}; options._id ? this.ppl.get(pid).user._id = options._id : {};
options.color ? this.ppl.get(pid).user.color = options.color : {}; options.color ? this.ppl.get(pid).user.color = options.color : {};
options.name ? this.ppl.get(pid).user.name = options.name : {};
this.connections.filter((ofo) => ofo.participantId == p.participantId).forEach((usr) => { this.connections.filter((ofo) => ofo.participantId == p.participantId).forEach((usr) => {
options.name ? usr.user.name = options.name : {}; options.name ? usr.user.name = options.name : {};
options._id ? usr.user._id = options._id : {}; options._id ? usr.user._id = options._id : {};
@ -185,30 +177,21 @@ class Room extends EventEmitter {
return false; return false;
} }
} }
getColor(_id) {
if (this.isLobby(_id)) {
return this.server.defaultLobbyColor;
} else {
return this.server.defaultRoomColor;
}
}
getColor2(_id) {
if (this.isLobby(_id)) {
return this.server.defaultLobbyColor2;
} else {
return;
delete this.settings.color2;
}
}
isLobby(_id) { isLobby(_id) {
if (_id.startsWith("lobby")) { if (_id.startsWith("lobby")) {
let lobbynum = _id.split("lobby")[1];
if (_id == "lobby") { if (_id == "lobby") {
return true; return true;
} else if (parseFloat(_id.split("lobby")[1] % 1) === 0) { }
return true; if (!(parseInt(lobbynum).toString() == lobbynum)) return false;
for (let i in lobbynum) {
if (parseInt(lobbynum[i]) >= 0) {
if (parseInt(i) + 1 == lobbynum.length) return true;
} else { } else {
return false; return false;
} }
}
} else if (_id.startsWith("test/")) { } else if (_id.startsWith("test/")) {
if (_id == "test/") { if (_id == "test/") {
return false; return false;
@ -402,6 +385,36 @@ class Room extends EventEmitter {
this.chat(participant, msg); this.chat(participant, msg);
}) })
} }
verifySet(_id,msg){
if(!isObj(msg.set)) msg.set = {visible:true,color:this.server.defaultRoomColor,chat:true,crownsolo:false};
if(isBool(msg.set.lobby)){
if(!this.isLobby(_id)) delete msg.set.lobby; // keep it nice and clean
}else{
if(this.isLobby(_id)) msg.set = {visible:true,color:this.server.defaultLobbyColor,color2:this.server.defaultLobbyColor2,chat:true,crownsolo:false,lobby:true};
}
if(!isBool(msg.set.visible)){
if(msg.set.visible == undefined) msg.set.visible = (!isObj(this.settings) ? true : this.settings.visible);
else msg.set.visible = true;
};
if(!isBool(msg.set.chat)){
if(msg.set.chat == undefined) msg.set.chat = (!isObj(this.settings) ? true : this.settings.chat);
else msg.set.chat = true;
};
if(!isBool(msg.set.crownsolo)){
if(msg.set.crownsolo == undefined) msg.set.crownsolo = (!isObj(this.settings) ? false : this.settings.crownsolo);
else msg.set.crownsolo = false;
};
if(!isString(msg.set.color) || !/^#[0-9a-f]{6}$/i.test(msg.set.color)) msg.set.color = (!isObj(this.settings) ? this.server.defaultRoomColor : this.settings.color);
if(isString(msg.set.color2)){
if(!/^#[0-9a-f]{6}$/i.test(msg.set.color2)){
if(this.settings){
if(this.settings.color2) msg.set.color2 = this.settings.color2;
else delete msg.set.color2; // keep it nice and clean
}
}
};
return msg.set;
}
} }
module.exports = Room; module.exports = Room;