diff --git a/public/javascripts/index.js b/public/javascripts/index.js index c6bbc29..eaf46ed 100644 --- a/public/javascripts/index.js +++ b/public/javascripts/index.js @@ -7,7 +7,6 @@ socket.on("connected", () => { }); socket.on("getDeck", d => { - console.log(d); let data = d.data, name = d.name; deck.innerHTML = ""; diff --git a/sockets/trigger.js b/sockets/trigger.js index 858eab0..444de9c 100644 --- a/sockets/trigger.js +++ b/sockets/trigger.js @@ -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 diff --git a/types/Base.js b/types/Base.js new file mode 100644 index 0000000..10d8e3f --- /dev/null +++ b/types/Base.js @@ -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; diff --git a/types/Deck.js b/types/Deck.js new file mode 100644 index 0000000..b0289a3 --- /dev/null +++ b/types/Deck.js @@ -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; diff --git a/types/ExecCommand.js b/types/ExecCommand.js new file mode 100644 index 0000000..d63b4ca --- /dev/null +++ b/types/ExecCommand.js @@ -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; diff --git a/types/deck.js b/types/deck.js deleted file mode 100644 index f003ff5..0000000 --- a/types/deck.js +++ /dev/null @@ -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"}); -}; diff --git a/types/execCommand.js b/types/execCommand.js deleted file mode 100644 index ed6e227..0000000 --- a/types/execCommand.js +++ /dev/null @@ -1,8 +0,0 @@ -const { exec } = require('child_process'); - -module.exports.trigger = el => { - exec(el.options, (err) => { - if (err) - console.error(err); - }); -}; diff --git a/types/index.js b/types/index.js new file mode 100644 index 0000000..b93e8e8 --- /dev/null +++ b/types/index.js @@ -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; +}; diff --git a/views/index.pug b/views/index.pug index c329427..7254d4f 100644 --- a/views/index.pug +++ b/views/index.pug @@ -3,3 +3,5 @@ extends layout block content h1.center OpenDeck .container#deck + + script(src="/javascripts/index.js") diff --git a/views/layout.pug b/views/layout.pug index c602f58..bbaa814 100644 --- a/views/layout.pug +++ b/views/layout.pug @@ -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