From 080b466bfc460b9320c3581e639abbc598e39a46 Mon Sep 17 00:00:00 2001 From: Sainan Date: Fri, 24 Jan 2025 16:12:39 +0100 Subject: [PATCH] fix(webui): add items (#863) --- src/app.ts | 4 +- src/controllers/custom/addItemsController.ts | 49 ++----------- src/controllers/custom/importController.ts | 2 +- src/services/inventoryService.ts | 15 ++++ static/webui/script.js | 72 +++++++++----------- 5 files changed, 55 insertions(+), 87 deletions(-) diff --git a/src/app.ts b/src/app.ts index 667b45ccf..6f9bc0b97 100644 --- a/src/app.ts +++ b/src/app.ts @@ -16,8 +16,8 @@ import { webuiRouter } from "@/src/routes/webui"; const app = express(); app.use(bodyParser.raw()); -app.use(express.json()); -app.use(bodyParser.text({ limit: "4mb" })); +app.use(express.json({ limit: "4mb" })); +app.use(bodyParser.text()); app.use(requestLogger); app.use("/api", apiRouter); diff --git a/src/controllers/custom/addItemsController.ts b/src/controllers/custom/addItemsController.ts index eb8814bea..1eb50ed64 100644 --- a/src/controllers/custom/addItemsController.ts +++ b/src/controllers/custom/addItemsController.ts @@ -1,7 +1,5 @@ import { getAccountIdForRequest } from "@/src/services/loginService"; -import { addEquipment, addPowerSuit, addMechSuit, getInventory, updateSlots } from "@/src/services/inventoryService"; -import { SlotNames } from "@/src/types/purchaseTypes"; -import { InventorySlot } from "@/src/types/inventoryTypes/inventoryTypes"; +import { getInventory, addItem } from "@/src/services/inventoryService"; import { RequestHandler } from "express"; export const addItemsController: RequestHandler = async (req, res) => { @@ -9,52 +7,13 @@ export const addItemsController: RequestHandler = async (req, res) => { const requests = req.body as IAddItemRequest[]; const inventory = await getInventory(accountId); for (const request of requests) { - updateSlots(inventory, productCategoryToSlotName[request.type], 0, 1); - switch (request.type) { - case ItemType.Suits: - addPowerSuit(inventory, request.internalName); - break; - - case ItemType.MechSuits: - addMechSuit(inventory, request.internalName); - break; - - default: - addEquipment(inventory, request.type, request.internalName); - break; - } + await addItem(inventory, request.ItemType, request.ItemCount); } await inventory.save(); res.end(); }; -const productCategoryToSlotName: Record = { - Suits: InventorySlot.SUITS, - Pistols: InventorySlot.WEAPONS, - Melee: InventorySlot.WEAPONS, - LongGuns: InventorySlot.WEAPONS, - SpaceSuits: InventorySlot.SPACESUITS, - SpaceGuns: InventorySlot.SPACESUITS, - SpaceMelee: InventorySlot.SPACESUITS, - Sentinels: InventorySlot.SENTINELS, - SentinelWeapons: InventorySlot.SENTINELS, - MechSuits: InventorySlot.MECHSUITS -}; - -enum ItemType { - Suits = "Suits", - SpaceSuits = "SpaceSuits", - LongGuns = "LongGuns", - Pistols = "Pistols", - Melee = "Melee", - SpaceGuns = "SpaceGuns", - SpaceMelee = "SpaceMelee", - SentinelWeapons = "SentinelWeapons", - Sentinels = "Sentinels", - MechSuits = "MechSuits" -} - interface IAddItemRequest { - type: ItemType; - internalName: string; + ItemType: string; + ItemCount: number; } diff --git a/src/controllers/custom/importController.ts b/src/controllers/custom/importController.ts index 77d4d2cbc..93722fa03 100644 --- a/src/controllers/custom/importController.ts +++ b/src/controllers/custom/importController.ts @@ -7,7 +7,7 @@ import { RequestHandler } from "express"; export const importController: RequestHandler = async (req, res) => { const accountId = await getAccountIdForRequest(req); - const request = JSON.parse(String(req.body)) as IImportRequest; + const request = req.body as IImportRequest; const inventory = await getInventory(accountId); importInventory(inventory, request.inventory); diff --git a/src/services/inventoryService.ts b/src/services/inventoryService.ts index 0043ef355..3426ee294 100644 --- a/src/services/inventoryService.ts +++ b/src/services/inventoryService.ts @@ -366,6 +366,21 @@ export const addItem = async ( } }; } + case "Upgrades": { + // Needed to add Traumatic Peculiar + const changes = [ + { + ItemType: typeName, + ItemCount: quantity + } + ]; + addMods(inventory, changes); + return { + InventoryChanges: { + RawUpgrades: changes + } + }; + } case "Types": switch (typeName.substr(1).split("/")[2]) { case "Sentinels": { diff --git a/static/webui/script.js b/static/webui/script.js index 56a9ce980..6e3319016 100644 --- a/static/webui/script.js +++ b/static/webui/script.js @@ -528,8 +528,8 @@ function doAcquireEquipment(category) { contentType: "application/json", data: JSON.stringify([ { - type: category, - internalName: uniqueName + ItemType: uniqueName, + ItemCount: 1 } ]) }); @@ -566,7 +566,7 @@ function addMissingEquipment(categories) { "#" + category + "-list [data-item-type='" + elm.getAttribute("data-key") + "']" ) ) { - requests.push({ type: category, internalName: elm.getAttribute("data-key") }); + requests.push({ ItemType: elm.getAttribute("data-key"), ItemCount: 1 }); } }); }); @@ -734,16 +734,14 @@ function doAcquireMiscItems() { const [category, uniqueName] = data.split(":"); revalidateAuthz(() => { $.post({ - url: "/api/missionInventoryUpdate.php?" + window.authz, - contentType: "text/plain", - data: JSON.stringify({ - [category]: [ - { - ItemType: uniqueName, - ItemCount: parseInt($("#miscitem-count").val()) - } - ] - }) + url: "/custom/addItems?" + window.authz, + contentType: "application/json", + data: JSON.stringify([ + { + ItemType: uniqueName, + ItemCount: parseInt($("#miscitem-count").val()) + } + ]) }).done(function () { alert("Successfully added."); }); @@ -771,16 +769,14 @@ function doAcquireRiven() { revalidateAuthz(() => { // Add riven type to inventory $.post({ - url: "/api/missionInventoryUpdate.php?" + window.authz, - contentType: "text/plain", - data: JSON.stringify({ - RawUpgrades: [ - { - ItemType: uniqueName, - ItemCount: 1 - } - ] - }) + url: "/custom/addItems?" + window.authz, + contentType: "application/json", + data: JSON.stringify([ + { + ItemType: uniqueName, + ItemCount: 1 + } + ]) }).done(function () { // Get riven's assigned id $.get("/api/inventory.php?" + window.authz + "&xpBasedLevelCapDisabled=1").done(data => { @@ -845,16 +841,14 @@ function doAcquireMod() { } revalidateAuthz(() => { $.post({ - url: "/api/missionInventoryUpdate.php?" + window.authz, - contentType: "text/plain", - data: JSON.stringify({ - RawUpgrades: [ - { - ItemType: uniqueName, - ItemCount: parseInt($("#mod-count").val()) - } - ] - }) + url: "/custom/addItems?" + window.authz, + contentType: "application/json", + data: JSON.stringify([ + { + ItemType: uniqueName, + ItemCount: parseInt($("#mod-count").val()) + } + ]) }).done(function () { document.getElementById("mod-to-acquire").value = ""; updateInventory(); @@ -1033,14 +1027,14 @@ function doAddAllMods() { window.confirm("Are you sure you want to add " + modsAll.length + " mods to your account?") ) { $.post({ - url: "/api/missionInventoryUpdate.php?" + window.authz, - contentType: "text/plain", - data: JSON.stringify({ - RawUpgrades: modsAll.map(mod => ({ + url: "/custom/addItems?" + window.authz, + contentType: "application/json", + data: JSON.stringify( + modsAll.map(mod => ({ ItemType: mod, ItemCount: 21 // To fully upgrade certain arcanes })) - }) + ) }).done(function () { updateInventory(); }); @@ -1100,7 +1094,7 @@ function doImport() { revalidateAuthz(() => { $.post({ url: "/custom/import?" + window.authz, - contentType: "text/plain", + contentType: "application/json", data: JSON.stringify({ inventory: JSON.parse($("#import-inventory").val()) })