2023-03-03 16:23:46 +01:00
|
|
|
import { inject, injectable } from "tsyringe";
|
|
|
|
|
|
|
|
import { HealthHelper } from "../helpers/HealthHelper";
|
|
|
|
import { InventoryHelper } from "../helpers/InventoryHelper";
|
|
|
|
import { ItemHelper } from "../helpers/ItemHelper";
|
|
|
|
import { IPmcData } from "../models/eft/common/IPmcData";
|
|
|
|
import {
|
|
|
|
BodyPart, IHealthTreatmentRequestData
|
|
|
|
} from "../models/eft/health/IHealthTreatmentRequestData";
|
|
|
|
import { IOffraidEatRequestData } from "../models/eft/health/IOffraidEatRequestData";
|
|
|
|
import { IOffraidHealRequestData } from "../models/eft/health/IOffraidHealRequestData";
|
|
|
|
import { ISyncHealthRequestData } from "../models/eft/health/ISyncHealthRequestData";
|
|
|
|
import { IWorkoutData } from "../models/eft/health/IWorkoutData";
|
|
|
|
import { IItemEventRouterResponse } from "../models/eft/itemEvent/IItemEventRouterResponse";
|
|
|
|
import { IProcessBuyTradeRequestData } from "../models/eft/trade/IProcessBuyTradeRequestData";
|
|
|
|
import { Traders } from "../models/enums/Traders";
|
|
|
|
import { ILogger } from "../models/spt/utils/ILogger";
|
|
|
|
import { EventOutputHolder } from "../routers/EventOutputHolder";
|
|
|
|
import { LocalisationService } from "../services/LocalisationService";
|
|
|
|
import { PaymentService } from "../services/PaymentService";
|
2023-07-14 13:53:31 +02:00
|
|
|
import { HttpResponseUtil } from "../utils/HttpResponseUtil";
|
2023-03-03 16:23:46 +01:00
|
|
|
import { JsonUtil } from "../utils/JsonUtil";
|
|
|
|
|
|
|
|
@injectable()
|
|
|
|
export class HealthController
|
|
|
|
{
|
|
|
|
constructor(
|
|
|
|
@inject("WinstonLogger") protected logger: ILogger,
|
|
|
|
@inject("JsonUtil") protected jsonUtil: JsonUtil,
|
|
|
|
@inject("EventOutputHolder") protected eventOutputHolder: EventOutputHolder,
|
|
|
|
@inject("ItemHelper") protected itemHelper: ItemHelper,
|
|
|
|
@inject("PaymentService") protected paymentService: PaymentService,
|
|
|
|
@inject("InventoryHelper") protected inventoryHelper: InventoryHelper,
|
|
|
|
@inject("LocalisationService") protected localisationService: LocalisationService,
|
2023-07-14 13:53:31 +02:00
|
|
|
@inject("HttpResponseUtil") protected httpResponse: HttpResponseUtil,
|
2023-03-03 16:23:46 +01:00
|
|
|
@inject("HealthHelper") protected healthHelper: HealthHelper
|
|
|
|
)
|
|
|
|
{}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* stores in-raid player health
|
|
|
|
* @param pmcData Player profile
|
|
|
|
* @param info Request data
|
2023-10-10 13:03:20 +02:00
|
|
|
* @param sessionID Player id
|
2023-03-03 16:23:46 +01:00
|
|
|
* @param addEffects Should effects found be added or removed from profile
|
2023-10-10 13:03:20 +02:00
|
|
|
* @param deleteExistingEffects Should all prior effects be removed before apply new ones
|
2023-03-03 16:23:46 +01:00
|
|
|
*/
|
|
|
|
public saveVitality(pmcData: IPmcData, info: ISyncHealthRequestData, sessionID: string, addEffects = true, deleteExistingEffects = true): void
|
|
|
|
{
|
|
|
|
this.healthHelper.saveVitality(pmcData, info, sessionID, addEffects, deleteExistingEffects);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When healing in menu
|
2023-10-10 13:03:20 +02:00
|
|
|
* @param pmcData Player profile
|
|
|
|
* @param request Healing request
|
|
|
|
* @param sessionID Player id
|
|
|
|
* @returns IItemEventRouterResponse
|
2023-03-03 16:23:46 +01:00
|
|
|
*/
|
2023-10-10 13:03:20 +02:00
|
|
|
public offraidHeal(pmcData: IPmcData, request: IOffraidHealRequestData, sessionID: string): IItemEventRouterResponse
|
2023-03-03 16:23:46 +01:00
|
|
|
{
|
|
|
|
const output = this.eventOutputHolder.getOutput(sessionID);
|
|
|
|
|
2023-10-10 13:03:20 +02:00
|
|
|
// Update medkit used (hpresource)
|
|
|
|
const healingItemToUse = pmcData.Inventory.items.find(item => item._id === request.item);
|
|
|
|
if (!healingItemToUse)
|
2023-03-03 16:23:46 +01:00
|
|
|
{
|
2023-10-10 13:03:20 +02:00
|
|
|
const errorMessage = this.localisationService.getText("health-healing_item_not_found", healingItemToUse._id);
|
|
|
|
this.logger.error(errorMessage);
|
2023-03-03 16:23:46 +01:00
|
|
|
|
2023-10-10 13:03:20 +02:00
|
|
|
return this.httpResponse.appendErrorToOutput(output, errorMessage);
|
2023-03-03 16:23:46 +01:00
|
|
|
}
|
|
|
|
|
2023-10-10 13:03:20 +02:00
|
|
|
// Ensure item has a upd object
|
|
|
|
if (!healingItemToUse.upd)
|
2023-03-03 16:23:46 +01:00
|
|
|
{
|
2023-10-10 13:03:20 +02:00
|
|
|
healingItemToUse.upd = {};
|
2023-03-03 16:23:46 +01:00
|
|
|
}
|
|
|
|
|
2023-10-10 13:03:20 +02:00
|
|
|
if (healingItemToUse.upd.MedKit)
|
2023-03-03 16:23:46 +01:00
|
|
|
{
|
2023-10-10 13:03:20 +02:00
|
|
|
healingItemToUse.upd.MedKit.HpResource -= request.count;
|
2023-03-03 16:23:46 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-10-10 13:03:20 +02:00
|
|
|
// Get max healing from db
|
|
|
|
const maxhp = this.itemHelper.getItem(healingItemToUse._tpl)[1]._props.MaxHpResource;
|
|
|
|
healingItemToUse.upd.MedKit = { HpResource: maxhp - request.count }; // Subtract amout used from max
|
2023-03-03 16:23:46 +01:00
|
|
|
}
|
|
|
|
|
2023-10-10 13:03:20 +02:00
|
|
|
// Resource in medkit is spent, delete it
|
|
|
|
if (healingItemToUse.upd.MedKit.HpResource <= 0)
|
2023-03-03 16:23:46 +01:00
|
|
|
{
|
2023-10-10 13:03:20 +02:00
|
|
|
this.inventoryHelper.removeItem(pmcData, request.item, sessionID, output);
|
2023-03-03 16:23:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-07-14 13:53:31 +02:00
|
|
|
* Handle Eat event
|
2023-03-03 16:23:46 +01:00
|
|
|
* Consume food/water outside of a raid
|
|
|
|
* @param pmcData Player profile
|
2023-10-10 13:03:20 +02:00
|
|
|
* @param request Eat request
|
2023-03-03 16:23:46 +01:00
|
|
|
* @param sessionID Session id
|
|
|
|
* @returns IItemEventRouterResponse
|
|
|
|
*/
|
2023-10-10 13:03:20 +02:00
|
|
|
public offraidEat(pmcData: IPmcData, request: IOffraidEatRequestData, sessionID: string): IItemEventRouterResponse
|
2023-03-03 16:23:46 +01:00
|
|
|
{
|
|
|
|
let output = this.eventOutputHolder.getOutput(sessionID);
|
|
|
|
let resourceLeft = 0;
|
|
|
|
|
2023-10-10 13:03:20 +02:00
|
|
|
const itemToConsume = pmcData.Inventory.items.find(x => x._id === request.item);
|
2023-07-14 13:53:31 +02:00
|
|
|
if (!itemToConsume)
|
2023-03-03 16:23:46 +01:00
|
|
|
{
|
2023-07-14 13:53:31 +02:00
|
|
|
// Item not found, very bad
|
2023-10-10 13:03:20 +02:00
|
|
|
return this.httpResponse.appendErrorToOutput(output, this.localisationService.getText("health-unable_to_find_item_to_consume", request.item));
|
2023-07-14 13:53:31 +02:00
|
|
|
}
|
|
|
|
|
2023-10-10 13:03:20 +02:00
|
|
|
const consumedItemMaxResource = this.itemHelper.getItem(itemToConsume._tpl)[1]._props.MaxResource;
|
2023-07-14 13:53:31 +02:00
|
|
|
if (consumedItemMaxResource > 1)
|
|
|
|
{
|
|
|
|
if (itemToConsume.upd.FoodDrink === undefined)
|
2023-03-03 16:23:46 +01:00
|
|
|
{
|
2023-10-10 13:03:20 +02:00
|
|
|
itemToConsume.upd.FoodDrink = { HpPercent: consumedItemMaxResource - request.count };
|
2023-03-03 16:23:46 +01:00
|
|
|
}
|
2023-07-14 13:53:31 +02:00
|
|
|
else
|
2023-03-03 16:23:46 +01:00
|
|
|
{
|
2023-10-10 13:03:20 +02:00
|
|
|
itemToConsume.upd.FoodDrink.HpPercent -= request.count;
|
2023-03-03 16:23:46 +01:00
|
|
|
}
|
|
|
|
|
2023-07-14 13:53:31 +02:00
|
|
|
resourceLeft = itemToConsume.upd.FoodDrink.HpPercent;
|
2023-03-03 16:23:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove item from inventory if resource has dropped below threshold
|
2023-07-14 13:53:31 +02:00
|
|
|
if (consumedItemMaxResource === 1 || resourceLeft < 1)
|
2023-03-03 16:23:46 +01:00
|
|
|
{
|
2023-10-10 13:03:20 +02:00
|
|
|
output = this.inventoryHelper.removeItem(pmcData, request.item, sessionID, output);
|
2023-03-03 16:23:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-07-14 13:55:32 +02:00
|
|
|
* Handle RestoreHealth event
|
2023-03-03 16:23:46 +01:00
|
|
|
* Occurs on post-raid healing page
|
|
|
|
* @param pmcData player profile
|
|
|
|
* @param healthTreatmentRequest Request data from client
|
|
|
|
* @param sessionID Session id
|
2023-10-10 13:03:20 +02:00
|
|
|
* @returns IItemEventRouterResponse
|
2023-03-03 16:23:46 +01:00
|
|
|
*/
|
|
|
|
public healthTreatment(pmcData: IPmcData, healthTreatmentRequest: IHealthTreatmentRequestData, sessionID: string): IItemEventRouterResponse
|
|
|
|
{
|
|
|
|
let output = this.eventOutputHolder.getOutput(sessionID);
|
|
|
|
const payMoneyRequest: IProcessBuyTradeRequestData = {
|
2023-07-14 13:55:32 +02:00
|
|
|
Action: healthTreatmentRequest.Action,
|
2023-03-03 16:23:46 +01:00
|
|
|
tid: Traders.THERAPIST,
|
|
|
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
|
|
scheme_items: healthTreatmentRequest.items,
|
|
|
|
type: "",
|
|
|
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
|
|
item_id: "",
|
|
|
|
count: 0,
|
|
|
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
|
|
scheme_id: 0
|
|
|
|
};
|
|
|
|
|
|
|
|
output = this.paymentService.payMoney(pmcData, payMoneyRequest, sessionID, output);
|
|
|
|
if (output.warnings.length > 0)
|
|
|
|
{
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2023-10-10 13:03:20 +02:00
|
|
|
for (const bodyPartKey in healthTreatmentRequest.difference.BodyParts)
|
2023-03-03 16:23:46 +01:00
|
|
|
{
|
2023-10-10 13:03:20 +02:00
|
|
|
// Get body part from request + from pmc profile
|
|
|
|
const partRequest: BodyPart = healthTreatmentRequest.difference.BodyParts[bodyPartKey];
|
|
|
|
const profilePart = pmcData.Health.BodyParts[bodyPartKey];
|
2023-03-03 16:23:46 +01:00
|
|
|
|
2023-10-10 13:03:20 +02:00
|
|
|
// Set profile bodypart to max
|
|
|
|
profilePart.Health.Current = profilePart.Health.Maximum;
|
2023-03-03 16:23:46 +01:00
|
|
|
|
2023-10-10 13:03:20 +02:00
|
|
|
// Check for effects to remove
|
|
|
|
if (partRequest.Effects?.length > 0)
|
2023-03-03 16:23:46 +01:00
|
|
|
{
|
2023-10-10 13:03:20 +02:00
|
|
|
// Found some, loop over them and remove from pmc profile
|
|
|
|
for (const effect of partRequest.Effects)
|
2023-03-03 16:23:46 +01:00
|
|
|
{
|
2023-10-10 13:03:20 +02:00
|
|
|
delete pmcData.Health.BodyParts[bodyPartKey].Effects[effect];
|
|
|
|
}
|
2023-03-03 16:23:46 +01:00
|
|
|
|
2023-10-10 13:03:20 +02:00
|
|
|
// Remove empty effect object
|
|
|
|
if (Object.keys(pmcData.Health.BodyParts[bodyPartKey].Effects).length === 0)
|
|
|
|
{
|
|
|
|
delete pmcData.Health.BodyParts[bodyPartKey].Effects;
|
2023-03-03 16:23:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-10 13:03:20 +02:00
|
|
|
// Inform client of new post-raid, post-therapist heal values
|
|
|
|
output.profileChanges[sessionID].health = this.jsonUtil.clone(pmcData.Health);
|
2023-03-03 16:23:46 +01:00
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* applies skills from hideout workout.
|
|
|
|
* @param pmcData Player profile
|
|
|
|
* @param info Request data
|
|
|
|
* @param sessionID
|
|
|
|
*/
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
public applyWorkoutChanges(pmcData: IPmcData, info: IWorkoutData, sessionId: string): void
|
|
|
|
{
|
|
|
|
// https://dev.sp-tarkov.com/SPT-AKI/Server/issues/2674
|
|
|
|
// TODO:
|
|
|
|
// Health effects (fractures etc) are handled in /player/health/sync.
|
|
|
|
pmcData.Skills.Common = info.skills.Common;
|
|
|
|
}
|
|
|
|
}
|