Server/project/src/routers/ItemEventRouter.ts

56 lines
2.1 KiB
TypeScript
Raw Normal View History

2023-11-13 17:43:37 +01:00
import { inject, injectAll, injectable } from "tsyringe";
2023-03-03 16:23:46 +01:00
import { ItemEventRouterDefinition } from "@spt-aki/di/Router";
import { ProfileHelper } from "@spt-aki/helpers/ProfileHelper";
import { IItemEventRouterRequest } from "@spt-aki/models/eft/itemEvent/IItemEventRouterRequest";
import { IItemEventRouterResponse } from "@spt-aki/models/eft/itemEvent/IItemEventRouterResponse";
import { ILogger } from "@spt-aki/models/spt/utils/ILogger";
import { EventOutputHolder } from "@spt-aki/routers/EventOutputHolder";
import { LocalisationService } from "@spt-aki/services/LocalisationService";
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,
)
{}
2023-03-03 16:23:46 +01:00
/**
* @param info Event request
* @param sessionID Session id
* @returns Item response
*/
2023-11-13 17:12:17 +01:00
public handleEvents(info: IItemEventRouterRequest, sessionID: string): IItemEventRouterResponse
2023-03-03 16:23:46 +01:00
{
this.eventOutputHolder.resetOutput(sessionID);
let result = this.eventOutputHolder.getOutput(sessionID);
for (const body of info.data)
{
const pmcData = this.profileHelper.getPmcProfile(sessionID);
2023-11-13 17:12:17 +01:00
const eventRouter = this.itemEventRouters.find((r) => r.canHandle(body.Action));
if (eventRouter)
2023-03-03 16:23:46 +01:00
{
this.logger.debug(`event: ${body.Action}`);
result = eventRouter.handleItemEvent(body.Action, pmcData, body, sessionID);
}
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);
return result;
}
}