From 17296fabfde84d506695ab89eaec329e311444d0 Mon Sep 17 00:00:00 2001 From: Dev Date: Mon, 6 May 2024 15:20:44 +0100 Subject: [PATCH] Moved reduceWeightValues from PMCLootGenerator into weightedRandomHelper and made public --- project/src/generators/PMCLootGenerator.ts | 70 ++------------------- project/src/helpers/WeightedRandomHelper.ts | 62 ++++++++++++++++++ project/src/services/RagfairPriceService.ts | 6 +- 3 files changed, 70 insertions(+), 68 deletions(-) diff --git a/project/src/generators/PMCLootGenerator.ts b/project/src/generators/PMCLootGenerator.ts index b4f1b765..e2ed90a1 100644 --- a/project/src/generators/PMCLootGenerator.ts +++ b/project/src/generators/PMCLootGenerator.ts @@ -1,6 +1,7 @@ import { inject, injectable } from "tsyringe"; import { ItemHelper } from "@spt-aki/helpers/ItemHelper"; +import { WeightedRandomHelper } from "@spt-aki/helpers/WeightedRandomHelper"; import { ITemplateItem } from "@spt-aki/models/eft/common/tables/ITemplateItem"; import { ConfigTypes } from "@spt-aki/models/enums/ConfigTypes"; import { IPmcConfig } from "@spt-aki/models/spt/config/IPmcConfig"; @@ -29,6 +30,7 @@ export class PMCLootGenerator @inject("ItemFilterService") protected itemFilterService: ItemFilterService, @inject("RagfairPriceService") protected ragfairPriceService: RagfairPriceService, @inject("SeasonalEventService") protected seasonalEventService: SeasonalEventService, + @inject("WeightedRandomHelper") protected weightedRandomHelper: WeightedRandomHelper, ) { this.pmcConfig = this.configServer.getConfig(ConfigTypes.PMC); @@ -87,7 +89,7 @@ export class PMCLootGenerator this.pocketLootPool[key] = Math.round((1 / this.pocketLootPool[key]) * highestPrice); } - this.reduceWeightValues(this.pocketLootPool); + this.weightedRandomHelper.reduceWeightValues(this.pocketLootPool); } return this.pocketLootPool; @@ -145,7 +147,7 @@ export class PMCLootGenerator this.vestLootPool[key] = Math.round((1 / this.vestLootPool[key]) * highestPrice); } - this.reduceWeightValues(this.vestLootPool); + this.weightedRandomHelper.reduceWeightValues(this.vestLootPool); } return this.vestLootPool; @@ -213,71 +215,9 @@ export class PMCLootGenerator this.backpackLootPool[key] = Math.round((1 / this.backpackLootPool[key]) * highestPrice); } - this.reduceWeightValues(this.backpackLootPool); + this.weightedRandomHelper.reduceWeightValues(this.backpackLootPool); } return this.backpackLootPool; } - - /** - * Find the greated common divisor of all weights and use it on the passed in dictionary - * @param weightedDict - */ - protected reduceWeightValues(weightedDict: Record): void - { - // No values, nothing to reduce - if (Object.keys(weightedDict).length === 0) - { - return; - } - - // Only one value, set to 1 and exit - if (Object.keys(weightedDict).length === 1) - { - const key = Object.keys(weightedDict)[0]; - weightedDict[key] = 1; - return; - } - - const weights = Object.values(weightedDict).slice(); - const commonDivisor = this.commonDivisor(weights); - - // No point in dividing by 1 - if (commonDivisor === 1) - { - return; - } - - for (const key in weightedDict) - { - if (Object.hasOwn(weightedDict, key)) - { - weightedDict[key] /= commonDivisor; - } - } - } - - protected commonDivisor(numbers: number[]): number - { - let result = numbers[0]; - for (let i = 1; i < numbers.length; i++) - { - result = this.gcd(result, numbers[i]); - } - - return result; - } - - protected gcd(a: number, b: number): number - { - let x = a; - let y = b; - while (y !== 0) - { - const temp = y; - y = x % y; - x = temp; - } - return x; - } } diff --git a/project/src/helpers/WeightedRandomHelper.ts b/project/src/helpers/WeightedRandomHelper.ts index 61049b2d..f145da59 100644 --- a/project/src/helpers/WeightedRandomHelper.ts +++ b/project/src/helpers/WeightedRandomHelper.ts @@ -92,4 +92,66 @@ export class WeightedRandomHelper } } } + + /** + * Find the greated common divisor of all weights and use it on the passed in dictionary + * @param weightedDict values to reduce + */ + public reduceWeightValues(weightedDict: Record): void + { + // No values, nothing to reduce + if (Object.keys(weightedDict).length === 0) + { + return; + } + + // Only one value, set to 1 and exit + if (Object.keys(weightedDict).length === 1) + { + const key = Object.keys(weightedDict)[0]; + weightedDict[key] = 1; + return; + } + + const weights = Object.values(weightedDict).slice(); + const commonDivisor = this.commonDivisor(weights); + + // No point in dividing by 1 + if (commonDivisor === 1) + { + return; + } + + for (const key in weightedDict) + { + if (Object.hasOwn(weightedDict, key)) + { + weightedDict[key] /= commonDivisor; + } + } + } + + protected commonDivisor(numbers: number[]): number + { + let result = numbers[0]; + for (let i = 1; i < numbers.length; i++) + { + result = this.gcd(result, numbers[i]); + } + + return result; + } + + protected gcd(a: number, b: number): number + { + let x = a; + let y = b; + while (y !== 0) + { + const temp = y; + y = x % y; + x = temp; + } + return x; + } } diff --git a/project/src/services/RagfairPriceService.ts b/project/src/services/RagfairPriceService.ts index 6c3e578b..6f1d4adb 100644 --- a/project/src/services/RagfairPriceService.ts +++ b/project/src/services/RagfairPriceService.ts @@ -253,9 +253,9 @@ export class RagfairPriceService implements OnLoad } /** - * @param itemTemplateId - * @param desiredCurrency - * @param item + * @param itemTemplateId items tpl value + * @param desiredCurrency Currency to return result in + * @param item Item object (used for weapon presets) * @param offerItems * @param isPackOffer * @returns