From 769ccf7bd9b0a8281c3bea0b284852cd1d66a869 Mon Sep 17 00:00:00 2001 From: flifloo Date: Mon, 1 Jun 2020 17:32:49 +0200 Subject: [PATCH] Fiw service and WIP commands --- public/javascripts/kitchen.js | 19 ++++++++++++------- public/javascripts/service.js | 1 - sockets/WIPCommand.js | 14 +++++++------- sockets/set/setService.js | 8 ++++++-- sockets/utils.js | 2 +- views/commands.pug | 2 ++ 6 files changed, 28 insertions(+), 18 deletions(-) diff --git a/public/javascripts/kitchen.js b/public/javascripts/kitchen.js index 4c55da4..aa87a96 100644 --- a/public/javascripts/kitchen.js +++ b/public/javascripts/kitchen.js @@ -5,6 +5,9 @@ const WIP = document.getElementById("WIP"); const done = document.getElementById("done"); const waiting = document.getElementById("waiting"); +let service = {}; + + function addCmd(c) { done.insertAdjacentHTML("beforeend", `

${c.number}

@@ -54,14 +57,14 @@ function wait(e) { } function waiter() { - if (WIP.children.length < 3) { + let limit = Object.keys(service).filter(x=>x.startsWith("sandwich") && service[x]).length; + if (WIP.children.length < limit) { let i; - if (waiting.children.length < 3 - WIP.children.length) + if (waiting.children.length < limit - WIP.children.length) i = waiting.children.length; else - i = 3 - WIP.children.length; - for (i-=1; i >= 0; i--) - socket.emit("WIP command", waiting.children[i].querySelector("h1").innerHTML); + i = limit - WIP.children.length; + socket.emit("WIP command", waiting.children[i-1].querySelector("h1").innerHTML); } } @@ -84,10 +87,12 @@ socket.on("list command", data => { }); socket.on("list service", async data => { - if (!data || Object.keys(data).length === 0) { + if (!data || Object.keys(data).length === 0) await alert("No service set !"); - } else + else { + service = data; socket.emit("list command"); + } }); socket.on("set service", () => { diff --git a/public/javascripts/service.js b/public/javascripts/service.js index 2eec74f..d0f49f7 100644 --- a/public/javascripts/service.js +++ b/public/javascripts/service.js @@ -20,7 +20,6 @@ async function next() { await addUser(usersAdd.pop()); else socket.emit("set service", users); - console.log(users) } function hinter(ev) { diff --git a/sockets/WIPCommand.js b/sockets/WIPCommand.js index 1432fbc..79c0348 100644 --- a/sockets/WIPCommand.js +++ b/sockets/WIPCommand.js @@ -12,22 +12,22 @@ module.exports = socket => { throw new Error("Service not found"); for (let sn of ["sandwich1", "sandwich2", "sandwich3"]) { - console.log(sn + " " + s[sn + "Busy"]) - if (!s[sn + "Busy"]) { - s[sn + "Busy"] = true; - sandwich = s[sn]; + await s.reload(); + if (!c.WIP && s[sn] && !s[sn + "Busy"]) { + sandwich = sn; break; } } if (sandwich) { + s[sandwich + "Busy"] = true; + await c.setSandwich(s[sandwich]); c.WIP = true; - await c.setSandwich(sandwich); - await c.save(); await s.save(); + await c.save(); let send = { number: data, - sandwich: sandwich.username + sandwich: s[sandwich].username } socket.emit("WIP command", send); socket.broadcast.emit("WIP command", send); diff --git a/sockets/set/setService.js b/sockets/set/setService.js index 84c7e4d..14bb7df 100644 --- a/sockets/set/setService.js +++ b/sockets/set/setService.js @@ -7,9 +7,13 @@ module.exports = socket => { if (!s) s = await models.Service.create(); - for (let u in data) - await s["set"+u.charAt(0).toUpperCase()+u.slice(1)](data[u]); + for (let u in data) { + if (!(s[u] && s[u].username === data[u]) || !data[u]) + s[u + "Busy"] = false; + await s["set" + u.charAt(0).toUpperCase() + u.slice(1)](data[u]); + } + await s.save(); socket.emit("set service", data); socket.broadcast.emit("set service", data); } catch (e) { diff --git a/sockets/utils.js b/sockets/utils.js index 38c1320..3d7bc69 100644 --- a/sockets/utils.js +++ b/sockets/utils.js @@ -80,7 +80,7 @@ async function resetService(c) { let service = await models.Service.findOne({where:{date:{[models.Sequelize.Op.eq]: new Date()}}, include: ["sandwich1", "sandwich2", "sandwich3"]}); if (c.WIP && service) { for (let sn of ["sandwich1", "sandwich2", "sandwich3"]) { - if (service[sn].username === c.sandwichUsername) { + if (service[sn] && service[sn].username === c.sandwichUsername) { service[sn + "Busy"] = false; await service.save(); break; diff --git a/views/commands.pug b/views/commands.pug index 505b5a1..46852fb 100644 --- a/views/commands.pug +++ b/views/commands.pug @@ -9,6 +9,8 @@ block content li Menu a(href="/service" target="popup") li Service + a(href="/kitchen") + li Kitchen a(href="#popup") li#logout Disconnect