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 { class Products {
static instance static instance
@ -6,6 +17,8 @@ class Products {
this.gridProducts = [] this.gridProducts = []
this.gridProductsAmount = [] this.gridProductsAmount = []
this.min = 50;
for (let i = 0; i < 20; ++i) { for (let i = 0; i < 20; ++i) {
const gridProductsColumn = [] const gridProductsColumn = []
const gridProductsAmountColumn = [] const gridProductsAmountColumn = []
@ -13,7 +26,9 @@ class Products {
for (let j = 0; j < 9; ++j) { for (let j = 0; j < 9; ++j) {
if (j % 3 !== 1 && (i+1) % 7 !== 0) { if (j % 3 !== 1 && (i+1) % 7 !== 0) {
gridProductsColumn.push(Product.RANDOM_FROM_REGISTRY()); 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 { } else {
gridProductsColumn.push(''); gridProductsColumn.push('');
gridProductsAmountColumn.push(''); gridProductsAmountColumn.push('');
@ -42,7 +57,7 @@ class Products {
} }
drawProducts () { 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()) { for (let [y, type] of line.entries()) {
if (type === GRID_FIELD_TYPE.SHELF) { if (type === GRID_FIELD_TYPE.SHELF) {
let v = this.drawValue(x, y); let v = this.drawValue(x, y);
@ -71,7 +86,8 @@ class Products {
// white - full shelf // white - full shelf
// red - empty shelf // red - empty shelf
const t = Math.cbrt 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) if (color.length == 1)
color = "0" + color color = "0" + color
this.ctx.fillStyle = `#ff${color}${color}`; this.ctx.fillStyle = `#ff${color}${color}`;
@ -126,13 +142,17 @@ class Products {
* @returns {{product: Product; amount: number} | null } Old shelf content * @returns {{product: Product; amount: number} | null } Old shelf content
*/ */
exchange(x, y, incomingProduct, incomingAmount) { exchange(x, y, incomingProduct, incomingAmount) {
const product = this.gridProducts[x][y] const product = this.gridProducts[x][y];
const amount = this.gridProductsAmount[x][y] const amount = this.gridProductsAmount[x][y];
this.gridProducts[x][y] = incomingProduct this.gridProducts[x][y] = incomingProduct;
this.gridProductsAmount[x][y] = incomingAmount this.gridProductsAmount[x][y] = incomingAmount;
this.min = filterReduce2D(this.gridProductsAmount, (p, c) => Math.min(p, c),
x => typeof x === 'number', 50);
this.clearCanvas(); this.clearCanvas();
this.drawProducts(); 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) => { const maybeProduct = this.filter((_product, amount, _i, _j, end) => amount < 50).sort((a, b) => {
return a.amount - b.amount return a.amount - b.amount
}) })
console.log(maybeProduct);
return maybeProduct.length === 0 ? null : maybeProduct[0] return maybeProduct.length === 0 ? null : maybeProduct[0]
} }