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,24 +6,22 @@ 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) * @param modName mod folder name
* @param modName mod folder name * @param preSptModLoader mod loader class - used to get the mods file path
* @param preSptModLoader mod loader class - used to get the mods file path * @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
{ const imageFilepath = `./${preSptModLoader.getModPath(modName)}res`;
// Reference the mod "res" folder
const imageFilepath = `./${preSptModLoader.getModPath(modName)}res`;
// Register a route to point to the profile picture - remember to remove the .jpg from it // Register a route to point to the profile picture - remember to remove the .jpg from it
imageRouter.addRoute(baseJson.avatar.replace(".jpg", ""), `${imageFilepath}/${traderImageName}`); imageRouter.addRoute(baseJson.avatar.replace(".jpg", ""), `${imageFilepath}/${traderImageName}`);
} }
/** /**
* Add record to trader config to set the refresh time of trader in seconds (default is 60 minutes) * Add record to trader config to set the refresh time of trader in seconds (default is 60 minutes)
@ -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[] = [];
@ -110,15 +104,15 @@ export class TraderHelper
// Add reciever // Add reciever
glock.push({ glock.push({
_id: "glockReciever", _id: "glockReciever",
_tpl:"5a9685b1a2750c0032157104", _tpl: "5a9685b1a2750c0032157104",
parentId: "glockBase", parentId: "glockBase",
slotId: "mod_reciever", slotId: "mod_reciever",
}); });
// Add compensator // Add compensator
glock.push({ glock.push({
_id: "glockCompensator", _id: "glockCompensator",
_tpl:"5a7b32a2e899ef00135e345a", _tpl: "5a7b32a2e899ef00135e345a",
parentId: "glockReciever", // The parent of this mod is the reciever NOT weapon, be careful to get the correct parent parentId: "glockReciever", // The parent of this mod is the reciever NOT weapon, be careful to get the correct parent
slotId: "mod_muzzle", slotId: "mod_muzzle",
}); });
@ -126,7 +120,7 @@ export class TraderHelper
// Add Pistol grip // Add Pistol grip
glock.push({ glock.push({
_id: "glockPistolGrip", _id: "glockPistolGrip",
_tpl:"5a7b4960e899ef197b331a2d", _tpl: "5a7b4960e899ef197b331a2d",
parentId: "glockBase", parentId: "glockBase",
slotId: "mod_pistol_grip", slotId: "mod_pistol_grip",
}); });
@ -158,18 +152,17 @@ export class TraderHelper
return glock; return glock;
} }
/** /**
* Add traders name/location/description to the locale table * Add traders name/location/description to the locale table
* @param baseJson json file for trader (db/base.json) * @param baseJson json file for trader (db/base.json)
* @param tables database tables * @param tables database tables
* @param fullName Complete name of trader * @param fullName Complete name of trader
* @param firstName First name of trader * @param firstName First name of trader
* @param nickName Nickname of trader * @param nickName Nickname of trader
* @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);