From 455bde264c3bfc877a0eb6364100ac931f087ca1 Mon Sep 17 00:00:00 2001 From: Dev Date: Fri, 14 Jul 2023 12:53:31 +0100 Subject: [PATCH] Improve offraidEat use .find() instead of iterating over every item in player inventory Return client error if consumable item not found + localised --- .../assets/database/locales/server/en.json | 2 + project/src/controllers/HealthController.ts | 43 ++++++++++--------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/project/assets/database/locales/server/en.json b/project/assets/database/locales/server/en.json index b2c5401f..000815d8 100644 --- a/project/assets/database/locales/server/en.json +++ b/project/assets/database/locales/server/en.json @@ -66,6 +66,8 @@ "hideout-unable_to_find_scavcase_requested_item_in_profile_inventory": "Unable to find item: %s requested by ScavCase", "hideout-unhandled_remove_item_from_area_request": "Unhandled attempt to remove item from hideout area: %s", "http-unknown_error": "An unknown error occurred", + "health-healing_item_not_found": "Unable to find healing item %s in player inventory", + "health-unable_to_find_item_to_consume": "Unable to find consumable item %s in player inventory", "importing_database": "Importing database...", "importing_database_finish": "Database import finished", "validation_not_found": "The file checks.dat was not found. File validation skipped.", diff --git a/project/src/controllers/HealthController.ts b/project/src/controllers/HealthController.ts index 23423fa0..0dd0a0a8 100644 --- a/project/src/controllers/HealthController.ts +++ b/project/src/controllers/HealthController.ts @@ -18,6 +18,7 @@ import { ILogger } from "../models/spt/utils/ILogger"; import { EventOutputHolder } from "../routers/EventOutputHolder"; import { LocalisationService } from "../services/LocalisationService"; import { PaymentService } from "../services/PaymentService"; +import { HttpResponseUtil } from "../utils/HttpResponseUtil"; import { JsonUtil } from "../utils/JsonUtil"; @injectable() @@ -31,6 +32,7 @@ export class HealthController @inject("PaymentService") protected paymentService: PaymentService, @inject("InventoryHelper") protected inventoryHelper: InventoryHelper, @inject("LocalisationService") protected localisationService: LocalisationService, + @inject("HttpResponseUtil") protected httpResponse: HttpResponseUtil, @inject("HealthHelper") protected healthHelper: HealthHelper ) {} @@ -92,6 +94,7 @@ export class HealthController } /** + * Handle Eat event * Consume food/water outside of a raid * @param pmcData Player profile * @param body request Object @@ -102,35 +105,33 @@ export class HealthController { let output = this.eventOutputHolder.getOutput(sessionID); let resourceLeft = 0; - let maxResource = 0; + let consumedItemMaxResource = 0; - for (const item of pmcData.Inventory.items) + const itemToConsume = pmcData.Inventory.items.find(x => x._id === body.item); + if (!itemToConsume) { - if (item._id !== body.item) + // Item not found, very bad + return this.httpResponse.appendErrorToOutput(output, this.localisationService.getText("health-unable_to_find_item_to_consume", body.item)); + } + + consumedItemMaxResource = this.itemHelper.getItem(itemToConsume._tpl)[1]._props.MaxResource; + if (consumedItemMaxResource > 1) + { + if (itemToConsume.upd.FoodDrink === undefined) { - continue; + itemToConsume.upd.FoodDrink = { + "HpPercent": consumedItemMaxResource - body.count }; + } + else + { + itemToConsume.upd.FoodDrink.HpPercent -= body.count; } - maxResource = this.itemHelper.getItem(item._tpl)[1]._props.MaxResource; - if (maxResource > 1) - { - if (item.upd.FoodDrink === undefined) - { - item.upd.FoodDrink = { "HpPercent": maxResource - body.count }; - } - else - { - item.upd.FoodDrink.HpPercent -= body.count; - } - - resourceLeft = item.upd.FoodDrink.HpPercent; - } - - break; + resourceLeft = itemToConsume.upd.FoodDrink.HpPercent; } // Remove item from inventory if resource has dropped below threshold - if (maxResource === 1 || resourceLeft < 1) + if (consumedItemMaxResource === 1 || resourceLeft < 1) { output = this.inventoryHelper.removeItem(pmcData, body.item, sessionID, output); }