Improve offraidEat

use .find() instead of iterating over every item in player inventory
Return client error if consumable item not found + localised
This commit is contained in:
Dev 2023-07-14 12:53:31 +01:00
parent 0b68fb53e0
commit 455bde264c
2 changed files with 24 additions and 21 deletions

View File

@ -66,6 +66,8 @@
"hideout-unable_to_find_scavcase_requested_item_in_profile_inventory": "Unable to find item: %s requested by ScavCase", "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", "hideout-unhandled_remove_item_from_area_request": "Unhandled attempt to remove item from hideout area: %s",
"http-unknown_error": "An unknown error occurred", "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": "Importing database...",
"importing_database_finish": "Database import finished", "importing_database_finish": "Database import finished",
"validation_not_found": "The file checks.dat was not found. File validation skipped.", "validation_not_found": "The file checks.dat was not found. File validation skipped.",

View File

@ -18,6 +18,7 @@ import { ILogger } from "../models/spt/utils/ILogger";
import { EventOutputHolder } from "../routers/EventOutputHolder"; import { EventOutputHolder } from "../routers/EventOutputHolder";
import { LocalisationService } from "../services/LocalisationService"; import { LocalisationService } from "../services/LocalisationService";
import { PaymentService } from "../services/PaymentService"; import { PaymentService } from "../services/PaymentService";
import { HttpResponseUtil } from "../utils/HttpResponseUtil";
import { JsonUtil } from "../utils/JsonUtil"; import { JsonUtil } from "../utils/JsonUtil";
@injectable() @injectable()
@ -31,6 +32,7 @@ export class HealthController
@inject("PaymentService") protected paymentService: PaymentService, @inject("PaymentService") protected paymentService: PaymentService,
@inject("InventoryHelper") protected inventoryHelper: InventoryHelper, @inject("InventoryHelper") protected inventoryHelper: InventoryHelper,
@inject("LocalisationService") protected localisationService: LocalisationService, @inject("LocalisationService") protected localisationService: LocalisationService,
@inject("HttpResponseUtil") protected httpResponse: HttpResponseUtil,
@inject("HealthHelper") protected healthHelper: HealthHelper @inject("HealthHelper") protected healthHelper: HealthHelper
) )
{} {}
@ -92,6 +94,7 @@ export class HealthController
} }
/** /**
* Handle Eat event
* Consume food/water outside of a raid * Consume food/water outside of a raid
* @param pmcData Player profile * @param pmcData Player profile
* @param body request Object * @param body request Object
@ -102,35 +105,33 @@ export class HealthController
{ {
let output = this.eventOutputHolder.getOutput(sessionID); let output = this.eventOutputHolder.getOutput(sessionID);
let resourceLeft = 0; 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));
continue;
} }
maxResource = this.itemHelper.getItem(item._tpl)[1]._props.MaxResource; consumedItemMaxResource = this.itemHelper.getItem(itemToConsume._tpl)[1]._props.MaxResource;
if (maxResource > 1) if (consumedItemMaxResource > 1)
{ {
if (item.upd.FoodDrink === undefined) if (itemToConsume.upd.FoodDrink === undefined)
{ {
item.upd.FoodDrink = { "HpPercent": maxResource - body.count }; itemToConsume.upd.FoodDrink = {
"HpPercent": consumedItemMaxResource - body.count };
} }
else else
{ {
item.upd.FoodDrink.HpPercent -= body.count; itemToConsume.upd.FoodDrink.HpPercent -= body.count;
} }
resourceLeft = item.upd.FoodDrink.HpPercent; resourceLeft = itemToConsume.upd.FoodDrink.HpPercent;
}
break;
} }
// Remove item from inventory if resource has dropped below threshold // 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); output = this.inventoryHelper.removeItem(pmcData, body.item, sessionID, output);
} }