From 2d3fe0df9b5d67f9141854911bfcf4da5a92135d Mon Sep 17 00:00:00 2001 From: flifloo Date: Mon, 8 Mar 2021 15:22:03 +0100 Subject: [PATCH] Add multi/sub decks --- public/javascripts/index.js | 8 +++++--- sockets/getDeck.js | 9 +++++++-- sockets/trigger.js | 34 ++++++++++++++++++++-------------- types/deck.js | 10 ++++++++++ 4 files changed, 42 insertions(+), 19 deletions(-) create mode 100644 types/deck.js diff --git a/public/javascripts/index.js b/public/javascripts/index.js index c146505..c6bbc29 100644 --- a/public/javascripts/index.js +++ b/public/javascripts/index.js @@ -6,7 +6,9 @@ socket.on("connected", () => { socket.emit("getDeck"); }); -socket.on("getDeck", data => { +socket.on("getDeck", d => { + console.log(d); + let data = d.data, name = d.name; deck.innerHTML = ""; for (let x = 0; x < data.x; x++) { @@ -22,7 +24,7 @@ socket.on("getDeck", data => { 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}`); + let e = document.getElementById(`r${x}c${y}`); if (e) { if (col.image) e.insertAdjacentHTML("beforeend", `${col.text}`); @@ -31,7 +33,7 @@ socket.on("getDeck", data => { e.addEventListener("click", ev => { ev.stopPropagation(); - socket.emit("trigger", [x, y]); + socket.emit("trigger", [name, x, y]); }) } } diff --git a/sockets/getDeck.js b/sockets/getDeck.js index a7a8421..34480da 100644 --- a/sockets/getDeck.js +++ b/sockets/getDeck.js @@ -1,5 +1,10 @@ +const db = require("../db.json"); + + module.exports = socket => { - return () => { - socket.emit("getDeck", require("../db.json").deck); + return name => { + if (!name) + name = "default"; + socket.emit("getDeck", {name: name, data: db.decks[name]}); } }; diff --git a/sockets/trigger.js b/sockets/trigger.js index 3142cd6..858eab0 100644 --- a/sockets/trigger.js +++ b/sockets/trigger.js @@ -3,21 +3,27 @@ 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"; + if (data && Array.isArray(data) && data.length === 3) { + if (db.decks[data[0]]) { + const el = db.decks[data[0]].rows[data[1]][data[2]]; + + 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"; + console.error(exc) + } + } + } else { + err = "notFound"; } - } else { - err = "notFound"; - } + } else + err = "deckNotFound"; } else err = "badData"; if (err) diff --git a/types/deck.js b/types/deck.js new file mode 100644 index 0000000..f003ff5 --- /dev/null +++ b/types/deck.js @@ -0,0 +1,10 @@ +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"}); +};