OpenDeck/types/Base.js

65 lines
1.4 KiB
JavaScript
Raw Permalink Normal View History

const db = require("../db.json");
const fs = require("fs");
2021-03-08 19:42:32 +01:00
/**
* @abstract
*/
class Base {
static name = "Base";
2021-03-08 19:42:32 +01:00
constructor(text, image = null, options = null) {
this.text = text;
this.image = image;
this.options = options;
}
trigger() {
return;
};
save(name, position) {
if (!(position[0] in db.decks[name].rows))
db.decks[name].rows[position[0]] = {};
db.decks[name].rows[position[0]][position[1]] = this.toJSON();
Base.write()
}
static saveConfig(type, configuration) {
db.types[type] = configuration;
Base.write();
2021-03-09 08:23:20 +01:00
}
remove(name, position) {
if (position[0] in db.decks[name].rows && position[1] in db.decks[name].rows[position[0]]) {
delete db.decks[name].rows[position[0]][position[1]];
Base.write();
2021-03-09 08:23:20 +01:00
return true;
}
return false;
}
static write() {
fs.writeFileSync("./db.json", JSON.stringify(db));
}
static staticToJSON(name, type, fields, config) {
return {
"name": name,
"type": type,
"fields": fields,
"configuration": config
}
}
2021-03-08 19:42:32 +01:00
toJSON(type) {
return {
"text": this.text,
"image": this.image,
"type": type,
"options": this.options
}
}
}
module.exports = Base;