mpp-server-dev2/oldsrc/User.js

132 lines
3.8 KiB
JavaScript
Raw Normal View History

2021-04-12 23:29:02 +02:00
const Database = require("./Database");
2023-03-31 16:43:24 +02:00
const { Cow } = require("./Cow");
2021-04-12 23:29:02 +02:00
2021-05-09 10:52:41 +02:00
function hslToHex(h, s, l) {
l /= 100;
2023-03-31 16:43:24 +02:00
const a = (s * Math.min(l, 1 - l)) / 100;
2021-05-09 10:52:41 +02:00
const f = n => {
const k = (n + h / 30) % 12;
const color = l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
2023-03-31 16:43:24 +02:00
return Math.round(255 * color)
.toString(16)
.padStart(2, "0");
2021-05-09 10:52:41 +02:00
};
return `#${f(0)}${f(8)}${f(4)}`;
}
2020-04-07 09:55:16 +02:00
class User {
2021-04-12 23:29:02 +02:00
constructor(cl, data) {
2021-04-13 23:44:28 +02:00
this.name = data.name;
2021-05-09 10:52:41 +02:00
this.cl = cl;
2021-04-12 23:29:02 +02:00
this._id = data._id;
this.color = data.color;
2023-03-31 16:43:24 +02:00
this.flags =
typeof data.flags == "object"
? data.flags
: {
volume: 100,
"no chat rate limit": false,
freeze_name: false
};
this.inventory = {};
2021-05-09 10:52:41 +02:00
}
getPublicUser() {
let t = {};
t.name = this.name;
t.color = this.color;
t._id = this._id;
return t;
}
checkFlags() {
2023-03-31 16:43:24 +02:00
if (typeof this.cl.server.specialIntervals[this._id] == "undefined") {
2021-05-10 04:02:04 +02:00
this.cl.server.specialIntervals[this._id] = {};
}
2023-04-01 07:05:12 +02:00
2023-03-31 16:43:24 +02:00
if (this.hasFlag("rainbow", true)) {
if (
!this.cl.server.specialIntervals[this._id].hasOwnProperty(
"rainbow"
)
) {
2021-05-09 10:52:41 +02:00
let h = Math.floor(Math.random() * 360);
let s = 50;
let l = 50;
let hvel = 5;
let svel = 1;
let lvel = 0.5;
2023-03-31 16:43:24 +02:00
this.cl.server.specialIntervals[this._id].rainbow = setInterval(
() => {
hvel = Math.floor(Math.random() * 10);
h += hvel;
if (h > 360) h = 0;
s += svel;
if (s >= 100 || s <= 50) {
svel = -svel;
}
l += lvel;
if (l >= 75 || l <= 25) {
lvel = -lvel;
}
this.color = hslToHex(h, s, l);
// Database.updateUser(this._id, this);
// this.cl.channel.updateParticipant(this._id, this);
for (const ch of this.cl.server.channels.values()) {
if (ch.hasUser(this.cl.id)) {
ch.updateParticipant(this._id, this);
}
}
},
1000 / 15
);
2021-05-09 10:52:41 +02:00
}
2023-03-31 16:43:24 +02:00
} else if (this.hasFlag("rainbow", false)) {
2022-06-17 08:36:49 +02:00
this.stopFlagEvents();
2021-05-09 10:52:41 +02:00
}
}
2021-05-10 04:02:04 +02:00
stopFlagEvents() {
2022-06-17 08:36:49 +02:00
let ints = this.cl.server.specialIntervals[this._id];
if (!ints) {
this.cl.server.specialIntervals[this._id] = {};
ints = this.cl.server.specialIntervals[this._id];
}
2023-03-31 16:43:24 +02:00
if ("rainbow" in ints) {
2022-06-17 08:36:49 +02:00
clearInterval(this.cl.server.specialIntervals[this._id].rainbow);
delete this.cl.server.specialIntervals[this._id].rainbow;
}
2021-05-10 04:02:04 +02:00
}
2021-05-09 10:52:41 +02:00
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-06-17 08:36:49 +02:00
setFlag(flag, val) {
2023-03-31 16:43:24 +02:00
if (typeof this.flags[flag] == "undefined") {
2022-06-17 08:36:49 +02:00
this.flags[flag] = val;
}
}
2021-04-12 23:29:02 +02:00
static updateUserModel(cl, user) {
let u2 = new User(cl, user);
2023-03-31 16:43:24 +02:00
if (typeof u2 == "undefined") return;
2021-04-12 23:29:02 +02:00
for (let id in Object.keys(u2)) {
if (!user.hasOwnProperty(id)) {
user[id] = u2[id];
}
2020-04-07 09:55:16 +02:00
}
}
}
2021-04-12 23:29:02 +02:00
2020-04-07 23:09:00 +02:00
module.exports = User;