mpp-server-dev2/oldsrc/RoomSettings.js

213 lines
6.3 KiB
JavaScript
Raw Normal View History

2023-04-17 01:34:32 +02:00
const config = require("../config");
2021-05-09 10:52:41 +02:00
2021-04-12 23:29:02 +02:00
class RoomSettings {
static allowedProperties = {
color: {
2023-04-17 01:34:32 +02:00
type: "color",
2021-05-09 10:52:41 +02:00
default: config.defaultRoomSettings.color,
2021-04-12 23:29:02 +02:00
allowedChange: true,
required: true
},
color2: {
2023-04-17 01:34:32 +02:00
type: "color2",
2021-05-09 10:52:41 +02:00
default: config.defaultRoomSettings.color2,
2021-04-12 23:29:02 +02:00
allowedChange: true,
required: false
},
lobby: {
2023-04-17 01:34:32 +02:00
type: "boolean",
2021-04-12 23:29:02 +02:00
allowedChange: false,
required: false
},
visible: {
2023-04-17 01:34:32 +02:00
type: "boolean",
2021-04-12 23:29:02 +02:00
default: true,
allowedChange: true,
required: true
},
chat: {
2023-04-17 01:34:32 +02:00
type: "boolean",
2021-04-12 23:29:02 +02:00
default: true,
allowedChange: true,
required: true
},
owner_id: {
2023-04-17 01:34:32 +02:00
type: "string",
2021-04-12 23:29:02 +02:00
allowedChange: false,
required: false
},
crownsolo: {
2023-04-17 01:34:32 +02:00
type: "boolean",
2021-04-12 23:29:02 +02:00
default: false,
allowedChange: true,
required: true
2021-05-09 10:52:41 +02:00
},
"no cussing": {
2023-04-17 01:34:32 +02:00
type: "boolean",
2021-05-09 10:52:41 +02:00
allowedChange: true,
required: false
2023-04-17 01:34:32 +02:00
},
"lyrical notes": {
type: "boolean",
allowedChange: false,
required: false
2021-04-12 23:29:02 +02:00
}
2023-04-17 01:34:32 +02:00
};
2021-04-12 23:29:02 +02:00
2023-04-17 01:34:32 +02:00
constructor(set, context) {
2021-04-12 23:29:02 +02:00
Object.keys(RoomSettings.allowedProperties).forEach(key => {
2023-04-17 01:34:32 +02:00
if (
typeof RoomSettings.allowedProperties[key].default !==
"undefined"
) {
2021-04-12 23:29:02 +02:00
if (this[key] !== RoomSettings.allowedProperties[key].default) {
this[key] = RoomSettings.allowedProperties[key].default;
}
}
});
Object.keys(RoomSettings.allowedProperties).forEach(key => {
if (RoomSettings.allowedProperties[key].required == true) {
2023-04-17 01:34:32 +02:00
if (typeof this[key] == "undefined") {
2021-04-12 23:29:02 +02:00
this[key] = RoomSettings.allowedProperties[key].default;
}
}
});
2023-04-17 01:34:32 +02:00
if (typeof set !== "undefined") {
2021-04-12 23:29:02 +02:00
Object.keys(set).forEach(key => {
2023-04-17 01:34:32 +02:00
if (typeof set[key] == "undefined") return;
if (
Object.keys(RoomSettings.allowedProperties).indexOf(key) !==
-1
) {
if (typeof context == "undefined") {
this[key] = this.verifyPropertyType(
key,
set[key],
RoomSettings.allowedProperties[key].type
);
2021-04-12 23:29:02 +02:00
} else {
2023-04-17 01:34:32 +02:00
if (context == "user") {
if (
RoomSettings.allowedProperties[key]
.allowedChange
) {
this[key] = this.verifyPropertyType(
key,
set[key],
RoomSettings.allowedProperties[key].type
);
2021-04-12 23:29:02 +02:00
}
}
}
}
});
}
}
verifyPropertyType(key, pr, type) {
let ret;
2023-04-17 01:34:32 +02:00
if (typeof RoomSettings.allowedProperties[key] !== "object") return;
2021-04-12 23:29:02 +02:00
switch (type) {
2023-04-17 01:34:32 +02:00
case "color":
2021-04-12 23:29:02 +02:00
if (/^#[0-9a-f]{6}$/i.test(pr)) {
ret = pr;
} else {
ret = RoomSettings.allowedProperties[key].default;
}
break;
2023-04-17 01:34:32 +02:00
case "color2":
2021-04-12 23:29:02 +02:00
if (/^#[0-9a-f]{6}$/i.test(pr)) {
ret = pr;
} else {
ret = RoomSettings.allowedProperties[key].default;
}
break;
default:
2023-04-17 01:34:32 +02:00
if (typeof pr == type) {
2021-04-12 23:29:02 +02:00
ret = pr;
2023-04-17 01:34:32 +02:00
} else if (
typeof RoomSettings.allowedProperties[key].default !==
"undefined"
) {
2021-04-12 23:29:02 +02:00
ret = RoomSettings.allowedProperties[key].default;
} else {
ret = undefined;
}
break;
}
return ret;
}
changeSettings(set) {
Object.keys(set).forEach(key => {
if (RoomSettings.allowedProperties[key].allowedChange) {
2023-04-17 01:34:32 +02:00
this[key] = this.verifyPropertyType(
key,
set[key],
RoomSettings.allowedProperties[key].type
);
2021-04-12 23:29:02 +02:00
}
});
}
2022-06-17 08:36:49 +02:00
static changeSettings(set, admin) {
2021-04-12 23:29:02 +02:00
Object.keys(set).forEach(key => {
2023-04-17 01:34:32 +02:00
if (
RoomSettings.allowedProperties[key].allowedChange ||
admin == true
) {
set[key] = RoomSettings.verifyPropertyType(
key,
set[key],
RoomSettings.allowedProperties[key].type
);
2021-04-12 23:29:02 +02:00
}
});
return set;
}
static verifyPropertyType(key, pr, type) {
let ret;
2023-04-17 01:34:32 +02:00
if (typeof RoomSettings.allowedProperties[key] !== "object") return;
2021-04-12 23:29:02 +02:00
switch (type) {
2023-04-17 01:34:32 +02:00
case "color":
2021-04-12 23:29:02 +02:00
if (/^#[0-9a-f]{6}$/i.test(pr)) {
ret = pr;
} else {
ret = RoomSettings.allowedProperties[key].default;
}
break;
2023-04-17 01:34:32 +02:00
case "color2":
2021-04-12 23:29:02 +02:00
if (/^#[0-9a-f]{6}$/i.test(pr)) {
ret = pr;
} else {
ret = RoomSettings.allowedProperties[key].default;
}
break;
default:
2023-04-17 01:34:32 +02:00
if (typeof pr == type) {
2021-04-12 23:29:02 +02:00
ret = pr;
2023-04-17 01:34:32 +02:00
} else if (
typeof RoomSettings.allowedProperties[key].default !==
"undefined"
) {
2021-04-12 23:29:02 +02:00
ret = RoomSettings.allowedProperties[key].default;
} else {
ret = undefined;
}
break;
}
return ret;
}
}
module.exports = RoomSettings;