Replaced various array.find() with array.some()

Rewrote `tagItem()` to use `.find()`
This commit is contained in:
Dev 2024-06-13 13:41:29 +01:00
parent 3327bc916a
commit f8bd65ed90
19 changed files with 63 additions and 73 deletions

View File

@ -133,7 +133,7 @@ export class DialogueController
// User to user messages are special in that they need the player to exist in them, add if they don't
if (
messageType === MessageType.USER_MESSAGE
&& !dialog.Users?.find((userDialog) => userDialog._id === profile.characters.pmc.sessionId)
&& !dialog.Users?.some((userDialog) => userDialog._id === profile.characters.pmc.sessionId)
)
{
if (!dialog.Users)
@ -237,7 +237,7 @@ export class DialogueController
{
result.push(...dialogUsers);
if (!result.find((userDialog) => userDialog._id === fullProfile.info.id))
if (!result.some((userDialog) => userDialog._id === fullProfile.info.id))
{
// Player doesnt exist, add them in before returning
const pmcProfile = fullProfile.characters.pmc;

View File

@ -117,7 +117,7 @@ export class HealthController
const output = this.eventOutputHolder.getOutput(sessionID);
let resourceLeft = 0;
const itemToConsume = pmcData.Inventory.items.find((x) => x._id === request.item);
const itemToConsume = pmcData.Inventory.items.find((item) => item._id === request.item);
if (!itemToConsume)
{
// Item not found, very bad

View File

@ -321,9 +321,10 @@ export class HideoutController
// Set child area level to same as parent area
pmcData.Hideout.Areas
.find((x) => x.type === childDbArea.type).level = pmcData.Hideout.Areas
.find((x) => x.type === profileParentHideoutArea.type,
).level;
.find((hideoutArea) => hideoutArea.type === childDbArea.type).level
= pmcData.Hideout.Areas
.find((x) => x.type === profileParentHideoutArea.type,
).level;
// Add/upgrade stash item in player inventory
const childDbAreaStage = childDbArea.stages[profileParentHideoutArea.level];
@ -346,7 +347,7 @@ export class HideoutController
hideoutStage: Stage,
): void
{
const existingInventoryItem = pmcData.Inventory.items.find((x) => x._id === dbHideoutData._id);
const existingInventoryItem = pmcData.Inventory.items.find((item) => item._id === dbHideoutData._id);
if (existingInventoryItem)
{
// Update existing items container tpl to point to new id (tpl)
@ -599,7 +600,7 @@ export class HideoutController
// Find the recipe of the production
const recipe = this.databaseService.getHideout().production
.find((p) => p._id === body.recipeId);
.find((production) => production._id === body.recipeId);
// Find the actual amount of items we need to remove because body can send weird data
const recipeRequirementsClone = this.cloner.clone(
@ -1203,7 +1204,7 @@ export class HideoutController
public recordShootingRangePoints(sessionId: string, pmcData: IPmcData, request: IRecordShootingRangePoints): void
{
// Check if counter exists, add placeholder if it doesnt
if (!pmcData.Stats.Eft.OverallCounters.Items.find((x) => x.Key.includes("ShootingRangePoints")))
if (!pmcData.Stats.Eft.OverallCounters.Items.some((counter) => counter.Key.includes("ShootingRangePoints")))
{
pmcData.Stats.Eft.OverallCounters.Items.push({ Key: ["ShootingRangePoints"], Value: 0 });
}

View File

@ -540,25 +540,20 @@ export class InventoryController
*/
public tagItem(pmcData: IPmcData, body: IInventoryTagRequestData, sessionID: string): IItemEventRouterResponse
{
// TODO - replace with single .find() call
for (const item of pmcData.Inventory.items)
const itemToTag = pmcData.Inventory.items.find((item) => item._id === body.item);
if (!itemToTag)
{
if (item._id === body.item)
{
if ("upd" in item)
{
item.upd.Tag = { Color: body.TagColor, Name: body.TagName };
}
else
{
item.upd = { Tag: { Color: body.TagColor, Name: body.TagName } };
}
return this.eventOutputHolder.getOutput(sessionID);
}
return { warnings: [], profileChanges: {} };
}
return { warnings: [], profileChanges: {} };
if (!itemToTag.upd)
{
itemToTag.upd = {};
}
itemToTag.upd.Tag = { Color: body.TagColor, Name: body.TagName };
return this.eventOutputHolder.getOutput(sessionID);
}
/**
@ -651,12 +646,8 @@ export class InventoryController
if (!itemId)
{
// player inventory
const target = pmcData.Inventory.items.find((item) =>
{
return body.item === item._id;
});
// Player inventory
const target = pmcData.Inventory.items.find((item) => item._id === body.item);
if (target)
{
itemId = target._tpl;
@ -719,7 +710,7 @@ export class InventoryController
if (request.fromOwner.id === Traders.FENCE)
{
// Get tpl from fence assorts
return this.fenceService.getRawFenceAssorts().items.find((x) => x._id === request.item)._tpl;
return this.fenceService.getRawFenceAssorts().items.find((x) => x._id === request.item)?._tpl;
}
if (request.fromOwner.type === "Trader")
@ -727,7 +718,7 @@ export class InventoryController
// Not fence
// get tpl from trader assort
return this.databaseService.getTrader(request.fromOwner.id).assort.items
.find((item) => item._id === request.item)._tpl;
.find((item) => item._id === request.item)?._tpl;
}
if (request.fromOwner.type === "RagFair")
@ -932,8 +923,8 @@ export class InventoryController
// Hard coded to `SYSTEM` for now
// TODO: make this dynamic
const dialog = fullProfile.dialogues["59e7125688a45068a6249071"];
const mail = dialog.messages.find((x) => x._id === event.MessageId);
const mailEvent = mail.profileChangeEvents.find((x) => x._id === event.EventId);
const mail = dialog.messages.find((message) => message._id === event.MessageId);
const mailEvent = mail.profileChangeEvents.find((changeEvent) => changeEvent._id === event.EventId);
switch (mailEvent.Type)
{

View File

@ -158,7 +158,7 @@ export class RagfairController
public getOfferById(sessionId: string, request: IGetRagfairOfferByIdRequest): IRagfairOffer
{
const offers = this.ragfairOfferService.getOffers();
const offerToReturn = offers.find((x) => x.intId === request.id);
const offerToReturn = offers.find((offer) => offer.intId === request.id);
return offerToReturn;
}
@ -659,7 +659,7 @@ export class RagfairController
const loyalLevel = 1;
const formattedItems: Item[] = items.map((item) =>
{
const isChild = items.find((it) => it._id === item.parentId);
const isChild = items.some((it) => it._id === item.parentId);
return {
_id: item._id,

View File

@ -461,8 +461,8 @@ export class BotEquipmentModGenerator
// Force spawn chance to be 100% to ensure it gets added
if (
modSlot === "mod_handguard"
&& modToAddTemplate._props.Slots.find((slot) => slot._name === "mod_handguard")
&& !request.weapon.find((item) => item.slotId === "mod_launcher")
&& modToAddTemplate._props.Slots.some((slot) => slot._name === "mod_handguard")
&& !request.weapon.some((item) => item.slotId === "mod_launcher")
)
{
// Needed for handguards with lower
@ -473,7 +473,7 @@ export class BotEquipmentModGenerator
// Or if mod_stock is configured to be forced on
if (
modSlot === "mod_stock"
&& modToAddTemplate._props.Slots.find(
&& modToAddTemplate._props.Slots.some(
(slot) => slot._name.includes("mod_stock") || botEquipConfig.forceStock,
)
)

View File

@ -344,12 +344,12 @@ export class BotLootGenerator
{
const result = [EquipmentSlots.POCKETS];
if (botInventory.items.find((item) => item.slotId === EquipmentSlots.TACTICAL_VEST))
if (botInventory.items.some((item) => item.slotId === EquipmentSlots.TACTICAL_VEST))
{
result.push(EquipmentSlots.TACTICAL_VEST);
}
if (botInventory.items.find((item) => item.slotId === EquipmentSlots.BACKPACK))
if (botInventory.items.some((item) => item.slotId === EquipmentSlots.BACKPACK))
{
result.push(EquipmentSlots.BACKPACK);
}

View File

@ -375,10 +375,10 @@ export class BotWeaponGenerator
for (const modSlotTemplate of modTemplate._props.Slots.filter((slot) => slot._required))
{
const slotName = modSlotTemplate._name;
const weaponSlotItem = weaponItemArray.find(
const hasWeaponSlotItem = weaponItemArray.some(
(weaponItem) => weaponItem.parentId === mod._id && weaponItem.slotId === slotName,
);
if (!weaponSlotItem)
if (!hasWeaponSlotItem)
{
this.logger.warning(
this.localisationService.getText("bot-weapons_required_slot_missing_item", {

View File

@ -841,7 +841,7 @@ export class LocationGenerator
locationTemplateToAdd.Items[0]._id = locationTemplateToAdd.Root;
// Push forced location into array as long as it doesnt exist already
const existingLocation = lootLocationTemplates.find(
const existingLocation = lootLocationTemplates.some(
(spawnPoint) => spawnPoint.Id === locationTemplateToAdd.Id,
);
if (!existingLocation)

View File

@ -802,9 +802,7 @@ export class HideoutHelper
*/
protected getTotalProductionTimeSeconds(prodId: string): number
{
const recipe = this.databaseService.getHideout().production.find((prod) => prod._id === prodId);
return recipe.productionTime || 0;
return this.databaseService.getHideout().production.find((prod) => prod._id === prodId)?.productionTime ?? 0;
}
/**

View File

@ -1305,7 +1305,7 @@ export class InventoryHelper
*/
public isItemInStash(pmcData: IPmcData, itemToCheck: Item): boolean
{
let container = itemToCheck;
const container = itemToCheck;
while ("parentId" in container)
{
@ -1314,12 +1314,12 @@ export class InventoryHelper
return true;
}
container = pmcData.Inventory.items.find((item) => item._id === container.parentId);
if (!container)
if (!pmcData.Inventory.items.some((item) => item._id === container.parentId))
{
break;
}
}
return false;
}
}

View File

@ -331,7 +331,7 @@ export class ItemHelper
// Check if item has slots that match soft insert name ids
const softInsertIds = this.getSoftInsertSlotIds();
if (itemDbDetails[1]._props.Slots.find((slot) => softInsertIds.includes(slot._name.toLowerCase())))
if (itemDbDetails[1]._props.Slots.some((slot) => softInsertIds.includes(slot._name.toLowerCase())))
{
return true;
}
@ -668,7 +668,7 @@ export class ItemHelper
}
// Items parentid matches root item AND returned items doesnt contain current child
if (childItem.parentId === baseItemId && !list.find((item) => childItem._id === item._id))
if (childItem.parentId === baseItemId && !list.some((item) => childItem._id === item._id))
{
list.push(...this.findAndReturnChildrenAsItems(items, childItem._id));
}
@ -689,7 +689,7 @@ export class ItemHelper
for (const itemFromAssort of assort)
{
if (itemFromAssort.parentId === itemIdToFind && !list.find((item) => itemFromAssort._id === item._id))
if (itemFromAssort.parentId === itemIdToFind && !list.some((item) => itemFromAssort._id === item._id))
{
list.push(itemFromAssort);
list = list.concat(this.findAndReturnChildrenByAssort(itemFromAssort._id, assort));
@ -1260,7 +1260,7 @@ export class ItemHelper
const cartridgeMaxStackSize = cartridgeDetails[1]._props.StackMaxSize;
// Exit if ammo already exists in box
if (ammoBox.find((item) => item._tpl === cartridgeTpl))
if (ammoBox.some((item) => item._tpl === cartridgeTpl))
{
return;
}

View File

@ -1107,7 +1107,7 @@ export class QuestHelper
{
// Quest from db matches quests in profile, skip
const questData = quests[questIdKey];
if (pmcProfile.Quests.find((x) => x.qid === questData._id))
if (pmcProfile.Quests.some((x) => x.qid === questData._id))
{
continue;
}

View File

@ -158,7 +158,7 @@ export class RagfairHelper
for (let item of items)
{
item = this.itemHelper.fixItemStackCount(item);
const isChild = items.find((it) => it._id === item.parentId);
const isChild = items.some((it) => it._id === item.parentId);
if (!isChild)
{

View File

@ -790,7 +790,7 @@ export class RagfairOfferHelper
}
if (
!traderAssorts[offer.user.id].items.find((item) =>
!traderAssorts[offer.user.id].items.some((item) =>
{
return item._id === offer.root;
})

View File

@ -80,7 +80,7 @@ export class CustomLocationWaveService
for (const bossWave of bossWavesToApply[mapKey])
{
if (locationBase.BossLocationSpawn.find((x) => x.sptId === bossWave.sptId))
if (locationBase.BossLocationSpawn.some((x) => x.sptId === bossWave.sptId))
{
// Already exists, skip
continue;
@ -104,7 +104,7 @@ export class CustomLocationWaveService
for (const normalWave of normalWavesToApply[mapKey])
{
if (locationBase.waves.find((x) => x.sptId === normalWave.sptId))
if (locationBase.waves.some((x) => x.sptId === normalWave.sptId))
{
// Already exists, skip
continue;

View File

@ -169,7 +169,7 @@ export class InsuranceService
for (const insuredItem of this.getInsurance(sessionId)[traderId])
{
// Find insured items parent
const insuredItemsParent = insuredItems.find((x) => x._id === insuredItem.parentId);
const insuredItemsParent = insuredItems.some((x) => x._id === insuredItem.parentId);
if (!insuredItemsParent)
{
// Remove location + set slotId of insured items parent

View File

@ -196,7 +196,7 @@ export class ProfileFixerService
= hideoutStandSecondaryAreaDb._id;
// Add stash item to profile
const gunStandStashItem = pmcProfile.Inventory.items.find((x) => x._id === hideoutStandAreaDb._id);
const gunStandStashItem = pmcProfile.Inventory.items.find((item) => item._id === hideoutStandAreaDb._id);
if (gunStandStashItem)
{
gunStandStashItem._tpl = stageCurrentAt.container!;
@ -214,7 +214,7 @@ export class ProfileFixerService
// Add secondary stash item to profile
const gunStandStashSecondaryItem = pmcProfile.Inventory.items.find(
(x) => x._id === hideoutStandSecondaryAreaDb._id,
(item) => item._id === hideoutStandSecondaryAreaDb._id,
)!;
if (gunStandStashItem)
{
@ -299,7 +299,7 @@ export class ProfileFixerService
pmcProfile.Inventory.hideoutAreaStashes[HideoutAreas.PLACE_OF_FAME] = placeOfFameAreaDb._id;
// Add stash item to profile
const placeOfFameStashItem = pmcProfile.Inventory.items.find((x) => x._id === placeOfFameAreaDb._id);
const placeOfFameStashItem = pmcProfile.Inventory.items.find((item) => item._id === placeOfFameAreaDb._id);
if (placeOfFameStashItem)
{
placeOfFameStashItem._tpl = stageCurrentlyAt.container!;
@ -363,7 +363,7 @@ export class ProfileFixerService
protected addMissingHideoutWallAreas(pmcProfile: IPmcData): void
{
if (!pmcProfile.Hideout.Areas.find((x) => x.type === HideoutAreas.WEAPON_STAND))
if (!pmcProfile.Hideout.Areas.some((x) => x.type === HideoutAreas.WEAPON_STAND))
{
pmcProfile.Hideout.Areas.push({
type: 24,
@ -377,7 +377,7 @@ export class ProfileFixerService
});
}
if (!pmcProfile.Hideout.Areas.find((x) => x.type === HideoutAreas.WEAPON_STAND_SECONDARY))
if (!pmcProfile.Hideout.Areas.some((x) => x.type === HideoutAreas.WEAPON_STAND_SECONDARY))
{
pmcProfile.Hideout.Areas.push({
type: 25,
@ -767,7 +767,7 @@ export class ProfileFixerService
{
for (let i = 0; i < count; i++)
{
if (!slots.find((x) => x.locationIndex === i))
if (!slots.some((x) => x.locationIndex === i))
{
slots.push({ locationIndex: i });
}
@ -1168,9 +1168,9 @@ export class ProfileFixerService
const itemsHaveChildren = pmcProfile.Inventory.items.some((x) => x.parentId === key);
if (!itemsHaveChildren)
{
const itemToAdjustId = pmcProfile.Inventory.items.find((x) => x._id === key)!;
itemToAdjustId._id = this.hashUtil.generate();
this.logger.warning(`Replace duplicate item Id: ${key} with ${itemToAdjustId._id}`);
const itemToAdjust = pmcProfile.Inventory.items.find((x) => x._id === key)!;
itemToAdjust._id = this.hashUtil.generate();
this.logger.warning(`Replace duplicate item Id: ${key} with ${itemToAdjust._id}`);
}
}
}
@ -1285,7 +1285,7 @@ export class ProfileFixerService
// Get all areas from templates/profiles.json
for (const area of profileTemplate.character.Hideout.Areas)
{
if (!pmcProfile.Hideout.Areas.find((x) => x.type === area.type))
if (!pmcProfile.Hideout.Areas.some((x) => x.type === area.type))
{
pmcProfile.Hideout.Areas.push(area);
this.logger.debug(`Added missing hideout area ${area.type} to profile`);
@ -1444,7 +1444,7 @@ export class ProfileFixerService
for (let i = profileQuests.length - 1; i >= 0; i--)
{
if (!(quests[profileQuests[i].qid] || repeatableQuests.find((x) => x._id === profileQuests[i].qid)))
if (!(quests[profileQuests[i].qid] || repeatableQuests.some((x) => x._id === profileQuests[i].qid)))
{
profileQuests.splice(i, 1);
this.logger.success("Successfully removed orphaned quest that doesnt exist in our quest data");

View File

@ -441,7 +441,7 @@ export class SeasonalEventService
const locations = this.databaseService.getLocations();
const mapBosses: BossLocationSpawn[] = locations[mapKey].base.BossLocationSpawn;
if (!mapBosses.find((bossSpawn) => bossSpawn.BossName === boss.BossName))
if (!mapBosses.some((bossSpawn) => bossSpawn.BossName === boss.BossName))
{
locations[mapKey].base.BossLocationSpawn.push(...bossesToAdd);
}