2023-11-13 17:43:37 +01:00
|
|
|
import { inject, injectAll, injectable } from "tsyringe";
|
2024-05-21 19:59:04 +02:00
|
|
|
import { ItemEventRouterDefinition } from "@spt/di/Router";
|
|
|
|
import { ProfileHelper } from "@spt/helpers/ProfileHelper";
|
|
|
|
import { IItemEventRouterRequest } from "@spt/models/eft/itemEvent/IItemEventRouterRequest";
|
|
|
|
import { IItemEventRouterResponse } from "@spt/models/eft/itemEvent/IItemEventRouterResponse";
|
|
|
|
import { ILogger } from "@spt/models/spt/utils/ILogger";
|
|
|
|
import { EventOutputHolder } from "@spt/routers/EventOutputHolder";
|
|
|
|
import { LocalisationService } from "@spt/services/LocalisationService";
|
|
|
|
import { ICloner } from "@spt/utils/cloners/ICloner";
|
2023-03-03 16:23:46 +01:00
|
|
|
|
|
|
|
@injectable()
|
2023-11-13 17:12:17 +01:00
|
|
|
export class ItemEventRouter
|
2023-03-03 16:23:46 +01:00
|
|
|
{
|
|
|
|
constructor(
|
|
|
|
@inject("WinstonLogger") protected logger: ILogger,
|
|
|
|
@inject("ProfileHelper") protected profileHelper: ProfileHelper,
|
|
|
|
@injectAll("IERouters") protected itemEventRouters: ItemEventRouterDefinition[],
|
|
|
|
@inject("LocalisationService") protected localisationService: LocalisationService,
|
2023-11-13 17:12:17 +01:00
|
|
|
@inject("EventOutputHolder") protected eventOutputHolder: EventOutputHolder,
|
2024-05-13 19:58:17 +02:00
|
|
|
@inject("RecursiveCloner") protected cloner: ICloner,
|
2023-11-13 17:12:17 +01:00
|
|
|
)
|
|
|
|
{}
|
2023-03-03 16:23:46 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param info Event request
|
|
|
|
* @param sessionID Session id
|
|
|
|
* @returns Item response
|
|
|
|
*/
|
2024-04-28 15:45:36 +02:00
|
|
|
public async handleEvents(info: IItemEventRouterRequest, sessionID: string): Promise<IItemEventRouterResponse>
|
2023-03-03 16:23:46 +01:00
|
|
|
{
|
2024-02-10 16:44:21 +01:00
|
|
|
const output = this.eventOutputHolder.getOutput(sessionID);
|
2023-03-03 16:23:46 +01:00
|
|
|
|
|
|
|
for (const body of info.data)
|
|
|
|
{
|
|
|
|
const pmcData = this.profileHelper.getPmcProfile(sessionID);
|
|
|
|
|
2024-05-17 21:32:41 +02:00
|
|
|
const eventRouter = this.itemEventRouters.find((r) => r.canHandle(body.Action));
|
2023-11-13 17:12:17 +01:00
|
|
|
if (eventRouter)
|
2023-03-03 16:23:46 +01:00
|
|
|
{
|
|
|
|
this.logger.debug(`event: ${body.Action}`);
|
2024-04-28 15:45:36 +02:00
|
|
|
await eventRouter.handleItemEvent(body.Action, pmcData, body, sessionID, output);
|
2024-02-10 16:44:21 +01:00
|
|
|
if (output.warnings.length > 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2023-03-03 16:23:46 +01:00
|
|
|
}
|
2023-11-13 17:12:17 +01:00
|
|
|
else
|
2023-03-03 16:23:46 +01:00
|
|
|
{
|
|
|
|
this.logger.error(this.localisationService.getText("event-unhandled_event", body.Action));
|
|
|
|
this.logger.writeToLogFile(body);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.eventOutputHolder.updateOutputProperties(sessionID);
|
|
|
|
|
2024-02-10 17:23:26 +01:00
|
|
|
// Clone output before resetting the output object ready for use next time
|
2024-05-13 19:58:17 +02:00
|
|
|
const outputClone = this.cloner.clone(output);
|
2024-02-10 17:23:26 +01:00
|
|
|
this.eventOutputHolder.resetOutput(sessionID);
|
|
|
|
|
|
|
|
return outputClone;
|
2023-03-03 16:23:46 +01:00
|
|
|
}
|
|
|
|
}
|