From 8de882f012f90cfc2a5712f0278b442ab0327239 Mon Sep 17 00:00:00 2001 From: flifloo Date: Mon, 8 Mar 2021 14:16:18 +0100 Subject: [PATCH] Add deck structure and basic trigger --- public/javascripts/index.js | 40 ++++++++++++++++++++++++++++++++++- public/stylesheets/style.sass | 19 ++++++++++++++++- sockets/getDeck.js | 5 +++++ sockets/index.js | 2 ++ sockets/trigger.js | 26 +++++++++++++++++++++++ types/execCommand.js | 8 +++++++ views/index.pug | 2 +- 7 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 sockets/getDeck.js create mode 100644 sockets/trigger.js create mode 100644 types/execCommand.js diff --git a/public/javascripts/index.js b/public/javascripts/index.js index fb6fe40..c146505 100644 --- a/public/javascripts/index.js +++ b/public/javascripts/index.js @@ -1,5 +1,43 @@ const socket = io(); +const deck = document.getElementById("deck"); -socket.on("connected", socket => { +socket.on("connected", () => { console.log("Connected !"); + socket.emit("getDeck"); +}); + +socket.on("getDeck", data => { + deck.innerHTML = ""; + + for (let x = 0; x < data.x; x++) { + const r = document.createElement("div"); + r.id = "r" + x; + for (let y = 0; y < data.y; y++) { + const c = document.createElement("div"); + c.id = r.id+"c"+y; + r.insertAdjacentElement("beforeend", c); + } + deck.insertAdjacentElement("beforeend", r); + } + + for (const [x, cols] of Object.entries(data.rows)) + for (const [y, col] of Object.entries(cols)) { + const e = document.getElementById(`r${x}c${y}`); + if (e) { + if (col.image) + e.insertAdjacentHTML("beforeend", `${col.text}`); + else if (col.text) + e.insertAdjacentHTML("beforeend", `

${col.text}

`); + + e.addEventListener("click", ev => { + ev.stopPropagation(); + socket.emit("trigger", [x, y]); + }) + } + } +}); + +socket.on("trigger", data => { + if (data.error) + alert(data.error) }); diff --git a/public/stylesheets/style.sass b/public/stylesheets/style.sass index 9299363..8091472 100644 --- a/public/stylesheets/style.sass +++ b/public/stylesheets/style.sass @@ -1,7 +1,24 @@ //noinspection CssUnknownTarget @import "materialize-css/sass/materialize.scss" -#deck>.row>.col +#deck p + font-size: 1.3vw + +#deck + display: flex + align-items: center + justify-content: center + +#deck>div>div border: black 3px solid margin: 0.3em text-align: center + height: 15vh + width: 15vh + display: flex + align-items: center + justify-content: center + +#deck>div>div>img + max-height: 15vh + max-width: 15vh diff --git a/sockets/getDeck.js b/sockets/getDeck.js new file mode 100644 index 0000000..a7a8421 --- /dev/null +++ b/sockets/getDeck.js @@ -0,0 +1,5 @@ +module.exports = socket => { + return () => { + socket.emit("getDeck", require("../db.json").deck); + } +}; diff --git a/sockets/index.js b/sockets/index.js index cebbe5a..496007f 100644 --- a/sockets/index.js +++ b/sockets/index.js @@ -1,4 +1,6 @@ module.exports = socket => { + socket.on("getDeck", require("./getDeck")(socket)); + socket.on("trigger", require("./trigger")(socket)); console.log("New connection !"); socket.emit("connected"); }; diff --git a/sockets/trigger.js b/sockets/trigger.js new file mode 100644 index 0000000..3142cd6 --- /dev/null +++ b/sockets/trigger.js @@ -0,0 +1,26 @@ +const db = require("../db.json"); + +module.exports = socket => { + return data => { + let err = null; + if (data && Array.isArray(data) && data.length === 2) { + const el = db.deck.rows[data[0]][data[1]]; + if (el) { + try { + const type = require("../types/" + el.type); + type.trigger(el, socket); + } catch (exc) { + if (exc.code === "MODULE_NOT_FOUND") + err = "typeNotFound"; + else + err = "unknown"; + } + } else { + err = "notFound"; + } + } else + err = "badData"; + if (err) + socket.emit("trigger", {error: err}) + } +}; diff --git a/types/execCommand.js b/types/execCommand.js new file mode 100644 index 0000000..ed6e227 --- /dev/null +++ b/types/execCommand.js @@ -0,0 +1,8 @@ +const { exec } = require('child_process'); + +module.exports.trigger = el => { + exec(el.options, (err) => { + if (err) + console.error(err); + }); +}; diff --git a/views/index.pug b/views/index.pug index 892cbb0..c329427 100644 --- a/views/index.pug +++ b/views/index.pug @@ -1,5 +1,5 @@ extends layout block content + h1.center OpenDeck .container#deck - h1.center OpenDeck