Replaced magic strings with use of ItemTpl enum

This commit is contained in:
Dev 2024-07-29 10:19:02 +01:00
parent 3099403181
commit 530aad88b9
2 changed files with 46 additions and 57 deletions

View File

@ -21,9 +21,9 @@ import * as baseJson from "../db/base.json";
import { TraderHelper } from "./traderHelpers"; import { TraderHelper } from "./traderHelpers";
import { FluentAssortConstructor as FluentAssortCreator } from "./fluentTraderAssortCreator"; import { FluentAssortConstructor as FluentAssortCreator } from "./fluentTraderAssortCreator";
import { ItemTpl } from "@spt/models/enums/ItemTpl";
class SampleTrader implements IPreSptLoadMod, IPostDBLoadMod class SampleTrader implements IPreSptLoadMod, IPostDBLoadMod {
{
private mod: string; private mod: string;
private logger: ILogger; private logger: ILogger;
private traderHelper: TraderHelper; private traderHelper: TraderHelper;
@ -37,8 +37,7 @@ class SampleTrader implements IPreSptLoadMod, IPostDBLoadMod
* Some work needs to be done prior to SPT code being loaded, registering the profile image + setting trader update time inside the trader config json * Some work needs to be done prior to SPT code being loaded, registering the profile image + setting trader update time inside the trader config json
* @param container Dependency container * @param container Dependency container
*/ */
public preSptLoad(container: DependencyContainer): void public preSptLoad(container: DependencyContainer): void {
{
// Get a logger // Get a logger
this.logger = container.resolve<ILogger>("WinstonLogger"); this.logger = container.resolve<ILogger>("WinstonLogger");
this.logger.debug(`[${this.mod}] preSpt Loading... `); this.logger.debug(`[${this.mod}] preSpt Loading... `);
@ -70,8 +69,7 @@ class SampleTrader implements IPreSptLoadMod, IPostDBLoadMod
* Majority of trader-related work occurs after the aki database has been loaded but prior to SPT code being run * Majority of trader-related work occurs after the aki database has been loaded but prior to SPT code being run
* @param container Dependency container * @param container Dependency container
*/ */
public postDBLoad(container: DependencyContainer): void public postDBLoad(container: DependencyContainer): void {
{
this.logger.debug(`[${this.mod}] postDb Loading... `); this.logger.debug(`[${this.mod}] postDb Loading... `);
// Resolve SPT classes we'll use // Resolve SPT classes we'll use
@ -86,9 +84,9 @@ class SampleTrader implements IPreSptLoadMod, IPostDBLoadMod
this.traderHelper.addTraderToDb(baseJson, tables, jsonUtil); this.traderHelper.addTraderToDb(baseJson, tables, jsonUtil);
// Add milk // Add milk
const MILK_ID = "575146b724597720a27126d5"; // Can find item ids in `database\templates\items.json` or with https://db.sp-tarkov.com/search
this.fluentAssortCreator this.fluentAssortCreator
.createSingleAssortItem(MILK_ID) .createSingleAssortItem(ItemTpl.DRINK_PACK_OF_MILK)
.addStackCount(200) .addStackCount(200)
.addBuyRestriction(10) .addBuyRestriction(10)
.addMoneyCost(Money.ROUBLES, 2000) .addMoneyCost(Money.ROUBLES, 2000)
@ -96,13 +94,11 @@ class SampleTrader implements IPreSptLoadMod, IPostDBLoadMod
.export(tables.traders[baseJson._id]); .export(tables.traders[baseJson._id]);
// Add 3x bitcoin + salewa for milk barter // Add 3x bitcoin + salewa for milk barter
const BITCOIN_ID = "59faff1d86f7746c51718c9c";
const SALEWA_ID = "544fb45d4bdc2dee738b4568";
this.fluentAssortCreator this.fluentAssortCreator
.createSingleAssortItem(MILK_ID) .createSingleAssortItem(ItemTpl.DRINK_PACK_OF_MILK)
.addStackCount(100) .addStackCount(100)
.addBarterCost(BITCOIN_ID, 3) .addBarterCost(ItemTpl.BARTER_PHYSICAL_BITCOIN, 3)
.addBarterCost(SALEWA_ID, 1) .addBarterCost(ItemTpl.MEDKIT_SALEWA_FIRST_AID_KIT, 1)
.addLoyaltyLevel(1) .addLoyaltyLevel(1)
.export(tables.traders[baseJson._id]); .export(tables.traders[baseJson._id]);
@ -118,9 +114,9 @@ class SampleTrader implements IPreSptLoadMod, IPostDBLoadMod
// Add mp133 preset as mayo barter // Add mp133 preset as mayo barter
this.fluentAssortCreator this.fluentAssortCreator
.createComplexAssortItem(tables.globals.ItemPresets["584148f2245977598f1ad387"]._items) .createComplexAssortItem(tables.globals.ItemPresets["584148f2245977598f1ad387"]._items) // Weapon preset id comes from globals.json
.addStackCount(200) .addStackCount(200)
.addBarterCost("5bc9b156d4351e00367fbce9", 1) .addBarterCost(ItemTpl.FOOD_JAR_OF_DEVILDOG_MAYO, 1)
.addBuyRestriction(3) .addBuyRestriction(3)
.addLoyaltyLevel(1) .addLoyaltyLevel(1)
.export(tables.traders[baseJson._id]); .export(tables.traders[baseJson._id]);

View File

@ -6,8 +6,7 @@ import { IDatabaseTables } from "@spt/models/spt/server/IDatabaseTables";
import { ImageRouter } from "@spt/routers/ImageRouter"; import { ImageRouter } from "@spt/routers/ImageRouter";
import { JsonUtil } from "@spt/utils/JsonUtil"; import { JsonUtil } from "@spt/utils/JsonUtil";
export class TraderHelper export class TraderHelper {
{
/** /**
* Add profile picture to our trader * Add profile picture to our trader
* @param baseJson json file for trader (db/base.json) * @param baseJson json file for trader (db/base.json)
@ -16,8 +15,7 @@ export class TraderHelper
* @param imageRouter image router class - used to register the trader image path so we see their image on trader page * @param imageRouter image router class - used to register the trader image path so we see their image on trader page
* @param traderImageName Filename of the trader icon to use * @param traderImageName Filename of the trader icon to use
*/ */
public registerProfileImage(baseJson: any, modName: string, preSptModLoader: PreSptModLoader, imageRouter: ImageRouter, traderImageName: string): void public registerProfileImage(baseJson: any, modName: string, preSptModLoader: PreSptModLoader, imageRouter: ImageRouter, traderImageName: string): void {
{
// Reference the mod "res" folder // Reference the mod "res" folder
const imageFilepath = `./${preSptModLoader.getModPath(modName)}res`; const imageFilepath = `./${preSptModLoader.getModPath(modName)}res`;
@ -32,8 +30,7 @@ export class TraderHelper
* @param refreshTimeSecondsMin How many seconds between trader stock refresh min time * @param refreshTimeSecondsMin How many seconds between trader stock refresh min time
* @param refreshTimeSecondsMax How many seconds between trader stock refresh max time * @param refreshTimeSecondsMax How many seconds between trader stock refresh max time
*/ */
public setTraderUpdateTime(traderConfig: ITraderConfig, baseJson: any, refreshTimeSecondsMin: number, refreshTimeSecondsMax: number): void public setTraderUpdateTime(traderConfig: ITraderConfig, baseJson: any, refreshTimeSecondsMin: number, refreshTimeSecondsMax: number): void {
{
// Add refresh time in seconds to config // Add refresh time in seconds to config
const traderRefreshRecord: UpdateTime = { const traderRefreshRecord: UpdateTime = {
traderId: baseJson._id, traderId: baseJson._id,
@ -53,8 +50,7 @@ export class TraderHelper
* @param jsonUtil json utility class * @param jsonUtil json utility class
*/ */
// rome-ignore lint/suspicious/noExplicitAny: traderDetailsToAdd comes from base.json, so no type // rome-ignore lint/suspicious/noExplicitAny: traderDetailsToAdd comes from base.json, so no type
public addTraderToDb(traderDetailsToAdd: any, tables: IDatabaseTables, jsonUtil: JsonUtil): void public addTraderToDb(traderDetailsToAdd: any, tables: IDatabaseTables, jsonUtil: JsonUtil): void {
{
// Add trader to trader table, key is the traders id // Add trader to trader table, key is the traders id
tables.traders[traderDetailsToAdd._id] = { tables.traders[traderDetailsToAdd._id] = {
assort: this.createAssortTable(), // assorts are the 'offers' trader sells, can be a single item (e.g. carton of milk) or multiple items as a collection (e.g. a gun) assort: this.createAssortTable(), // assorts are the 'offers' trader sells, can be a single item (e.g. carton of milk) or multiple items as a collection (e.g. a gun)
@ -71,8 +67,7 @@ export class TraderHelper
* Create basic data for trader + add empty assorts table for trader * Create basic data for trader + add empty assorts table for trader
* @returns ITraderAssort * @returns ITraderAssort
*/ */
private createAssortTable(): ITraderAssort private createAssortTable(): ITraderAssort {
{
// Create a blank assort object, ready to have items added // Create a blank assort object, ready to have items added
const assortTable: ITraderAssort = { const assortTable: ITraderAssort = {
nextResupply: 0, nextResupply: 0,
@ -88,8 +83,7 @@ export class TraderHelper
* Create a weapon from scratch, ready to be added to trader * Create a weapon from scratch, ready to be added to trader
* @returns Item[] * @returns Item[]
*/ */
public createGlock(): Item[] public createGlock(): Item[] {
{
// Create an array ready to hold weapon + all mods // Create an array ready to hold weapon + all mods
const glock: Item[] = []; const glock: Item[] = [];
@ -168,8 +162,7 @@ export class TraderHelper
* @param location Location of trader (e.g. "Here in the cat shop") * @param location Location of trader (e.g. "Here in the cat shop")
* @param description Description of trader * @param description Description of trader
*/ */
public addTraderToLocales(baseJson: any, tables: IDatabaseTables, fullName: string, firstName: string, nickName: string, location: string, description: string) public addTraderToLocales(baseJson: any, tables: IDatabaseTables, fullName: string, firstName: string, nickName: string, location: string, description: string) {
{
// For each language, add locale for the new trader // For each language, add locale for the new trader
const locales = Object.values(tables.locales.global); const locales = Object.values(tables.locales.global);