Refactor InsuranceController.processReturn()

This commit is contained in:
Dev 2023-05-19 17:39:26 +01:00
parent 52f9dbefa9
commit dcd1da28a6

View File

@ -54,21 +54,23 @@ export class InsuranceController
{ {
const time = this.timeUtil.getTimestamp(); const time = this.timeUtil.getTimestamp();
// Process each profile in turn
for (const sessionID in this.saveServer.getProfiles()) for (const sessionID in this.saveServer.getProfiles())
{ {
const insurance = this.saveServer.getProfile(sessionID).insurance; const insurance = this.saveServer.getProfile(sessionID).insurance;
let i = insurance.length; let insuredItemCount = insurance.length;
// Skip profile with no insurance items // Skip profile with no insurance items
if (i === 0) if (insuredItemCount === 0)
{ {
continue; continue;
} }
while (i-- > 0) // Use count as array index
while (insuredItemCount-- > 0)
{ {
const insured = insurance[i]; const insured = insurance[insuredItemCount];
const traderReturnChance = this.insuranceConfig.returnChancePercent[insured.traderId];
// Return time not reached, skip // Return time not reached, skip
if (time < insured.scheduledTime) if (time < insured.scheduledTime)
@ -76,16 +78,13 @@ export class InsuranceController
continue; continue;
} }
// Gather up items that can fail // Items to be removed from inventory
const slotIdsThatCanFail = this.insuranceConfig.slotIdsWithChanceOfNotReturning; const toDelete: string[] = [];
const toDelete = [];
// Loop over insurance items // Loop over insurance items, find items to delete from player inventory
for (const insuredItem of insured.items) for (const insuredItem of insured.items)
{ {
// Roll from 0 to 9999, then divide it by 100: 9999 = 99.99% if (this.itemShouldBeLost(insuredItem, insured.traderId, toDelete))
const returnChance = this.randomUtil.getInt(0, 9999) / 100;
if ((slotIdsThatCanFail.includes(insuredItem.slotId)) && returnChance >= traderReturnChance && !toDelete.includes(insuredItem._id))
{ {
// Skip if not an item // Skip if not an item
const itemDetails = this.itemHelper.getItem(insuredItem._tpl); const itemDetails = this.itemHelper.getItem(insuredItem._tpl);
@ -100,7 +99,7 @@ export class InsuranceController
continue; continue;
} }
// Remove item and its sub-items to prevent orphaned items // Remove item and its sub-items to prevent orphans
toDelete.push(...this.itemHelper.findAndReturnChildrenByItems(insured.items, insuredItem._id)); toDelete.push(...this.itemHelper.findAndReturnChildrenByItems(insured.items, insuredItem._id));
} }
} }
@ -123,13 +122,30 @@ export class InsuranceController
this.dialogueHelper.addDialogueMessage(insured.traderId, insured.messageContent, sessionID, insured.items); this.dialogueHelper.addDialogueMessage(insured.traderId, insured.messageContent, sessionID, insured.items);
// Remove insurance package from profile now we've processed it // Remove insurance package from profile now we've processed it
insurance.splice(i, 1); insurance.splice(insuredItemCount, 1);
} }
this.saveServer.getProfile(sessionID).insurance = insurance; this.saveServer.getProfile(sessionID).insurance = insurance;
} }
} }
/**
* Should the passed in item be removed from player inventory
* @param insuredItem Insurued item to roll to lose
* @param traderId Trader the item was insured by
* @param itemsBeingDeleted All items to remove from player
* @returns True if item should be removed
*/
protected itemShouldBeLost(insuredItem: Item, traderId: string, itemsBeingDeleted: string[]): boolean
{
// Roll from 0 to 9999, then divide it by 100: 9999 = 99.99%
const returnChance = this.randomUtil.getInt(0, 9999) / 100;
const traderReturnChance = this.insuranceConfig.returnChancePercent[traderId];
const slotIdsThatCanFail = this.insuranceConfig.slotIdsWithChanceOfNotReturning;
return (slotIdsThatCanFail.includes(insuredItem.slotId)) && returnChance >= traderReturnChance && !itemsBeingDeleted.includes(insuredItem._id);
}
/** /**
* Add insurance to an item * Add insurance to an item
* @param pmcData Player profile * @param pmcData Player profile