fix(webui): add items #863

Merged
Sainan merged 2 commits from add-items-fix into main 2025-01-24 07:12:39 -08:00
5 changed files with 55 additions and 87 deletions

View File

@ -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);

View File

@ -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<ItemType, SlotNames> = {
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;
}

View File

@ -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);

View File

@ -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": {

View File

@ -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]: [
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: [
url: "/custom/addItems?" + window.authz,
contentType: "application/json",
data: JSON.stringify([
coderabbitai[bot] commented 2025-01-24 07:10:34 -08:00 (Migrated from github.com)
Review

💡 Codebase verification

Validation needed for parsed item count, but jQuery setup is correct.
• The jQuery concern can be safely ignored as jQuery is properly included via /webui/libs/jquery-3.6.0.min.js.
• The parseInt validation concern is valid - there's no error handling for invalid input which could result in NaN.
• Consider adding a fallback value as suggested in the original review.

🔗 Analysis chain

Validate parsed item count and address linter warning for jQuery.
parseInt($("#miscitem-count").val()) can yield NaN if the user input is invalid. Consider a fallback or user-friendly error handling.
• ESLint flags $ as undefined at line 742. Verify that jQuery is properly declared (e.g., globally) or imported so the linter recognizes $.

Example fallback fix:

- ItemCount: parseInt($("#miscitem-count").val())
+ ItemCount: parseInt($("#miscitem-count").val()) || 1
🏁 Scripts executed

The following scripts were executed for the analysis:

Error: No shell scripts found


Script:

#!/bin/bash
# Search for jQuery script tags and imports
echo "=== Searching for jQuery inclusions ==="
rg -i 'jquery' --type html
rg -i "jquery" --type js

echo -e "\n=== Searching for parseInt usage patterns ==="
# Look for parseInt usage patterns
rg "parseInt\(" --type js -C 2

echo -e "\n=== Checking script.js file structure ==="
# Check the beginning of the file for any global declarations
head -n 20 static/webui/script.js

Length of output: 66181


Script:

#!/bin/bash
# Search for miscitem-count validation and usage
echo "=== Searching for miscitem-count handling ==="
rg "miscitem-count" --type js -C 3

echo -e "\n=== Searching for parseInt usage in script.js ==="
rg "parseInt\(" static/webui/script.js -C 2

Length of output: 2378

🧰 Tools
🪛 eslint

[error] 742-742: '$' is not defined.

(no-undef)

_:bulb: Codebase verification_ **Validation needed for parsed item count, but jQuery setup is correct.** • The jQuery concern can be safely ignored as jQuery is properly included via `/webui/libs/jquery-3.6.0.min.js`. • The parseInt validation concern is valid - there's no error handling for invalid input which could result in NaN. • Consider adding a fallback value as suggested in the original review. <details> <summary>🔗 Analysis chain</summary> **Validate parsed item count and address linter warning for jQuery.** • `parseInt($("#miscitem-count").val())` can yield `NaN` if the user input is invalid. Consider a fallback or user-friendly error handling. • ESLint flags `$` as undefined at line 742. Verify that jQuery is properly declared (e.g., globally) or imported so the linter recognizes `$`. Example fallback fix: ```diff - ItemCount: parseInt($("#miscitem-count").val()) + ItemCount: parseInt($("#miscitem-count").val()) || 1 ``` </details> <details> <summary>🏁 Scripts executed</summary> The following scripts were executed for the analysis: Error: No shell scripts found --- Script: ```shell #!/bin/bash # Search for jQuery script tags and imports echo "=== Searching for jQuery inclusions ===" rg -i 'jquery' --type html rg -i "jquery" --type js echo -e "\n=== Searching for parseInt usage patterns ===" # Look for parseInt usage patterns rg "parseInt\(" --type js -C 2 echo -e "\n=== Checking script.js file structure ===" # Check the beginning of the file for any global declarations head -n 20 static/webui/script.js ``` Length of output: 66181 --- Script: ```shell #!/bin/bash # Search for miscitem-count validation and usage echo "=== Searching for miscitem-count handling ===" rg "miscitem-count" --type js -C 3 echo -e "\n=== Searching for parseInt usage in script.js ===" rg "parseInt\(" static/webui/script.js -C 2 ``` Length of output: 2378 </details> <details> <summary>🧰 Tools</summary> <details> <summary>🪛 eslint</summary> [error] 742-742: '$' is not defined. (no-undef) </details> </details> <!-- This is an auto-generated comment by CodeRabbit -->
{
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: [
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())
})