2024-07-25 00:25:11 +03:00
|
|
|
import server from "./server.js";
|
|
|
|
|
2024-12-19 20:44:21 +02:00
|
|
|
type OptionValue = number | string;
|
2024-07-25 00:25:11 +03:00
|
|
|
|
|
|
|
class Options {
|
2024-07-25 19:27:42 +03:00
|
|
|
initializedPromise: Promise<void>;
|
2024-07-25 00:25:11 +03:00
|
|
|
private arr!: Record<string, OptionValue>;
|
|
|
|
|
|
|
|
constructor() {
|
2025-01-09 18:07:02 +02:00
|
|
|
this.initializedPromise = server.get<Record<string, OptionValue>>("options").then((data) => this.load(data));
|
2024-07-25 00:25:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
load(arr: Record<string, OptionValue>) {
|
|
|
|
this.arr = arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
get(key: string) {
|
2025-01-07 12:34:10 +02:00
|
|
|
return this.arr?.[key] as string;
|
2024-07-25 00:25:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
getNames() {
|
|
|
|
return Object.keys(this.arr || []);
|
|
|
|
}
|
|
|
|
|
2025-01-07 12:34:10 +02:00
|
|
|
getJson(key: string) {
|
2024-07-25 00:25:11 +03:00
|
|
|
const value = this.arr?.[key];
|
|
|
|
if (typeof value !== "string") {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
return JSON.parse(value);
|
2025-01-09 18:07:02 +02:00
|
|
|
} catch (e) {
|
2024-07-25 00:25:11 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getInt(key: string) {
|
|
|
|
const value = this.arr?.[key];
|
2025-01-18 12:54:59 +02:00
|
|
|
if (typeof value === "number") {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
if (typeof value == "string") {
|
|
|
|
return parseInt(value);
|
2024-07-25 00:25:11 +03:00
|
|
|
}
|
2025-01-18 12:54:59 +02:00
|
|
|
console.warn("Attempting to read int for unsupported value: ", value);
|
|
|
|
return null;
|
2024-07-25 00:25:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
getFloat(key: string) {
|
|
|
|
const value = this.arr?.[key];
|
|
|
|
if (typeof value !== "string") {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return parseFloat(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
is(key: string) {
|
2025-01-09 18:07:02 +02:00
|
|
|
return this.arr[key] === "true";
|
2024-07-25 00:25:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
set(key: string, value: OptionValue) {
|
|
|
|
this.arr[key] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
async save(key: string, value: OptionValue) {
|
|
|
|
this.set(key, value);
|
|
|
|
|
|
|
|
const payload: Record<string, OptionValue> = {};
|
|
|
|
payload[key] = value;
|
|
|
|
|
|
|
|
await server.put(`options`, payload);
|
|
|
|
}
|
|
|
|
|
|
|
|
async toggle(key: string) {
|
|
|
|
await this.save(key, (!this.is(key)).toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const options = new Options();
|
|
|
|
|
2025-01-07 12:34:10 +02:00
|
|
|
export default options;
|