Server/project/src/helpers/AssortHelper.ts

169 lines
6.1 KiB
TypeScript
Raw Normal View History

2023-03-03 16:23:46 +01:00
import { inject, injectable } from "tsyringe";
import { ItemHelper } from "@spt-aki/helpers/ItemHelper";
import { QuestHelper } from "@spt-aki/helpers/QuestHelper";
import { IPmcData } from "@spt-aki/models/eft/common/IPmcData";
import { ITraderAssort } from "@spt-aki/models/eft/common/tables/ITrader";
import { QuestStatus } from "@spt-aki/models/enums/QuestStatus";
import { ILogger } from "@spt-aki/models/spt/utils/ILogger";
import { DatabaseServer } from "@spt-aki/servers/DatabaseServer";
import { LocalisationService } from "@spt-aki/services/LocalisationService";
2023-03-03 16:23:46 +01:00
@injectable()
export class AssortHelper
{
constructor(
@inject("WinstonLogger") protected logger: ILogger,
@inject("ItemHelper") protected itemHelper: ItemHelper,
@inject("DatabaseServer") protected databaseServer: DatabaseServer,
@inject("LocalisationService") protected localisationService: LocalisationService,
2023-11-13 17:07:59 +01:00
@inject("QuestHelper") protected questHelper: QuestHelper,
2023-03-03 16:23:46 +01:00
)
2023-11-13 17:07:59 +01:00
{}
2023-03-03 16:23:46 +01:00
/**
2023-11-13 17:07:59 +01:00
* Remove assorts from a trader that have not been unlocked yet (via player completing corresponding quest)
2023-07-09 11:00:00 +02:00
* @param pmcProfile Player profile
* @param traderId Traders id the assort belongs to
2023-03-03 16:23:46 +01:00
* @param traderAssorts All assort items from same trader
* @param mergedQuestAssorts Dict of quest assort to quest id unlocks for all traders (key = started/failed/complete)
2023-07-09 11:00:00 +02:00
* @returns Assort items minus locked quest assorts
2023-03-03 16:23:46 +01:00
*/
2023-11-13 17:07:59 +01:00
public stripLockedQuestAssort(
pmcProfile: IPmcData,
traderId: string,
traderAssorts: ITraderAssort,
mergedQuestAssorts: Record<string, Record<string, string>>,
flea = false,
): ITraderAssort
2023-03-03 16:23:46 +01:00
{
// Trader assort does not always contain loyal_level_items
if (!traderAssorts.loyal_level_items)
{
this.logger.warning(this.localisationService.getText("assort-missing_loyalty_level_object", traderId));
return traderAssorts;
}
// Iterate over all assorts, removing items that don't fulfil the requirements
for (const assortId in traderAssorts.loyal_level_items)
{
// Get quest id that unlocks assort + statuses quest can be in to show assort
const unlockValues = this.getQuestIdAndStatusThatShowAssort(mergedQuestAssorts, assortId);
if (!unlockValues)
{
continue;
}
// Remove assort if quest in profile does not have status that unlocks assort
const questStatusInProfile = this.questHelper.getQuestStatus(pmcProfile, unlockValues.questId);
if (!unlockValues.status.includes(questStatusInProfile))
{
traderAssorts = this.removeItemFromAssort(traderAssorts, assortId, flea);
}
}
return traderAssorts;
}
/**
* Get a quest id + the statuses quest can be in to unlock assort
* @param mergedQuestAssorts quest assorts to search for assort id
* @param assortId Assort to look for linked quest id
* @returns quest id + array of quest status the assort should show for
*/
2023-11-13 17:07:59 +01:00
protected getQuestIdAndStatusThatShowAssort(
mergedQuestAssorts: Record<string, Record<string, string>>,
assortId: string,
): { questId: string; status: QuestStatus[]; }
2023-03-03 16:23:46 +01:00
{
if (assortId in mergedQuestAssorts.started)
{
// Assort unlocked by starting quest, assort is visible to player when : started or ready to hand in + handed in
2023-11-13 17:07:59 +01:00
return {
questId: mergedQuestAssorts.started[assortId],
status: [QuestStatus.Started, QuestStatus.AvailableForFinish, QuestStatus.Success],
};
2023-03-03 16:23:46 +01:00
}
if (assortId in mergedQuestAssorts.success)
2023-03-03 16:23:46 +01:00
{
return { questId: mergedQuestAssorts.success[assortId], status: [QuestStatus.Success] };
2023-03-03 16:23:46 +01:00
}
if (assortId in mergedQuestAssorts.fail)
2023-03-03 16:23:46 +01:00
{
return { questId: mergedQuestAssorts.fail[assortId], status: [QuestStatus.Fail] };
2023-03-03 16:23:46 +01:00
}
return undefined;
}
/**
* Remove assorts from a trader that have not been unlocked yet
* @param pmcProfile player profile
* @param traderId traders id
* @param assort traders assorts
* @returns traders assorts minus locked loyalty assorts
*/
public stripLockedLoyaltyAssort(pmcProfile: IPmcData, traderId: string, assort: ITraderAssort): ITraderAssort
{
// Trader assort does not always contain loyal_level_items
if (!assort.loyal_level_items)
{
this.logger.warning(this.localisationService.getText("assort-missing_loyalty_level_object", traderId));
return assort;
}
// Remove items not unlocked from quest status change (ASSORTMENT_UNLOCK)
2023-03-03 16:23:46 +01:00
for (const itemId in assort.loyal_level_items)
{
if (assort.loyal_level_items[itemId] > pmcProfile.TradersInfo[traderId].loyaltyLevel)
{
assort = this.removeItemFromAssort(assort, itemId);
}
}
return assort;
}
/**
* Remove an item from an assort
* @param assort assort to modify
* @param itemID item id to remove from asort
* @returns Modified assort
*/
public removeItemFromAssort(assort: ITraderAssort, itemID: string, flea = false): ITraderAssort
{
const idsToRemove = this.itemHelper.findAndReturnChildrenByItems(assort.items, itemID);
if (assort.barter_scheme[itemID] && flea)
{
2023-10-31 18:46:14 +01:00
for (const barterSchemes of assort.barter_scheme[itemID])
{
for (const barterScheme of barterSchemes)
{
barterScheme.sptQuestLocked = true;
}
}
2023-03-03 16:23:46 +01:00
return assort;
}
delete assort.barter_scheme[itemID];
delete assort.loyal_level_items[itemID];
for (const i in idsToRemove)
{
for (const a in assort.items)
{
if (assort.items[a]._id === idsToRemove[i])
{
assort.items.splice(parseInt(a), 1);
}
}
}
return assort;
}
2023-11-13 17:07:59 +01:00
}