Some logic for products
This commit is contained in:
parent
821b7b739e
commit
37c068e91b
@ -10,6 +10,10 @@ class Product {
|
|||||||
this.icon = icon;
|
this.icon = icon;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
equals(other) {
|
||||||
|
return this.name === other.name
|
||||||
|
}
|
||||||
|
|
||||||
static get REGISTRY() {
|
static get REGISTRY() {
|
||||||
return {
|
return {
|
||||||
empty: new Product('', ''),
|
empty: new Product('', ''),
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
class Products {
|
class Products {
|
||||||
|
static instance
|
||||||
|
|
||||||
constructor(canvas) {
|
constructor(canvas) {
|
||||||
const { random, floor } = Math
|
const { random, floor } = Math
|
||||||
@ -26,6 +27,8 @@ class Products {
|
|||||||
this.ctx = canvas.getContext('2d');
|
this.ctx = canvas.getContext('2d');
|
||||||
this.setCanvasSize(canvas);
|
this.setCanvasSize(canvas);
|
||||||
this.update();
|
this.update();
|
||||||
|
|
||||||
|
Products.instance = this
|
||||||
}
|
}
|
||||||
|
|
||||||
setCanvasSize (canvas) {
|
setCanvasSize (canvas) {
|
||||||
@ -59,10 +62,6 @@ class Products {
|
|||||||
return number;
|
return number;
|
||||||
}
|
}
|
||||||
|
|
||||||
getJobRequest() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
product(x, y, v, productsKey, productsValue) {
|
product(x, y, v, productsKey, productsValue) {
|
||||||
let fontSize = 40;
|
let fontSize = 40;
|
||||||
this.ctx.font = `900 ${fontSize}px "Font Awesome 5 Free"`;
|
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 {Product} incomingProduct
|
||||||
* @param {number} incomingAmount
|
* @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) {
|
exchange(x, y, incomingProduct, incomingAmount) {
|
||||||
const product = this.gridProducts[x][y]
|
const product = this.gridProducts[x][y]
|
||||||
@ -107,4 +132,28 @@ class Products {
|
|||||||
this.gridProductsAmount[x][y] = incomingAmount
|
this.gridProductsAmount[x][y] = incomingAmount
|
||||||
return { product, amount }
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user