From cef188c51516b81471c3715b78ee75f83b2053cd Mon Sep 17 00:00:00 2001 From: Robert Bendun Date: Mon, 10 May 2021 23:11:07 +0200 Subject: [PATCH] Red means now lowest amount in store, not 0 amount --- src/view/products.js | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/src/view/products.js b/src/view/products.js index 58dd37a..2055894 100644 --- a/src/view/products.js +++ b/src/view/products.js @@ -1,3 +1,14 @@ +function filterReduce2D(array, reducer, filter, init) { + let result = init; + + for (let row of array) + for (let element of row) + if (filter(element)) + result = reducer(result, element); + + return result; +} + class Products { static instance @@ -6,6 +17,8 @@ class Products { this.gridProducts = [] this.gridProductsAmount = [] + this.min = 50; + for (let i = 0; i < 20; ++i) { const gridProductsColumn = [] const gridProductsAmountColumn = [] @@ -13,7 +26,9 @@ class Products { for (let j = 0; j < 9; ++j) { if (j % 3 !== 1 && (i+1) % 7 !== 0) { gridProductsColumn.push(Product.RANDOM_FROM_REGISTRY()); - gridProductsAmountColumn.push(floor(random() * 51)); + const amount = floor(random() * 51); + gridProductsAmountColumn.push(amount); + this.min = this.min > amount ? amount : this.min; } else { gridProductsColumn.push(''); gridProductsAmountColumn.push(''); @@ -42,7 +57,7 @@ class Products { } drawProducts () { - for (let [x, line] of Grid.instance.grid.entries()) { + for (let [x, line] of Grid.instance.grid.entries()) { for (let [y, type] of line.entries()) { if (type === GRID_FIELD_TYPE.SHELF) { let v = this.drawValue(x, y); @@ -71,7 +86,8 @@ class Products { // white - full shelf // red - empty shelf const t = Math.cbrt - let color = Math.floor(t(v / 50) * 255).toString(16) + v = (v - this.min) / (50 - this.min); + let color = Math.floor(t(v) * 255).toString(16) if (color.length == 1) color = "0" + color this.ctx.fillStyle = `#ff${color}${color}`; @@ -126,13 +142,17 @@ class Products { * @returns {{product: Product; amount: number} | null } Old shelf content */ exchange(x, y, incomingProduct, incomingAmount) { - const product = this.gridProducts[x][y] - const amount = this.gridProductsAmount[x][y] - this.gridProducts[x][y] = incomingProduct - this.gridProductsAmount[x][y] = incomingAmount + const product = this.gridProducts[x][y]; + const amount = this.gridProductsAmount[x][y]; + this.gridProducts[x][y] = incomingProduct; + this.gridProductsAmount[x][y] = incomingAmount; + + this.min = filterReduce2D(this.gridProductsAmount, (p, c) => Math.min(p, c), + x => typeof x === 'number', 50); + this.clearCanvas(); this.drawProducts(); - return { product, amount } + return { product, amount }; } /** @@ -154,7 +174,6 @@ class Products { const maybeProduct = this.filter((_product, amount, _i, _j, end) => amount < 50).sort((a, b) => { return a.amount - b.amount }) - console.log(maybeProduct); return maybeProduct.length === 0 ? null : maybeProduct[0] }