Add multi/sub decks

This commit is contained in:
Ethanell 2021-03-08 15:22:03 +01:00
parent 8de882f012
commit 2d3fe0df9b
4 changed files with 42 additions and 19 deletions

View file

@ -6,7 +6,9 @@ socket.on("connected", () => {
socket.emit("getDeck"); socket.emit("getDeck");
}); });
socket.on("getDeck", data => { socket.on("getDeck", d => {
console.log(d);
let data = d.data, name = d.name;
deck.innerHTML = ""; deck.innerHTML = "";
for (let x = 0; x < data.x; x++) { 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 [x, cols] of Object.entries(data.rows))
for (const [y, col] of Object.entries(cols)) { 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 (e) {
if (col.image) if (col.image)
e.insertAdjacentHTML("beforeend", `<img src="${col.image}" alt="${col.text}">`); e.insertAdjacentHTML("beforeend", `<img src="${col.image}" alt="${col.text}">`);
@ -31,7 +33,7 @@ socket.on("getDeck", data => {
e.addEventListener("click", ev => { e.addEventListener("click", ev => {
ev.stopPropagation(); ev.stopPropagation();
socket.emit("trigger", [x, y]); socket.emit("trigger", [name, x, y]);
}) })
} }
} }

View file

@ -1,5 +1,10 @@
const db = require("../db.json");
module.exports = socket => { module.exports = socket => {
return () => { return name => {
socket.emit("getDeck", require("../db.json").deck); if (!name)
name = "default";
socket.emit("getDeck", {name: name, data: db.decks[name]});
} }
}; };

View file

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

10
types/deck.js Normal file
View file

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