mpp-server-dev2/oldsrc/Channel.js

849 lines
25 KiB
JavaScript
Raw Normal View History

2023-03-31 16:43:24 +02:00
const createKeccakHash = require("keccak");
const Crown = require("./Crown.js");
const Database = require("./Database.js");
const Logger = require("./Logger.js");
2020-04-07 23:02:35 +02:00
const Quota = require("./Quota.js");
2023-03-31 16:43:24 +02:00
const RoomSettings = require("./RoomSettings.js");
const ftc = require("fancy-text-converter");
const Notification = require("./Notification");
const Color = require("./Color");
const { getTimeColor } = require("./ColorEncoder.js");
const { InternalBot } = require("./InternalBot");
function ansiRegex({ onlyFirst = false } = {}) {
const pattern = [
"[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",
"(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))"
].join("|");
return new RegExp(pattern, onlyFirst ? undefined : "g");
2022-09-07 09:10:07 +02:00
}
const LOGGER_PARTICIPANT = {
2023-03-31 16:43:24 +02:00
name: "Logger",
color: "#72f1b8",
_id: "logger",
id: "logger"
};
2022-09-07 09:10:07 +02:00
2023-04-17 01:34:32 +02:00
const SERVER_PARTICIPANT = {
name: "mpp",
color: "#ffffff",
_id: "0",
id: "0"
};
2023-03-31 16:43:24 +02:00
const LOGGING_CHANNEL = "lolwutsecretloggingchannel";
const BAN_CHANNEL = "test/awkward";
2022-09-10 07:14:22 +02:00
2022-03-29 06:41:30 +02:00
class Channel extends EventEmitter {
2023-03-31 16:43:24 +02:00
static loggingChannel = LOGGING_CHANNEL;
static loggerParticipant = LOGGER_PARTICIPANT;
2023-04-17 01:34:32 +02:00
static serverParticipant = SERVER_PARTICIPANT;
2023-03-31 16:43:24 +02:00
static banChannel = BAN_CHANNEL;
2023-01-15 01:48:04 +01:00
2022-09-07 09:10:07 +02:00
constructor(server, _id, settings, cl) {
2020-04-07 09:55:16 +02:00
super();
2022-05-24 20:22:19 +02:00
this.logger = new Logger(`Room - ${_id}`);
2020-04-07 09:55:16 +02:00
this._id = _id;
this.server = server;
2021-05-09 10:52:41 +02:00
this.crown;
2020-04-07 09:55:16 +02:00
this.crowndropped = false;
2022-06-17 08:36:49 +02:00
if (this.isLobby(this._id)) {
this.settings = new RoomSettings(this.server.lobbySettings);
// this.settings.lobby = true;
// this.settings.color = this.server.lobbySettings.color;
// this.settings.color2 = this.server.lobbySettings.color2;
} else {
2023-03-31 16:43:24 +02:00
this.settings = new RoomSettings(settings, "user");
2022-06-17 08:36:49 +02:00
}
2020-04-07 09:55:16 +02:00
this.chatmsgs = [];
this.ppl = new Map();
this.connections = [];
this.bindEventListeners();
2023-03-31 16:43:24 +02:00
this.server.channels.set(_id, this);
2020-04-07 09:55:16 +02:00
this.bans = new Map();
2023-03-31 16:43:24 +02:00
this.flags = {};
2022-05-24 20:22:19 +02:00
this.destroyed = false;
2021-05-09 10:52:41 +02:00
2023-03-31 16:43:24 +02:00
this.logger.log("Created");
2022-03-29 19:37:04 +02:00
2023-03-31 16:43:24 +02:00
if (this._id == LOGGING_CHANNEL) {
if (cl.user.hasFlag("admin")) {
2022-09-07 09:10:07 +02:00
delete this.crown;
Logger.buffer.forEach(str => {
this.chatmsgs.push({
2023-03-31 16:43:24 +02:00
m: "a",
p: LOGGER_PARTICIPANT,
a: str.replace(ansiRegex(), "")
2022-09-07 09:10:07 +02:00
});
});
2023-03-31 16:43:24 +02:00
Logger.on("buffer update", str => {
2022-09-07 09:10:07 +02:00
this.chatmsgs.push({
2023-03-31 16:43:24 +02:00
m: "a",
p: LOGGER_PARTICIPANT,
a: str.replace(ansiRegex(), "")
2022-09-07 09:10:07 +02:00
});
this.sendChatArray();
});
2023-03-31 16:43:24 +02:00
this.emit("update");
let c = new Color(LOGGER_PARTICIPANT.color);
2022-09-07 09:10:07 +02:00
c.add(-0x40, -0x40, -0x40);
2023-03-31 16:43:24 +02:00
let c2 = new Color(c.toHexa());
c2.add(-0x40, -0x40, -0x40);
this.settings = RoomSettings.changeSettings(
{
color: c.toHexa(),
color2: c2.toHexa(),
chat: true,
crownsolo: true,
lobby: false,
owner_id: LOGGER_PARTICIPANT._id
},
true
);
2022-09-07 09:10:07 +02:00
} else {
2023-03-31 16:43:24 +02:00
cl.setChannel("test/awkward");
2021-05-09 10:52:41 +02:00
}
2022-09-07 09:10:07 +02:00
} else {
Database.getRoomSettings(this._id, (err, set) => {
if (err) {
return;
}
2022-07-09 11:06:51 +02:00
2023-03-31 16:43:24 +02:00
this.settings = RoomSettings.changeSettings(
this.settings,
true
);
2022-09-07 09:10:07 +02:00
this.chatmsgs = set.chat;
this.sendChatArray();
this.setData();
2022-03-29 03:32:56 +02:00
});
2022-09-07 09:10:07 +02:00
}
2022-07-09 11:06:51 +02:00
if (this.isLobby(this._id)) {
this.colorInterval = setInterval(() => {
this.setDefaultLobbyColorBasedOnDate();
2023-04-17 01:34:32 +02:00
}, 5000);
2022-07-09 11:06:51 +02:00
this.setDefaultLobbyColorBasedOnDate();
}
2020-04-07 09:55:16 +02:00
}
2021-04-12 23:29:02 +02:00
2022-03-29 06:41:30 +02:00
setChatArray(arr) {
this.chatmsgs = arr || [];
2023-03-31 16:43:24 +02:00
this.sendArray([
{
m: "c",
c: this.chatmsgs.slice(-1 * 32)
}
]);
2022-03-29 06:41:30 +02:00
this.setData();
}
2022-09-07 09:10:07 +02:00
sendChatArray() {
this.connections.forEach(cl => {
2023-03-31 16:43:24 +02:00
cl.sendArray([
{
m: "c",
c: this.chatmsgs.slice(-1 * 32)
}
]);
2022-09-07 09:10:07 +02:00
});
}
2022-07-09 11:06:51 +02:00
setDefaultLobbyColorBasedOnDate() {
let col = getTimeColor();
let col2 = new Color(col.r - 0x40, col.g - 0x40, col.b - 0x40);
2022-12-06 17:28:20 +01:00
if (!this.settings) {
this.settings = new RoomSettings(this.server.lobbySettings);
}
2023-03-31 16:43:24 +02:00
2023-04-01 07:05:12 +02:00
this.settings.changeSettings({
color: col.toHexa(),
color2: col.toHexa()
});
2022-07-09 11:06:51 +02:00
for (let key in this.settings) {
this.server.lobbySettings[key] = this.settings[key];
}
2023-03-31 16:43:24 +02:00
this.emit("update");
2022-07-09 11:06:51 +02:00
}
2023-03-31 16:43:24 +02:00
join(cl, set) {
//this stuff is complicated
let otheruser = this.connections.find(a => a.user._id == cl.user._id);
2020-04-07 09:55:16 +02:00
if (!otheruser) {
2022-07-09 11:06:51 +02:00
// we don't exist yet
// create id hash
2023-03-31 16:43:24 +02:00
let participantId = createKeccakHash("keccak256")
.update(Math.random().toString() + cl.ip)
.digest("hex")
.substr(0, 24);
2021-05-09 10:52:41 +02:00
2022-07-09 11:06:51 +02:00
// set id
2020-04-07 09:55:16 +02:00
cl.user.id = participantId;
cl.participantId = participantId;
2022-07-09 11:06:51 +02:00
// init quotas (TODO pass type of room in?)
2020-04-07 23:02:35 +02:00
cl.initParticipantQuotas();
2022-06-17 06:12:55 +02:00
2022-07-09 11:06:51 +02:00
// no users / already had crown? give crown
2023-03-31 16:43:24 +02:00
if (
(this.connections.length == 0 &&
Array.from(this.ppl.values()).length == 0 &&
this.isLobby(this._id) == false) ||
(this.crown &&
(this.crown.userId == cl.user._id ||
this.settings["owner_id"] == cl.user._id))
) {
2022-06-17 06:12:55 +02:00
// user owns the room
// we need to switch the crown to them
2020-04-07 23:02:35 +02:00
//cl.quotas.a.setParams(Quota.PARAMS_A_CROWNED);
2023-04-01 07:05:12 +02:00
this.emit("add crown", {
participantId: cl.participantId,
userId: cl.user._id
});
2021-04-12 23:29:02 +02:00
2020-04-07 09:55:16 +02:00
this.crowndropped = false;
2022-06-17 08:36:49 +02:00
// this.settings = new RoomSettings(set, 'user');
2020-04-07 23:02:35 +02:00
} else {
//cl.quotas.a.setParams(Quota.PARAMS_A_NORMAL);
2021-04-12 23:29:02 +02:00
2022-06-17 06:12:55 +02:00
if (this.isLobby(this._id) && this.settings.lobby !== true) {
2022-07-09 11:06:51 +02:00
// fix lobby setting
2023-03-31 16:43:24 +02:00
this.settings.changeSettings({ lobby: true });
2022-06-17 08:36:49 +02:00
// this.settings.visible = true;
// this.settings.crownsolo = false;
// this.settings.lobby = true;
// this.settings.color = this.server.lobbySettings.color;
// this.settings.color2 = this.server.lobbySettings.color2;
2021-04-12 23:29:02 +02:00
} else {
2022-06-17 08:36:49 +02:00
if (!this.isLobby) {
2023-03-31 16:43:24 +02:00
if (typeof set == "undefined") {
if (typeof this.settings == "undefined") {
this.settings = new RoomSettings(
this.server.defaultRoomSettings,
"user"
);
2022-06-17 08:36:49 +02:00
} else {
2023-03-31 16:43:24 +02:00
this.settings = new RoomSettings(
cl.channel.settings,
"user"
);
2022-06-17 08:36:49 +02:00
}
2021-04-13 04:08:29 +02:00
} else {
2023-03-31 16:43:24 +02:00
this.settings = new RoomSettings(set, "user");
2021-04-13 04:08:29 +02:00
}
}
2021-04-12 23:29:02 +02:00
}
2020-04-07 09:55:16 +02:00
}
2021-04-12 23:29:02 +02:00
2020-04-07 09:55:16 +02:00
this.ppl.set(participantId, cl);
2020-04-07 23:09:00 +02:00
2020-04-07 09:55:16 +02:00
this.connections.push(cl);
2021-04-12 23:29:02 +02:00
2023-03-31 16:43:24 +02:00
cl.sendArray([
{
m: "c",
c: this.chatmsgs.slice(-1 * 32)
}
]);
2022-07-09 11:06:51 +02:00
// this.updateCh(cl, this.settings);
if (!cl.user.hasFlag("hidden", true)) {
2023-03-31 16:43:24 +02:00
this.sendArray(
[
{
m: "p",
_id: cl.user._id,
name: cl.user.name,
color: cl.user.color,
id: cl.participantId,
x: this.ppl.get(cl.participantId).x || 200,
y: this.ppl.get(cl.participantId).y || 100
}
],
cl,
false
);
2022-07-09 11:06:51 +02:00
}
2021-03-01 05:04:31 +01:00
this.updateCh(cl, this.settings);
2020-04-07 09:55:16 +02:00
} else {
cl.user.id = otheruser.participantId;
cl.participantId = otheruser.participantId;
2020-04-07 23:02:35 +02:00
cl.quotas = otheruser.quotas;
2020-04-07 09:55:16 +02:00
this.connections.push(cl);
2023-03-31 16:43:24 +02:00
cl.sendArray([
{
m: "c",
c: this.chatmsgs.slice(-1 * 32)
}
]);
2021-03-01 05:04:31 +01:00
this.updateCh(cl, this.settings);
2020-04-07 09:55:16 +02:00
}
2022-06-17 08:36:49 +02:00
}
2023-03-31 16:43:24 +02:00
remove(p) {
// remove user
2022-06-17 08:36:49 +02:00
if (!p) return;
2023-03-31 16:43:24 +02:00
if (!p.user) return;
let otheruser = this.connections.filter(a => a.user._id == p.user._id);
2020-04-07 09:55:16 +02:00
if (!(otheruser.length > 1)) {
this.ppl.delete(p.participantId);
2023-03-31 16:43:24 +02:00
this.connections.splice(
this.connections.findIndex(
a => a.connectionid == p.connectionid
),
1
);
this.sendArray(
[
{
m: "bye",
p: p.participantId
}
],
p,
false
);
2020-04-07 09:55:16 +02:00
if (this.crown)
if (this.crown.userId == p.user._id && !this.crowndropped) {
this.chown();
}
this.updateCh();
} else {
2023-03-31 16:43:24 +02:00
this.connections.splice(
this.connections.findIndex(
a => a.connectionid == p.connectionid
),
1
);
2020-04-07 09:55:16 +02:00
}
}
2021-04-12 23:29:02 +02:00
2023-03-31 16:43:24 +02:00
updateCh(cl, set) {
//update channel for all people in channel
2021-04-02 20:02:04 +02:00
if (Array.from(this.ppl.values()).length <= 0) {
setTimeout(() => {
this.destroy();
2023-05-21 16:20:03 +02:00
}, 3000);
return;
2021-04-02 20:02:04 +02:00
}
2021-04-12 23:29:02 +02:00
2023-03-31 16:43:24 +02:00
this.connections.forEach(usr => {
2021-05-09 10:52:41 +02:00
let u = this.fetchChannelData(usr, cl);
this.server.connections.get(usr.connectionid).sendArray([u]);
2021-04-12 23:29:02 +02:00
});
2023-03-31 16:43:24 +02:00
this.server.updateChannelList([this.fetchChannelData()]);
2020-04-07 09:55:16 +02:00
}
2021-04-12 23:29:02 +02:00
2020-04-07 09:55:16 +02:00
updateParticipant(pid, options) {
2021-04-12 23:29:02 +02:00
let p;
2020-05-28 17:45:52 +02:00
Array.from(this.ppl).map(rpg => {
2021-04-12 23:29:02 +02:00
if (rpg[1].user._id == pid) p = rpg[1];
2020-05-28 17:45:52 +02:00
});
2021-04-12 23:29:02 +02:00
2023-03-31 16:43:24 +02:00
if (typeof p == "undefined") return;
2021-04-12 23:29:02 +02:00
2023-03-31 16:43:24 +02:00
options.name ? (p.user.name = options.name) : {};
options._id ? (p.user._id = options._id) : {};
options.color ? (p.user.color = options.color) : {};
2021-04-12 23:29:02 +02:00
2023-03-31 16:43:24 +02:00
this.connections
.filter(ofo => ofo.participantId == p.participantId)
.forEach(usr => {
options.name ? (usr.user.name = options.name) : {};
options._id ? (usr.user._id = options._id) : {};
options.color ? (usr.user.color = options.color) : {};
});
2021-04-12 23:29:02 +02:00
2021-04-13 04:08:29 +02:00
if (!p.hidden) {
2023-03-31 16:43:24 +02:00
this.sendArray([
{
color: p.user.color,
id: p.participantId,
m: "p",
name: p.user.name,
x: p.x || 200,
y: p.y || 100,
_id: p.user._id
}
]);
2021-04-13 04:08:29 +02:00
}
2020-04-07 09:55:16 +02:00
}
2023-03-31 16:43:24 +02:00
destroy() {
//destroy room
2022-05-24 20:22:19 +02:00
if (this.destroyed) return;
2021-04-12 23:29:02 +02:00
if (this.ppl.size > 0) return;
2022-07-09 11:06:51 +02:00
if (this._id == "lobby") return;
2022-05-24 20:22:19 +02:00
this.destroyed = true;
2020-04-07 09:55:16 +02:00
this._id;
2022-09-07 09:10:07 +02:00
this.logger.log(`Deleted room ${this._id}`);
2021-04-12 23:29:02 +02:00
this.settings = undefined;
2020-04-07 09:55:16 +02:00
this.ppl;
this.connnections;
this.chatmsgs;
2023-03-31 16:43:24 +02:00
this.server.channels.delete(this._id);
2020-04-07 09:55:16 +02:00
}
2021-04-12 23:29:02 +02:00
2020-04-07 09:55:16 +02:00
sendArray(arr, not, onlythisparticipant) {
2023-03-31 16:43:24 +02:00
this.connections.forEach(usr => {
if (
!not ||
(usr.participantId != not.participantId &&
!onlythisparticipant) ||
(usr.connectionid != not.connectionid && onlythisparticipant)
) {
2020-04-07 09:55:16 +02:00
try {
2022-06-17 08:36:49 +02:00
let cl = this.server.connections.get(usr.connectionid);
if (!cl) return;
2023-03-31 16:43:24 +02:00
this.server.connections
.get(usr.connectionid)
.sendArray(arr);
2020-04-07 09:55:16 +02:00
} catch (e) {
2023-04-01 07:05:12 +02:00
this.logger.error(e);
2020-04-07 09:55:16 +02:00
}
}
2021-04-12 23:29:02 +02:00
});
2020-04-07 09:55:16 +02:00
}
2021-04-12 23:29:02 +02:00
2021-05-09 10:52:41 +02:00
fetchChannelData(usr, cl) {
2020-04-07 09:55:16 +02:00
let chppl = [];
2021-04-12 23:29:02 +02:00
2021-05-09 10:52:41 +02:00
[...this.ppl.values()].forEach(c => {
2022-06-18 06:43:33 +02:00
if (cl) {
if (c.hidden == true && c.user._id !== cl.user._id) {
2022-07-09 11:06:51 +02:00
// client is hidden and we are that client
2022-06-18 06:43:33 +02:00
return;
2022-06-18 06:47:51 +02:00
} else if (c.user._id == cl.user._id) {
2022-06-18 06:49:51 +02:00
// let u = {
// _id: c.user._id,
// name: c.user.name + ' [HIDDEN]',
// color: c.user.color,
// id: c.participantId
// }
// chppl.push(u);
2022-06-18 06:43:33 +02:00
}
}
2021-05-09 10:52:41 +02:00
let u = {
_id: c.user._id,
name: c.user.name,
color: c.user.color,
id: c.participantId
2023-03-31 16:43:24 +02:00
};
2021-05-09 10:52:41 +02:00
chppl.push(u);
2021-04-12 23:29:02 +02:00
});
2020-04-07 09:55:16 +02:00
let data = {
m: "ch",
p: "ofo",
ch: {
count: chppl.length,
crown: this.crown,
settings: this.settings,
_id: this._id
},
ppl: chppl
2023-03-31 16:43:24 +02:00
};
2021-04-12 23:29:02 +02:00
2020-04-07 09:55:16 +02:00
if (cl) {
if (usr.connectionid == cl.connectionid) {
data.p = cl.participantId;
} else {
delete data.p;
}
} else {
delete data.p;
}
2021-04-12 23:29:02 +02:00
2020-04-07 09:55:16 +02:00
if (data.ch.crown == null) {
delete data.ch.crown;
} else {
}
2021-04-12 23:29:02 +02:00
2020-04-07 09:55:16 +02:00
return data;
}
2021-04-12 23:29:02 +02:00
2020-04-07 23:02:35 +02:00
verifyColor(strColor) {
2020-04-07 09:55:16 +02:00
var test2 = /^#[0-9A-F]{6}$/i.test(strColor);
2020-04-07 23:02:35 +02:00
if (test2 == true) {
return strColor;
} else {
return false;
2020-04-07 09:55:16 +02:00
}
}
2021-04-12 23:29:02 +02:00
2020-04-07 09:55:16 +02:00
isLobby(_id) {
if (_id.startsWith("lobby")) {
if (_id == "lobby") {
return true;
2020-04-07 23:02:35 +02:00
}
2021-04-12 18:41:44 +02:00
let lobbynum = _id.split("lobby")[1];
2020-04-07 09:55:16 +02:00
if (!(parseInt(lobbynum).toString() == lobbynum)) return false;
2021-04-12 18:41:44 +02:00
2020-04-07 23:02:35 +02:00
for (let i in lobbynum) {
if (parseInt(lobbynum[i]) >= 0) {
if (parseInt(i) + 1 == lobbynum.length) return true;
} else {
return false;
2020-04-07 09:55:16 +02:00
}
2020-04-07 23:02:35 +02:00
}
2023-03-31 16:43:24 +02:00
} else if (
_id.startsWith("test/") ||
_id.toLowerCase().includes("grant")
) {
2020-04-07 09:55:16 +02:00
if (_id == "test/") {
return false;
} else {
return true;
}
} else {
return false;
}
}
2021-04-12 23:29:02 +02:00
2020-04-07 09:55:16 +02:00
chown(id) {
let prsn = this.ppl.get(id);
if (prsn) {
2021-05-09 10:52:41 +02:00
this.crown = new Crown(id, prsn.user._id);
2020-04-07 09:55:16 +02:00
this.crowndropped = false;
} else {
2022-07-09 06:38:32 +02:00
if (this.crown) {
this.crown = new Crown(id, this.crown.userId);
this.crowndropped = true;
}
2020-04-07 09:55:16 +02:00
}
2021-04-12 23:29:02 +02:00
2020-04-07 09:55:16 +02:00
this.updateCh();
}
2021-04-12 23:29:02 +02:00
setCoords(p, x, y) {
2020-04-07 09:55:16 +02:00
if (p.participantId && this.ppl.get(p.participantId)) {
2023-03-31 16:43:24 +02:00
x ? (this.ppl.get(p.participantId).x = x) : {};
y ? (this.ppl.get(p.participantId).y = y) : {};
this.sendArray(
[
{
m: "m",
id: p.participantId,
x: this.ppl.get(p.participantId).x,
y: this.ppl.get(p.participantId).y
}
],
p,
false
);
2020-04-07 09:55:16 +02:00
}
}
2021-04-12 23:29:02 +02:00
2020-04-07 09:55:16 +02:00
chat(p, msg) {
if (msg.message.length > 512) return;
2022-06-15 08:21:50 +02:00
2021-04-12 18:37:19 +02:00
if (p.participantId == 0) {
let message = {};
2022-06-15 08:21:50 +02:00
2021-04-12 18:37:19 +02:00
message.m = "a";
2022-06-15 08:21:50 +02:00
message.t = Date.now();
2022-07-04 00:46:05 +02:00
message.a = msg.message;
2022-06-15 08:21:50 +02:00
2023-03-31 16:43:24 +02:00
if (message.a.length > 0 && message.a.length <= 512) {
message.p = {
color: "#ffffff",
id: "0",
name: "mpp",
_id: "0"
};
2022-06-15 08:21:50 +02:00
2023-03-31 16:43:24 +02:00
this.sendArray([message]);
2021-05-09 10:52:41 +02:00
2023-03-31 16:43:24 +02:00
this.chatmsgs.push(message);
this.setData();
return;
}
2021-04-12 18:37:19 +02:00
}
2022-06-15 08:21:50 +02:00
2023-03-31 16:43:24 +02:00
let filter = ["AMIGHTYWIND", "CHECKLYHQ"];
let regexp = new RegExp("\\b(" + filter.join("|") + ")\\b", "i");
if (regexp.test(msg.message.split(" ").join(""))) return;
2020-04-07 09:55:16 +02:00
let prsn = this.ppl.get(p.participantId);
2022-05-24 21:40:09 +02:00
if (!prsn) return;
let message = {};
message.m = "a";
message.a = msg.message;
2023-03-31 16:43:24 +02:00
if (prsn.user.hasFlag("chat_curse_1")) {
if (prsn.user.flags["chat_curse_1"] != false)
message.a = message.a
.split(/[aeiou]/)
.join("o")
.split(/[AEIOU]/)
.join("O");
2020-04-07 09:55:16 +02:00
}
2023-03-31 16:43:24 +02:00
if (prsn.user.hasFlag("chat_curse_2")) {
2022-05-24 21:40:09 +02:00
}
2023-03-31 16:43:24 +02:00
2022-05-24 21:40:09 +02:00
message.p = {
color: p.user.color,
id: p.participantId,
name: p.user.name,
_id: p.user._id
};
2022-08-16 15:42:09 +02:00
2022-05-24 21:40:09 +02:00
message.t = Date.now();
2022-08-16 15:42:09 +02:00
2022-05-24 21:40:09 +02:00
this.sendArray([message]);
this.chatmsgs.push(message);
this.setData();
2023-03-31 16:43:24 +02:00
InternalBot.emit("receive message", message, prsn, this);
2022-05-24 21:40:09 +02:00
}
adminChat(str) {
2023-03-31 16:43:24 +02:00
this.chat(
{
participantId: 0
},
{
message: str
}
);
2020-04-07 09:55:16 +02:00
}
2021-04-12 23:29:02 +02:00
2022-06-15 08:21:50 +02:00
hasUser(id) {
2023-03-31 16:43:24 +02:00
// return this.ppl.has(id);
for (const p of this.ppl.values()) {
if (p.id == id) return true;
}
2022-06-15 08:21:50 +02:00
}
2020-04-07 09:55:16 +02:00
playNote(cl, note) {
2023-03-31 16:43:24 +02:00
if (cl.user.hasFlag("mute", true)) {
2022-07-09 11:06:51 +02:00
return;
}
2023-03-31 16:43:24 +02:00
if (cl.user.hasFlag("mute")) {
if (Array.isArray(cl.user.flags["mute"])) {
if (cl.user.flags["mute"].includes(this._id)) return;
2022-07-09 11:06:51 +02:00
}
}
let vol;
2023-03-31 16:43:24 +02:00
if (cl.user.hasFlag("volume")) {
2022-07-09 11:06:51 +02:00
vol = Math.round(cl.user.flags["volume"]) / 100;
}
2023-03-31 16:43:24 +02:00
if (typeof vol == "number") {
2022-03-29 03:32:56 +02:00
for (let no of note.n) {
2022-07-09 11:06:51 +02:00
if (no.v) {
if (vol == 0) {
no.v = vol;
} else {
no.v *= vol;
}
}
2022-03-29 03:32:56 +02:00
}
}
2021-05-09 10:52:41 +02:00
2023-03-31 16:43:24 +02:00
this.sendArray(
[
{
m: "n",
n: note.n,
p: cl.participantId,
t: note.t
}
],
cl,
true
);
2020-04-07 09:55:16 +02:00
}
2021-04-12 23:29:02 +02:00
2020-04-07 09:55:16 +02:00
kickban(_id, ms) {
ms = parseInt(ms);
2021-05-09 10:52:41 +02:00
2023-03-31 16:43:24 +02:00
if (ms >= 1000 * 60 * 60) return;
2020-04-07 09:55:16 +02:00
if (ms < 0) return;
2021-05-09 10:52:41 +02:00
2020-04-07 09:55:16 +02:00
ms = Math.round(ms / 1000) * 1000;
2021-05-09 10:52:41 +02:00
2023-03-31 16:43:24 +02:00
let user = this.connections.find(usr => usr.user._id == _id);
2020-04-07 09:55:16 +02:00
if (!user) return;
let asd = true;
let pthatbanned = this.ppl.get(this.crown.participantId);
2021-05-09 10:52:41 +02:00
2023-03-31 16:43:24 +02:00
this.connections
.filter(usr => usr.participantId == user.participantId)
.forEach(u => {
user.bantime = Math.floor(Math.floor(ms / 1000) / 60);
user.bannedtime = Date.now();
user.msbanned = ms;
2021-05-09 10:52:41 +02:00
2023-03-31 16:43:24 +02:00
this.bans.set(user.user._id, user);
2021-05-09 10:52:41 +02:00
2023-03-31 16:43:24 +02:00
//if (this.crown && (this.crown.userId)) {
2023-01-15 01:48:04 +01:00
u.setChannel(Channel.banChannel, {});
2021-05-09 10:52:41 +02:00
2020-04-07 09:55:16 +02:00
if (asd)
2023-04-01 07:05:12 +02:00
new Notification(this.server, {
id: "",
title: "Notice",
text: `Banned from "${this._id}" for ${Math.floor(
2023-03-31 16:43:24 +02:00
Math.floor(ms / 1000) / 60
)} minutes.`,
2023-04-01 07:05:12 +02:00
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();
2020-04-07 09:55:16 +02:00
if (asd)
2023-04-01 07:05:12 +02:00
new Notification(this.server, {
id: "",
class: "short",
target: "#room",
title: "Notice",
text: `${pthatbanned.user.name} banned ${
2023-03-31 16:43:24 +02:00
user.user.name
} from the channel for ${Math.floor(
Math.floor(ms / 1000) / 60
)} minutes.`,
2023-04-01 07:05:12 +02:00
duration: 7000,
targetChannel: "room",
cl: pthatbanned
}).send();
2023-03-31 16:43:24 +02:00
if (this.crown && this.crown.userId == _id) {
2023-04-01 07:05:12 +02:00
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();
2020-04-07 09:55:16 +02:00
}
2023-03-31 16:43:24 +02:00
//}
});
2020-04-07 09:55:16 +02:00
}
2021-04-12 23:29:02 +02:00
2022-07-09 11:12:13 +02:00
unban(_id) {
2023-01-15 01:48:04 +01:00
let ban = this.bans.get(_id);
2023-03-31 16:43:24 +02:00
if (!ban) return;
if (ban.bantime) {
delete ban.bantime;
}
2022-07-09 11:12:13 +02:00
2023-03-31 16:43:24 +02:00
if (ban.bannedtime) {
delete ban.bannedtime;
}
2022-07-09 11:12:13 +02:00
2023-03-31 16:43:24 +02:00
this.bans.delete(ban.user._id);
2022-07-09 11:12:13 +02:00
}
2020-04-07 09:55:16 +02:00
bindEventListeners() {
this.on("bye", participant => {
this.remove(participant);
2022-07-09 11:06:51 +02:00
});
2020-04-07 09:55:16 +02:00
2022-06-17 08:36:49 +02:00
this.on("m", msg => {
let p = this.ppl.get(msg.p);
if (!p) return;
this.setCoords(p, msg.x, msg.y);
2022-07-09 11:06:51 +02:00
});
2020-04-07 09:55:16 +02:00
2022-07-09 11:24:23 +02:00
this.on("a", (participant, msg) => {
this.chat(participant, msg);
2022-07-09 11:06:51 +02:00
});
2022-06-15 08:21:50 +02:00
2022-07-09 11:06:51 +02:00
this.on("update", (cl, set) => {
this.updateCh(cl, set);
2022-06-15 08:21:50 +02:00
});
2022-06-17 08:36:49 +02:00
this.on("remove crown", () => {
this.crown = undefined;
delete this.crown;
2023-03-31 16:43:24 +02:00
this.emit("update");
2022-06-17 08:36:49 +02:00
});
2023-04-01 07:05:12 +02:00
this.on("add crown", msg => {
this.crown = new Crown(msg.participantId, msg.userId);
this.emit("update");
2022-06-17 08:36:49 +02:00
});
2022-08-16 15:42:09 +02:00
2023-04-01 07:05:12 +02:00
this.on("kickban", msg => {
if (!msg._id) return;
if (!msg.ms) msg.ms = 30 * 60 * 1000;
this.kickban(msg._id, msg.ms);
2022-08-16 15:42:09 +02:00
});
2020-04-07 09:55:16 +02:00
}
2021-04-12 18:37:19 +02:00
2021-05-09 10:52:41 +02:00
verifySet(_id, msg) {
2023-03-31 16:43:24 +02:00
if (typeof msg.set !== "object") {
2021-04-12 23:29:02 +02:00
msg.set = {
visible: true,
2023-03-31 16:43:24 +02:00
color: this.server.defaultSettings.color,
chat: true,
crownsolo: false
};
2020-04-07 09:55:16 +02:00
}
2021-04-12 23:29:02 +02:00
msg.set = RoomSettings.changeSettings(msg.set);
2023-03-31 16:43:24 +02:00
if (typeof msg.set.lobby !== "undefined") {
2021-04-12 23:29:02 +02:00
if (msg.set.lobby == true) {
2023-04-01 07:05:12 +02:00
if (!this.isLobby(_id)) delete msg.set.lobby;
2021-04-12 23:29:02 +02:00
} else {
if (this.isLobby(_id)) {
msg.set = this.server.lobbySettings;
2020-04-07 09:55:16 +02:00
}
}
2021-04-12 23:29:02 +02:00
}
2020-04-07 09:55:16 +02:00
}
2021-05-09 10:52:41 +02:00
setData() {
Database.setRoomSettings(this._id, this.settings, this.chatmsgs);
}
hasFlag(flag, val) {
if (!val) return this.flags.hasOwnProperty(flag);
return this.flags.hasOwnProperty(flag) && this.flags[flag] == val;
}
2020-04-07 09:55:16 +02:00
}
2021-04-12 23:29:02 +02:00
2022-03-29 06:41:30 +02:00
module.exports = Channel;