forked from OpenWF/SpaceNinjaServer
		
	fix(webui): add items (#863)
This commit is contained in:
		
							parent
							
								
									3cd66391b6
								
							
						
					
					
						commit
						080b466bfc
					
				@ -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);
 | 
			
		||||
 | 
			
		||||
@ -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;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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);
 | 
			
		||||
 | 
			
		||||
@ -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": {
 | 
			
		||||
 | 
			
		||||
@ -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([
 | 
			
		||||
                {
 | 
			
		||||
                    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())
 | 
			
		||||
            })
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user