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();
 | 
					const app = express();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
app.use(bodyParser.raw());
 | 
					app.use(bodyParser.raw());
 | 
				
			||||||
app.use(express.json());
 | 
					app.use(express.json({ limit: "4mb" }));
 | 
				
			||||||
app.use(bodyParser.text({ limit: "4mb" }));
 | 
					app.use(bodyParser.text());
 | 
				
			||||||
app.use(requestLogger);
 | 
					app.use(requestLogger);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
app.use("/api", apiRouter);
 | 
					app.use("/api", apiRouter);
 | 
				
			||||||
 | 
				
			|||||||
@ -1,7 +1,5 @@
 | 
				
			|||||||
import { getAccountIdForRequest } from "@/src/services/loginService";
 | 
					import { getAccountIdForRequest } from "@/src/services/loginService";
 | 
				
			||||||
import { addEquipment, addPowerSuit, addMechSuit, getInventory, updateSlots } from "@/src/services/inventoryService";
 | 
					import { getInventory, addItem } from "@/src/services/inventoryService";
 | 
				
			||||||
import { SlotNames } from "@/src/types/purchaseTypes";
 | 
					 | 
				
			||||||
import { InventorySlot } from "@/src/types/inventoryTypes/inventoryTypes";
 | 
					 | 
				
			||||||
