2023-03-03 15:23:46 +00:00
import { inject , injectable } from "tsyringe" ;
2023-10-19 17:21:17 +00:00
import { OnLoad } from "@spt-aki/di/OnLoad" ;
import { HandbookHelper } from "@spt-aki/helpers/HandbookHelper" ;
import { ItemHelper } from "@spt-aki/helpers/ItemHelper" ;
import { PresetHelper } from "@spt-aki/helpers/PresetHelper" ;
import { TraderHelper } from "@spt-aki/helpers/TraderHelper" ;
import { MinMax } from "@spt-aki/models/common/MinMax" ;
import { IPreset } from "@spt-aki/models/eft/common/IGlobals" ;
2024-02-09 14:13:48 +00:00
import { HandbookItem } from "@spt-aki/models/eft/common/tables/IHandbookBase" ;
2023-10-19 17:21:17 +00:00
import { Item } from "@spt-aki/models/eft/common/tables/IItem" ;
import { IBarterScheme } from "@spt-aki/models/eft/common/tables/ITrader" ;
2024-02-06 15:52:22 +00:00
import { BaseClasses } from "@spt-aki/models/enums/BaseClasses" ;
2023-10-19 17:21:17 +00:00
import { ConfigTypes } from "@spt-aki/models/enums/ConfigTypes" ;
import { Money } from "@spt-aki/models/enums/Money" ;
2024-02-09 14:13:48 +00:00
import { IRagfairConfig , IUnreasonableModPrices } from "@spt-aki/models/spt/config/IRagfairConfig" ;
2023-10-19 17:21:17 +00:00
import { IRagfairServerPrices } from "@spt-aki/models/spt/ragfair/IRagfairServerPrices" ;
import { ILogger } from "@spt-aki/models/spt/utils/ILogger" ;
import { ConfigServer } from "@spt-aki/servers/ConfigServer" ;
import { DatabaseServer } from "@spt-aki/servers/DatabaseServer" ;
import { LocalisationService } from "@spt-aki/services/LocalisationService" ;
import { RandomUtil } from "@spt-aki/utils/RandomUtil" ;
2023-03-03 15:23:46 +00:00
/ * *
* Stores flea prices for items as well as methods to interact with them
* /
@injectable ( )
export class RagfairPriceService implements OnLoad
{
protected ragfairConfig : IRagfairConfig ;
2023-11-15 20:35:05 -05:00
protected prices : IRagfairServerPrices = { static : { } , dynamic : { } } ;
2023-03-03 15:23:46 +00:00
constructor (
@inject ( "HandbookHelper" ) protected handbookHelper : HandbookHelper ,
@inject ( "DatabaseServer" ) protected databaseServer : DatabaseServer ,
@inject ( "WinstonLogger" ) protected logger : ILogger ,
@inject ( "ItemHelper" ) protected itemHelper : ItemHelper ,
@inject ( "PresetHelper" ) protected presetHelper : PresetHelper ,
@inject ( "TraderHelper" ) protected traderHelper : TraderHelper ,
@inject ( "RandomUtil" ) protected randomUtil : RandomUtil ,
2023-07-19 13:16:45 +01:00
@inject ( "LocalisationService" ) protected localisationService : LocalisationService ,
2023-11-15 20:35:05 -05:00
@inject ( "ConfigServer" ) protected configServer : ConfigServer ,
2023-03-03 15:23:46 +00:00
)
{
this . ragfairConfig = this . configServer . getConfig ( ConfigTypes . RAGFAIR ) ;
}
/ * *
* Generate static ( handbook ) and dynamic ( prices . json ) flea prices , store inside class as dictionaries
* /
2023-11-15 20:35:05 -05:00
public async onLoad ( ) : Promise < void >
2023-03-03 15:23:46 +00:00
{
2024-05-20 07:58:13 +00:00
this . refreshStaticPrices ( ) ;
this . refreshDynamicPrices ( ) ;
2023-03-03 15:23:46 +00:00
}
2023-11-15 20:35:05 -05:00
public getRoute ( ) : string
2023-03-03 15:23:46 +00:00
{
return "RagfairPriceService" ;
}
/ * *
* Iterate over all items of type "Item" in db and get template price , store in cache
* /
2024-05-20 07:58:13 +00:00
public refreshStaticPrices ( ) : void
2023-03-03 15:23:46 +00:00
{
2024-05-17 15:32:41 -04:00
for ( const item of Object . values ( this . databaseServer . getTables ( ) . templates . items ) . filter (
( x ) = > x . _type === "Item" ,
) )
2023-03-03 15:23:46 +00:00
{
this . prices . static [ item . _id ] = Math . round ( this . handbookHelper . getTemplatePrice ( item . _id ) ) ;
}
}
/ * *
2024-05-20 07:58:13 +00:00
* Copy the prices . json data into our dynamic price dictionary
2023-03-03 15:23:46 +00:00
* /
2024-05-20 07:58:13 +00:00
public refreshDynamicPrices ( ) : void
2023-03-03 15:23:46 +00:00
{
2024-05-20 07:58:13 +00:00
const pricesTable = this . databaseServer . getTables ( ) . templates . prices ;
this . prices . dynamic = { . . . this . prices . dynamic , . . . pricesTable } ;
2023-03-03 15:23:46 +00:00
}
/ * *
* Get the dynamic price for an item . If value doesn ' t exist , use static ( handbook ) value .
* if no static value , return 1
* @param tplId Item tpl id to get price for
* @returns price in roubles
* /
public getFleaPriceForItem ( tplId : string ) : number
{
// Get dynamic price (templates/prices), if that doesnt exist get price from static array (templates/handbook)
2023-04-23 11:25:42 +01:00
let itemPrice = this . getDynamicPriceForItem ( tplId ) || this . getStaticPriceForItem ( tplId ) ;
2023-11-10 20:11:36 +00:00
if ( itemPrice === undefined )
2023-03-03 15:23:46 +00:00
{
2023-11-15 20:35:05 -05:00
this . logger . warning (
this . localisationService . getText ( "ragfair-unable_to_find_item_price_for_item_in_flea_handbook" , tplId ) ,
) ;
2023-03-03 15:23:46 +00:00
}
2023-06-26 08:10:36 +01:00
// If no price in dynamic/static, set to 1
itemPrice = itemPrice || 1 ;
2023-03-03 15:23:46 +00:00
return itemPrice ;
}
2024-02-06 15:52:22 +00:00
/ * *
* Get the flea price for an offers items + children
* @param offerItems offer item + children to process
* @returns Rouble price
* /
public getFleaPriceForOfferItems ( offerItems : Item [ ] ) : number
{
// Preset weapons take the direct prices.json value, otherwise they're massivly inflated
if ( this . itemHelper . isOfBaseclass ( offerItems [ 0 ] . _tpl , BaseClasses . WEAPON ) )
{
return this . getFleaPriceForItem ( offerItems [ 0 ] . _tpl ) ;
}
let totalPrice = 0 ;
for ( const item of offerItems )
{
totalPrice += this . getFleaPriceForItem ( item . _tpl ) ;
}
return totalPrice ;
}
2023-03-03 15:23:46 +00:00
/ * *
* get the dynamic ( flea ) price for an item
* @param itemTpl item template id to look up
* @returns price in roubles
* /
public getDynamicPriceForItem ( itemTpl : string ) : number
{
2024-05-20 07:58:13 +00:00
// If the price doesn't exist in the cache yet, try to find it
if ( ! this . prices . dynamic [ itemTpl ] )
2023-03-03 15:23:46 +00:00
{
2024-05-20 07:58:13 +00:00
this . prices . dynamic [ itemTpl ] = this . databaseServer . getTables ( ) . templates . prices [ itemTpl ] ;
2023-03-03 15:23:46 +00:00
}
return this . prices . dynamic [ itemTpl ] ;
}
/ * *
* Grab the static ( handbook ) for an item by its tplId
* @param itemTpl item template id to look up
* @returns price in roubles
* /
public getStaticPriceForItem ( itemTpl : string ) : number
{
2024-05-20 07:58:13 +00:00
// If the price doesn't exist in the cache yet, try to find it
if ( ! this . prices . static [ itemTpl ] )
2023-03-03 15:23:46 +00:00
{
2024-05-20 07:58:13 +00:00
// Store the price in the cache only if it exists
const itemPrice = Math . round ( this . handbookHelper . getTemplatePrice ( itemTpl ) ) ;
if ( itemPrice !== 0 )
{
this . prices . static [ itemTpl ] = itemPrice ;
}
2023-03-03 15:23:46 +00:00
}
return this . prices . static [ itemTpl ] ;
}
/ * *
2024-03-17 08:54:34 +00:00
* Get prices for all items on flea , prioritize handbook prices first , use prices from prices . json if missing
2024-05-20 07:58:13 +00:00
* This will refresh the caches prior to building the output
2023-03-03 15:23:46 +00:00
* @returns Dictionary of item tpls and rouble cost
* /
public getAllFleaPrices ( ) : Record < string , number >
{
2024-05-20 07:58:13 +00:00
// Refresh the caches so we include any newly added custom items
this . refreshDynamicPrices ( ) ;
this . refreshStaticPrices ( ) ;
2024-03-17 08:54:34 +00:00
// assign dynamic (prices.json) values first, then overwrite them with static (handbook.json)
// any values not stored in static data will be covered by dynamic data
return { . . . this . prices . dynamic , . . . this . prices . static } ;
2023-03-03 15:23:46 +00:00
}
public getAllStaticPrices ( ) : Record < string , number >
{
2024-05-20 07:58:13 +00:00
// Refresh the cache so we include any newly added custom items
this . refreshStaticPrices ( ) ;
2023-11-15 20:35:05 -05:00
return { . . . this . prices . static } ;
2023-03-03 15:23:46 +00:00
}
/ * *
* Get the percentage difference between two values
* @param a numerical value a
* @param b numerical value b
* @returns different in percent
* /
protected getPriceDifference ( a : number , b : number ) : number
{
2024-05-17 15:32:41 -04:00
return ( 100 * a ) / ( a + b ) ;
2023-03-03 15:23:46 +00:00
}
/ * *
* Get the rouble price for an assorts barter scheme
2023-11-15 20:35:05 -05:00
* @param barterScheme
2023-03-03 15:23:46 +00:00
* @returns Rouble price
* /
public getBarterPrice ( barterScheme : IBarterScheme [ ] ) : number
{
let price = 0 ;
for ( const item of barterScheme )
{
2024-05-20 07:58:13 +00:00
price += this . getStaticPriceForItem ( item . _tpl ) * item . count ;
2023-03-03 15:23:46 +00:00
}
return Math . round ( price ) ;
}
/ * *
* Generate a currency cost for an item and its mods
2024-03-18 16:23:04 +00:00
* @param offerItems Item with mods to get price for
2023-03-03 15:23:46 +00:00
* @param desiredCurrency Currency price desired in
2023-10-10 11:03:20 +00:00
* @param isPackOffer Price is for a pack type offer
2023-03-03 15:23:46 +00:00
* @returns cost of item in desired currency
* /
2024-03-18 16:23:04 +00:00
public getDynamicOfferPriceForOffer ( offerItems : Item [ ] , desiredCurrency : string , isPackOffer : boolean ) : number
2023-03-03 15:23:46 +00:00
{
2024-04-10 12:23:19 -04:00
// Price to return.
2023-03-03 15:23:46 +00:00
let price = 0 ;
2024-04-10 12:23:19 -04:00
// Iterate over each item in the offer.
2024-03-18 16:23:04 +00:00
for ( const item of offerItems )
2023-03-03 15:23:46 +00:00
{
2024-04-10 12:23:19 -04:00
// Skip over armour inserts as those are not factored into item prices.
2024-02-12 16:39:20 +00:00
if ( this . itemHelper . isOfBaseclass ( item . _tpl , BaseClasses . BUILT_IN_INSERTS ) )
{
continue ;
}
2024-04-10 12:23:19 -04:00
price += this . getDynamicItemPrice ( item . _tpl , desiredCurrency , item , offerItems , isPackOffer ) ;
2023-03-15 13:28:51 +00:00
2024-04-10 12:23:19 -04:00
// Check if the item is a weapon preset.
2024-05-17 15:32:41 -04:00
if (
item ? . upd ? . sptPresetId
&& this . presetHelper . isPresetBaseClass ( item . upd . sptPresetId , BaseClasses . WEAPON )
)
2023-03-15 13:28:51 +00:00
{
2024-04-10 12:23:19 -04:00
// This is a weapon preset, which has it's own price calculation that takes into account the mods in the
// preset. Since we've already calculated the price for the preset entire preset in
// `getDynamicItemPrice`, we can skip the rest of the items in the offer.
break ;
2023-03-15 13:28:51 +00:00
}
2024-04-10 12:23:19 -04:00
}
2023-03-03 15:23:46 +00:00
2024-04-10 12:23:19 -04:00
return Math . round ( price ) ;
}
2023-11-15 20:35:05 -05:00
2024-04-10 12:23:19 -04:00
/ * *
2024-05-06 15:20:44 +01:00
* @param itemTemplateId items tpl value
* @param desiredCurrency Currency to return result in
* @param item Item object ( used for weapon presets )
2024-04-10 12:23:19 -04:00
* @param offerItems
* @param isPackOffer
* @returns
* /
public getDynamicItemPrice (
itemTemplateId : string ,
desiredCurrency : string ,
item? : Item ,
offerItems? : Item [ ] ,
isPackOffer? : boolean ,
) : number
{
let isPreset = false ;
let price = this . getFleaPriceForItem ( itemTemplateId ) ;
// Adjust price if below handbook price, based on config.
if ( this . ragfairConfig . dynamic . offerAdjustment . adjustPriceWhenBelowHandbookPrice )
{
price = this . adjustPriceIfBelowHandbook ( price , itemTemplateId ) ;
}
2023-03-03 15:23:46 +00:00
2024-04-10 12:23:19 -04:00
// Use trader price if higher, based on config.
if ( this . ragfairConfig . dynamic . useTraderPriceForOffersIfHigher )
{
const traderPrice = this . traderHelper . getHighestSellToTraderPrice ( itemTemplateId ) ;
if ( traderPrice > price )
2024-02-09 09:56:18 +00:00
{
2024-04-10 12:23:19 -04:00
price = traderPrice ;
2024-02-09 09:56:18 +00:00
}
2024-04-10 12:23:19 -04:00
}
2024-02-09 09:56:18 +00:00
2024-04-10 12:23:19 -04:00
// Prices for weapon presets are handled differently.
if (
item ? . upd ? . sptPresetId
&& offerItems
&& this . presetHelper . isPresetBaseClass ( item . upd . sptPresetId , BaseClasses . WEAPON )
)
{
price = this . getWeaponPresetPrice ( item , offerItems , price ) ;
isPreset = true ;
}
2023-03-03 15:23:46 +00:00
2024-04-10 12:23:19 -04:00
// Check for existence of manual price adjustment multiplier
const multiplier = this . ragfairConfig . dynamic . itemPriceMultiplier [ itemTemplateId ] ;
if ( multiplier )
{
price *= multiplier ;
2023-03-03 15:23:46 +00:00
}
2024-04-10 12:23:19 -04:00
// The quality of the item affects the price.
if ( item )
2024-02-09 14:13:48 +00:00
{
2024-04-10 12:23:19 -04:00
const qualityModifier = this . itemHelper . getItemQualityModifier ( item ) ;
price *= qualityModifier ;
}
// Make adjustments for unreasonably priced items.
for ( const baseClassTemplateId of Object . keys ( this . ragfairConfig . dynamic . unreasonableModPrices ) )
{
if ( this . itemHelper . isOfBaseclass ( itemTemplateId , baseClassTemplateId ) )
2024-02-09 22:28:25 +00:00
{
2024-04-10 12:23:19 -04:00
// Found an unreasonable price type.
2024-05-07 23:57:08 -04:00
const unreasonableModifier : IUnreasonableModPrices
= this . ragfairConfig . dynamic . unreasonableModPrices [ baseClassTemplateId ] ;
2024-02-09 22:28:25 +00:00
2024-04-10 12:23:19 -04:00
if ( unreasonableModifier . enabled )
{
price = this . adjustUnreasonablePrice (
this . databaseServer . getTables ( ) . templates . handbook . Items ,
unreasonableModifier ,
itemTemplateId ,
price ,
) ;
2024-02-09 22:28:25 +00:00
}
}
2024-02-09 14:13:48 +00:00
}
2024-04-10 12:23:19 -04:00
// Vary the price based on the type of offer.
const range = this . getOfferTypeRangeValues ( isPreset , isPackOffer ) ;
price = this . randomiseOfferPrice ( price , range ) ;
2023-11-15 20:35:05 -05:00
2024-04-10 12:23:19 -04:00
// Convert to different currency if required.
const roublesId = Money . ROUBLES ;
if ( desiredCurrency !== roublesId )
2024-03-18 16:23:04 +00:00
{
price = this . handbookHelper . fromRUB ( price , desiredCurrency ) ;
}
2023-03-03 15:23:46 +00:00
if ( price < 1 )
{
2024-04-10 12:23:19 -04:00
return 1 ;
2023-03-03 15:23:46 +00:00
}
return price ;
}
2024-02-09 14:13:48 +00:00
/ * *
* using data from config , adjust an items price to be relative to its handbook price
* @param handbookPrices Prices of items in handbook
* @param unreasonableItemChange Change object from config
* @param itemTpl Item being adjusted
* @param price Current price of item
* @returns Adjusted price of item
* /
protected adjustUnreasonablePrice (
handbookPrices : HandbookItem [ ] ,
unreasonableItemChange : IUnreasonableModPrices ,
itemTpl : string ,
price : number ,
) : number
{
2024-05-17 15:32:41 -04:00
const itemHandbookPrice = handbookPrices . find ( ( handbookItem ) = > handbookItem . Id === itemTpl ) ;
2024-02-09 14:13:48 +00:00
if ( ! itemHandbookPrice )
{
return price ;
}
// Flea price is over handbook price
2024-05-07 23:57:08 -04:00
if ( price > itemHandbookPrice . Price * unreasonableItemChange . handbookPriceOverMultiplier )
2024-02-09 14:13:48 +00:00
{
// Skip extreme values
if ( price <= 1 )
{
return price ;
}
// Price is over limit, adjust
return itemHandbookPrice . Price * unreasonableItemChange . newPriceHandbookMultiplier ;
}
return price ;
}
2023-10-10 11:03:20 +00:00
/ * *
* Get different min / max price multipliers for different offer types ( preset / pack / default )
* @param isPreset Offer is a preset
* @param isPack Offer is a pack
* @returns MinMax values
* /
protected getOfferTypeRangeValues ( isPreset : boolean , isPack : boolean ) : MinMax
{
// Use different min/max values if the item is a preset or pack
const priceRanges = this . ragfairConfig . dynamic . priceRanges ;
if ( isPreset )
{
return priceRanges . preset ;
}
2024-02-02 15:00:12 -05:00
if ( isPack )
2023-10-10 11:03:20 +00:00
{
return priceRanges . pack ;
}
return priceRanges . default ;
}
2023-03-03 15:23:46 +00:00
/ * *
2024-02-02 15:00:12 -05:00
* Check to see if an items price is below its handbook price and adjust according to values set to config / ragfair . json
2023-03-03 15:23:46 +00:00
* @param itemPrice price of item
* @param itemTpl item template Id being checked
* @returns adjusted price value in roubles
* /
protected adjustPriceIfBelowHandbook ( itemPrice : number , itemTpl : string ) : number
{
const itemHandbookPrice = this . getStaticPriceForItem ( itemTpl ) ;
const priceDifferencePercent = this . getPriceDifference ( itemHandbookPrice , itemPrice ) ;
2024-04-10 12:23:19 -04:00
// Only adjust price if difference is > a percent AND item price passes threshold set in config
2023-11-15 20:35:05 -05:00
if (
2024-05-17 15:32:41 -04:00
priceDifferencePercent
> this . ragfairConfig . dynamic . offerAdjustment . maxPriceDifferenceBelowHandbookPercent
2023-11-15 20:35:05 -05:00
&& itemPrice >= this . ragfairConfig . dynamic . offerAdjustment . priceThreshholdRub
)
2023-03-03 15:23:46 +00:00
{
2023-11-15 20:35:05 -05:00
// const itemDetails = this.itemHelper.getItem(itemTpl);
// this.logger.debug(`item below handbook price ${itemDetails[1]._name} handbook: ${itemHandbookPrice} flea: ${itemPrice} ${priceDifferencePercent}%`);
2024-04-17 13:10:43 +00:00
return Math . round ( itemHandbookPrice * this . ragfairConfig . dynamic . offerAdjustment . handbookPriceMultipier ) ;
2023-03-03 15:23:46 +00:00
}
return itemPrice ;
}
/ * *
* Multiply the price by a randomised curve where n = 2 , shift = 2
* @param existingPrice price to alter
2023-10-10 11:03:20 +00:00
* @param rangeValues min and max to adjust price by
2023-03-03 15:23:46 +00:00
* @returns multiplied price
* /
2023-10-10 11:03:20 +00:00
protected randomiseOfferPrice ( existingPrice : number , rangeValues : MinMax ) : number
2023-03-03 15:23:46 +00:00
{
// Multiply by 100 to get 2 decimal places of precision
2023-10-10 11:03:20 +00:00
const multiplier = this . randomUtil . getBiasedRandomNumber ( rangeValues . min * 100 , rangeValues . max * 100 , 2 , 2 ) ;
2023-03-03 15:23:46 +00:00
// return multiplier back to its original decimal place location
return existingPrice * ( multiplier / 100 ) ;
}
/ * *
* Calculate the cost of a weapon preset by adding together the price of its mods + base price of default weapon preset
2024-01-27 20:00:18 +00:00
* @param weaponRootItem base weapon
* @param weaponWithChildren weapon plus mods
2023-03-03 15:23:46 +00:00
* @param existingPrice price of existing base weapon
2023-07-19 13:16:45 +01:00
* @returns price of weapon in roubles
2023-03-03 15:23:46 +00:00
* /
2024-01-27 20:00:18 +00:00
protected getWeaponPresetPrice ( weaponRootItem : Item , weaponWithChildren : Item [ ] , existingPrice : number ) : number
2023-03-03 15:23:46 +00:00
{
// Get the default preset for this weapon
2024-01-27 20:00:18 +00:00
const presetResult = this . getWeaponPreset ( weaponRootItem ) ;
2023-03-03 15:23:46 +00:00
if ( presetResult . isDefault )
{
2024-01-27 20:00:18 +00:00
return this . getFleaPriceForItem ( weaponRootItem . _tpl ) ;
2023-03-03 15:23:46 +00:00
}
// Get mods on current gun not in default preset
2024-05-17 15:32:41 -04:00
const newOrReplacedModsInPresetVsDefault = weaponWithChildren . filter (
( x ) = > ! presetResult . preset . _items . some ( ( y ) = > y . _tpl === x . _tpl ) ,
2023-11-15 20:35:05 -05:00
) ;
2023-03-03 15:23:46 +00:00
// Add up extra mods price
let extraModsPrice = 0 ;
for ( const mod of newOrReplacedModsInPresetVsDefault )
{
// Use handbook or trader price, whatever is higher (dont use dynamic flea price as purchased item cannot be relisted)
extraModsPrice += this . getHighestHandbookOrTraderPriceAsRouble ( mod . _tpl ) ;
}
// Only deduct cost of replaced mods if there's replaced/new mods
if ( newOrReplacedModsInPresetVsDefault . length >= 1 )
{
// Add up cost of mods replaced
2024-05-17 15:32:41 -04:00
const modsReplacedByNewMods = newOrReplacedModsInPresetVsDefault . filter ( ( x ) = >
presetResult . preset . _items . some ( ( y ) = > y . slotId === x . slotId ) ,
2023-11-15 20:35:05 -05:00
) ;
2023-03-03 15:23:46 +00:00
// Add up replaced mods price
let replacedModsPrice = 0 ;
for ( const replacedMod of modsReplacedByNewMods )
{
replacedModsPrice += this . getHighestHandbookOrTraderPriceAsRouble ( replacedMod . _tpl ) ;
}
// Subtract replaced mods total from extra mods total
extraModsPrice -= replacedModsPrice ;
}
// return extra mods price + base gun price
2023-10-10 11:03:20 +00:00
return existingPrice + extraModsPrice ;
2023-03-03 15:23:46 +00:00
}
/ * *
* Get the highest price for an item that is stored in handbook or trader assorts
* @param itemTpl Item to get highest price of
* @returns rouble cost
* /
protected getHighestHandbookOrTraderPriceAsRouble ( itemTpl : string ) : number
{
let price = this . getStaticPriceForItem ( itemTpl ) ;
2023-03-15 19:06:03 +00:00
const traderPrice = this . traderHelper . getHighestSellToTraderPrice ( itemTpl ) ;
2023-03-03 15:23:46 +00:00
if ( traderPrice > price )
{
price = traderPrice ;
}
return price ;
}
/ * *
* Attempt to get the default preset for a weapon , failing that get the first preset in the array
* ( assumes default = has encyclopedia entry )
* @param presets weapon presets to choose from
* @returns Default preset object
* /
2024-05-07 23:57:08 -04:00
protected getWeaponPreset ( weapon : Item ) : { isDefault : boolean , preset : IPreset }
2023-03-03 15:23:46 +00:00
{
2024-02-02 13:54:07 -05:00
const defaultPreset = this . presetHelper . getDefaultPreset ( weapon . _tpl ) ;
2023-03-03 15:23:46 +00:00
if ( defaultPreset )
{
2023-11-15 20:35:05 -05:00
return { isDefault : true , preset : defaultPreset } ;
2023-03-03 15:23:46 +00:00
}
2024-02-02 13:54:07 -05:00
const nonDefaultPresets = this . presetHelper . getPresets ( weapon . _tpl ) ;
2024-01-27 20:00:18 +00:00
if ( nonDefaultPresets . length === 1 )
2023-03-03 15:23:46 +00:00
{
2023-11-15 20:35:05 -05:00
this . logger . debug (
2024-05-17 15:32:41 -04:00
` Item Id: ${ weapon . _tpl } has no default encyclopedia entry but only one preset ( ${ nonDefaultPresets [ 0 ] . _name } ), choosing preset ( ${ nonDefaultPresets [ 0 ] . _name } ) ` ,
2023-11-15 20:35:05 -05:00
) ;
2023-03-03 15:23:46 +00:00
}
else
{
2023-11-15 20:35:05 -05:00
this . logger . debug (
2024-05-17 15:32:41 -04:00
` Item Id: ${ weapon . _tpl } has no default encyclopedia entry, choosing first preset ( ${ nonDefaultPresets [ 0 ] . _name } ) of ${ nonDefaultPresets . length } ` ,
2023-11-15 20:35:05 -05:00
) ;
2023-03-03 15:23:46 +00:00
}
2024-01-27 20:00:18 +00:00
return { isDefault : false , preset : nonDefaultPresets [ 0 ] } ;
2023-03-03 15:23:46 +00:00
}
2023-11-15 20:35:05 -05:00
}