update-addtrader-modexample #41

Closed
Echo55 wants to merge 2 commits from (deleted):update-addtrader-modexample into master
2 changed files with 27 additions and 39 deletions
Showing only changes of commit 46e56d6a16 - Show all commits

View File

@ -4,7 +4,6 @@ import { DependencyContainer } from "tsyringe";
import { IPreSptLoadMod } from "@spt/models/external/IPreSptLoadMod";
import { IPostDBLoadMod } from "@spt/models/external/IPostDBLoadMod";
import { ILogger } from "@spt/models/spt/utils/ILogger";
import { PreSptModLoader } from "@spt/loaders/PreSptModLoader";
import { DatabaseServer } from "@spt/servers/DatabaseServer";
import { ImageRouter } from "@spt/routers/ImageRouter";
import { ConfigServer } from "@spt/servers/ConfigServer";
@ -25,6 +24,7 @@ import { ItemTpl } from "@spt/models/enums/ItemTpl";
class SampleTrader implements IPreSptLoadMod, IPostDBLoadMod {
private mod: string;
private traderImgPath: string;
private logger: ILogger;
private traderHelper: TraderHelper;
private fluentAssortCreator: FluentAssortCreator;
@ -34,6 +34,7 @@ class SampleTrader implements IPreSptLoadMod, IPostDBLoadMod {
constructor() {
this.mod = "13AddTrader"; // Set name of mod so we can log it to console later
this.traderImgPath = "res/cat.jpg"; // Set path to trader image
// Get base json from /db/ folder
this.traderBase = baseJson;
this.traderNickname = "Cat"
@ -49,7 +50,6 @@ class SampleTrader implements IPreSptLoadMod, IPostDBLoadMod {
this.logger.debug(`[${this.mod}] preSpt Loading... `);
// Get SPT code/data we need later
const preSptModLoader: PreSptModLoader = container.resolve<PreSptModLoader>("PreSptModLoader");
const imageRouter: ImageRouter = container.resolve<ImageRouter>("ImageRouter");
const hashUtil: HashUtil = container.resolve<HashUtil>("HashUtil");
const configServer = container.resolve<ConfigServer>("ConfigServer");
@ -58,12 +58,15 @@ class SampleTrader implements IPreSptLoadMod, IPostDBLoadMod {
// IMPORTANT - YOUR TRADER NEEDS A UNIQUE MONGO-ID, ASK IN DISCORD IF YOU DONT KNOW WHAT THAT IS
// GOOGLE "mongoid generator"
this.traderBase._id = "66eeef8b2a166b73d2066a7e";
this.traderBase = {
...baseJson,
_id: "66eeef8b2a166b73d2066a7e" // This is a unique ID for your trader, also the first key in base.json
};
// Create helper class and use it to register our traders image/icon + set its stock refresh time
this.traderHelper = new TraderHelper();
this.fluentAssortCreator = new FluentAssortCreator(hashUtil, this.logger);
this.traderHelper.registerProfileImage(this.traderBase, this.mod, preSptModLoader, imageRouter, "cat.jpg");
imageRouter.addRoute(this.traderBase.avatar.replace(".jpg", ""), this.traderImgPath);
this.traderHelper.setTraderUpdateTime(traderConfig, this.traderBase, 3600, 4000);
// Add trader to trader enum

View File

@ -1,28 +1,13 @@
import { PreSptModLoader } from "@spt/loaders/PreSptModLoader";
import { Item } from "@spt/models/eft/common/tables/IItem";
import { ITraderBase, ITraderAssort } from "@spt/models/eft/common/tables/ITrader";
import { ITraderConfig, UpdateTime } from "@spt/models/spt/config/ITraderConfig";
import { IDatabaseTables } from "@spt/models/spt/server/IDatabaseTables";
import { ImageRouter } from "@spt/routers/ImageRouter";
import { JsonUtil } from "@spt/utils/JsonUtil";
import { NewItemIds } from "./newitemIds";
import {PreSptModLoader} from "@spt/loaders/PreSptModLoader";
import {IItem} from "@spt/models/eft/common/tables/IItem";
import {ITraderBase, ITraderAssort} from "@spt/models/eft/common/tables/ITrader";
import {ITraderConfig, IUpdateTime} from "@spt/models/spt/config/ITraderConfig";
import {IDatabaseTables} from "@spt/models/spt/server/IDatabaseTables";
import {ImageRouter} from "@spt/routers/ImageRouter";
import {JsonUtil} from "@spt/utils/JsonUtil";
import {NewItemIds} from "./newItemIds";
export class TraderHelper {
/**
* Add profile picture to our trader
* @param baseJson json file for trader (db/base.json)
* @param modName mod folder name
* @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 traderImageName Filename of the trader icon to use
*/
public registerProfileImage(baseJson: any, modName: string, preSptModLoader: PreSptModLoader, imageRouter: ImageRouter, traderImageName: string): void {
// 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
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)
@ -33,7 +18,7 @@ export class TraderHelper {
*/
public setTraderUpdateTime(traderConfig: ITraderConfig, baseJson: any, refreshTimeSecondsMin: number, refreshTimeSecondsMax: number): void {
// Add refresh time in seconds to config
const traderRefreshRecord: UpdateTime = {
const traderRefreshRecord: IUpdateTime = {
traderId: baseJson._id,
seconds: {
min: refreshTimeSecondsMin,
@ -83,9 +68,9 @@ export class TraderHelper {
* Create a weapon from scratch, ready to be added to trader
* @returns Item[]
*/
public createGlock(): Item[] {
public createGlock(): IItem[] {
// Create an array ready to hold weapon + all mods
const glock: Item[] = [];
const glock: IItem[] = [];
// Add the base first
glock.push({ // Add the base weapon first