import { RequestHandler } from "express";
 | 
					import { RequestHandler } from "express";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const addItemsController: RequestHandler = async (req, res) => {
 | 
					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 requests = req.body as IAddItemRequest[];
 | 
				
			||||||
    const inventory = await getInventory(accountId);
 | 
					    const inventory = await getInventory(accountId);
 | 
				
			||||||
    for (const request of requests) {
 | 
					    for (const request of requests) {
 | 
				
			||||||
        updateSlots(inventory, productCategoryToSlotName[request.type], 0, 1);
 | 
					        await addItem(inventory, request.ItemType, request.ItemCount);
 | 
				
			||||||
        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 inventory.save();
 | 
					    await inventory.save();
 | 
				
			||||||
    res.end();
 | 
					    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 {
 | 
					interface IAddItemRequest {
 | 
				
			||||||
    type: ItemType;
 | 
					    ItemType: string;
 | 
				
			||||||
    internalName: string;
 | 
					    ItemCount: number;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -7,7 +7,7 @@ import { RequestHandler } from "express";
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
export const importController: RequestHandler = async (req, res) => {
 | 
					export const importController: RequestHandler = async (req, res) => {
 | 
				
			||||||
    const accountId = await getAccountIdForRequest(req);
 | 
					    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);
 | 
					    const inventory = await getInventory(accountId);
 | 
				
			||||||
    importInventory(inventory, request.inventory);
 | 
					    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":
 | 
					        case "Types":
 | 
				
			||||||
            switch (typeName.substr(1).split("/")[2]) {
 | 
					            switch (typeName.substr(1).split("/")[2]) {
 | 
				
			||||||
                case "Sentinels": {
 | 
					                case "Sentinels": {
 | 
				
			||||||
 | 
				
			|||||||
@ -528,8 +528,8 @@ function doAcquireEquipment(category) {
 | 
				
			|||||||
            contentType: "application/json",
 | 
					            contentType: "application/json",
 | 
				
			||||||
            data: JSON.stringify([
 | 
					            data: JSON.stringify([
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
                    type: category,
 | 
					                    ItemType: uniqueName,
 | 
				
			||||||
                    internalName: uniqueName
 | 
					                    ItemCount: 1
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            ])
 | 
					            ])
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
@ -566,7 +566,7 @@ function addMissingEquipment(categories) {
 | 
				
			|||||||
                    "#" + category + "-list [data-item-type='" + elm.getAttribute("data-key") + "']"
 | 
					                    "#" + 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(":");
 | 
					    const [category, uniqueName] = data.split(":");
 | 
				
			||||||
    revalidateAuthz(() => {
 | 
					    revalidateAuthz(() => {
 | 
				
			||||||
        $.post({
 | 
					        $.post({
 | 
				
			||||||
            url: "/api/missionInventoryUpdate.php?" + window.authz,
 | 
					            url: "/custom/addItems?" + window.authz,
 | 
				
			||||||
            contentType: "text/plain",
 | 
					            contentType: "application/json",
 | 
				
			||||||
            data: JSON.stringify({
 | 
					            data: JSON.stringify([
 | 
				
			||||||
                [category]: [
 | 
					                {
 | 
				
			||||||
                    {
 | 
					                    ItemType: uniqueName,
 | 
				
			||||||
                        ItemType: uniqueName,
 | 
					                    ItemCount: parseInt($("#miscitem-count").val())
 | 
				
			||||||
                        ItemCount: parseInt($("#miscitem-count").val())
 | 
					                }
 | 
				
			||||||
                    }
 | 
					            ])
 | 
				
			||||||
                ]
 | 
					 | 
				
			||||||
            })
 | 
					 | 
				
			||||||
        }).done(function () {
 | 
					        }).done(function () {
 | 
				
			||||||
            alert("Successfully added.");
 | 
					            alert("Successfully added.");
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
@ -771,16 +769,14 @@ function doAcquireRiven() {
 | 
				
			|||||||
    revalidateAuthz(() => {
 | 
					    revalidateAuthz(() => {
 | 
				
			||||||
        // Add riven type to inventory
 | 
					        // Add riven type to inventory
 | 
				
			||||||
        $.post({
 | 
					        $.post({
 | 
				
			||||||
            url: "/api/missionInventoryUpdate.php?" + window.authz,
 | 
					            url: "/custom/addItems?" + window.authz,
 | 
				
			||||||
            contentType: "text/plain",
 | 
					            contentType: "application/json",
 | 
				
			||||||
            data: JSON.stringify({
 | 
					            data: JSON.stringify([
 | 
				
			||||||
                RawUpgrades: [
 | 
					                {
 | 
				
			||||||
                    {
 | 
					                    ItemType: uniqueName,
 | 
				
			||||||
                        ItemType: uniqueName,
 | 
					                    ItemCount: 1
 | 
				
			||||||
                        ItemCount: 1
 | 
					                }
 | 
				
			||||||
                    }
 | 
					            ])
 | 
				
			||||||
                ]
 | 
					 | 
				
			||||||
            })
 | 
					 | 
				
			||||||
        }).done(function () {
 | 
					        }).done(function () {
 | 
				
			||||||
            // Get riven's assigned id
 | 
					            // Get riven's assigned id
 | 
				
			||||||
            $.get("/api/inventory.php?" + window.authz + "&xpBasedLevelCapDisabled=1").done(data => {
 | 
					            $.get("/api/inventory.php?" + window.authz + "&xpBasedLevelCapDisabled=1").done(data => {
 | 
				
			||||||
@ -845,16 +841,14 @@ function doAcquireMod() {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
    revalidateAuthz(() => {
 | 
					    revalidateAuthz(() => {
 | 
				
			||||||
        $.post({
 | 
					        $.post({
 | 
				
			||||||
            url: "/api/missionInventoryUpdate.php?" + window.authz,
 | 
					            url: "/custom/addItems?" + window.authz,
 | 
				
			||||||
            contentType: "text/plain",
 | 
					            contentType: "application/json",
 | 
				
			||||||
            data: JSON.stringify({
 | 
					            data: JSON.stringify([
 | 
				
			||||||
                RawUpgrades: [
 | 
					                {
 | 
				
			||||||
                    {
 | 
					                    ItemType: uniqueName,
 | 
				
			||||||
                        ItemType: uniqueName,
 | 
					                    ItemCount: parseInt($("#mod-count").val())
 | 
				
			||||||
                        ItemCount: parseInt($("#mod-count").val())
 | 
					                }
 | 
				
			||||||
                    }
 | 
					            ])
 | 
				
			||||||
                ]
 | 
					 | 
				
			||||||
            })
 | 
					 | 
				
			||||||
        }).done(function () {
 | 
					        }).done(function () {
 | 
				
			||||||
            document.getElementById("mod-to-acquire").value = "";
 | 
					            document.getElementById("mod-to-acquire").value = "";
 | 
				
			||||||
            updateInventory();
 | 
					            updateInventory();
 | 
				
			||||||
@ -1033,14 +1027,14 @@ function doAddAllMods() {
 | 
				
			|||||||
                window.confirm("Are you sure you want to add " + modsAll.length + " mods to your account?")
 | 
					                window.confirm("Are you sure you want to add " + modsAll.length + " mods to your account?")
 | 
				
			||||||
            ) {
 | 
					            ) {
 | 
				
			||||||
                $.post({
 | 
					                $.post({
 | 
				
			||||||
                    url: "/api/missionInventoryUpdate.php?" + window.authz,
 | 
					                    url: "/custom/addItems?" + window.authz,
 | 
				
			||||||
                    contentType: "text/plain",
 | 
					                    contentType: "application/json",
 | 
				
			||||||
                    data: JSON.stringify({
 | 
					                    data: JSON.stringify(
 | 
				
			||||||
                        RawUpgrades: modsAll.map(mod => ({
 | 
					                        modsAll.map(mod => ({
 | 
				
			||||||
                            ItemType: mod,
 | 
					                            ItemType: mod,
 | 
				
			||||||
                            ItemCount: 21 // To fully upgrade certain arcanes
 | 
					                            ItemCount: 21 // To fully upgrade certain arcanes
 | 
				
			||||||
                        }))
 | 
					                        }))
 | 
				
			||||||
                    })
 | 
					                    )
 | 
				
			||||||
                }).done(function () {
 | 
					                }).done(function () {
 | 
				
			||||||
                    updateInventory();
 | 
					                    updateInventory();
 | 
				
			||||||
                });
 | 
					                });
 | 
				
			||||||
@ -1100,7 +1094,7 @@ function doImport() {
 | 
				
			|||||||
    revalidateAuthz(() => {
 | 
					    revalidateAuthz(() => {
 | 
				
			||||||
        $.post({
 | 
					        $.post({
 | 
				
			||||||
            url: "/custom/import?" + window.authz,
 | 
					            url: "/custom/import?" + window.authz,
 | 
				
			||||||
            contentType: "text/plain",
 | 
					            contentType: "application/json",
 | 
				
			||||||
            data: JSON.stringify({
 | 
					            data: JSON.stringify({
 | 
				
			||||||
                inventory: JSON.parse($("#import-inventory").val())
 | 
					                inventory: JSON.parse($("#import-inventory").val())
 | 
				
			||||||
            })
 | 
					            })
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user