2023-03-03 15:23:46 +00:00
|
|
|
import { inject, injectable } from "tsyringe";
|
|
|
|
|
2023-10-19 17:21:17 +00:00
|
|
|
import { IGetBodyResponseData } from "@spt-aki/models/eft/httpResponse/IGetBodyResponseData";
|
|
|
|
import { Warning } from "@spt-aki/models/eft/itemEvent/IItemEventRouterBase";
|
|
|
|
import { IItemEventRouterRequest } from "@spt-aki/models/eft/itemEvent/IItemEventRouterRequest";
|
|
|
|
import { IItemEventRouterResponse } from "@spt-aki/models/eft/itemEvent/IItemEventRouterResponse";
|
|
|
|
import { BackendErrorCodes } from "@spt-aki/models/enums/BackendErrorCodes";
|
|
|
|
import { ItemEventRouter } from "@spt-aki/routers/ItemEventRouter";
|
|
|
|
import { HttpResponseUtil } from "@spt-aki/utils/HttpResponseUtil";
|
2023-03-03 15:23:46 +00:00
|
|
|
|
|
|
|
@injectable()
|
|
|
|
export class ItemEventCallbacks
|
|
|
|
{
|
|
|
|
constructor(
|
|
|
|
@inject("HttpResponseUtil") protected httpResponse: HttpResponseUtil,
|
2023-11-10 15:19:56 -05:00
|
|
|
@inject("ItemEventRouter") protected itemEventRouter: ItemEventRouter,
|
2023-03-03 15:23:46 +00:00
|
|
|
)
|
2023-11-10 15:19:56 -05:00
|
|
|
{}
|
2023-03-03 15:23:46 +00:00
|
|
|
|
2023-11-10 15:19:56 -05:00
|
|
|
public handleEvents(
|
|
|
|
url: string,
|
|
|
|
info: IItemEventRouterRequest,
|
|
|
|
sessionID: string,
|
|
|
|
): IGetBodyResponseData<IItemEventRouterResponse>
|
2023-03-03 15:23:46 +00:00
|
|
|
{
|
|
|
|
const eventResponse = this.itemEventRouter.handleEvents(info, sessionID);
|
2024-03-08 08:59:14 +00:00
|
|
|
const result = (this.isCriticalError(eventResponse.warnings))
|
2023-11-13 12:29:16 -05:00
|
|
|
? this.httpResponse.getBody(
|
2023-11-10 15:19:56 -05:00
|
|
|
eventResponse,
|
|
|
|
this.getErrorCode(eventResponse.warnings),
|
|
|
|
eventResponse.warnings[0].errmsg,
|
2024-03-08 08:59:14 +00:00
|
|
|
)
|
2023-11-13 12:29:16 -05:00
|
|
|
: this.httpResponse.getBody(eventResponse);
|
2023-03-03 15:23:46 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-03-08 08:59:14 +00:00
|
|
|
/**
|
|
|
|
* Return true if the passed in list of warnings contains critical issues
|
|
|
|
* @param warnings The list of warnings to check for critical errors
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
private isCriticalError(warnings: Warning[]): boolean
|
|
|
|
{
|
|
|
|
// List of non-critical error codes, we return true if any error NOT included is passed in
|
|
|
|
const nonCriticalErrorCodes: BackendErrorCodes[] = [BackendErrorCodes.NOTENOUGHSPACE];
|
|
|
|
|
|
|
|
for (const warning of warnings)
|
|
|
|
{
|
|
|
|
if (!nonCriticalErrorCodes.includes(+warning.code))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-03-03 15:23:46 +00:00
|
|
|
protected getErrorCode(warnings: Warning[]): number
|
|
|
|
{
|
|
|
|
if (warnings[0]?.code)
|
|
|
|
{
|
|
|
|
return Number(warnings[0].code);
|
|
|
|
}
|
|
|
|
return BackendErrorCodes.UNKNOWN_ERROR;
|
|
|
|
}
|
2023-11-10 15:19:56 -05:00
|
|
|
}
|