Some logic for products

This commit is contained in:
Robert Bendun 2021-04-19 11:41:53 +02:00
parent 821b7b739e
commit 37c068e91b
2 changed files with 59 additions and 6 deletions

View File

@ -10,6 +10,10 @@ class Product {
this.icon = icon;
}
equals(other) {
return this.name === other.name
}
static get REGISTRY() {
return {
empty: new Product('', ''),

View File

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