From bad1e3876016c3722dfb7544b0853f247ee0c5a7 Mon Sep 17 00:00:00 2001 From: flifloo Date: Sat, 30 May 2020 21:40:36 +0200 Subject: [PATCH] Add done and WIP command, fix kitchen page --- models/service.js | 15 ++++++ public/javascripts/commands.js | 6 +-- public/javascripts/kitchen.js | 92 ++++++++++++++++++---------------- public/javascripts/service.js | 4 +- sockets/WIPCommand.js | 40 +++++++++++++++ sockets/clearCommand.js | 2 - sockets/doneCommand.js | 23 +++++++++ sockets/giveCommand.js | 2 +- sockets/index.js | 4 ++ sockets/utils.js | 13 +++-- 10 files changed, 147 insertions(+), 54 deletions(-) create mode 100644 sockets/WIPCommand.js create mode 100644 sockets/doneCommand.js diff --git a/models/service.js b/models/service.js index bb9d5de..917a920 100644 --- a/models/service.js +++ b/models/service.js @@ -5,6 +5,21 @@ module.exports = (sequelize, DataTypes) => { type: DataTypes.DATEONLY, defaultValue: DataTypes.NOW, primaryKey: true + }, + sandwich1Busy: { + type: DataTypes.BOOLEAN, + defaultValue: false, + allowNull: false + }, + sandwich2Busy: { + type: DataTypes.BOOLEAN, + defaultValue: false, + allowNull: false + }, + sandwich3Busy: { + type: DataTypes.BOOLEAN, + defaultValue: false, + allowNull: false } }, { tableName: "Services" diff --git a/public/javascripts/commands.js b/public/javascripts/commands.js index 8fb87bc..4b4b252 100644 --- a/public/javascripts/commands.js +++ b/public/javascripts/commands.js @@ -323,11 +323,11 @@ socket.on("clear command", data => { clear(document.querySelector(`.list #cmd${data}`)) }); -socket.on("WIPed command", data => { - WIP(document.querySelector(`.list #cmd${data.id}`), data.sandwich) +socket.on("WIP command", data => { + WIP(document.querySelector(`.list #cmd${data.number}`), data.sandwich) }); -socket.on("finish command", data => { +socket.on("done command", data => { done(document.querySelector(`.list #cmd${data}`)) }); diff --git a/public/javascripts/kitchen.js b/public/javascripts/kitchen.js index 495d256..31ec5eb 100644 --- a/public/javascripts/kitchen.js +++ b/public/javascripts/kitchen.js @@ -1,22 +1,25 @@ -let socket = io(); -let WIP = document.getElementById("encours"); -let done = document.getElementById("realisee"); -let waiting = document.getElementById("attente"); +const socket = io(); +const WIP = document.getElementById("WIP"); +const done = document.getElementById("done"); +const waiting = document.getElementById("waiting"); -function addcmd(id, plate, ingredient, sauce, drink, dessert, state, client, sandwich) { - for (let i of ["plate", "ingredient", "sauce", "drink", "dessert", "state", "sandwich"]) - if (!eval(i)) - eval(`${i} = ""`); - done.insertAdjacentHTML("beforeend", `

${id}

${sandwich}

${client}

${plate} | ${ingredient}

${sauce}

${drink}

${dessert}

`); - let e = document.getElementById(`cmd${id}`); - switch (state) { - case "WIP": - WIPed(e, sandwich); - break; - case "waiting": - wait(e); - break; - } +function addCmd(c) { + done.insertAdjacentHTML("beforeend", `
+

${c.number}

+

${c.sandwich}

+

${c.client}

+

${c.dish} | ${c.ingredient}

+

${c.sauce}

+

${c.drink}

+

${c.dessert}

+
`); + let e = document.getElementById(`cmd${c.number}`); + if (c.error || c.give || c.done) + finish(e) + else if (c.WIP) + WIPed(e, c.sandwich) + else + wait(e) } function WIPed(e, name) { @@ -32,8 +35,8 @@ function WIPed(e, name) { WIP.children[names.indexOf(name)-1].insertAdjacentHTML("afterend", e.outerHTML); } - WIP.querySelector(`#${e.id}`).addEventListener("click", ev => { - socket.emit("done command", {"id": parseInt(e.id.replace("cmd", ""))}); + WIP.querySelector(`#${e.id}`).addEventListener("click", () => { + socket.emit("done command", parseInt(e.id.replace("cmd", ""))); }); e.remove(); } @@ -55,16 +58,13 @@ function waiter() { i = waiting.children.length; else i = 3 - WIP.children.length; - for (i-=1; i >= 0; i--) { - socket.emit("WIP command", {"id": waiting.children[i].querySelector("h1").innerHTML}) - } + for (i-=1; i >= 0; i--) + socket.emit("WIP command", waiting.children[i].querySelector("h1").innerHTML); } } -socket.on("connect", data => { - if (data === "ok") { - socket.emit("list service"); - } +socket.on("connected", () => { + socket.emit("list service"); }); socket.on("list command", data => { @@ -75,49 +75,57 @@ socket.on("list command", data => { child = e.lastElementChild; } } - for (let c of data.list) { - addcmd(c.id, c.plate, c.ingredient, c.sauce, c.drink, c.dessert, c.state, c.client,c.sandwich); + for (let c of data) { + addCmd(c); } waiter(); }); socket.on("list service", data => { - if (Object.keys(data).length === 0) + if (!data || Object.keys(data).length === 0) { alert("No service set !"); - else + } else socket.emit("list command"); }); +socket.on("set service", () => { + socket.emit("list service"); +}) + socket.on("new command", data => { - addcmd(data.id, data.plate, data.ingredient, data.sauce, data.drink, data.dessert, data.state, data.client, data.sandwich); + addCmd(data); waiter(); }); -socket.on("cleared command", data => { - wait(document.getElementById((`cmd${data.id}`))); +socket.on("clear command", data => { + wait(document.getElementById((`cmd${data}`))); waiter(); }); -socket.on("WIPed command", data => { - WIPed(document.getElementById((`cmd${data.id}`)), data.sandwich); +socket.on("WIP command", data => { + WIPed(document.getElementById((`cmd${data.number}`)), data.sandwich); waiter(); }); -socket.on("finish command", data => { - finish(document.getElementById((`cmd${data.id}`))); +socket.on("done command", data => { + finish(document.getElementById((`cmd${data}`))); waiter(); }); -socket.on("gave command", data => { - finish(document.getElementById((`cmd${data.id}`))); +socket.on("give command", data => { + finish(document.getElementById((`cmd${data}`))); waiter(); }); -socket.on("glitched command", data => { - finish(document.getElementById(`cmd${data.id}`)); +socket.on("error command", data => { + finish(document.getElementById(`cmd${data}`)); waiter(); }); +socket.on("internal error", () => { + alert("An error occurred !"); +}) + document.addEventListener("keyup", ev => { let keys = [["Digit1", "Numpad1"], ["Digit2", "Numpad2"], ["Digit3", "Numpad3"]]; for (let i=0; i { socket.on("set service", () => { console.log("close !") window.close(); -}) +}); socket.on("fail add user", data => { alert("User creation fail !"); @@ -76,7 +76,7 @@ socket.on("fail add user", data => { socket.on("internal error", () => { alert("An error occurred !"); -}) +}); document.querySelectorAll("input[type='text']").forEach(e => { e.addEventListener("keyup", ev => {hinter(ev)}) diff --git a/sockets/WIPCommand.js b/sockets/WIPCommand.js new file mode 100644 index 0000000..1432fbc --- /dev/null +++ b/sockets/WIPCommand.js @@ -0,0 +1,40 @@ +const models = require("../models"); + +module.exports = socket => { + return async (data) => { + try { + let sandwich; + let c = await models.Command.findOne({where: {number: {[models.Sequelize.Op.eq]: data}, date: {[models.Sequelize.Op.eq]: new Date()}}}); + let s = await models.Service.findOne({where:{date:{[models.Sequelize.Op.eq]: new Date()}}, include: ["sandwich1", "sandwich2", "sandwich3"]}); + if (!c) + throw new Error("Command not found") + else if (!s) + 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]; + break; + } + } + + if (sandwich) { + c.WIP = true; + await c.setSandwich(sandwich); + await c.save(); + await s.save(); + let send = { + number: data, + sandwich: sandwich.username + } + socket.emit("WIP command", send); + socket.broadcast.emit("WIP command", send); + } + } catch (e) { + socket.emit("internal error"); + console.error(e); + } + } +} diff --git a/sockets/clearCommand.js b/sockets/clearCommand.js index e9ceaf6..334b839 100644 --- a/sockets/clearCommand.js +++ b/sockets/clearCommand.js @@ -1,5 +1,4 @@ const models = require("../models"); -const utils = require("./utils"); module.exports = socket => { return async (data) => { @@ -11,7 +10,6 @@ module.exports = socket => { c.give = null; c.error = false; - utils.resetService(c, await models.Service.findOne({where:{date:{[models.Sequelize.Op.eq]: new Date()}}})); c.WIP = false; await c.save(); diff --git a/sockets/doneCommand.js b/sockets/doneCommand.js new file mode 100644 index 0000000..2eb8ce2 --- /dev/null +++ b/sockets/doneCommand.js @@ -0,0 +1,23 @@ +const models = require("../models"); +const utils = require("./utils"); + +module.exports = socket => { + return async (data) => { + try { + let c = await models.Command.findOne({where: {number: {[models.Sequelize.Op.eq]: data}, date: {[models.Sequelize.Op.eq]: new Date()}}}); + if (!c) + throw new Error("Command not found"); + + c.done = new Date(); + await utils.resetService(c); + c.WIP = false; + + await c.save(); + socket.emit("done command", data); + socket.broadcast.emit("done command", data); + } catch (e) { + socket.emit("internal error"); + console.error(e); + } + } +} diff --git a/sockets/giveCommand.js b/sockets/giveCommand.js index 880feb3..d42277f 100644 --- a/sockets/giveCommand.js +++ b/sockets/giveCommand.js @@ -10,7 +10,7 @@ module.exports = socket => { c.give = new Date() - utils.resetService(c, await models.Service.findOne({where:{date:{[models.Sequelize.Op.eq]: new Date()}}})); + await utils.resetService(c); c.WIP = false; await c.save(); diff --git a/sockets/index.js b/sockets/index.js index f368407..a81cfe8 100644 --- a/sockets/index.js +++ b/sockets/index.js @@ -10,6 +10,8 @@ const addCommand = require("./addCommand"); const addUSer = require("./addUser"); const setService = require("./setService"); const giveCommand = require("./giveCommand"); +const WIPCommand = require("./WIPCommand"); +const doneCommand = require("./doneCommand"); const errorCommand = require("./errorCommand"); const clearCommand = require("./clearCommand"); const price = require("./price") @@ -28,6 +30,8 @@ module.exports = socket => { socket.on("add user", addUSer(socket)); socket.on("set service", setService(socket)); socket.on("give command", giveCommand(socket)); + socket.on("WIP command", WIPCommand(socket)); + socket.on("done command", doneCommand(socket)); socket.on("error command", errorCommand(socket)); socket.on("clear command", clearCommand(socket)); socket.on("price", price(socket)); diff --git a/sockets/utils.js b/sockets/utils.js index 138e076..59fd784 100644 --- a/sockets/utils.js +++ b/sockets/utils.js @@ -17,11 +17,16 @@ function commandExport (c) { } } -function resetService(c, service) { +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) { - let sandwiches = [service.sandwich1Id, service.sandwich2Id, service.sandwich3Id] - if (c.sandwichId in sandwiches) - service["sandwich"+sandwiches.indexOf(c.sandwichId)+1] = false; + for (let sn of ["sandwich1", "sandwich2", "sandwich3"]) { + if (service[sn].username === c.sandwichUsername) { + service[sn + "Busy"] = false; + await service.save(); + break; + } + } } }