chore: reimplement setWeaponSkillTree as a mongo query (#1406)

This is faster by like 1-2 ms

Reviewed-on: OpenWF/SpaceNinjaServer#1406
Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com>
Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com>
This commit is contained in:
Sainan 2025-03-31 09:18:33 -07:00 committed by Sainan
parent 3d69828610
commit fb58aeb07f

View File

@ -1,18 +1,25 @@
import { RequestHandler } from "express"; import { RequestHandler } from "express";
import { getAccountIdForRequest } from "@/src/services/loginService"; import { getAccountIdForRequest } from "@/src/services/loginService";
import { getInventory } from "@/src/services/inventoryService";
import { getJSONfromString } from "@/src/helpers/stringHelpers"; import { getJSONfromString } from "@/src/helpers/stringHelpers";
import { WeaponTypeInternal } from "@/src/services/itemDataService"; import { Inventory } from "@/src/models/inventoryModels/inventoryModel";
import { equipmentKeys, TEquipmentKey } from "@/src/types/inventoryTypes/inventoryTypes";
export const setWeaponSkillTreeController: RequestHandler = async (req, res) => { export const setWeaponSkillTreeController: RequestHandler = async (req, res) => {
const accountId = await getAccountIdForRequest(req); const accountId = await getAccountIdForRequest(req);
const inventory = await getInventory(accountId, req.query.Category as string);
const payload = getJSONfromString<ISetWeaponSkillTreeRequest>(String(req.body)); const payload = getJSONfromString<ISetWeaponSkillTreeRequest>(String(req.body));
const item = inventory[req.query.Category as WeaponTypeInternal].id(req.query.ItemId as string)!; if (equipmentKeys.indexOf(req.query.Category as TEquipmentKey) != -1) {
item.SkillTree = payload.SkillTree; await Inventory.updateOne(
{
accountOwnerId: accountId,
[`${req.query.Category as string}._id`]: req.query.ItemId as string
},
{
[`${req.query.Category as string}.$.SkillTree`]: payload.SkillTree
}
);
}
await inventory.save();
res.end(); res.end();
}; };