From eeb5ef4948426e998324041fd1fd97e855ac695d Mon Sep 17 00:00:00 2001 From: Robert Bendun Date: Mon, 29 Mar 2021 03:40:37 +0200 Subject: [PATCH] Cleanup --- src/index.html | 5 ++-- src/modules/knowledge.js | 28 +++++++++++++++++++-- src/modules/shop.js | 54 +--------------------------------------- 3 files changed, 30 insertions(+), 57 deletions(-) diff --git a/src/index.html b/src/index.html index e56f684..4bdacea 100644 --- a/src/index.html +++ b/src/index.html @@ -13,14 +13,15 @@ + + + - - diff --git a/src/modules/knowledge.js b/src/modules/knowledge.js index 6123506..7027b1f 100644 --- a/src/modules/knowledge.js +++ b/src/modules/knowledge.js @@ -1,7 +1,6 @@ -function flattenToUnique(array, view = x => x) { +function flattenToUnique(array, view) { return array.reduce((set, value) => { const v = view(value) - console.log(v) v && v.forEach(cat => set.add(cat)) return set }, new Set()) @@ -61,6 +60,26 @@ class SemanticNetwork { class AgentSemanticNetwork extends SemanticNetwork {} +function nearbyStorageUnitsCoords(gridX, gridY) { + function outsideOfStorageCenter(v) { + const { x, y, w, h } = StorageCenterLocation + return v.x <= x || v.x >= x + w || v.y <= y || v.y >= y + h + } + return (gridX % UnitsInGroupCount == 0 + ? (gridY % 3 == 1 + ? /* skrzyżowanie */[[-1, -1], [-1, 1], [1, -1], [1, 1]] + : /* korytarz góra-dół */[[-1, 0], [1, 0]]) + : /* korytarz lewo-prawo */[[0, -1], [0, 1]]) + .map(([xoff, yoff]) => ({ x: gridX + xoff, y: yGrid + yoff })) + .filter(outsideOfStorageCenter) +} + + +function nearbyStorageUnitsIndexes(gridX, gridY) { + return nearbyStorageUnitsCoords(gridX, gridY) + .map(({ x, y }) => x * UnitsCount + y * UnitsCount * RowsOfGroupsCount) +} + class Arrangement { constructor() { this.products = [...Array(UnitsCount)].map(() => ({ @@ -69,6 +88,11 @@ class Arrangement { icon: '' })) } + + nearbyProducts(gridX, gridY) { + return nearbyStorageUnitsIndexes(gridX, gridY) + .map(i => this.products[i]) + } } class Knowledge { diff --git a/src/modules/shop.js b/src/modules/shop.js index 8b6fa9a..b7c5341 100644 --- a/src/modules/shop.js +++ b/src/modules/shop.js @@ -1,56 +1,6 @@ -/** - * Zwraca koordynaty półek przy danym kafelku ścieżki - * @param {number} gridX - * @param {number} gridY - * @returns {{x:number;y:number}[]} Koordynaty sąsiednich półek do kafelka - */ -function nearbyStorageUnitsCoords(gridX, gridY) { - function outsideOfStorageCenter(v) { - const { x, y, w, h } = StorageCenterLocation - return v.x <= x || v.x >= x + w || v.y <= y || v.y >= y + h - } - return (gridX % UnitsInGroupCount == 0 - ? (gridY % 3 == 1 - ? /* skrzyżowanie */[[-1, -1], [-1, 1], [1, -1], [1, 1]] - : /* korytarz góra-dół */[[-1, 0], [1, 0]]) - : /* korytarz lewo-prawo */[[0, -1], [0, 1]]) - .map(([xoff, yoff]) => ({ x: gridX + xoff, y: yGrid + yoff })) - .filter(outsideOfStorageCenter) -} - -/** - * Zwraca indeksy półek przy danym kafelku ścieżki - * @param {number} gridX - * @param {number} gridY - * @returns {{x:number;y:number}[]} Indeksy sąsiednich półek do kafelka - */ -function nearbyStorageUnitsIndexes(gridX, gridY) { - return nearbyStorageUnitsCoords(gridX, gridY) - .map(({ x, y }) => x * UnitsCount + y * UnitsCount * RowsOfGroupsCount) -} - -function ensureArray(maybeArray) { - return Array.isArray(maybeArray) ? maybeArray : [] -} - class Shop { - constructor(semanticNetwork) { - this.semanticNetwork = semanticNetwork - this.products = [...Array(UnitsCount)].map(x => ({ name: '', count: 0 })) - } - - /** - * Zwraca listę produktów w okolicy danego kafelka ścieżki - * @param {number} gridX - * @param {number} gridY - */ - nearbyProducts(gridX, gridY) { - return nearbyStorageUnitsIndexes(gridX, gridY) - .map(i => this.productPlacement[i]) - } - productsSimilarityScore(name1, name2) { - const [ o1, o2 ] = [name1, name2].map(name => this.semanticNetwork.findByName(name)) + const [ o1, o2 ] = [name1, name2].map(name => Knowledge.semanticNetwork.findByName(name)) if (!o1 || !o2) throw new Error("names should be in semantic network!") @@ -67,5 +17,3 @@ class Shop { return l <= Number.EPSILON ? c * 0.7 : l * 0.65 + c * 0.35 // TODO: maybe find better formula? needs testing } } - -const shop = new Shop(semanticNetwork)