mpp-server-dev2/src/Message.js

134 lines
5.0 KiB
JavaScript
Raw Normal View History

2019-06-06 19:22:43 +02:00
const User = require("./User.js");
const Room = require("./Room.js");
2019-06-09 20:10:25 +02:00
module.exports = (cl) => {
cl.once("hi", () => {
let user = new User(cl);
user.getUserData().then((data) => {
let msg = {};
msg.m = "hi";
msg.motd = cl.server.welcome_motd;
msg.t = Date.now();
msg.u = data;
2019-06-11 23:01:44 +02:00
msg.v = "Beta";
2019-06-09 20:10:25 +02:00
cl.sendArray([msg])
cl.user = data;
})
2019-06-06 19:22:43 +02:00
})
2019-06-09 20:10:25 +02:00
cl.on("t", msg => {
if (msg.hasOwnProperty("e") && !isNaN(msg.e))
cl.sendArray([{
m: "t",
t: Date.now(),
e: msg.e
}])
})
cl.on("ch", msg => {
if (!msg.hasOwnProperty("set") || !msg.set) msg.set = {};
if (msg.hasOwnProperty("_id") && typeof msg._id == "string") {
if (msg._id.length > 512) return;
cl.setChannel(msg._id, msg.set);
}
})
cl.on("m", msg => {
if (!(cl.channel && cl.participantId)) return;
if (!msg.hasOwnProperty("x")) msg.x = null;
if (!msg.hasOwnProperty("y")) msg.y = null;
if (parseInt(msg.x) == NaN) msg.x = null;
if (parseInt(msg.y) == NaN) msg.y = null;
cl.channel.emit("m", cl, msg.x, msg.y)
2019-06-09 20:10:25 +02:00
})
cl.on("chown", msg => {
if (!(cl.channel && cl.participantId)) return;
2019-06-09 21:05:52 +02:00
//console.log((Date.now() - cl.channel.crown.time))
//console.log(!(cl.channel.crown.userId != cl.user._id), !((Date.now() - cl.channel.crown.time) > 15000));
2019-06-09 20:10:25 +02:00
if (!(cl.channel.crown.userId == cl.user._id) && !((Date.now() - cl.channel.crown.time) > 15000)) return;
if (msg.hasOwnProperty("id")) {
2019-06-09 21:05:52 +02:00
// console.log(cl.channel.crown)
if (cl.user._id == cl.channel.crown.userId || cl.channel.crowndropped)
cl.channel.chown(msg.id);
2019-06-09 20:10:25 +02:00
} else {
2019-06-09 21:05:52 +02:00
if (cl.user._id == cl.channel.crown.userId || cl.channel.crowndropped)
cl.channel.chown();
2019-06-09 20:10:25 +02:00
}
})
2019-06-09 21:05:52 +02:00
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;
cl.channel.updateCh();
})
2019-06-11 23:01:44 +02:00
cl.on("a", msg => {
if (!(cl.channel && cl.participantId)) return;
if (!msg.hasOwnProperty('message')) return;
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);
})
cl.on('+ls', msg => {
2019-06-12 19:28:15 +02:00
if (!(cl.channel && cl.participantId)) return;
2019-06-11 23:01:44 +02:00
cl.server.roomlisteners.set(cl.connectionid, cl);
let rooms = [];
for (let room of Array.from(cl.server.rooms.values())) {
let data = room.fetchData().ch;
if (room.bans.get(cl.user._id)) {
data.banned = true;
}
rooms.push(data);
}
cl.sendArray([{
"m": "ls",
"c": true,
"u": rooms
}])
})
cl.on('-ls', msg => {
2019-06-12 19:28:15 +02:00
if (!(cl.channel && cl.participantId)) return;
2019-06-11 23:01:44 +02:00
cl.server.roomlisteners.delete(cl.connectionid);
})
cl.on("userset", msg => {
2019-06-12 19:28:15 +02:00
if (!(cl.channel && cl.participantId)) return;
2019-06-11 23:01:44 +02:00
if (!msg.hasOwnProperty("set") || !msg.set) msg.set = {};
if (msg.set.hasOwnProperty('name') && typeof msg.set.name == "string") {
if (msg.set.name.length > 40) return;
cl.user.name = msg.set.name;
let user = new User(cl);
user.getUserData().then((usr) => {
let dbentry = user.userdb.get(cl.user._id);
if (!dbentry) return;
dbentry.name = msg.set.name;
user.updatedb();
2019-06-12 19:28:15 +02:00
console.log("Updateing user ", usr.name, msg.set.name);
2019-06-11 23:01:44 +02:00
cl.server.rooms.forEach((room) => {
room.updateParticipant(cl.participantId, msg.set.name);
})
})
}
})
cl.on('kickban', msg => {
if (!(cl.channel && cl.participantId)) return;
if (!(cl.user._id == cl.channel.crown.userId)) return;
if (msg.hasOwnProperty('_id') && typeof msg._id == "string") {
let _id = msg._id;
let ms = msg.ms || 0;
cl.channel.kickban(_id, ms);
}
})
cl.on("bye", msg => {
cl.destroy();
})
2019-06-06 19:22:43 +02:00
}