Cleaned up createBarterBarterScheme()

This commit is contained in:
Dev 2024-10-19 10:58:25 +01:00
parent 9ab0b40854
commit b6b527df2a
2 changed files with 27 additions and 21 deletions

View File

@ -19,8 +19,10 @@ import {
Condition, Condition,
Dynamic, Dynamic,
IArmorPlateBlacklistSettings, IArmorPlateBlacklistSettings,
IBarterDetails,
IRagfairConfig, IRagfairConfig,
} from "@spt/models/spt/config/IRagfairConfig"; } from "@spt/models/spt/config/IRagfairConfig";
import { ITplWithFleaPrice } from "@spt/models/spt/ragfair/ITplWithFleaPrice";
import { ILogger } from "@spt/models/spt/utils/ILogger"; import { ILogger } from "@spt/models/spt/utils/ILogger";
import { ConfigServer } from "@spt/servers/ConfigServer"; import { ConfigServer } from "@spt/servers/ConfigServer";
import { SaveServer } from "@spt/servers/SaveServer"; import { SaveServer } from "@spt/servers/SaveServer";
@ -502,7 +504,7 @@ export class RagfairOfferGenerator {
} else if (isBarterOffer) { } else if (isBarterOffer) {
// Apply randomised properties // Apply randomised properties
this.randomiseOfferItemUpdProperties(randomUserId, itemWithChildren, itemDetails[1]); this.randomiseOfferItemUpdProperties(randomUserId, itemWithChildren, itemDetails[1]);
barterScheme = this.createBarterBarterScheme(itemWithChildren); barterScheme = this.createBarterBarterScheme(itemWithChildren, this.ragfairConfig.dynamic.barter);
} else { } else {
// Apply randomised properties // Apply randomised properties
this.randomiseOfferItemUpdProperties(randomUserId, itemWithChildren, itemDetails[1]); this.randomiseOfferItemUpdProperties(randomUserId, itemWithChildren, itemDetails[1]);
@ -826,50 +828,49 @@ export class RagfairOfferGenerator {
/** /**
* Create a barter-based barter scheme, if not possible, fall back to making barter scheme currency based * Create a barter-based barter scheme, if not possible, fall back to making barter scheme currency based
* @param offerItems Items for sale in offer * @param offerItems Items for sale in offer
* @param barterConfig Barter config from ragfairConfig.dynamic.barter
* @returns Barter scheme * @returns Barter scheme
*/ */
protected createBarterBarterScheme(offerItems: IItem[]): IBarterScheme[] { protected createBarterBarterScheme(offerItems: IItem[], barterConfig: IBarterDetails): IBarterScheme[] {
// get flea price of item being sold // Get flea price of item being sold
const priceOfItemOffer = this.ragfairPriceService.getDynamicOfferPriceForOffer( const priceOfOfferItem = this.ragfairPriceService.getDynamicOfferPriceForOffer(
offerItems, offerItems,
Money.ROUBLES, Money.ROUBLES,
false, false,
); );
// Dont make items under a designated rouble value into barter offers // Dont make items under a designated rouble value into barter offers
if (priceOfItemOffer < this.ragfairConfig.dynamic.barter.minRoubleCostToBecomeBarter) { if (priceOfOfferItem < barterConfig.minRoubleCostToBecomeBarter) {
return this.createCurrencyBarterScheme(offerItems, false); return this.createCurrencyBarterScheme(offerItems, false);
} }
// Get a randomised number of barter items to list offer for // Get a randomised number of barter items to list offer for
const barterItemCount = this.randomUtil.getInt( const barterItemCount = this.randomUtil.getInt(barterConfig.itemCountMin, barterConfig.itemCountMax);
this.ragfairConfig.dynamic.barter.itemCountMin,
this.ragfairConfig.dynamic.barter.itemCountMax,
);
// Get desired cost of individual item offer will be listed for e.g. offer = 15k, item count = 3, desired item cost = 5k // Get desired cost of individual item offer will be listed for e.g. offer = 15k, item count = 3, desired item cost = 5k
const desiredItemCost = Math.round(priceOfItemOffer / barterItemCount); const desiredItemCostRouble = Math.round(priceOfOfferItem / barterItemCount);
// Amount to go above/below when looking for an item (Wiggle cost of item a little) // Rouble amount to go above/below when looking for an item (Wiggle cost of item a little)
const offerCostVariance = (desiredItemCost * this.ragfairConfig.dynamic.barter.priceRangeVariancePercent) / 100; const offerCostVarianceRoubles = (desiredItemCostRouble * barterConfig.priceRangeVariancePercent) / 100;
const fleaPrices = this.getFleaPricesAsArray(); // Dict of items and their flea price (cached on first use)
const itemFleaPrices = this.getFleaPricesAsArray();
// Filter possible barters to items that match the price range + not itself // Filter possible barters to items that match the price range + not itself
const filtered = fleaPrices.filter( const itemsInsidePriceBounds = itemFleaPrices.filter(
(x) => (itemAndPrice) =>
x.price >= desiredItemCost - offerCostVariance && itemAndPrice.price >= desiredItemCostRouble - offerCostVarianceRoubles &&
x.price <= desiredItemCost + offerCostVariance && itemAndPrice.price <= desiredItemCostRouble + offerCostVarianceRoubles &&
x.tpl !== offerItems[0]._tpl, itemAndPrice.tpl !== offerItems[0]._tpl, // Don't allow the item being sold to be chosen
); );
// No items on flea have a matching price, fall back to currency // No items on flea have a matching price, fall back to currency
if (filtered.length === 0) { if (itemsInsidePriceBounds.length === 0) {
return this.createCurrencyBarterScheme(offerItems, false); return this.createCurrencyBarterScheme(offerItems, false);
} }
// Choose random item from price-filtered flea items // Choose random item from price-filtered flea items
const randomItem = this.randomUtil.getArrayValue(filtered); const randomItem = this.randomUtil.getArrayValue(itemsInsidePriceBounds);
return [{ count: barterItemCount, _tpl: randomItem.tpl }]; return [{ count: barterItemCount, _tpl: randomItem.tpl }];
} }
@ -878,7 +879,7 @@ export class RagfairOfferGenerator {
* Get an array of flea prices + item tpl, cached in generator class inside `allowedFleaPriceItemsForBarter` * Get an array of flea prices + item tpl, cached in generator class inside `allowedFleaPriceItemsForBarter`
* @returns array with tpl/price values * @returns array with tpl/price values
*/ */
protected getFleaPricesAsArray(): { tpl: string; price: number }[] { protected getFleaPricesAsArray(): ITplWithFleaPrice[] {
// Generate if needed // Generate if needed
if (!this.allowedFleaPriceItemsForBarter) { if (!this.allowedFleaPriceItemsForBarter) {
const fleaPrices = this.databaseService.getPrices(); const fleaPrices = this.databaseService.getPrices();

View File

@ -0,0 +1,5 @@
export interface ITplWithFleaPrice {
tpl: string;
/** Roubles */
price: number;
}