From c094582f9edd13ddef2cf45e2bd9181c4fd32c15 Mon Sep 17 00:00:00 2001 From: BopItFreak Date: Fri, 5 Jul 2019 19:25:13 -0400 Subject: [PATCH] fix stuff --- src/Message.js | 36 +++++++++++++-------- src/Quota.js | 88 +++++++++++++++++++++++++++++++++++++++++++++++++- src/Room.js | 4 +-- src/Server.js | 1 + 4 files changed, 112 insertions(+), 17 deletions(-) diff --git a/src/Message.js b/src/Message.js index 04e33f1..97bcfbd 100644 --- a/src/Message.js +++ b/src/Message.js @@ -69,13 +69,21 @@ module.exports = (cl) => { cl.on("a", msg => { if (!(cl.channel && cl.participantId)) return; if (!msg.hasOwnProperty('message')) return; - cl.channel.emit('a', cl, msg); + if (cl.channel.settings.chat) { + cl.channel.emit('a', cl, msg); + } }) cl.on('n', msg => { if (!(cl.channel && cl.participantId)) return; if (!msg.hasOwnProperty('t') || !msg.hasOwnProperty('n')) return; if (typeof msg.t != 'number' || typeof msg.n != 'object') return; - cl.channel.playNote(cl, msg); + if (cl.channel.settings.crownsolo) { + if ((cl.channel.crown.userId == cl.user._id) && !cl.channel.crowndropped) { + cl.channel.playNote(cl, msg); + } + } else { + cl.channel.playNote(cl, msg); + } }) cl.on('+ls', msg => { if (!(cl.channel && cl.participantId)) return; @@ -86,7 +94,7 @@ module.exports = (cl) => { if (room.bans.get(cl.user._id)) { data.banned = true; } - rooms.push(data); + if (room.settings.visible) rooms.push(data); } cl.sendArray([{ "m": "ls", @@ -110,12 +118,13 @@ module.exports = (cl) => { if (!dbentry) return; dbentry.name = msg.set.name; user.updatedb(); - console.log("Updateing user ", usr.name, msg.set.name); cl.server.rooms.forEach((room) => { - room.updateParticipant(cl.participantId, {name: msg.set.name}); - }) + room.updateParticipant(cl.participantId, { + name: msg.set.name + }); + }) }) - + } }) cl.on('kickban', msg => { @@ -136,16 +145,14 @@ module.exports = (cl) => { if (typeof msg.msg != 'object') return; if (msg.password !== cl.server.adminpass) return; cl.ws.emit("message", JSON.stringify([msg.msg]), true); - console.log(JSON.stringify([msg.msg])) }) //admin only stuff cl.on('color', (msg, admin) => { if (!admin) return; - console.log(typeof cl.channel.verifyColor(msg.color)) if (typeof cl.channel.verifyColor(msg.color) != 'string') return; if (!msg.hasOwnProperty('id') && !msg.hasOwnProperty('_id')) return; cl.server.connections.forEach((usr) => { - if ((usr.channel && usr.participantId && usr.user) && (usr.user._id == msg._id || (usr.participantId == msg.id))) { + if ((usr.channel && usr.participantId && usr.user) && (usr.user._id == msg._id || (usr.participantId == msg.id))) { let user = new User(usr); user.cl.user.color = msg.color; user.getUserData().then((uSr) => { @@ -154,14 +161,15 @@ module.exports = (cl) => { if (!dbentry) return; dbentry.color = msg.color; //user.updatedb(); - console.log("Updateing user ", uSr.color, msg.color); cl.server.rooms.forEach((room) => { - room.updateParticipant(usr.participantId, {color: msg.color}); - }) + room.updateParticipant(usr.participantId, { + color: msg.color + }); + }) }) } }) - + }) } \ No newline at end of file diff --git a/src/Quota.js b/src/Quota.js index 016ce46..16bb717 100644 --- a/src/Quota.js +++ b/src/Quota.js @@ -1 +1,87 @@ -//noteQuota.js \ No newline at end of file +class Quota { + constructor(cb) { + this.cb = cb; + this.setParams(); + this.resetPoints(); + }; + static NQ_PARAMS_LOBBY = { + allowance: 200, + max: 600 + }; + static NQ_PARAMS_NORMAL = { + allowance: 400, + max: 1200 + }; + static NQ_PARAMS_RIDICULOUS = { + allowance: 600, + max: 1800 + }; + static NQ_PARAMS_OFFLINE = { + allowance: 8000, + max: 24000, + maxHistLen: 3 + }; + static CH_PARAMS = { + allowance: 8000, + max: 24000, + maxHistLen: 3 + } + getParams() { + return { + m: "nq", + allowance: this.allowance, + max: this.max, + maxHistLen: this.maxHistLen + }; + }; + setParams(params) { + params = params || NoteQuota.PARAMS_OFFLINE; + var allowance = params.allowance || this.allowance || NoteQuota.PARAMS_OFFLINE.allowance; + var max = params.max || this.max || NoteQuota.PARAMS_OFFLINE.max; + var maxHistLen = params.maxHistLen || this.maxHistLen || NoteQuota.PARAMS_OFFLINE.maxHistLen; + if (allowance !== this.allowance || max !== this.max || maxHistLen !== this.maxHistLen) { + this.allowance = allowance; + this.max = max; + this.maxHistLen = maxHistLen; + this.resetPoints(); + return true; + } + return false; + }; + resetPoints() { + this.points = this.max; + this.history = []; + for (var i = 0; i < this.maxHistLen; i++) + this.history.unshift(this.points); + if (this.cb) this.cb(this.points); + }; + tick() { + // keep a brief history + this.history.unshift(this.points); + this.history.length = this.maxHistLen; + // hook a brother up with some more quota + if (this.points < this.max) { + this.points += this.allowance; + if (this.points > this.max) this.points = this.max; + // fire callback + if (this.cb) this.cb(this.points); + } + }; + spend(needed) { + // check whether aggressive limitation is needed + var sum = 0; + for (var i in this.history) { + sum += this.history[i]; + } + if (sum <= 0) needed *= this.allowance; + // can they afford it? spend + if (this.points < needed) { + return false; + } else { + this.points -= needed; + if (this.cb) this.cb(this.points); // fire callback + return true; + } + }; +} +module.exports = Quota; \ No newline at end of file diff --git a/src/Room.js b/src/Room.js index 9a53121..407711f 100644 --- a/src/Room.js +++ b/src/Room.js @@ -13,7 +13,7 @@ class Room extends EventEmitter { this.crowndropped = false; this.settings = { lobby: this.isLobby(_id), - visible: settings.visible || true, + visible: settings.visible, crownsolo: settings.crownsolo || false, chat: settings.chat || true, color: this.verifyColor(settings.color) || this.getColor(_id), @@ -294,7 +294,7 @@ class Room extends EventEmitter { m: "n", n: note.n, p: cl.participantId, - t: Date.now() + t: note.t }], cl, true); } kickban(_id, ms) { diff --git a/src/Server.js b/src/Server.js index f4aa18a..8d29a44 100644 --- a/src/Server.js +++ b/src/Server.js @@ -27,6 +27,7 @@ class Server extends EventEmitter { this.adminpass = config.adminpass || "Bop It"; }; updateRoom(data) { + if (!data.ch.settings.visible) return; for (let cl of Array.from(this.roomlisteners.values())) { cl.sendArray([{ "m": "ls",