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.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", `<img src="${col.image}" alt="${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]);
})
}
}

View file

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

View file

@ -3,8 +3,10 @@ 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 (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);
@ -12,12 +14,16 @@ module.exports = socket => {
} catch (exc) {
if (exc.code === "MODULE_NOT_FOUND")
err = "typeNotFound";
else
else {
err = "unknown";
console.error(exc)
}
}
} else {
err = "notFound";
}
} else
err = "deckNotFound";
} else
err = "badData";
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"});
};