From 445b799ff5b3bc90d0de801542d687578937d417 Mon Sep 17 00:00:00 2001 From: Spythere Date: Sun, 11 Jun 2023 16:38:38 +0200 Subject: [PATCH 1/3] =?UTF-8?q?fix:=20generowanie=20sk=C5=82ad=C3=B3w?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/tabs/StockGeneratorTab.vue | 18 +++++++++++++----- src/mixins/stockMixin.ts | 16 ---------------- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/src/components/tabs/StockGeneratorTab.vue b/src/components/tabs/StockGeneratorTab.vue index b09fbc6..3a86425 100644 --- a/src/components/tabs/StockGeneratorTab.vue +++ b/src/components/tabs/StockGeneratorTab.vue @@ -183,17 +183,25 @@ export default defineComponent({ const headingLoco = this.store.stockList[0]?.isLoco ? this.store.stockList[0] : undefined; this.store.stockList.length = headingLoco ? 1 : 0; - const maxMass = this.store.acceptableMass || this.maxMass; + const maxMass = Math.min(this.store.acceptableMass, this.maxMass); - new Array(this.maxCarCount).fill(0).forEach(() => { + let exceeded = false; + + while (!exceeded) { const randomStockType = generatedChosenStockList[~~(Math.random() * generatedChosenStockList.length)]; const { carWagon, cargo } = randomStockType.carPool[~~(Math.random() * randomStockType.carPool.length)]; - if (this.store.totalMass + (cargo?.totalMass || carWagon.mass) > maxMass) return; - if (this.store.totalLength + carWagon.length > this.maxLength) return; + if ( + this.store.totalMass + (cargo?.totalMass || carWagon.mass) > maxMass || + this.store.totalLength + carWagon.length > this.maxLength || + this.store.stockList.length > this.maxCarCount + ) { + exceeded = true; + break; + } this.addCarWagon(carWagon, cargo); - }); + } this.store.stockSectionMode = 'stock-list'; }, diff --git a/src/mixins/stockMixin.ts b/src/mixins/stockMixin.ts index d8ac80c..5b5565c 100644 --- a/src/mixins/stockMixin.ts +++ b/src/mixins/stockMixin.ts @@ -43,13 +43,6 @@ export default defineComponent({ }, addLocomotive(loco: ILocomotive) { - // const previousStock = - // this.store.stockList.length > 0 ? this.store.stockList[this.store.stockList.length - 1] : null; - // if (previousStock && previousStock.type == loco.type) { - // this.store.stockList[this.store.stockList.length - 1].count++; - // return; - // } - const stockObj = this.getStockObject(loco); if (this.store.stockList.length > 0 && !this.store.stockList[0].isLoco) this.store.stockList.unshift(stockObj); @@ -57,15 +50,6 @@ export default defineComponent({ }, addCarWagon(car: ICarWagon, cargo?: ICargo) { - // const previousStock = - // this.store.stockList.length > 0 ? this.store.stockList[this.store.stockList.length - 1] : null; - - // if (previousStock && previousStock.type == car.type && previousStock.cargo?.id == cargo?.id) { - // this.store.stockList[this.store.stockList.length - 1].count++; - - // return; - // } - const stockObj = this.getStockObject(car, cargo); this.store.stockList.push(stockObj); From 89ceb6ae7f3e031a177c41d1af467559af0a849e Mon Sep 17 00:00:00 2001 From: Spythere Date: Sun, 11 Jun 2023 16:38:49 +0200 Subject: [PATCH 2/3] bump: 1.4.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8a3f3de..df1029f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pojazdownik", - "version": "1.4.1", + "version": "1.4.2", "private": true, "scripts": { "dev": "vite", From 1fa3d4c3a19ddad70b865cc4db6a6190be04fd51 Mon Sep 17 00:00:00 2001 From: Spythere Date: Sun, 11 Jun 2023 17:12:42 +0200 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20efektywniejsze=20generowanie=20sk?= =?UTF-8?q?=C5=82adu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/tabs/StockGeneratorTab.vue | 45 +++++++++++++++-------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/src/components/tabs/StockGeneratorTab.vue b/src/components/tabs/StockGeneratorTab.vue index 3a86425..09a2cf7 100644 --- a/src/components/tabs/StockGeneratorTab.vue +++ b/src/components/tabs/StockGeneratorTab.vue @@ -99,7 +99,7 @@ import { defineComponent } from 'vue'; import { useStore } from '../../store'; import stockMixin from '../../mixins/stockMixin'; -import { ICargo, ICarWagon } from '../../types'; +import { ICargo, ICarWagon, IStock } from '../../types'; import warningsMixin from '../../mixins/warningsMixin'; export default defineComponent({ @@ -180,29 +180,42 @@ export default defineComponent({ return acc; }, [] as { constructionType: string; carPool: { carWagon: ICarWagon; cargo?: ICargo }[] }[]); - const headingLoco = this.store.stockList[0]?.isLoco ? this.store.stockList[0] : undefined; + let bestGeneration: { stockList: IStock[]; value: number } = { stockList: [], value: 0 }; - this.store.stockList.length = headingLoco ? 1 : 0; - const maxMass = Math.min(this.store.acceptableMass, this.maxMass); + for (let i = 0; i < 10; i++) { + const headingLoco = this.store.stockList[0]?.isLoco ? this.store.stockList[0] : undefined; + this.store.stockList.length = headingLoco ? 1 : 0; - let exceeded = false; + const maxMass = + this.store.acceptableMass > 0 ? Math.min(this.store.acceptableMass, this.maxMass) : this.maxMass; - while (!exceeded) { - const randomStockType = generatedChosenStockList[~~(Math.random() * generatedChosenStockList.length)]; - const { carWagon, cargo } = randomStockType.carPool[~~(Math.random() * randomStockType.carPool.length)]; + let exceeded = false; - if ( - this.store.totalMass + (cargo?.totalMass || carWagon.mass) > maxMass || - this.store.totalLength + carWagon.length > this.maxLength || - this.store.stockList.length > this.maxCarCount - ) { - exceeded = true; - break; + while (!exceeded) { + const randomStockType = generatedChosenStockList[~~(Math.random() * generatedChosenStockList.length)]; + const { carWagon, cargo } = randomStockType.carPool[~~(Math.random() * randomStockType.carPool.length)]; + + if ( + this.store.totalMass + (cargo?.totalMass || carWagon.mass) > maxMass || + this.store.totalLength + carWagon.length > this.maxLength || + this.store.stockList.length > this.maxCarCount + ) { + exceeded = true; + break; + } + + this.addCarWagon(carWagon, cargo); } - this.addCarWagon(carWagon, cargo); + const currentGenerationValue = this.store.totalLength + this.store.totalMass + this.store.stockList.length; + + if (bestGeneration.value < currentGenerationValue) { + bestGeneration.stockList = this.store.stockList; + bestGeneration.value = currentGenerationValue; + } } + this.store.stockList = bestGeneration.stockList; this.store.stockSectionMode = 'stock-list'; },