Red means now lowest amount in store, not 0 amount

This commit is contained in:
Robert Bendun 2021-05-10 23:11:07 +02:00
parent 49a12b1b92
commit cef188c515

View File

@ -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]
}