From a0de8c2e022963d1933d0df4d95365af8188149e Mon Sep 17 00:00:00 2001 From: Hri7566 Date: Sat, 1 Apr 2023 01:05:12 -0400 Subject: [PATCH] stuff --- mpp.hri7566.info | 2 +- src/Channel.js | 180 +++++++++++++------------------------ src/Client.js | 66 +++++++++++--- src/Database.js | 25 +++++- src/InternalBot/Command.js | 43 ++++++++- src/MOTDGenerator.js | 4 +- src/Message.js | 33 +++++-- src/Notification.js | 100 ++++++++++++++------- src/Rank.js | 6 -- src/Server.js | 31 +++++-- src/User.js | 1 + src/scripts/fishing.js | 2 +- 12 files changed, 303 insertions(+), 190 deletions(-) delete mode 100644 src/Rank.js diff --git a/mpp.hri7566.info b/mpp.hri7566.info index 327c677..1bcce0f 160000 --- a/mpp.hri7566.info +++ b/mpp.hri7566.info @@ -1 +1 @@ -Subproject commit 327c6775aceb4518437bf3f2c262c6e6f7423579 +Subproject commit 1bcce0f2b59d1d5407c510d738438a3d78ce199a diff --git a/src/Channel.js b/src/Channel.js index fb6f1c6..5f4dcb2 100644 --- a/src/Channel.js +++ b/src/Channel.js @@ -160,8 +160,10 @@ class Channel extends EventEmitter { this.settings = new RoomSettings(this.server.lobbySettings); } - this.settings.color = col.toHexa(); - this.settings.color2 = col.toHexa(); + this.settings.changeSettings({ + color: col.toHexa(), + color2: col.toHexa() + }); for (let key in this.settings) { this.server.lobbySettings[key] = this.settings[key]; @@ -200,7 +202,10 @@ class Channel extends EventEmitter { // user owns the room // we need to switch the crown to them //cl.quotas.a.setParams(Quota.PARAMS_A_CROWNED); - this.crown = new Crown(cl.participantId, cl.user._id); + this.emit("add crown", { + participantId: cl.participantId, + userId: cl.user._id + }); this.crowndropped = false; // this.settings = new RoomSettings(set, 'user'); @@ -281,39 +286,6 @@ class Channel extends EventEmitter { ]); this.updateCh(cl, this.settings); } - - if (this.flags.spin == true) { - this.spin(cl); - } - } - - spin(cl) { - // speeeeeeen - let id = cl.user._id; - if (!id) id = "room"; - this.Notification( - id, - "", - ``, - ``, - 1, - "#names", - "short" - ); - } - - stopSpin(cl) { - let id = cl.user._id; - if (!id) id = "room"; - this.Notification( - id, - "", - ``, - ``, - 1, - "#names", - "short" - ); } remove(p) { @@ -436,7 +408,7 @@ class Channel extends EventEmitter { .get(usr.connectionid) .sendArray(arr); } catch (e) { - console.log(e); + this.logger.error(e); } } }); @@ -582,9 +554,6 @@ class Channel extends EventEmitter { message.t = Date.now(); message.a = msg.message; - console.log("here:", p, msg); - console.log(message.length); - if (message.a.length > 0 && message.a.length <= 512) { message.p = { color: "#ffffff", @@ -726,40 +695,58 @@ class Channel extends EventEmitter { u.setChannel(Channel.banChannel, {}); if (asd) - this.Notification( - user.user._id, - "Notice", - `Banned from \"${this._id}\" for ${Math.floor( + new Notification(this.server, { + id: "", + title: "Notice", + text: `Banned from "${this._id}" for ${Math.floor( Math.floor(ms / 1000) / 60 )} minutes.`, - "", - 7000, - "#room", - "short" - ); + duration: 7000, + target: "#room", + class: "short", + targetUser: user.participantId, + targetChannel: "all", + cl: user + }).send(); + new Notification(this.server, { + id: "", + title: "Notice", + text: `Banned from "${this._id}" for ${Math.floor( + Math.floor(ms / 1000) / 60 + )} minutes.`, + duration: 7000, + target: "#room", + class: "short", + targetUser: user.participantId, + targetChannel: "all", + cl: user + }).send(); if (asd) - this.Notification( - "room", - "Notice", - `${pthatbanned.user.name} banned ${ + new Notification(this.server, { + id: "", + class: "short", + target: "#room", + title: "Notice", + text: `${pthatbanned.user.name} banned ${ user.user.name } from the channel for ${Math.floor( Math.floor(ms / 1000) / 60 )} minutes.`, - "", - 7000, - "#room", - "short" - ); + duration: 7000, + targetChannel: "room", + cl: pthatbanned + }).send(); if (this.crown && this.crown.userId == _id) { - this.Notification( - "room", - "Certificate of Award", - `Let it be known that ${user.user.name} kickbanned him/her self.`, - "", - 7000, - "#room" - ); + new Notification(this.server, { + id: "", + class: "short", + target: "#room", + title: "Certificate of Award", + text: `Let it be known that ${user.user.name} kickbanned him/her self.`, + targetChannel: "room", + duration: 7000, + cl: pthatbanned + }).send(); } //} }); @@ -779,20 +766,6 @@ class Channel extends EventEmitter { this.bans.delete(ban.user._id); } - Notification(who, title, text, html, duration, target, klass, id) { - new Notification({ - id: id, - chat: undefined, - refresh: undefined, - title: title, - text: text, - html: html, - duration: duration, - target: target, - class: klass - }).send(who, this); - } - bindEventListeners() { this.on("bye", participant => { this.remove(participant); @@ -818,24 +791,15 @@ class Channel extends EventEmitter { this.emit("update"); }); - this.on("flag spin", spin => { - if (spin) { - for (let cl of this.connections) { - this.spin(cl); - } - } else { - for (let cl of this.connections) { - this.stopSpin(cl); - } - } + this.on("add crown", msg => { + this.crown = new Crown(msg.participantId, msg.userId); + this.emit("update"); }); - this.on("flag among us", amongus => { - if (amongus) { - this.startAmongUs(); - } else { - this.stopAmongUs(); - } + this.on("kickban", msg => { + if (!msg._id) return; + if (!msg.ms) msg.ms = 30 * 60 * 1000; + this.kickban(msg._id, msg.ms); }); } @@ -853,7 +817,7 @@ class Channel extends EventEmitter { if (typeof msg.set.lobby !== "undefined") { if (msg.set.lobby == true) { - if (!this.isLobby(_id)) delete msg.set.lobby; // keep it nice and clean + if (!this.isLobby(_id)) delete msg.set.lobby; } else { if (this.isLobby(_id)) { msg.set = this.server.lobbySettings; @@ -870,28 +834,6 @@ class Channel extends EventEmitter { if (!val) return this.flags.hasOwnProperty(flag); return this.flags.hasOwnProperty(flag) && this.flags[flag] == val; } - - async startAmongUs() { - if (!this.amongus) { - this.amongus = {}; - } - - if (this.amongus.started) return; - - if (!this.amongus.started) { - this.amongus.started = true; - } - - let imposter = - this.connections[ - Math.floor(Math.random() * this.connections.length) - ]; - imposter.user.setFlag("freeze_name", true); - } - - stopAmongUs() { - this.amongus.started = false; - } } module.exports = Channel; diff --git a/src/Client.js b/src/Client.js index b134575..7d83e27 100644 --- a/src/Client.js +++ b/src/Client.js @@ -78,21 +78,23 @@ class Client extends EventEmitter { } if (userbanned) { - ch.Notification( - this.user._id, - "Notice", - `Currently banned from \"${_id}\" for ${Math.ceil( + new Notification(this.server, { + targetUser: this.participantId, + targetChannel: all, + title: "Notice", + text: `Currently banned from "${_id}" for ${Math.ceil( Math.floor( (userbanned.msbanned - (Date.now() - userbanned.bannedtime)) / 1000 ) / 60 )} minutes.`, - 7000, - "", - "#room", - "short" - ); + duration: 7000, + id: "", + target: "#room", + class: "short", + cl: this + }).send(); this.setChannel(Channel.banChannel, settings); return; } @@ -199,9 +201,22 @@ class Client extends EventEmitter { */ destroy() { if (this.user) { - this.user.stopFlagEvents(); + let lastClient = true; + + for (const cl of this.server.connections.values()) { + if (cl.user) { + if (cl.user._id == this.user._id) { + lastClient = false; + break; + } + } + } + + if (lastClient) this.user.stopFlagEvents(); } + this.ws.close(); + if (this.channel) { this.channel.emit("bye", this); } @@ -235,11 +250,13 @@ class Client extends EventEmitter { // this.destroy(); } }); + this.ws.on("close", () => { if (!this.destroied) { this.destroy(); } }); + this.ws.addEventListener("error", err => { console.error(err); if (!this.destroied) { @@ -258,26 +275,36 @@ class Client extends EventEmitter { let channels = []; this.server.channels.forEach(ch => { let ppl = []; - for (let p of ch.fetchChannelData().ppl) { + const chdata = ch.fetchChannelData(); + + for (let p of chdata.ppl) { ppl.push({ user: p }); } + channels.push({ + chat: ch.chatmsgs.slice(-1 * 32), participants: ppl }); }); let users = []; + let clients = []; this.server.connections.forEach(cl => { + let c = { + ip: cl.ip, + participantId: cl.participantId + }; + clients.push(c); if (!cl.user) return; let u = { p: { _id: cl.user._id, name: cl.user.name, color: cl.user.color, - flags: cl.user.flags, - inventory: cl.user.inventory + flags: cl.user.flags + // inventory: cl.user.inventory }, id: cl.participantId }; @@ -292,10 +319,21 @@ class Client extends EventEmitter { }; data.clientManager = { - users + users, + clients }; data.uptime = Date.now() - this.server.startTime; + data.config = this.server.config; + if (this.user) { + data.p = { + _id: this.user._id, + name: this.user.name, + color: this.user.color, + flags: this.user.flags + // inventory: this.user.inventory + }; + } this.sendArray([data]); } diff --git a/src/Database.js b/src/Database.js index de6a9ba..f29b710 100644 --- a/src/Database.js +++ b/src/Database.js @@ -34,10 +34,13 @@ fs.mkdirSync("db/", { class Database { static userdb; static roomdb; + static bandb; static async load() { this.userdb = mongoose.connection; this.roomdb = level("db/rooms.db"); + this.bandb = level("db/ban.db"); + // const writeFile = promisify(fs.writeFile); // const readdir = promisify(fs.readdir); @@ -61,8 +64,6 @@ class Database { .substr(0, 24); let user = await UserModel.findById(_id).exec(); - console.log("_id:", _id); - console.log("user:", user); if (user == null) { user = await this.createUser(_id); @@ -156,6 +157,26 @@ class Database { static deleteRoomSettings(_id) { this.roomdb.del("room~" + _id); } + + static addIPBan(ip) { + this.bandb.put("ipban~" + ip, true); + } + + static removeIPBan(ip) { + this.bandb.del("ipban~" + ip); + } + + static isIPBanned(ip, cb) { + this.roomdb.get("ipban~" + ip, (err, value) => { + if (err) { + return false; + } + + console.log("ban:", value); + + if (value == true) return true; + }); + } } class RoomDataModel { diff --git a/src/InternalBot/Command.js b/src/InternalBot/Command.js index 206f4a4..954d675 100644 --- a/src/InternalBot/Command.js +++ b/src/InternalBot/Command.js @@ -1,6 +1,7 @@ const Logger = require("../Logger"); const Color = require("../Color"); const { Cow } = require("../Cow"); +const Database = require("../Database"); class Command { static commands = []; @@ -40,7 +41,7 @@ class Command { isAdmin, a: args.join(" ") }); - console.log(out); + // console.log(out); if (!out) return; if (out !== "") { ch.adminChat(out); @@ -190,6 +191,46 @@ Command.addCommand( ) ); +Command.addCommand( + new Command( + "inventory", + ["inventory", "inv"], + undefined, + `%Pinventory`, + 0, + (cl, ch, msg) => { + if (cl.user.inventory) { + const items = Object.values(cl.user.inventory) + .map( + it => + `${it.emoji ? it.emoji : ""}${it.display_name} (x${ + it.count + })` + ) + .join(", ") + .trim(); + + ch.adminChat(`Inventory: ${items == "" ? "(none)" : items}`); + } + }, + "user" + ) +); + +Command.addCommand( + new Command( + "js", + ["js"], + undefined, + `%Pjs`, + 0, + (cl, ch, msg) => { + return cl.server.ev(msg.argcat); + }, + "admin" + ) +); + module.exports = { Command }; diff --git a/src/MOTDGenerator.js b/src/MOTDGenerator.js index 3f1c2fd..363d35b 100644 --- a/src/MOTDGenerator.js +++ b/src/MOTDGenerator.js @@ -11,7 +11,9 @@ let dayOfTheWeekMOTD = [ "Don't forget to bring a towel!", "Never lick a pole in winter.", "Everyone loves a potato monkey!", - "Dear Mario: Please come to the castle. I've baked a cake for you. Yours truly-- Princess Toadstool" + "Dear Mario: Please come to the castle. I've baked a cake for you. Yours truly-- Princess Toadstool", + "The MotD generator is broken. Come back tomorrow.", + "There's an earthbound battle background script in my frontend files." ]; class MOTDGenerator { diff --git a/src/Message.js b/src/Message.js index bf6aaad..dcf77ef 100644 --- a/src/Message.js +++ b/src/Message.js @@ -4,6 +4,7 @@ const Channel = require("./Channel.js"); const RoomSettings = require("./RoomSettings"); const Database = require("./Database"); const { MOTDGenerator } = require("./MOTDGenerator"); +const Notification = require("./Notification"); module.exports = cl => { cl.once("hi", (msg, admin) => { @@ -13,8 +14,6 @@ module.exports = cl => { } } - console.log("hi"); - let m = {}; m.m = "hi"; m.motd = MOTDGenerator.getCurrentMOTD(); @@ -343,6 +342,7 @@ module.exports = cl => { let title; let text; let html; + let chat = msg.chat; if (msg.hasOwnProperty("class")) { klass = msg.class; @@ -357,16 +357,19 @@ module.exports = cl => { html = msg.html; } - cl.channel.Notification( - targetUser || targetChannel, + new Notification(cl.server, { + cl, + id, + targetChannel, + targetUser, + target, + class: klass, title, text, html, - duration, - target, - klass, - id - ); + chat, + duration + }).send(); }); cl.on("user_flag", (msg, admin) => { @@ -498,4 +501,16 @@ module.exports = cl => { if (!admin) return; cl.server.restart(msg.notification); }); + + cl.on("ipban", (msg, admin) => { + if (!admin) return; + if (!msg.ip) return; + cl.server.banIP(msg.ip); + }); + + cl.on("ipunban", (msg, admin) => { + if (!admin) return; + if (!msg.ip) return; + cl.server.unbanIP(msg.ip); + }); }; diff --git a/src/Notification.js b/src/Notification.js index 7c8df0e..b1eb032 100644 --- a/src/Notification.js +++ b/src/Notification.js @@ -1,45 +1,85 @@ +const Server = require("./Server"); + module.exports = class Notification { - constructor(data) { + constructor(server, data) { + this.server = server; + this.cl = data.cl; + this.id = data.id; - this.chat = data.chat; - this.refresh = data.refresh; this.title = data.title; this.text = data.text; this.html = data.html; this.target = data.target; this.duration = data.duration; this.class = data.class; - this.id = data.id; + this.targetChannel = data.targetChannel; + this.targetUser = data.targetUser; + + this.chat = data.chat; } - send(_id, room) { - let msg = {}; - Object.assign(msg, this); - msg.m = "notification"; + send() { + let msg = { + m: "notification", + id: this.id, + title: this.title, + text: this.text, + html: this.html, + target: this.target, + duration: this.duration, + class: this.class + }; - switch (_id) { - case "all": - for (let con of Array.from(room.server.connections.values())) { - con.sendArray([msg]); - } - break; - case "room": - case "channel": - for (let con of room.connections) { - con.sendArray([msg]); - } - break; - default: - Array.from(room.server.connections.values()) - .filter(usr => - typeof usr.user !== "undefined" - ? usr.user._id == _id - : null + // Object.assign(msg, this); + const targets = []; + + if (this.targetChannel) { + switch (this.targetChannel) { + case "all": + // every channel + for (const cl of this.server.connections.values()) { + targets.push(cl); + } + break; + case "room": + case "channel": + // current channel + if (!this.cl) break; + if (!this.cl.channel) break; + for (const cl of this.server.connections.values()) { + if (!cl.channel) continue; + if (cl.channel._id == this.cl.channel._id) { + targets.push(cl); + } + } + break; + default: + // specific channel + for (const cl of this.server.connections.values()) { + if (!cl.channel) continue; + if (cl.channel._id == this.targetChannel) { + targets.push(cl); + } + } + break; + } + } + + if (!this.chat) { + for (const cl of targets) { + if (this.targetUser) { + if (!cl.user) continue; + if ( + cl.user._id == this.targetUser || + cl.participantId == this.targetUser ) - .forEach(p => { - p.sendArray([msg]); - }); - break; + cl.sendArray([msg]); + } else { + cl.sendArray([msg]); + } + } + } else { + this.cl.sendChat(this.text); } } }; diff --git a/src/Rank.js b/src/Rank.js deleted file mode 100644 index 41b781e..0000000 --- a/src/Rank.js +++ /dev/null @@ -1,6 +0,0 @@ -module.exports = class Rank { - constructor (name, _id) { - this.name = name; - this._id = _id; - } -} diff --git a/src/Server.js b/src/Server.js index 887f24a..3a0c7a3 100644 --- a/src/Server.js +++ b/src/Server.js @@ -6,6 +6,7 @@ const fs = require("fs"); const RoomSettings = require("./RoomSettings"); const Logger = require("./Logger.js"); const Notification = require("./Notification"); +const Database = require("./Database.js"); class Server { static on = EventEmitter.prototype.on; @@ -80,7 +81,7 @@ class Server { this.specialIntervals = {}; this.wss.on("connection", (ws, req) => { - console.log("socket connected"); + // console.log("socket connected"); this.connections.set( ++this.connectionid, new Client(ws, req, this) @@ -117,7 +118,9 @@ class Server { "channel message", "channel_flag", "name", - "restart" + "restart", + "ipban", + "ipunban" ]; // this.welcome_motd = config.motd || "You agree to read this message."; @@ -164,7 +167,8 @@ class Server { } catch (err) { out = err; } - console.log(out); + // console.log(out); + return `(${typeof out}) ${out}`; } static getClient(id) { @@ -194,16 +198,31 @@ class Server { text: "The server will restart in a few moments.", target: "#piano", duration: 20000, - class: "classic" + class: "classic", + targetChannel: "all" } ) { - let n = new Notification(notif); - n.send("all", this.channels.get("lobby")); + let n = new Notification(this, notif); + n.send(); setTimeout(() => { process.exit(); }, n.duration || 20000); } + + static banIP(ip) { + Database.addIPBan(ip); + + for (const cl of this.connections.values()) { + if (cl.ip == ip) { + cl.destroy(); + } + } + } + + static unbanIP(ip) { + Database.removeIPBan(ip); + } } module.exports = Server; diff --git a/src/User.js b/src/User.js index ad1fb22..8f7303f 100644 --- a/src/User.js +++ b/src/User.js @@ -44,6 +44,7 @@ class User { if (typeof this.cl.server.specialIntervals[this._id] == "undefined") { this.cl.server.specialIntervals[this._id] = {}; } + if (this.hasFlag("rainbow", true)) { if ( !this.cl.server.specialIntervals[this._id].hasOwnProperty( diff --git a/src/scripts/fishing.js b/src/scripts/fishing.js index ded8188..26a5c1f 100644 --- a/src/scripts/fishing.js +++ b/src/scripts/fishing.js @@ -114,7 +114,7 @@ var fish_without_images = [ "Tadpole", "Whale Shark" ]; -// var newfish = require("./newfish.json"); +var newfish = require("./fish"); var pokedex = []; process.stdout.write(