types to class

This commit is contained in:
Ethanell 2021-03-08 19:42:32 +01:00
parent 2d3fe0df9b
commit 073189e04d
10 changed files with 119 additions and 33 deletions

View file

@ -7,7 +7,6 @@ socket.on("connected", () => {
});
socket.on("getDeck", d => {
console.log(d);
let data = d.data, name = d.name;
deck.innerHTML = "";

View file

@ -1,27 +1,22 @@
const db = require("../db.json");
const getSlot = require("../types").getSlot;
module.exports = socket => {
return data => {
let err = null;
if (data && Array.isArray(data) && data.length === 3) {
if (db.decks[data[0]]) {
const el = db.decks[data[0]].rows[data[1]][data[2]];
const el = getSlot(...data);
if (el) {
try {
const type = require("../types/" + el.type);
type.trigger(el, socket);
el.trigger(socket);
} catch (exc) {
if (exc.code === "MODULE_NOT_FOUND")
err = "typeNotFound";
else {
err = "unknown";
console.error(exc)
}
err = "unknown";
console.error(exc)
}
} else {
} else
err = "notFound";
}
} else
err = "deckNotFound";
} else

29
types/Base.js Normal file
View file

@ -0,0 +1,29 @@
/**
* @abstract
*/
class Base {
text;
image;
options;
constructor(text, image = null, options = null) {
this.text = text;
this.image = image;
this.options = options;
}
trigger() {
return;
};
toJSON(type) {
return {
"text": this.text,
"image": this.image,
"type": type,
"options": this.options
}
}
}
module.exports = Base;

28
types/Deck.js Normal file
View file

@ -0,0 +1,28 @@
const Base = require("./Base");
const db = require("../db.json");
class Deck extends Base {
constructor(text, image = null, options = null) {
super(text, image, options);
}
/**
* @override
*/
toJSON() {
return super.toJSON("deck")
}
/**
* @override
*/
trigger(socket) {
if (typeof this.options === "string") {
socket.emit("getDeck", {name: this.options, data: db.decks[this.options]});
} else
socket.emit("trigger", {error: "invalidOptions"});
}
}
module.exports = Deck;

27
types/ExecCommand.js Normal file
View file

@ -0,0 +1,27 @@
const Base = require("./Base");
const { exec } = require('child_process');
class ExecCommand extends Base {
constructor(text, image = null, options = null) {
super(text, image, options);
}
/**
* @override
*/
toJSON() {
return super.toJSON("execCommand")
}
/**
* @override
*/
trigger() {
exec(this.options, (err) => {
if (err)
console.error(err);
});
}
}
module.exports = ExecCommand;

View file

@ -1,10 +0,0 @@
const db = require("../db.json");
module.exports.trigger = (el, socket) => {
if (typeof el.options === "string") {
socket.emit("getDeck", {name: el.options, data: db.decks[el.options]});
}
else
socket.emit("trigger", {error: "invalidOptions"});
};

View file

@ -1,8 +0,0 @@
const { exec } = require('child_process');
module.exports.trigger = el => {
exec(el.options, (err) => {
if (err)
console.error(err);
});
};

25
types/index.js Normal file
View file

@ -0,0 +1,25 @@
const db = require("../db.json");
const types = {
"execCommand": require("./ExecCommand"),
"deck": require("./Deck")
};
module.exports.types = types;
/**
*
* @param name
* @param x
* @param y
* @returns {null|Base}
*/
module.exports.getSlot = (name, x, y) => {
if (name in db.decks)
if (x in db.decks[name].rows)
if (y in db.decks[name].rows[x]) {
let d = db.decks[name].rows[x][y];
return new types[d.type](d.text, d.image, d.options);
}
return null;
};

View file

@ -3,3 +3,5 @@ extends layout
block content
h1.center OpenDeck
.container#deck
script(src="/javascripts/index.js")

View file

@ -3,8 +3,7 @@ html
head
title= "OpenDeck" + (title ? " - " + title: "")
link(rel="stylesheet", href="/stylesheets/style.css")
body
block content
script(src="/javascripts/materialize.js")
script(src="/socket.io/socket.io.js")
script(src="/javascripts/index.js")
body
block content