From ae93af21439eb2c18f388ea2c533155f7010f4e5 Mon Sep 17 00:00:00 2001 From: Charsy89 Date: Tue, 19 Nov 2019 22:08:47 -0700 Subject: [PATCH] Fix lobby creation bug --- index.js | 10 ++++++++++ src/Message.js | 11 ++--------- src/Room.js | 52 ++++++++++++++++++++++++++++---------------------- 3 files changed, 41 insertions(+), 32 deletions(-) diff --git a/index.js b/index.js index 18e9de6..62db0b2 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,16 @@ global.fs = require('fs'); global.createKeccakHash = require('keccak'); 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 config = require('./src/db/config.json'); global.SERVER = new Server(config); diff --git a/src/Message.js b/src/Message.js index 97bcfbd..b8b79d9 100644 --- a/src/Message.js +++ b/src/Message.js @@ -55,15 +55,8 @@ module.exports = (cl) => { cl.on("chset", msg => { if (!(cl.channel && cl.participantId)) return; if (!(cl.user._id == cl.channel.crown.userId)) return; - if (!msg.hasOwnProperty("set") || !msg.set) msg.set = {}; - let settings = {}; - 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; + if (!msg.hasOwnProperty("set") || !msg.set) msg.set = cl.channel.verifySet(cl.channel._id,{}); + cl.channel.settings = msg.set; cl.channel.updateCh(); }) cl.on("a", msg => { diff --git a/src/Room.js b/src/Room.js index efb0475..a3019f1 100644 --- a/src/Room.js +++ b/src/Room.js @@ -11,14 +11,7 @@ class Room extends EventEmitter { this.server = server; this.crown = null; this.crowndropped = false; - this.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.settings = this.verifySet(this._id,settings); this.chatmsgs = []; this.ppl = new Map(); this.connections = []; @@ -184,21 +177,6 @@ class Room extends EventEmitter { } else{ 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) { if (_id.startsWith("lobby")) { @@ -408,6 +386,34 @@ class Room extends EventEmitter { 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 = (room.settings.visible || true); + else msg.set.visible = true; + }; + if(!isBool(msg.set.chat)){ + if(msg.set.chat == undefined) msg.set.chat = (room.settings.chat || true); + else msg.set.chat = true; + }; + if(!isBool(msg.set.crownsolo)){ + if(msg.set.crownsolo == undefined) msg.set.crownsolo = (room.settings.crownsolo || false); + else msg.set.crownsolo = false; + }; + if(!isString(msg.set.color) || !/^#[0-9a-f]{6}$/i.test(msg.set.color)) msg.set.color = (room.settings.color || this.server.defaultRoomColor); + if(isString(msg.set.color2)){ + if(!/^#[0-9a-f]{6}$/i.test(msg.set.color2)){ + if(room.settings.color2) msg.set.color2 = room.settings.color2; + else delete msg.set.color2; // keep it nice and clean + } + }; + return msg.set; + } } module.exports = Room;