Server/project/src/controllers/PresetBuildController.ts

69 lines
2.5 KiB
TypeScript
Raw Normal View History

2023-03-03 16:23:46 +01:00
import { inject, injectable } from "tsyringe";
import { ItemHelper } from "../helpers/ItemHelper";
import { IPmcData } from "../models/eft/common/IPmcData";
import { IItemEventRouterResponse } from "../models/eft/itemEvent/IItemEventRouterResponse";
import {
IPresetBuildActionRequestData
} from "../models/eft/presetBuild/IPresetBuildActionRequestData";
import { WeaponBuild } from "../models/eft/profile/IAkiProfile";
import { EventOutputHolder } from "../routers/EventOutputHolder";
import { SaveServer } from "../servers/SaveServer";
import { HashUtil } from "../utils/HashUtil";
@injectable()
export class PresetBuildController
{
constructor(
@inject("HashUtil") protected hashUtil: HashUtil,
@inject("EventOutputHolder") protected eventOutputHolder: EventOutputHolder,
@inject("ItemHelper") protected itemHelper: ItemHelper,
@inject("SaveServer") protected saveServer: SaveServer
)
{ }
2023-07-15 15:49:25 +02:00
/** Handle client/handbook/builds/my/list */
2023-03-03 16:23:46 +01:00
public getUserBuilds(sessionID: string): WeaponBuild[]
{
return Object.values(this.saveServer.getProfile(sessionID).weaponbuilds);
}
2023-07-15 15:49:25 +02:00
/** Handle SaveBuild event */
2023-03-03 16:23:46 +01:00
public saveBuild(pmcData: IPmcData, body: IPresetBuildActionRequestData, sessionID: string): IItemEventRouterResponse
{
delete body.Action;
body.id = this.hashUtil.generate();
const output = this.eventOutputHolder.getOutput(sessionID);
const savedBuilds = this.saveServer.getProfile(sessionID).weaponbuilds;
// replace duplicate ID's. The first item is the base item.
// The root ID and the base item ID need to match.
body.items = this.itemHelper.replaceIDs(pmcData, body.items);
body.root = body.items[0]._id;
savedBuilds[body.name] = body;
this.saveServer.getProfile(sessionID).weaponbuilds = savedBuilds;
output.profileChanges[sessionID].builds.push(body);
return output;
}
2023-07-15 15:49:25 +02:00
/** Handle RemoveBuild event*/
2023-03-03 16:23:46 +01:00
public removeBuild(pmcData: IPmcData, body: IPresetBuildActionRequestData, sessionID: string): IItemEventRouterResponse
{
const savedBuilds = this.saveServer.getProfile(sessionID).weaponbuilds;
for (const name in savedBuilds)
{
if (savedBuilds[name].id === body.id)
{
delete savedBuilds[name];
this.saveServer.getProfile(sessionID).weaponbuilds = savedBuilds;
break;
}
}
return this.eventOutputHolder.getOutput(sessionID);
}
}