diff --git a/public/javascripts/commands.js b/public/javascripts/commands.js index 3cd66d2..4f115f1 100644 --- a/public/javascripts/commands.js +++ b/public/javascripts/commands.js @@ -15,7 +15,7 @@ function addCmd(command) { list.insertAdjacentHTML("beforeend", `
${command.dish}
@@ -34,15 +34,15 @@ function addCmd(command) { }); e.querySelector(".give").addEventListener("click", ev => { ev.stopPropagation(); - socket.emit("give command", {"id": command.number}); + socket.emit("give command", command.number); }); e.querySelector(".cancel").addEventListener("click", ev => { ev.stopPropagation(); - socket.emit("clear command", {"id": command.number}); + socket.emit("clear command", command.number); }); e.querySelector(".error").addEventListener("click", ev => { ev.stopPropagation(); - socket.emit("error command", {"id": command.number}); + socket.emit("error command", command.number); }); if (command.error) error(e) @@ -161,12 +161,12 @@ function WIP(e, name) { function done(e) { e.classList.remove("show-spec"); - e.classList.add("finis"); + e.classList.add("done"); } function give(e) { e.classList.remove("show-spec"); - e.classList.add("donnee"); + e.classList.add("give"); list.appendChild(e); } @@ -270,7 +270,7 @@ socket.on("new command", data => { addCmd(data); }); -socket.on("cleared command", data => { +socket.on("clear command", data => { clear(document.querySelector(`.list #cmd${data}`)) }); @@ -282,11 +282,11 @@ socket.on("finish command", data => { done(document.querySelector(`.list #cmd${data}`)) }); -socket.on("gave command", data => { +socket.on("give command", data => { give(document.querySelector(`.list #cmd${data}`)) }); -socket.on("glitched command", data => { +socket.on("error command", data => { error(document.querySelector(`.list #cmd${data}`)) }); diff --git a/public/stylesheets/style.css b/public/stylesheets/style.css index 3c3a8fe..24fbc1b 100644 --- a/public/stylesheets/style.css +++ b/public/stylesheets/style.css @@ -524,7 +524,7 @@ textarea.input2 + .focus-input2::after { background-color: rgb(70, 170, 185); } -.list .finis { +.list .done { background-color: rgb(185, 176, 30); } @@ -536,7 +536,7 @@ textarea.input2 + .focus-input2::after { background-color: rgb(133, 1, 1); } -.list .warning .error, .list .warning .donner{ +.list .warning .error, .list .warning .give{ display: none; } @@ -549,7 +549,7 @@ textarea.input2 + .focus-input2::after { background-color: rgb(133, 1, 1); } -.list .give .donner, .list .give .error{ +.list .give .give, .list .give .error{ display : none; } diff --git a/sockets/clearCommand.js b/sockets/clearCommand.js new file mode 100644 index 0000000..e1de5f8 --- /dev/null +++ b/sockets/clearCommand.js @@ -0,0 +1,29 @@ +const models = require("../models") + +module.exports = socket => { + return async (data) => { + try { + let c = await models.Command.findByPk(data); + if (!c) + throw new Error("Command not found"); + c.done = null; + c.give = null; + c.error = false; + + let service = models.Service.findOne({where:{date:{[models.Sequelize.Op.eq]: new Date()}}}); + if (c.WIP && service) { + let sandwichs = [service.sandwich1Id, service.sandwich2Id, service.sandwich3Id] + if (c.sandwichId in sandwichs) + service["sandwich"+sandwichs.indexOf(c.sandwichId)+1] = false; + } + c.WIP = false; + + await c.save(); + socket.emit("clear command", data); + socket.broadcast.emit("clear command", data); + } catch (e) { + socket.emit("error"); + console.error(e); + } + } +} diff --git a/sockets/errorCommand.js b/sockets/errorCommand.js new file mode 100644 index 0000000..b9596ae --- /dev/null +++ b/sockets/errorCommand.js @@ -0,0 +1,20 @@ +const models = require("../models") + +module.exports = socket => { + return async (data) => { + try { + let c = await models.Command.findByPk(data); + if (!c) + throw new Error("Command not found"); + + c.error = true; + + await c.save(); + socket.emit("error command", data); + socket.broadcast.emit("error command", data); + } catch (e) { + socket.emit("error"); + console.error(e); + } + } +} diff --git a/sockets/giveCommand.js b/sockets/giveCommand.js new file mode 100644 index 0000000..d42001c --- /dev/null +++ b/sockets/giveCommand.js @@ -0,0 +1,28 @@ +const models = require("../models") + +module.exports = socket => { + return async (data) => { + try { + let c = await models.Command.findByPk(data); + if (!c) + throw new Error("Command not found"); + + c.give = new Date() + + let service = models.Service.findOne({where:{date:{[models.Sequelize.Op.eq]: new Date()}}}); + if (c.WIP && service) { + let sandwiches = [service.sandwich1Id, service.sandwich2Id, service.sandwich3Id] + if (c.sandwichId in sandwiches) + service["sandwich"+sandwiches.indexOf(c.sandwichId)+1] = false; + } + c.WIP = false; + + await c.save(); + socket.emit("give command", data); + socket.broadcast.emit("give command", data); + } catch (e) { + socket.emit("error"); + console.error(e); + } + } +} diff --git a/sockets/index.js b/sockets/index.js index de11676..19cb7eb 100644 --- a/sockets/index.js +++ b/sockets/index.js @@ -5,6 +5,9 @@ const listSauce = require("./listSauce") const listDrink = require("./listDrink") const listDessert = require("./listDessert") const addCommand = require("./addCommand") +const giveCommand = require("./giveCommand") +const errorCommand = require("./errorCommand") +const clearCommand = require("./clearCommand") module.exports = socket => { socket.on("list command", listCommand(socket)); @@ -15,6 +18,9 @@ module.exports = socket => { socket.on("list dessert", listDessert(socket)); socket.on("list dessert", listDessert(socket)); socket.on("add command", addCommand(socket)); + socket.on("give command", giveCommand(socket)); + socket.on("error command", errorCommand(socket)); + socket.on("clear command", clearCommand(socket)); console.log("New connection !"); socket.emit("connected"); }