SpaceNinjaServer/src/controllers/api/gildWeaponController.ts

67 lines
2.6 KiB
TypeScript
Raw Normal View History

2024-06-26 15:24:08 +02:00
import { RequestHandler } from "express";
import { getAccountIdForRequest } from "@/src/services/loginService";
import { getJSONfromString } from "@/src/helpers/stringHelpers";
import { getInventory } from "@/src/services/inventoryService";
import { WeaponTypeInternal } from "@/src/services/itemDataService";
import { ArtifactPolarity, EquipmentFeatures } from "@/src/types/inventoryTypes/commonInventoryTypes";
const modularWeaponCategory: (WeaponTypeInternal | "Hoverboards")[] = [
"LongGuns",
"Pistols",
"Melee",
"OperatorAmps",
"Hoverboards" // Not sure about hoverboards just coppied from modual crafting
];
interface IGildWeaponRequest {
ItemName: string;
Recipe: string; // /Lotus/Weapons/SolarisUnited/LotusGildKitgunBlueprint
PolarizeSlot?: number;
PolarizeValue?: ArtifactPolarity;
ItemId: string;
Caterogy: WeaponTypeInternal | "Hoverboards";
}
// In export there no recipes for gild action, so reputation and ressources only consumed visually
// eslint-disable-next-line @typescript-eslint/no-misused-promises
export const gildWeaponController: RequestHandler = async (req, res) => {
const accountId = await getAccountIdForRequest(req);
const data: IGildWeaponRequest = getJSONfromString(String(req.body));
data.ItemId = String(req.query.ItemId);
if (!modularWeaponCategory.includes(req.query.Category as WeaponTypeInternal | "Hoverboards")) {
throw new Error(`unknown modular weapon Category: ${req.query.Category}`);
}
data.Caterogy = req.query.Category as WeaponTypeInternal | "Hoverboards";
const inventory = await getInventory(accountId);
2024-06-26 13:28:33 +00:00
if (!inventory[data.Caterogy]) {
2024-06-26 15:24:08 +02:00
throw new Error(`Category ${req.query.Category} dont foudn in inventory`);
}
const weaponIndex = inventory[data.Caterogy].findIndex(x => String(x._id) === data.ItemId);
2024-06-26 13:28:33 +00:00
if (weaponIndex === -1) {
2024-06-26 15:24:08 +02:00
throw new Error(`Weapon with ${data.ItemId} not found in category ${req.query.Category}`);
}
const weapon = inventory[data.Caterogy][weaponIndex];
weapon.Features = EquipmentFeatures.GILDING; // maybe 9 idk if DOUBLE_CAPACITY is also given
2024-06-26 13:28:33 +00:00
weapon.ItemName = data.ItemName;
2024-06-26 15:24:08 +02:00
weapon.XP = 0;
2024-06-26 13:28:33 +00:00
if (data.Caterogy != "OperatorAmps" && data.PolarizeSlot && data.PolarizeValue) {
weapon.Polarity = [
{
Slot: data.PolarizeSlot,
Value: data.PolarizeValue
}
];
2024-06-26 15:24:08 +02:00
}
2024-06-26 13:28:33 +00:00
inventory[data.Caterogy][weaponIndex] = weapon;
2024-06-26 15:24:08 +02:00
await inventory.save();
res.json({
InventoryChanges: {
[data.Caterogy]: [weapon]
}
});
};