Refactor storeLostGear()

This commit is contained in:
Dev 2023-04-24 14:03:41 +01:00
parent 463e33d1b7
commit 8076ed9b29

View File

@ -125,15 +125,7 @@ export class InsuranceService
}; };
// Remove 'hideout' slotid property on all insurance items // Remove 'hideout' slotid property on all insurance items
for (const insuredItem of this.getInsurance(sessionID)[traderId]) this.removeLocationProperty(sessionID, traderId);
{
const isParentHere = this.getInsurance(sessionID)[traderId].find(isParent => isParent._id === insuredItem.parentId);
if (!isParentHere)
{
insuredItem.slotId = "hideout";
delete insuredItem.location;
}
}
this.saveServer.getProfile(sessionID).insurance.push({ this.saveServer.getProfile(sessionID).insurance.push({
scheduledTime: insuranceReturnTimestamp, scheduledTime: insuranceReturnTimestamp,
@ -146,6 +138,20 @@ export class InsuranceService
this.resetInsurance(sessionID); this.resetInsurance(sessionID);
} }
protected removeLocationProperty(sessionId: string, traderId: string): void
{
const insuredItems = this.getInsurance(sessionId)[traderId];
for (const insuredItem of this.getInsurance(sessionId)[traderId])
{
const isParentHere = insuredItems.find(isParent => isParent._id === insuredItem.parentId);
if (!isParentHere)
{
insuredItem.slotId = "hideout";
delete insuredItem.location;
}
}
}
/** /**
* Get a timestamp of what insurance items should be sent to player based on the type of trader used to insure * Get a timestamp of what insurance items should be sent to player based on the type of trader used to insure
* @param pmcData Player profile * @param pmcData Player profile
@ -181,22 +187,10 @@ export class InsuranceService
*/ */
public storeLostGear(pmcData: IPmcData, offraidData: ISaveProgressRequestData, preRaidGear: Item[], sessionID: string, playerDied: boolean): void public storeLostGear(pmcData: IPmcData, offraidData: ISaveProgressRequestData, preRaidGear: Item[], sessionID: string, playerDied: boolean): void
{ {
const preRaidGearHash: Record<string, Item> = {}; const preRaidGearHash = this.createItemHashTable(preRaidGear);
const offRaidGearHash: Record<string, Item> = {}; const offRaidGearHash = this.createItemHashTable(offraidData.profile.Inventory.items);
const gears = [];
// Build a hash table to reduce loops
for (const item of preRaidGear)
{
preRaidGearHash[item._id] = item;
}
// Build a hash of offRaidGear
for (const item of offraidData.profile.Inventory.items)
{
offRaidGearHash[item._id] = item;
}
const equipmentToSendToPlayer = [];
for (const insuredItem of pmcData.InsuredItems) for (const insuredItem of pmcData.InsuredItems)
{ {
// Check insured item was on player during raid // Check insured item was on player during raid
@ -206,7 +200,7 @@ export class InsuranceService
// Check if item missing OR player died with item on // Check if item missing OR player died with item on
if (!offRaidGearHash[insuredItem.itemId] || playerDied) if (!offRaidGearHash[insuredItem.itemId] || playerDied)
{ {
gears.push({ equipmentToSendToPlayer.push({
"pmcData": pmcData, "pmcData": pmcData,
"insuredItem": insuredItem, "insuredItem": insuredItem,
"item": preRaidGearHash[insuredItem.itemId], "item": preRaidGearHash[insuredItem.itemId],
@ -217,12 +211,28 @@ export class InsuranceService
} }
// Process all insured items lost in-raid // Process all insured items lost in-raid
for (const gear of gears) for (const gear of equipmentToSendToPlayer)
{ {
this.addGearToSend(gear.pmcData, gear.insuredItem, gear.item, gear.sessionID); this.addGearToSend(gear.pmcData, gear.insuredItem, gear.item, gear.sessionID);
} }
} }
/**
* Create a hash table for an array of items, keyed by items _id
* @param items Items to hash
* @returns Hashtable
*/
protected createItemHashTable(items: Item[]): Record<string, Item>
{
const hashTable: Record<string, Item> = {};
for (const item of items)
{
hashTable[item._id] = item;
}
return hashTable;
}
/** /**
* Store insured items on pmc death inside insurance array in player profile * Store insured items on pmc death inside insurance array in player profile
* @param pmcData Player profile * @param pmcData Player profile