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);