Server/project/src/routers/ItemEventRouter.ts

63 lines
2.5 KiB
TypeScript
Raw Normal View History

2023-11-13 17:43:37 +01:00
import { inject, injectAll, injectable } from "tsyringe";
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,
@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
*/
public async handleEvents(info: IItemEventRouterRequest, sessionID: string): Promise<IItemEventRouterResponse>
2023-03-03 16:23:46 +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);
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}`);
await eventRouter.handleItemEvent(body.Action, pmcData, body, sessionID, output);
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);
// Clone output before resetting the output object ready for use next time
const outputClone = this.cloner.clone(output);
this.eventOutputHolder.resetOutput(sessionID);
return outputClone;
2023-03-03 16:23:46 +01:00
}
}