Server/project/src/controllers/TraderController.ts

186 lines
6.9 KiB
TypeScript
Raw Normal View History

2023-03-03 15:23:46 +00:00
import { inject, injectable } from "tsyringe";
import { FenceBaseAssortGenerator } from "@spt-aki/generators/FenceBaseAssortGenerator";
import { ProfileHelper } from "@spt-aki/helpers/ProfileHelper";
import { TraderAssortHelper } from "@spt-aki/helpers/TraderAssortHelper";
import { TraderHelper } from "@spt-aki/helpers/TraderHelper";
import { ITraderAssort, ITraderBase } from "@spt-aki/models/eft/common/tables/ITrader";
import { ConfigTypes } from "@spt-aki/models/enums/ConfigTypes";
import { Traders } from "@spt-aki/models/enums/Traders";
import { ITraderConfig } from "@spt-aki/models/spt/config/ITraderConfig";
import { ILogger } from "@spt-aki/models/spt/utils/ILogger";
import { ConfigServer } from "@spt-aki/servers/ConfigServer";
import { DatabaseServer } from "@spt-aki/servers/DatabaseServer";
import { FenceService } from "@spt-aki/services/FenceService";
import { TraderAssortService } from "@spt-aki/services/TraderAssortService";
import { TraderPurchasePersisterService } from "@spt-aki/services/TraderPurchasePersisterService";
import { ICloner } from "@spt-aki/utils/cloners/ICloner";
import { TimeUtil } from "@spt-aki/utils/TimeUtil";
2023-03-03 15:23:46 +00:00
@injectable()
export class TraderController
{
protected traderConfig: ITraderConfig;
2023-03-03 15:23:46 +00:00
constructor(
@inject("WinstonLogger") protected logger: ILogger,
@inject("TimeUtil") protected timeUtil: TimeUtil,
2023-03-03 15:23:46 +00:00
@inject("DatabaseServer") protected databaseServer: DatabaseServer,
@inject("TraderAssortHelper") protected traderAssortHelper: TraderAssortHelper,
@inject("ProfileHelper") protected profileHelper: ProfileHelper,
@inject("TraderHelper") protected traderHelper: TraderHelper,
@inject("TraderAssortService") protected traderAssortService: TraderAssortService,
@inject("TraderPurchasePersisterService")
protected traderPurchasePersisterService: TraderPurchasePersisterService,
2023-03-03 15:23:46 +00:00
@inject("FenceService") protected fenceService: FenceService,
@inject("FenceBaseAssortGenerator") protected fenceBaseAssortGenerator: FenceBaseAssortGenerator,
@inject("ConfigServer") protected configServer: ConfigServer,
@inject("RecursiveCloner") protected cloner: ICloner,
2023-03-03 15:23:46 +00:00
)
{
this.traderConfig = this.configServer.getConfig(ConfigTypes.TRADER);
}
2023-03-03 15:23:46 +00:00
/**
* Runs when onLoad event is fired
* Iterate over traders, ensure a pristine copy of their assorts is stored in traderAssortService
2023-03-03 15:23:46 +00:00
* Store timestamp of next assort refresh in nextResupply property of traders .base object
*/
public load(): void
{
const nextHourTimestamp = this.timeUtil.getTimestampOfNextHour();
const traderResetStartsWithServer = this.traderConfig.tradersResetFromServerStart;
2023-03-03 15:23:46 +00:00
for (const traderId in this.databaseServer.getTables().traders)
{
if (traderId === "ragfair" || traderId === Traders.LIGHTHOUSEKEEPER)
{
continue;
}
if (traderId === Traders.FENCE)
{
this.fenceBaseAssortGenerator.generateFenceBaseAssorts();
this.fenceService.generateFenceAssorts();
continue;
}
const trader = this.databaseServer.getTables().traders[traderId];
// Create dict of trader assorts on server start
if (!this.traderAssortService.getPristineTraderAssort(traderId))
{
const assortsClone = this.cloner.clone(trader.assort);
this.traderAssortService.setPristineTraderAssort(traderId, assortsClone);
2023-03-03 15:23:46 +00:00
}
this.traderPurchasePersisterService.removeStalePurchasesFromProfiles(traderId);
// Set to next hour on clock or current time + 60 mins
trader.base.nextResupply = traderResetStartsWithServer
? this.traderHelper.getNextUpdateTimestamp(trader.base._id)
: nextHourTimestamp;
2023-03-03 15:23:46 +00:00
this.databaseServer.getTables().traders[trader.base._id].base = trader.base;
}
}
/**
* Runs when onUpdate is fired
* If current time is > nextResupply(expire) time of trader, refresh traders assorts and
* Fence is handled slightly differently
* @returns has run
*/
public update(): boolean
{
for (const traderId in this.databaseServer.getTables().traders)
{
if (traderId === "ragfair" || traderId === Traders.LIGHTHOUSEKEEPER)
{
continue;
}
if (traderId === Traders.FENCE)
{
if (this.fenceService.needsPartialRefresh())
{
this.fenceService.performPartialRefresh();
}
continue;
}
const trader = this.databaseServer.getTables().traders[traderId];
2023-11-15 20:35:05 -05:00
2023-03-03 15:23:46 +00:00
// trader needs to be refreshed
if (this.traderAssortHelper.traderAssortsHaveExpired(traderId))
{
this.traderAssortHelper.resetExpiredTrader(trader);
// Reset purchase data per trader as they have independent reset times
this.traderPurchasePersisterService.resetTraderPurchasesStoredInProfile(trader.base._id);
}
}
return true;
}
/**
2023-07-15 14:49:25 +01:00
* Handle client/trading/api/traderSettings
2023-03-03 15:23:46 +00:00
* Return an array of all traders
* @param sessionID Session id
* @returns array if ITraderBase objects
*/
public getAllTraders(sessionID: string): ITraderBase[]
{
const traders: ITraderBase[] = [];
const pmcData = this.profileHelper.getPmcProfile(sessionID);
for (const traderID in this.databaseServer.getTables().traders)
{
if (this.databaseServer.getTables().traders[traderID].base._id === "ragfair")
{
continue;
}
traders.push(this.traderHelper.getTrader(traderID, sessionID));
if (pmcData.Info)
{
this.traderHelper.lvlUp(traderID, pmcData);
2023-03-03 15:23:46 +00:00
}
}
2023-11-15 20:35:05 -05:00
return traders.sort((a, b) => this.sortByTraderId(a, b));
2023-03-03 15:23:46 +00:00
}
/**
* Order traders by their traderId (Ttid)
* @param traderA First trader to compare
* @param traderB Second trader to compare
* @returns 1,-1 or 0
*/
protected sortByTraderId(traderA: ITraderBase, traderB: ITraderBase): number
{
if (traderA._id > traderB._id)
{
return 1;
}
if (traderA._id < traderB._id)
{
return -1;
}
return 0;
}
2023-07-15 14:49:25 +01:00
/** Handle client/trading/api/getTrader */
2023-03-03 15:23:46 +00:00
public getTrader(sessionID: string, traderID: string): ITraderBase
{
return this.traderHelper.getTrader(sessionID, traderID);
}
2023-07-15 14:49:25 +01:00
/** Handle client/trading/api/getTraderAssort */
2023-03-03 15:23:46 +00:00
public getAssort(sessionId: string, traderId: string): ITraderAssort
{
return this.traderAssortHelper.getAssort(sessionId, traderId);
}
2023-11-15 20:35:05 -05:00
}