This commit is contained in:
Hri7566 2023-04-01 01:05:12 -04:00
parent 1ef018fa1a
commit a0de8c2e02
12 changed files with 303 additions and 190 deletions

@ -1 +1 @@
Subproject commit 327c6775aceb4518437bf3f2c262c6e6f7423579 Subproject commit 1bcce0f2b59d1d5407c510d738438a3d78ce199a

View File

@ -160,8 +160,10 @@ class Channel extends EventEmitter {
this.settings = new RoomSettings(this.server.lobbySettings); this.settings = new RoomSettings(this.server.lobbySettings);
} }
this.settings.color = col.toHexa(); this.settings.changeSettings({
this.settings.color2 = col.toHexa(); color: col.toHexa(),
color2: col.toHexa()
});
for (let key in this.settings) { for (let key in this.settings) {
this.server.lobbySettings[key] = this.settings[key]; this.server.lobbySettings[key] = this.settings[key];
@ -200,7 +202,10 @@ class Channel extends EventEmitter {
// user owns the room // user owns the room
// we need to switch the crown to them // we need to switch the crown to them
//cl.quotas.a.setParams(Quota.PARAMS_A_CROWNED); //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.crowndropped = false;
// this.settings = new RoomSettings(set, 'user'); // this.settings = new RoomSettings(set, 'user');
@ -281,39 +286,6 @@ class Channel extends EventEmitter {
]); ]);
this.updateCh(cl, this.settings); 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,
"",
``,
`<script>$("#piano").addClass("spin")</script>`,
1,
"#names",
"short"
);
}
stopSpin(cl) {
let id = cl.user._id;
if (!id) id = "room";
this.Notification(
id,
"",
``,
`<script>$("#piano").removeClass("spin")</script>`,
1,
"#names",
"short"
);
} }
remove(p) { remove(p) {
@ -436,7 +408,7 @@ class Channel extends EventEmitter {
.get(usr.connectionid) .get(usr.connectionid)
.sendArray(arr); .sendArray(arr);
} catch (e) { } catch (e) {
console.log(e); this.logger.error(e);
} }
} }
}); });
@ -582,9 +554,6 @@ class Channel extends EventEmitter {
message.t = Date.now(); message.t = Date.now();
message.a = msg.message; message.a = msg.message;
console.log("here:", p, msg);
console.log(message.length);
if (message.a.length > 0 && message.a.length <= 512) { if (message.a.length > 0 && message.a.length <= 512) {
message.p = { message.p = {
color: "#ffffff", color: "#ffffff",
@ -726,40 +695,58 @@ class Channel extends EventEmitter {
u.setChannel(Channel.banChannel, {}); u.setChannel(Channel.banChannel, {});
if (asd) if (asd)
this.Notification( new Notification(this.server, {
user.user._id, id: "",
"Notice", title: "Notice",
`Banned from \"${this._id}\" for ${Math.floor( text: `Banned from "${this._id}" for ${Math.floor(
Math.floor(ms / 1000) / 60 Math.floor(ms / 1000) / 60
)} minutes.`, )} minutes.`,
"", duration: 7000,
7000, target: "#room",
"#room", class: "short",
"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) if (asd)
this.Notification( new Notification(this.server, {
"room", id: "",
"Notice", class: "short",
`${pthatbanned.user.name} banned ${ target: "#room",
title: "Notice",
text: `${pthatbanned.user.name} banned ${
user.user.name user.user.name
} from the channel for ${Math.floor( } from the channel for ${Math.floor(
Math.floor(ms / 1000) / 60 Math.floor(ms / 1000) / 60
)} minutes.`, )} minutes.`,
"", duration: 7000,
7000, targetChannel: "room",
"#room", cl: pthatbanned
"short" }).send();
);
if (this.crown && this.crown.userId == _id) { if (this.crown && this.crown.userId == _id) {
this.Notification( new Notification(this.server, {
"room", id: "",
"Certificate of Award", class: "short",
`Let it be known that ${user.user.name} kickbanned him/her self.`, target: "#room",
"", title: "Certificate of Award",
7000, text: `Let it be known that ${user.user.name} kickbanned him/her self.`,
"#room" targetChannel: "room",
); duration: 7000,
cl: pthatbanned
}).send();
} }
//} //}
}); });
@ -779,20 +766,6 @@ class Channel extends EventEmitter {
this.bans.delete(ban.user._id); 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() { bindEventListeners() {
this.on("bye", participant => { this.on("bye", participant => {
this.remove(participant); this.remove(participant);
@ -818,24 +791,15 @@ class Channel extends EventEmitter {
this.emit("update"); this.emit("update");
}); });
this.on("flag spin", spin => { this.on("add crown", msg => {
if (spin) { this.crown = new Crown(msg.participantId, msg.userId);
for (let cl of this.connections) { this.emit("update");
this.spin(cl);
}
} else {
for (let cl of this.connections) {
this.stopSpin(cl);
}
}
}); });
this.on("flag among us", amongus => { this.on("kickban", msg => {
if (amongus) { if (!msg._id) return;
this.startAmongUs(); if (!msg.ms) msg.ms = 30 * 60 * 1000;
} else { this.kickban(msg._id, msg.ms);
this.stopAmongUs();
}
}); });
} }
@ -853,7 +817,7 @@ class Channel extends EventEmitter {
if (typeof msg.set.lobby !== "undefined") { if (typeof msg.set.lobby !== "undefined") {
if (msg.set.lobby == true) { 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 { } else {
if (this.isLobby(_id)) { if (this.isLobby(_id)) {
msg.set = this.server.lobbySettings; msg.set = this.server.lobbySettings;
@ -870,28 +834,6 @@ class Channel extends EventEmitter {
if (!val) return this.flags.hasOwnProperty(flag); if (!val) return this.flags.hasOwnProperty(flag);
return this.flags.hasOwnProperty(flag) && this.flags[flag] == val; 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; module.exports = Channel;

View File

@ -78,21 +78,23 @@ class Client extends EventEmitter {
} }
if (userbanned) { if (userbanned) {
ch.Notification( new Notification(this.server, {
this.user._id, targetUser: this.participantId,
"Notice", targetChannel: all,
`Currently banned from \"${_id}\" for ${Math.ceil( title: "Notice",
text: `Currently banned from "${_id}" for ${Math.ceil(
Math.floor( Math.floor(
(userbanned.msbanned - (userbanned.msbanned -
(Date.now() - userbanned.bannedtime)) / (Date.now() - userbanned.bannedtime)) /
1000 1000
) / 60 ) / 60
)} minutes.`, )} minutes.`,
7000, duration: 7000,
"", id: "",
"#room", target: "#room",
"short" class: "short",
); cl: this
}).send();
this.setChannel(Channel.banChannel, settings); this.setChannel(Channel.banChannel, settings);
return; return;
} }
@ -199,9 +201,22 @@ class Client extends EventEmitter {
*/ */
destroy() { destroy() {
if (this.user) { 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(); this.ws.close();
if (this.channel) { if (this.channel) {
this.channel.emit("bye", this); this.channel.emit("bye", this);
} }
@ -235,11 +250,13 @@ class Client extends EventEmitter {
// this.destroy(); // this.destroy();
} }
}); });
this.ws.on("close", () => { this.ws.on("close", () => {
if (!this.destroied) { if (!this.destroied) {
this.destroy(); this.destroy();
} }
}); });
this.ws.addEventListener("error", err => { this.ws.addEventListener("error", err => {
console.error(err); console.error(err);
if (!this.destroied) { if (!this.destroied) {
@ -258,26 +275,36 @@ class Client extends EventEmitter {
let channels = []; let channels = [];
this.server.channels.forEach(ch => { this.server.channels.forEach(ch => {
let ppl = []; let ppl = [];
for (let p of ch.fetchChannelData().ppl) { const chdata = ch.fetchChannelData();
for (let p of chdata.ppl) {
ppl.push({ ppl.push({
user: p user: p
}); });
} }
channels.push({ channels.push({
chat: ch.chatmsgs.slice(-1 * 32),
participants: ppl participants: ppl
}); });
}); });
let users = []; let users = [];
let clients = [];
this.server.connections.forEach(cl => { this.server.connections.forEach(cl => {
let c = {
ip: cl.ip,
participantId: cl.participantId
};
clients.push(c);
if (!cl.user) return; if (!cl.user) return;
let u = { let u = {
p: { p: {
_id: cl.user._id, _id: cl.user._id,
name: cl.user.name, name: cl.user.name,
color: cl.user.color, color: cl.user.color,
flags: cl.user.flags, flags: cl.user.flags
inventory: cl.user.inventory // inventory: cl.user.inventory
}, },
id: cl.participantId id: cl.participantId
}; };
@ -292,10 +319,21 @@ class Client extends EventEmitter {
}; };
data.clientManager = { data.clientManager = {
users users,
clients
}; };
data.uptime = Date.now() - this.server.startTime; 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]); this.sendArray([data]);
} }

View File

@ -34,10 +34,13 @@ fs.mkdirSync("db/", {
class Database { class Database {
static userdb; static userdb;
static roomdb; static roomdb;
static bandb;
static async load() { static async load() {
this.userdb = mongoose.connection; this.userdb = mongoose.connection;
this.roomdb = level("db/rooms.db"); this.roomdb = level("db/rooms.db");
this.bandb = level("db/ban.db");
// const writeFile = promisify(fs.writeFile); // const writeFile = promisify(fs.writeFile);
// const readdir = promisify(fs.readdir); // const readdir = promisify(fs.readdir);
@ -61,8 +64,6 @@ class Database {
.substr(0, 24); .substr(0, 24);
let user = await UserModel.findById(_id).exec(); let user = await UserModel.findById(_id).exec();
console.log("_id:", _id);
console.log("user:", user);
if (user == null) { if (user == null) {
user = await this.createUser(_id); user = await this.createUser(_id);
@ -156,6 +157,26 @@ class Database {
static deleteRoomSettings(_id) { static deleteRoomSettings(_id) {
this.roomdb.del("room~" + _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 { class RoomDataModel {

View File

@ -1,6 +1,7 @@
const Logger = require("../Logger"); const Logger = require("../Logger");
const Color = require("../Color"); const Color = require("../Color");
const { Cow } = require("../Cow"); const { Cow } = require("../Cow");
const Database = require("../Database");
class Command { class Command {
static commands = []; static commands = [];
@ -40,7 +41,7 @@ class Command {
isAdmin, isAdmin,
a: args.join(" ") a: args.join(" ")
}); });
console.log(out); // console.log(out);
if (!out) return; if (!out) return;
if (out !== "") { if (out !== "") {
ch.adminChat(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 = { module.exports = {
Command Command
}; };

View File

@ -11,7 +11,9 @@ let dayOfTheWeekMOTD = [
"Don't forget to bring a towel!", "Don't forget to bring a towel!",
"Never lick a pole in winter.", "Never lick a pole in winter.",
"Everyone loves a potato monkey!", "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 { class MOTDGenerator {

View File

@ -4,6 +4,7 @@ const Channel = require("./Channel.js");
const RoomSettings = require("./RoomSettings"); const RoomSettings = require("./RoomSettings");
const Database = require("./Database"); const Database = require("./Database");
const { MOTDGenerator } = require("./MOTDGenerator"); const { MOTDGenerator } = require("./MOTDGenerator");
const Notification = require("./Notification");
module.exports = cl => { module.exports = cl => {
cl.once("hi", (msg, admin) => { cl.once("hi", (msg, admin) => {
@ -13,8 +14,6 @@ module.exports = cl => {
} }
} }
console.log("hi");
let m = {}; let m = {};
m.m = "hi"; m.m = "hi";
m.motd = MOTDGenerator.getCurrentMOTD(); m.motd = MOTDGenerator.getCurrentMOTD();
@ -343,6 +342,7 @@ module.exports = cl => {
let title; let title;
let text; let text;
let html; let html;
let chat = msg.chat;
if (msg.hasOwnProperty("class")) { if (msg.hasOwnProperty("class")) {
klass = msg.class; klass = msg.class;
@ -357,16 +357,19 @@ module.exports = cl => {
html = msg.html; html = msg.html;
} }
cl.channel.Notification( new Notification(cl.server, {
targetUser || targetChannel, cl,
id,
targetChannel,
targetUser,
target,
class: klass,
title, title,
text, text,
html, html,
duration, chat,
target, duration
klass, }).send();
id
);
}); });
cl.on("user_flag", (msg, admin) => { cl.on("user_flag", (msg, admin) => {
@ -498,4 +501,16 @@ module.exports = cl => {
if (!admin) return; if (!admin) return;
cl.server.restart(msg.notification); 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);
});
}; };

View File

@ -1,45 +1,85 @@
const Server = require("./Server");
module.exports = class Notification { module.exports = class Notification {
constructor(data) { constructor(server, data) {
this.server = server;
this.cl = data.cl;
this.id = data.id; this.id = data.id;
this.chat = data.chat;
this.refresh = data.refresh;
this.title = data.title; this.title = data.title;
this.text = data.text; this.text = data.text;
this.html = data.html; this.html = data.html;
this.target = data.target; this.target = data.target;
this.duration = data.duration; this.duration = data.duration;
this.class = data.class; this.class = data.class;
this.id = data.id; this.targetChannel = data.targetChannel;
this.targetUser = data.targetUser;
this.chat = data.chat;
} }
send(_id, room) { send() {
let msg = {}; let msg = {
Object.assign(msg, this); m: "notification",
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) { // Object.assign(msg, this);
case "all": const targets = [];
for (let con of Array.from(room.server.connections.values())) {
con.sendArray([msg]); if (this.targetChannel) {
} switch (this.targetChannel) {
break; case "all":
case "room": // every channel
case "channel": for (const cl of this.server.connections.values()) {
for (let con of room.connections) { targets.push(cl);
con.sendArray([msg]); }
} break;
break; case "room":
default: case "channel":
Array.from(room.server.connections.values()) // current channel
.filter(usr => if (!this.cl) break;
typeof usr.user !== "undefined" if (!this.cl.channel) break;
? usr.user._id == _id for (const cl of this.server.connections.values()) {
: null 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 => { cl.sendArray([msg]);
p.sendArray([msg]); } else {
}); cl.sendArray([msg]);
break; }
}
} else {
this.cl.sendChat(this.text);
} }
} }
}; };

View File

@ -1,6 +0,0 @@
module.exports = class Rank {
constructor (name, _id) {
this.name = name;
this._id = _id;
}
}

View File

@ -6,6 +6,7 @@ const fs = require("fs");
const RoomSettings = require("./RoomSettings"); const RoomSettings = require("./RoomSettings");
const Logger = require("./Logger.js"); const Logger = require("./Logger.js");
const Notification = require("./Notification"); const Notification = require("./Notification");
const Database = require("./Database.js");
class Server { class Server {
static on = EventEmitter.prototype.on; static on = EventEmitter.prototype.on;
@ -80,7 +81,7 @@ class Server {
this.specialIntervals = {}; this.specialIntervals = {};
this.wss.on("connection", (ws, req) => { this.wss.on("connection", (ws, req) => {
console.log("socket connected"); // console.log("socket connected");
this.connections.set( this.connections.set(
++this.connectionid, ++this.connectionid,
new Client(ws, req, this) new Client(ws, req, this)
@ -117,7 +118,9 @@ class Server {
"channel message", "channel message",
"channel_flag", "channel_flag",
"name", "name",
"restart" "restart",
"ipban",
"ipunban"
]; ];
// this.welcome_motd = config.motd || "You agree to read this message."; // this.welcome_motd = config.motd || "You agree to read this message.";
@ -164,7 +167,8 @@ class Server {
} catch (err) { } catch (err) {
out = err; out = err;
} }
console.log(out); // console.log(out);
return `(${typeof out}) ${out}`;
} }
static getClient(id) { static getClient(id) {
@ -194,16 +198,31 @@ class Server {
text: "The server will restart in a few moments.", text: "The server will restart in a few moments.",
target: "#piano", target: "#piano",
duration: 20000, duration: 20000,
class: "classic" class: "classic",
targetChannel: "all"
} }
) { ) {
let n = new Notification(notif); let n = new Notification(this, notif);
n.send("all", this.channels.get("lobby")); n.send();
setTimeout(() => { setTimeout(() => {
process.exit(); process.exit();
}, n.duration || 20000); }, 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; module.exports = Server;

View File

@ -44,6 +44,7 @@ class User {
if (typeof this.cl.server.specialIntervals[this._id] == "undefined") { if (typeof this.cl.server.specialIntervals[this._id] == "undefined") {
this.cl.server.specialIntervals[this._id] = {}; this.cl.server.specialIntervals[this._id] = {};
} }
if (this.hasFlag("rainbow", true)) { if (this.hasFlag("rainbow", true)) {
if ( if (
!this.cl.server.specialIntervals[this._id].hasOwnProperty( !this.cl.server.specialIntervals[this._id].hasOwnProperty(

View File

@ -114,7 +114,7 @@ var fish_without_images = [
"Tadpole", "Tadpole",
"Whale Shark" "Whale Shark"
]; ];
// var newfish = require("./newfish.json"); var newfish = require("./fish");
var pokedex = []; var pokedex = [];
process.stdout.write( process.stdout.write(