Allow traders to have timers be based on amount of time to next hour (bsg style) OR spt server start time

This commit is contained in:
Dev 2024-03-12 14:27:07 +00:00
parent 8397e4690b
commit dcd70fb5b5
4 changed files with 29 additions and 3 deletions

View File

@ -42,6 +42,7 @@
} }
], ],
"updateTimeDefault": 3600, "updateTimeDefault": 3600,
"tradersResetFromServerStart": false,
"purchasesAreFoundInRaid": false, "purchasesAreFoundInRaid": false,
"traderPriceMultipler": 1, "traderPriceMultipler": 1,
"persistPurchaseDataInProfile": true, "persistPurchaseDataInProfile": true,

View File

@ -5,19 +5,26 @@ import { ProfileHelper } from "@spt-aki/helpers/ProfileHelper";
import { TraderAssortHelper } from "@spt-aki/helpers/TraderAssortHelper"; import { TraderAssortHelper } from "@spt-aki/helpers/TraderAssortHelper";
import { TraderHelper } from "@spt-aki/helpers/TraderHelper"; import { TraderHelper } from "@spt-aki/helpers/TraderHelper";
import { ITraderAssort, ITraderBase } from "@spt-aki/models/eft/common/tables/ITrader"; 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 { 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 { ILogger } from "@spt-aki/models/spt/utils/ILogger";
import { ConfigServer } from "@spt-aki/servers/ConfigServer";
import { DatabaseServer } from "@spt-aki/servers/DatabaseServer"; import { DatabaseServer } from "@spt-aki/servers/DatabaseServer";
import { FenceService } from "@spt-aki/services/FenceService"; import { FenceService } from "@spt-aki/services/FenceService";
import { TraderAssortService } from "@spt-aki/services/TraderAssortService"; import { TraderAssortService } from "@spt-aki/services/TraderAssortService";
import { TraderPurchasePersisterService } from "@spt-aki/services/TraderPurchasePersisterService"; import { TraderPurchasePersisterService } from "@spt-aki/services/TraderPurchasePersisterService";
import { JsonUtil } from "@spt-aki/utils/JsonUtil"; import { JsonUtil } from "@spt-aki/utils/JsonUtil";
import { TimeUtil } from "@spt-aki/utils/TimeUtil";
@injectable() @injectable()
export class TraderController export class TraderController
{ {
protected traderConfig: ITraderConfig;
constructor( constructor(
@inject("WinstonLogger") protected logger: ILogger, @inject("WinstonLogger") protected logger: ILogger,
@inject("TimeUtil") protected timeUtil: TimeUtil,
@inject("DatabaseServer") protected databaseServer: DatabaseServer, @inject("DatabaseServer") protected databaseServer: DatabaseServer,
@inject("TraderAssortHelper") protected traderAssortHelper: TraderAssortHelper, @inject("TraderAssortHelper") protected traderAssortHelper: TraderAssortHelper,
@inject("ProfileHelper") protected profileHelper: ProfileHelper, @inject("ProfileHelper") protected profileHelper: ProfileHelper,
@ -28,16 +35,21 @@ export class TraderController
@inject("FenceService") protected fenceService: FenceService, @inject("FenceService") protected fenceService: FenceService,
@inject("FenceBaseAssortGenerator") protected fenceBaseAssortGenerator: FenceBaseAssortGenerator, @inject("FenceBaseAssortGenerator") protected fenceBaseAssortGenerator: FenceBaseAssortGenerator,
@inject("JsonUtil") protected jsonUtil: JsonUtil, @inject("JsonUtil") protected jsonUtil: JsonUtil,
@inject("ConfigServer") protected configServer: ConfigServer,
) )
{} {
this.traderConfig = this.configServer.getConfig(ConfigTypes.TRADER);
}
/** /**
* Runs when onLoad event is fired * Runs when onLoad event is fired
* Iterate over traders, ensure an unmolested copy of their assorts is stored in traderAssortService * Iterate over traders, ensure a pristine copy of their assorts is stored in traderAssortService
* Store timestamp of next assort refresh in nextResupply property of traders .base object * Store timestamp of next assort refresh in nextResupply property of traders .base object
*/ */
public load(): void public load(): void
{ {
const nextHourTimestamp = this.timeUtil.getTimestampOfNextHour();
const traderResetStartsWithServer = this.traderConfig.tradersResetFromServerStart;
for (const traderId in this.databaseServer.getTables().traders) for (const traderId in this.databaseServer.getTables().traders)
{ {
if (traderId === "ragfair" || traderId === Traders.LIGHTHOUSEKEEPER) if (traderId === "ragfair" || traderId === Traders.LIGHTHOUSEKEEPER)
@ -63,7 +75,10 @@ export class TraderController
this.traderPurchasePersisterService.removeStalePurchasesFromProfiles(traderId); this.traderPurchasePersisterService.removeStalePurchasesFromProfiles(traderId);
trader.base.nextResupply = this.traderHelper.getNextUpdateTimestamp(trader.base._id); // Set to next hour on clock or current time + 60 mins
trader.base.nextResupply = traderResetStartsWithServer
? this.traderHelper.getNextUpdateTimestamp(trader.base._id)
: nextHourTimestamp;
this.databaseServer.getTables().traders[trader.base._id].base = trader.base; this.databaseServer.getTables().traders[trader.base._id].base = trader.base;
} }
} }

View File

@ -7,6 +7,8 @@ export interface ITraderConfig extends IBaseConfig
kind: "aki-trader"; kind: "aki-trader";
updateTime: UpdateTime[]; updateTime: UpdateTime[];
purchasesAreFoundInRaid: boolean; purchasesAreFoundInRaid: boolean;
/** Should trader reset times be set based on server start time (false = bsg time - on the hour) */
tradersResetFromServerStart: boolean;
updateTimeDefault: number; updateTimeDefault: number;
traderPriceMultipler: number; traderPriceMultipler: number;
/** Keep track of purchased trader-limited items beyond server restarts to prevent server-restart item scumming */ /** Keep track of purchased trader-limited items beyond server restarts to prevent server-restart item scumming */

View File

@ -108,4 +108,12 @@ export class TimeUtil
{ {
return hours * TimeUtil.ONE_HOUR_AS_SECONDS; return hours * TimeUtil.ONE_HOUR_AS_SECONDS;
} }
public getTimestampOfNextHour(): number
{
const now = new Date();
const millisecondsUntilNextHour = (60 - now.getMinutes()) * 60 * 1000 - now.getSeconds() * 1000
- now.getMilliseconds();
return (now.getTime() + millisecondsUntilNextHour) / 1000;
}
} }