Server/project/src/services/TraderPurchasePersisterService.ts

125 lines
4.5 KiB
TypeScript
Raw Normal View History

2023-03-03 16:23:46 +01:00
import { inject, injectable } from "tsyringe";
import { ProfileHelper } from "@spt-aki/helpers/ProfileHelper";
import { TraderPurchaseData } from "@spt-aki/models/eft/profile/IAkiProfile";
import { ConfigTypes } from "@spt-aki/models/enums/ConfigTypes";
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 { LocalisationService } from "@spt-aki/services/LocalisationService";
import { TimeUtil } from "@spt-aki/utils/TimeUtil";
2023-03-03 16:23:46 +01:00
/**
* Help with storing limited item purchases from traders in profile to persist them over server restarts
*/
@injectable()
export class TraderPurchasePersisterService
{
protected traderConfig: ITraderConfig;
constructor(
@inject("WinstonLogger") protected logger: ILogger,
@inject("TimeUtil") protected timeUtil: TimeUtil,
@inject("ProfileHelper") protected profileHelper: ProfileHelper,
2023-07-19 12:00:34 +02:00
@inject("LocalisationService") protected localisationService: LocalisationService,
2023-11-16 02:35:05 +01:00
@inject("ConfigServer") protected configServer: ConfigServer,
2023-03-03 16:23:46 +01:00
)
{
this.traderConfig = this.configServer.getConfig(ConfigTypes.TRADER);
}
/**
* Get the purchases made from a trader for this profile before the last trader reset
* @param sessionId Session id
* @param traderId Trader to loop up purchases for
* @returns Dict of assort id and count purchased
*/
public getProfileTraderPurchases(sessionId: string, traderId: string): Record<string, TraderPurchaseData>
{
const profile = this.profileHelper.getFullProfile(sessionId);
if (!profile.traderPurchases)
{
return null;
}
return profile.traderPurchases[traderId];
}
/**
* Remove all trader purchase records from all profiles that exist
* @param traderId Traders id
*/
public resetTraderPurchasesStoredInProfile(traderId: string): void
{
// Reset all profiles purchase dictionaries now a trader update has occured;
const profiles = this.profileHelper.getProfiles();
for (const profile of Object.values(profiles))
{
// Skip if no purchases
if (!profile.traderPurchases)
{
continue;
}
// Skip if no trader-speicifc purchases
if (!profile.traderPurchases[traderId])
{
continue;
}
profile.traderPurchases[traderId] = {};
}
}
/**
* Iterate over all server profiles and remove specific trader purchase data that has passed the trader refesh time
* @param traderId Trader id
*/
public removeStalePurchasesFromProfiles(traderId: string): void
{
const profiles = this.profileHelper.getProfiles();
for (const profile of Object.values(profiles))
{
// Skip if no purchases
if (!profile.traderPurchases)
{
continue;
}
// Skip if no trader-specifc purchases
if (!profile.traderPurchases[traderId])
{
continue;
}
for (const purchaseKey in profile.traderPurchases[traderId])
{
2023-11-16 02:35:05 +01:00
const traderUpdateDetails = this.traderConfig.updateTime.find((x) => x.traderId === traderId);
2023-03-03 16:23:46 +01:00
if (!traderUpdateDetails)
{
2023-11-16 02:35:05 +01:00
this.logger.error(
this.localisationService.getText("trader-unable_to_delete_stale_purchases", {
profileId: profile.info.id,
traderId: traderId,
}),
);
2023-03-03 16:23:46 +01:00
continue;
}
const purchaseDetails = profile.traderPurchases[traderId][purchaseKey];
const resetTimeForItem = purchaseDetails.purchaseTimestamp + traderUpdateDetails.seconds;
2023-11-16 02:35:05 +01:00
if (resetTimeForItem < this.timeUtil.getTimestamp())
2023-03-03 16:23:46 +01:00
{
// Item was purchased far enough in past a trader refresh would have occured, remove purchase record from profile
2023-11-16 02:35:05 +01:00
this.logger.debug(
`Removed trader: ${traderId} purchase: ${purchaseKey} from profile: ${profile.info.id}`,
);
2023-03-03 16:23:46 +01:00
delete profile.traderPurchases[traderId][purchaseKey];
}
}
}
}
2023-11-16 02:35:05 +01:00
}