OpenDeck/sockets/setSlot.js

36 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

const { getSlot, types } = require("../types");
module.exports = socket => {
return data => {
2021-03-09 08:23:20 +01:00
if (data.name && data.position) {
let s = getSlot(data.name, ...data.position);
if (data.data) {
try {
if (!s)
s = new types[data.data.type](data.data.text, data.data.image, data.data.options);
else {
s.text = data.data.text;
s.image = data.data.image;
s.options = data.data.options
}
s.save(data.name, data.position);
} catch (err) {
console.error(err);
socket.emit("setSlot", {error: err.code});
return;
}
} else if (s)
s.remove(data.name, data.position);
else {
2021-03-09 08:23:20 +01:00
socket.emit("setSlot", {error: "emptySlot"});
return;
}
socket.emit("setSlot", data);
socket.broadcast.emit("setSlot", data);
2021-03-09 08:23:20 +01:00
} else
socket.emit("setSlot", {error: "invalidData"});
}
};