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"}); +};