From fed23f424862d8e8bbe3582b76f5e54e4a146b6e Mon Sep 17 00:00:00 2001 From: flifloo Date: Fri, 29 May 2020 19:44:12 +0200 Subject: [PATCH] Fix order taking of commands --- bin/www | 2 +- models/command.js | 12 +- models/dish.js | 2 +- models/user.js | 18 +-- public/javascripts/commands.js | 266 +++++++++++++++++---------------- public/stylesheets/style.css | 61 ++++---- socket.js | 3 - sockets/addCommand.js | 51 +++++++ sockets/index.js | 20 +++ sockets/listCommand.js | 32 ++++ sockets/listDessert.js | 15 ++ sockets/listDish.js | 17 +++ sockets/listDrink.js | 15 ++ sockets/listIngredient.js | 15 ++ sockets/listSauce.js | 15 ++ views/commands.pug | 10 +- 16 files changed, 368 insertions(+), 186 deletions(-) delete mode 100644 socket.js create mode 100644 sockets/addCommand.js create mode 100644 sockets/index.js create mode 100644 sockets/listCommand.js create mode 100644 sockets/listDessert.js create mode 100644 sockets/listDish.js create mode 100644 sockets/listDrink.js create mode 100644 sockets/listIngredient.js create mode 100644 sockets/listSauce.js diff --git a/bin/www b/bin/www index 0f172b9..7415a6c 100755 --- a/bin/www +++ b/bin/www @@ -27,7 +27,7 @@ let server = http.createServer(app); */ const io = require("socket.io")(server); -io.on("connection", require("../socket")); +io.on("connection", require("../sockets")); /** * Listen on provided port, on all network interfaces. diff --git a/models/command.js b/models/command.js index ea25b53..a1c2467 100644 --- a/models/command.js +++ b/models/command.js @@ -3,7 +3,8 @@ module.exports = (sequelize, DataTypes) => { const Command = sequelize.define('Command', { number: { type: DataTypes.INTEGER, - allowNull: false + allowNull: false, + unique: "uniqueNumberPerDay" }, price: { type: DataTypes.FLOAT, @@ -12,7 +13,8 @@ module.exports = (sequelize, DataTypes) => { date: { type: DataTypes.DATEONLY, defaultValue: DataTypes.NOW, - allowNull: false + allowNull: false, + unique: "uniqueNumberPerDay" }, take: { type: DataTypes.DATE, @@ -41,13 +43,13 @@ module.exports = (sequelize, DataTypes) => { Command.associate = function(models) { // associations can be defined here Command.belongsTo(models.User, { - as: "command" + as: "client" }); Command.belongsTo(models.User, { - as: "pcCommand" + as: "pc" }); Command.belongsTo(models.User, { - as: "sandwichCommand" + as: "sandwich" }); Command.belongsTo(models.Dish); Command.belongsToMany(models.Ingredient, { diff --git a/models/dish.js b/models/dish.js index 2e5ecb3..0270fbf 100644 --- a/models/dish.js +++ b/models/dish.js @@ -22,7 +22,7 @@ module.exports = (sequelize, DataTypes) => { allowNull: false } }, { - tableName: "dishes" + tableName: "Dishes" }); Dish.associate = function(models) { Dish.hasMany(models.Command); diff --git a/models/user.js b/models/user.js index 4e39b0d..de2f0ca 100644 --- a/models/user.js +++ b/models/user.js @@ -3,33 +3,27 @@ module.exports = (sequelize, DataTypes) => { const User = sequelize.define('User', { username: { type: DataTypes.STRING, - allowNull: false + allowNull: false, + unique: true }, passwordHash: { type: DataTypes.STRING }, firstName: { type: DataTypes.STRING, - allowNull: false + allowNull: false, + unique: "userFullName" }, lastName: { type: DataTypes.STRING, - allowNull: false + allowNull: false, + unique: "userFullName" } }, { tableName: "Users" }); User.associate = function(models) { // associations can be defined here - User.hasMany(models.Command, { - as: "client" - }); - User.hasMany(models.Command, { - as: "pc" - }); - User.hasMany(models.Command, { - as: "sandwich" - }) }; return User; }; diff --git a/public/javascripts/commands.js b/public/javascripts/commands.js index 56bbffd..3cd66d2 100644 --- a/public/javascripts/commands.js +++ b/public/javascripts/commands.js @@ -1,145 +1,153 @@ -let socket = io(); -let plate = document.querySelector("#plat ul"); -let ingredient = document.querySelector("#ingredient ul"); -let sauce = document.querySelector("#sauce ul"); -let drink = document.querySelector("#boisson ul"); -let dessert = document.querySelector("#dessert ul"); -let list = document.querySelector(".liste"); -let current = {"plate": null, "ingredient": [], "sauce": [], "drink": null, "dessert": null, "price": {}}; -let radios = {"plate": null, "drink": null, "dessert": null}; -let db = {"plate": {}, "ingredient": {}, "sauce": {}, "drink": {}, "dessert": {}}; +const socket = io(); +const dish = document.querySelector("#dish ul"); +const ingredient = document.querySelector("#ingredient ul"); +const sauce = document.querySelector("#sauce ul"); +const drink = document.querySelector("#drink ul"); +const dessert = document.querySelector("#dessert ul"); +const list = document.querySelector(".list"); + +let current = {dish: null, ingredient: [], sauce: [], drink: null, dessert: null, price: {}}; +let radios = {dish: null, drink: null, dessert: null}; +let db = {dish: {}, ingredient: {}, sauce: {}, drink: {}, dessert: {}}; -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} = ""`); - list.insertAdjacentHTML("beforeend", `

${id}

${sandwich}

${client}

${plate}

${ingredient}

${sauce}

${drink}

${dessert}

`); - let e = document.querySelector(`.liste #cmd${id}`); +function addCmd(command) { + list.insertAdjacentHTML("beforeend", `
+ +

${command.number}

+
+

${command.sandwich}

+

${command.client}

+

${command.dish}

+

${command.ingredients}

+

${command.sauces}

+

${command.drink}

+

${command.dessert}

+ + +
+
`); + let e = document.querySelector(`.list #cmd${command.number}`); e.addEventListener( "click" ,ev => { ev.stopPropagation(); e.classList.toggle("show-spec"); }); - e.querySelector(".donner").addEventListener("click", ev => { + e.querySelector(".give").addEventListener("click", ev => { ev.stopPropagation(); - socket.emit("give command", {"id": id}); + socket.emit("give command", {"id": command.number}); }); - e.querySelector(".annuler").addEventListener("click", ev => { + e.querySelector(".cancel").addEventListener("click", ev => { ev.stopPropagation(); - socket.emit("clear command", {"id": id}); + socket.emit("clear command", {"id": command.number}); }); - e.querySelector(".erreur").addEventListener("click", ev => { + e.querySelector(".error").addEventListener("click", ev => { ev.stopPropagation(); - socket.emit("error command", {"id": id}); + socket.emit("error command", {"id": command.number}); }); - switch (state) { - case "WIP": - WIP(e, sandwich); - break; - case "done": - done(e); - break; - case "gave": - give(e); - break; - case "error": - error(e); - break; - } - document.querySelector("#resume>h1").innerHTML = `Commande ${id+1}`; + if (command.error) + error(e) + else if (command.give) + give(e) + else if (command.done) + done(e) + else if (command.WIP) + WIP(e, command.sandwich) + document.querySelector("#resume>h1").innerHTML = `Command ${command.number+1}`; } -function addplate(id, name) { - plate.insertAdjacentHTML("beforeend", `
  • `); - let e = document.querySelector(`input[id=${id} ]`); +function addDish(d) { + dish.insertAdjacentHTML("beforeend", `
  • `); + let e = document.querySelector(`input[id=dish${d.id}]`); e.addEventListener("click", () => { - radiocheck(e, "plate",0); + radioCheck(e); document.querySelectorAll("input[name=ingredient],input[name=sauce]").forEach( el => { if (el.checked) el.click(); }); document.querySelectorAll("input[name=ingredient],input[name=sauce]").forEach( el => { - el.disabled = !(e.checked && !db["plate"][e.id]["avoid " + el.name]); + el.disabled = !(e.checked && !db.dish[e.id.replace(e.name, "")]["avoid" + el.name.charAt(0).toUpperCase() + el.name.slice(1)]); }); }) } -function addingredient(id, name) { - ingredient.insertAdjacentHTML("beforeend", `
  • `); - let e = document.querySelector(`input[id=${id} ]`); +function addIngredient(i) { + ingredient.insertAdjacentHTML("beforeend", `
  • `); + let e = document.querySelector(`input[id=ingredient${i.id}]`); e.addEventListener("click", () => { - checkcheck(e, "ingredient", 1, 3) + checkCheck(e, 3) }) } -function addsauce(id, name) { - sauce.insertAdjacentHTML("beforeend", `
  • `); - let e = document.querySelector(`input[id=${id} ]`); +function addSauce(s) { + sauce.insertAdjacentHTML("beforeend", `
  • `); + let e = document.querySelector(`input[id=sauce${s.id}]`); e.addEventListener("click", () => { - checkcheck(e, "sauce", 2, 2) + checkCheck(e,2) }) } -function adddrink(id, name) { - drink.insertAdjacentHTML("beforeend", `
  • `); - let e = document.querySelector(`input[id=${id} ]`); +function addDrink(d) { + drink.insertAdjacentHTML("beforeend", `
  • `); + let e = document.querySelector(`input[id=drink${d.id}]`); e.addEventListener("click", () => { - radiocheck(e, "drink", 3) + radioCheck(e) }) } -function adddessert(id, name) { - dessert.insertAdjacentHTML("beforeend", `
  • `); - let e = document.querySelector(`input[id=${id} ]`); +function addDessert(d) { + dessert.insertAdjacentHTML("beforeend", `
  • `); + let e = document.querySelector(`input[id=dessert${d.id}]`); e.addEventListener("click", () => { - radiocheck(e, "dessert", 4) + radioCheck(e) }) } -function radiocheck (e, n, p) { +function radioCheck (e) { if (e.checked) { let curr, name; - if (e.id === radios[n]) { + if (e.id.replace(e.name, "") === radios[e.name]) { e.checked = false; - radios[n] = null; + radios[e.name] = null; curr = null; name = null; } else { - radios[n] = e.id; - curr = e.id; + radios[e.name] = e.id.replace(e.name, ""); + curr = e.id.replace(e.name, ""); name = document.querySelector(`label[for=${e.id}]`).innerHTML; } - current[n] = curr; + current[e.name] = curr; if (curr) - current["price"][n] = db[n][curr]["price"]; + current.price[e.name] = db[e.name][curr.replace(e.name, "")].price; else - current["price"][n] = 0; + current.price[e.name] = 0; price(); - document.querySelectorAll("#resume p")[p].innerHTML = name; + document.getElementById("p-"+e.name).innerHTML = name; } } -function checkcheck(e, n, p, l) { +function checkCheck(e, l) { if (e.checked) - current[n].push(e.id); + current[e.name].push(e.id.replace(e.name, "")); else - current[n].splice(current[n].indexOf(e.id), 1); - document.querySelectorAll(`input[name=${n}]`).forEach( e => { + current[e.name].splice(current[e.name].indexOf(e.id.replace(e.name, "")), 1); + document.querySelectorAll(`input[name=${e.name}]`).forEach( e => { if (!e.checked) - e.disabled = current[n].length === l + e.disabled = current[e.name].length === l }); - current["price"][n] = 0; - for (let i of current[n]) { - current["price"][n] += db[n][i]["price"] + current.price[e.name] = 0; + for (let i of current[e.name]) { + current.price[e.name] += db[e.name][i].price } price(); - document.querySelectorAll("#resume p")[p].innerHTML = current[n].join(" - "); + let names = []; + current[e.name].forEach(el=>names.push(document.querySelector(`label[for=${e.name}${el}]`).innerHTML)); + document.getElementById("p-"+e.name).innerHTML = names.join(" - "); } function clear(e) { - e.classList.remove("finis"); - e.classList.remove("donnee"); - e.classList.remove("probleme"); + e.classList.remove("done"); + e.classList.remove("give"); + e.classList.remove("warning"); e.classList.remove("WIP"); e.classList.remove("show-spec"); list.prepend(e); @@ -164,29 +172,27 @@ function give(e) { function error(e) { e.classList.remove("show-spec"); - e.classList.add("probleme"); + e.classList.add("warning"); list.appendChild(e); } function price() { let p = 0; - for (let i in current["price"]) { - p += current["price"][i] + for (let i in current.price) { + p += current.price[i] } p = p.toFixed(2); document.querySelector("#resume h2").innerHTML = p+"€"; return p; } -socket.on("connect", data => { - if (data === "ok") { - socket.emit("list plate"); - socket.emit("list ingredient"); - socket.emit("list sauce"); - socket.emit("list drink"); - socket.emit("list dessert"); - socket.emit("list command"); - } +socket.on("connected", () => { + socket.emit("list dish"); + socket.emit("list ingredient"); + socket.emit("list sauce"); + socket.emit("list drink"); + socket.emit("list dessert"); + socket.emit("list command"); }); socket.on("list command", data => { @@ -195,20 +201,20 @@ socket.on("list command", data => { list.removeChild(child); child = list.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); } }); -socket.on("list plate", data => { - let child = plate.lastElementChild; +socket.on("list dish", data => { + let child = dish.lastElementChild; while (child) { - plate.removeChild(child); - child = plate.lastElementChild; + dish.removeChild(child); + child = dish.lastElementChild; } - for (let p of data.list) { - addplate(p.id, p.name); - db["plate"][p.id] = {"name": p.name, "price": p.price, "avoid ingredient": p.avoid_ingredient, "avoid sauce": p.avoid_sauce} + for (let d of data) { + addDish(d); + db.dish[d.id] = d; } }); @@ -218,9 +224,9 @@ socket.on("list ingredient", data => { ingredient.removeChild(child); child = ingredient.lastElementChild; } - for (let i of data.list) { - addingredient(i.id, i.name); - db["ingredient"][i.id] = {"name": i.name, "price": i.price} + for (let i of data) { + addIngredient(i); + db.ingredient[i.id] = i; } }); @@ -230,9 +236,9 @@ socket.on("list sauce", data => { sauce.removeChild(child); child = sauce.lastElementChild; } - for (let s of data.list) { - addsauce(s.id, s.name); - db["sauce"][s.id] = {"name": s.name, "price": s.price} + for (let s of data) { + addSauce(s); + db.sauce[s.id] = s; } }); @@ -242,9 +248,9 @@ socket.on("list drink", data => { drink.removeChild(child); child = drink.lastElementChild; } - for (let d of data.list) { - adddrink(d.id, d.name); - db["drink"][d.id] = {"name": d.name, "price": d.price} + for (let d of data) { + addDrink(d); + db.drink[d.id] = d; } }); @@ -254,53 +260,57 @@ socket.on("list dessert", data => { dessert.removeChild(child); child = dessert.lastElementChild; } - for (let d of data.list) { - adddessert(d.id, d.name); - db["dessert"][d.id] = {"name": d.name, "price": d.price} + for (let d of data) { + addDessert(d); + db.dessert[d.id] = d; } }); 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); }); socket.on("cleared command", data => { - clear(document.querySelector(`.liste #cmd${data.id}`)) + clear(document.querySelector(`.list #cmd${data}`)) }); socket.on("WIPed command", data => { - WIP(document.querySelector(`.liste #cmd${data.id}`), data.sandwich) + WIP(document.querySelector(`.list #cmd${data.id}`), data.sandwich) }); socket.on("finish command", data => { - done(document.querySelector(`.liste #cmd${data.id}`)) + done(document.querySelector(`.list #cmd${data}`)) }); socket.on("gave command", data => { - give(document.querySelector(`.liste #cmd${data.id}`)) + give(document.querySelector(`.list #cmd${data}`)) }); socket.on("glitched command", data => { - error(document.querySelector(`.liste #cmd${data.id}`)) + error(document.querySelector(`.list #cmd${data}`)) }); +socket.on("error", () => { + alert("An error occurred :") +}) + document.querySelector(".validation").addEventListener("click", ev => { ev.stopPropagation(); let user = document.getElementById("user"); - if (!current.plate && !current.ingredient.length && !current.sauce.length && !current.drink && !current.dessert) { + if (!current.dish && !current.ingredient.length && !current.sauce.length && !current.drink && !current.dessert) { alert("Empty command !"); return; } else if (user.style.color === "red") { - current["firstname"] = prompt("Prénom"); - current["lastname"] = prompt("Nom"); + current.firstname = prompt("Prénom"); + current.lastname = prompt("Nom"); } - current["client"] = user.value; - current["pc"] = pc_id; - current["price"] = price(); + current.client = user.value; + current.pc = 0; + current.price = price(); socket.emit("add command", current); - current = {"plate": null, "ingredient": [], "sauce": [], "drink": null, "dessert": null, "price": {}}; - document.querySelectorAll("input[name=plate],input[name=drink],input[name=dessert]").forEach( e => { + current = {dish: null, ingredient: [], sauce: [], drink: null, dessert: null, price: {}}; + document.querySelectorAll("input[name=dish],input[name=drink],input[name=dessert]").forEach( e => { e.checked = false; }); document.querySelectorAll("input[name=ingredient],input[name=sauce]").forEach( e => { @@ -329,12 +339,12 @@ function hinter(ev) { socket.on("list users", data => { let user_list = document.getElementById("user_list"); user_list.innerHTML = ""; - for (let u of data["list"]) { + for (let u of data) { user_list.insertAdjacentHTML("beforeend", `