From f8bd65ed907b2f4e091909ddc313549247d87c06 Mon Sep 17 00:00:00 2001 From: Dev Date: Thu, 13 Jun 2024 13:41:29 +0100 Subject: [PATCH] Replaced various `array.find()` with `array.some()` Rewrote `tagItem()` to use `.find()` --- project/src/controllers/DialogueController.ts | 4 +- project/src/controllers/HealthController.ts | 2 +- project/src/controllers/HideoutController.ts | 13 +++--- .../src/controllers/InventoryController.ts | 43 ++++++++----------- project/src/controllers/RagfairController.ts | 4 +- .../generators/BotEquipmentModGenerator.ts | 6 +-- project/src/generators/BotLootGenerator.ts | 4 +- project/src/generators/BotWeaponGenerator.ts | 4 +- project/src/generators/LocationGenerator.ts | 2 +- project/src/helpers/HideoutHelper.ts | 4 +- project/src/helpers/InventoryHelper.ts | 6 +-- project/src/helpers/ItemHelper.ts | 8 ++-- project/src/helpers/QuestHelper.ts | 2 +- project/src/helpers/RagfairHelper.ts | 2 +- project/src/helpers/RagfairOfferHelper.ts | 2 +- .../src/services/CustomLocationWaveService.ts | 4 +- project/src/services/InsuranceService.ts | 2 +- project/src/services/ProfileFixerService.ts | 22 +++++----- project/src/services/SeasonalEventService.ts | 2 +- 19 files changed, 63 insertions(+), 73 deletions(-) diff --git a/project/src/controllers/DialogueController.ts b/project/src/controllers/DialogueController.ts index 5b0fb14d..608954b3 100644 --- a/project/src/controllers/DialogueController.ts +++ b/project/src/controllers/DialogueController.ts @@ -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; diff --git a/project/src/controllers/HealthController.ts b/project/src/controllers/HealthController.ts index 93d54d9d..781837af 100644 --- a/project/src/controllers/HealthController.ts +++ b/project/src/controllers/HealthController.ts @@ -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 diff --git a/project/src/controllers/HideoutController.ts b/project/src/controllers/HideoutController.ts index 826b6dd2..f6a47625 100644 --- a/project/src/controllers/HideoutController.ts +++ b/project/src/controllers/HideoutController.ts @@ -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 }); } diff --git a/project/src/controllers/InventoryController.ts b/project/src/controllers/InventoryController.ts index f6946ee7..c2bfb45e 100644 --- a/project/src/controllers/InventoryController.ts +++ b/project/src/controllers/InventoryController.ts @@ -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) { diff --git a/project/src/controllers/RagfairController.ts b/project/src/controllers/RagfairController.ts index e8d7d4a9..2a2f650e 100644 --- a/project/src/controllers/RagfairController.ts +++ b/project/src/controllers/RagfairController.ts @@ -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, diff --git a/project/src/generators/BotEquipmentModGenerator.ts b/project/src/generators/BotEquipmentModGenerator.ts index 84ba7ede..70842660 100644 --- a/project/src/generators/BotEquipmentModGenerator.ts +++ b/project/src/generators/BotEquipmentModGenerator.ts @@ -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, ) ) diff --git a/project/src/generators/BotLootGenerator.ts b/project/src/generators/BotLootGenerator.ts index 23696107..366c9e28 100644 --- a/project/src/generators/BotLootGenerator.ts +++ b/project/src/generators/BotLootGenerator.ts @@ -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); } diff --git a/project/src/generators/BotWeaponGenerator.ts b/project/src/generators/BotWeaponGenerator.ts index 9ba3ae1b..4ac7d670 100644 --- a/project/src/generators/BotWeaponGenerator.ts +++ b/project/src/generators/BotWeaponGenerator.ts @@ -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", { diff --git a/project/src/generators/LocationGenerator.ts b/project/src/generators/LocationGenerator.ts index b3a626c7..085db492 100644 --- a/project/src/generators/LocationGenerator.ts +++ b/project/src/generators/LocationGenerator.ts @@ -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) diff --git a/project/src/helpers/HideoutHelper.ts b/project/src/helpers/HideoutHelper.ts index 2850772e..315c4167 100644 --- a/project/src/helpers/HideoutHelper.ts +++ b/project/src/helpers/HideoutHelper.ts @@ -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; } /** diff --git a/project/src/helpers/InventoryHelper.ts b/project/src/helpers/InventoryHelper.ts index 2c0c8411..96e5759e 100644 --- a/project/src/helpers/InventoryHelper.ts +++ b/project/src/helpers/InventoryHelper.ts @@ -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; } } diff --git a/project/src/helpers/ItemHelper.ts b/project/src/helpers/ItemHelper.ts index 61a38753..9cbb9149 100644 --- a/project/src/helpers/ItemHelper.ts +++ b/project/src/helpers/ItemHelper.ts @@ -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; } diff --git a/project/src/helpers/QuestHelper.ts b/project/src/helpers/QuestHelper.ts index 02b6e3ae..c02ccff6 100644 --- a/project/src/helpers/QuestHelper.ts +++ b/project/src/helpers/QuestHelper.ts @@ -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; } diff --git a/project/src/helpers/RagfairHelper.ts b/project/src/helpers/RagfairHelper.ts index 03ac8763..bc0d5ade 100644 --- a/project/src/helpers/RagfairHelper.ts +++ b/project/src/helpers/RagfairHelper.ts @@ -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) { diff --git a/project/src/helpers/RagfairOfferHelper.ts b/project/src/helpers/RagfairOfferHelper.ts index c0af5ea7..c4d4bb47 100644 --- a/project/src/helpers/RagfairOfferHelper.ts +++ b/project/src/helpers/RagfairOfferHelper.ts @@ -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; }) diff --git a/project/src/services/CustomLocationWaveService.ts b/project/src/services/CustomLocationWaveService.ts index e79af932..ea6f0ea9 100644 --- a/project/src/services/CustomLocationWaveService.ts +++ b/project/src/services/CustomLocationWaveService.ts @@ -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; diff --git a/project/src/services/InsuranceService.ts b/project/src/services/InsuranceService.ts index 89f8497c..a6219b1a 100644 --- a/project/src/services/InsuranceService.ts +++ b/project/src/services/InsuranceService.ts @@ -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 diff --git a/project/src/services/ProfileFixerService.ts b/project/src/services/ProfileFixerService.ts index 1aa0f559..3e772823 100644 --- a/project/src/services/ProfileFixerService.ts +++ b/project/src/services/ProfileFixerService.ts @@ -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"); diff --git a/project/src/services/SeasonalEventService.ts b/project/src/services/SeasonalEventService.ts index 7339d496..5ad292ba 100644 --- a/project/src/services/SeasonalEventService.ts +++ b/project/src/services/SeasonalEventService.ts @@ -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); }