From 37c068e91b8adc7c40db929207887f172b6ff849 Mon Sep 17 00:00:00 2001 From: Robert Bendun Date: Mon, 19 Apr 2021 11:41:53 +0200 Subject: [PATCH] Some logic for products --- src/modules/product.js | 4 +++ src/modules/products.js | 61 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 59 insertions(+), 6 deletions(-) diff --git a/src/modules/product.js b/src/modules/product.js index 0fec2f4..f8f8bba 100644 --- a/src/modules/product.js +++ b/src/modules/product.js @@ -10,6 +10,10 @@ class Product { this.icon = icon; } + equals(other) { + return this.name === other.name + } + static get REGISTRY() { return { empty: new Product('', ''), diff --git a/src/modules/products.js b/src/modules/products.js index b859171..07e18d9 100644 --- a/src/modules/products.js +++ b/src/modules/products.js @@ -1,5 +1,6 @@ class Products { - + static instance + constructor(canvas) { const { random, floor } = Math @@ -26,6 +27,8 @@ class Products { this.ctx = canvas.getContext('2d'); this.setCanvasSize(canvas); this.update(); + + Products.instance = this } setCanvasSize (canvas) { @@ -59,10 +62,6 @@ class Products { return number; } - getJobRequest() { - - } - product(x, y, v, productsKey, productsValue) { let fontSize = 40; this.ctx.font = `900 ${fontSize}px "Font Awesome 5 Free"`; @@ -95,10 +94,36 @@ class Products { } } + /** + * + * @param {(product: Product, amount: number, i: number, j: number, end: () => bool) => bool} filter + * @returns + */ + filter(filter) { + const list = [] + for (let i = 0; i < 20; ++i) { + for (let j = 0; j < 9; ++j) { + let mustEnd = false + let end = (val = false) => (mustEnd = true, val) + const product = this.gridProducts[i][j] + const amount = this.gridProductsAmount[i][j] + + if (typeof(product) !== 'string' + && filter(product, amount, i, j, end)) { + list.push({ product, amount: Number(amount), x: i, y: j }) + } + + if (mustEnd) + return list + } + } + return list + } + /** * @param {Product} incomingProduct * @param {number} incomingAmount - * @returns {{product: Product; amount: number} | } Old shelf content + * @returns {{product: Product; amount: number} | null } Old shelf content */ exchange(x, y, incomingProduct, incomingAmount) { const product = this.gridProducts[x][y] @@ -107,4 +132,28 @@ class Products { this.gridProductsAmount[x][y] = incomingAmount return { product, amount } } + + /** + * @param {Product} incomingProduct + * @param {number} incomingAmount + * @returns {{product: Product; amount: number} | null } Old shelf content or leftover + */ + interact(x, y, incomingProduct, incomingAmount) { + const { max, min } = Math + const amount = this.gridProductsAmount[x][y] + if (this.gridProducts[x][y].equals(incomingProduct) && amount < 50) { + this.gridProductsAmount[x][y] = min(incomingAmount, 50) + return { product: incomingProduct, amount: max(0, (amount + incomingAmount) - 50) } + } + return this.exchange(x, y, incomingProduct, incomingAmount) + } + + getJobRequest() { + const maybeProduct = this.filter((_product, amount, _i, _j, end) => amount < 50 && end(true)) + return maybeProduct.length === 0 ? null : maybeProduct[0] + } + + findAvailableLocationsFor(product) { + return this.filter((p, amount) => product.equals(p) && amount < 50) + } } \ No newline at end of file