From c9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c Mon Sep 17 00:00:00 2001 From: Martial Simon Date: Mon, 15 Sep 2025 01:08:27 +0200 Subject: add: graphs et rushs --- graphs/js/foodTruck/foodTruck.js | 124 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 graphs/js/foodTruck/foodTruck.js (limited to 'graphs/js/foodTruck/foodTruck.js') diff --git a/graphs/js/foodTruck/foodTruck.js b/graphs/js/foodTruck/foodTruck.js new file mode 100644 index 0000000..afe14d4 --- /dev/null +++ b/graphs/js/foodTruck/foodTruck.js @@ -0,0 +1,124 @@ +if (typeof window === "undefined") { + if ( + !globalThis.stocks && + !globalThis.recipes && + !globalThis.globalThis.sleep + ) { + const { stocks, recipes } = require("./data"); + const { sleep } = require("./sleep"); + + globalThis.stocks = stocks; + globalThis.recipes = recipes; + globalThis.globalThis.sleep = sleep; + } +} + +if (typeof window === "undefined") { + module.exports = { + order, + }; +} + +/* + * + * Write your code below these lines + * + */ +async function end_of_order(recipeName, wait) { + if (wait) { + await globalThis.sleep(1); + } + + console.log("All ingredients have been prepared"); + await globalThis.sleep(2); + console.log(`Cooking ${recipeName}`); + await globalThis.sleep(5); + console.log(`Delivering ${recipeName}`); + return null; +} +function check_and_update_ingredient(recipeName, ingredient, quantity) { + if (globalThis.stocks[ingredient] < quantity) { + console.log( + `Not enough ingredients for ${recipeName} because there is no more ${ingredient}`, + ); + return false; + } + + globalThis.stocks[ingredient] -= quantity; + return true; +} + +function check_and_update_ingredients(recipeName, ingredients) { + for (const ingredient in ingredients) { + const quantity = ingredients[ingredient]; + + if (!check_and_update_ingredient(recipeName, ingredient, quantity)) { + return false; + } + } + + return true; +} + +async function order(recipeName) { + console.log(`Ordering ${recipeName}`); + + if ( + !(recipeName in globalThis.recipes.pizza) && + !(recipeName in globalThis.recipes.burger) + ) { + console.log(`Recipe ${recipeName} does not exist`); + return; + } + + await globalThis.sleep(2); + console.log(`Production has started for ${recipeName}`); + await globalThis.sleep(1); + if (recipeName in globalThis.recipes.burger) { + const burger = globalThis.recipes.burger[recipeName]; + + for (const ingredient in burger) { + console.log(`Preparing ${ingredient}`); + if ( + !check_and_update_ingredient( + recipeName, + ingredient, + burger[ingredient], + ) + ) { + return; + } + + await globalThis.sleep(1); + } + + return await end_of_order(recipeName, false); + } else if (recipeName in globalThis.recipes.pizza) { + const pizza = globalThis.recipes.pizza[recipeName]; + + console.log(`Preparing ${pizza.sauce}`); + if (!check_and_update_ingredient(recipeName, pizza.sauce, 1)) { + return; + } + + await globalThis.sleep(1); + console.log(`Preparing ${Object.keys(pizza.toppings).join(", ")}`); + if (!check_and_update_ingredients(recipeName, pizza.toppings)) { + return; + } + + await globalThis.sleep(1); + console.log(`Preparing ${Object.keys(pizza.cheese).join(", ")}`); + if (!check_and_update_ingredients(recipeName, pizza.cheese)) { + return; + } + + return await end_of_order(recipeName, true); + } +} + +if (typeof window === "undefined") { + module.exports = { + order, + }; +} -- cgit v1.2.3