XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFESpaceNinjaServer

A simple server for a small space ninja game

公开
关注 0 Fork 0 Star 0
UTF-8
import type { RequestHandler } from "express";
import { getAccountIdForRequest } from "../../services/loginService.ts";
import { broadcastInventoryUpdate } from "../../services/wsService.ts";
import { getJSONfromString } from "../../helpers/stringHelpers.ts";
import { addMiscItems, getInventory2 } from "../../services/inventoryService.ts";
import type { TEquipmentKey } from "../../types/inventoryTypes/inventoryTypes.ts";
import type { TArtifactPolarity } from "../../types/inventoryTypes/commonInventoryTypes.ts";
import { ExportRecipes } from "warframe-public-export-plus";
import type { IInventoryChanges } from "../../types/purchaseTypes.ts";
import type { IEquipmentClient } from "../../types/equipmentTypes.ts";
import { eEquipmentFeatures } from "../../types/equipmentTypes.ts";

interface IGildWeaponRequest {
    ItemName: string;
    Recipe: string; // e.g. /Lotus/Weapons/SolarisUnited/LotusGildKitgunBlueprint
    PolarizeSlot?: number;
    PolarizeValue?: TArtifactPolarity;
    ItemId: string;
    Category: TEquipmentKey;
}

export const gildWeaponController: RequestHandler = async (req, res) => {
    const accountId = await getAccountIdForRequest(req);
    const data = getJSONfromString<IGildWeaponRequest>(String(req.body));
    data.ItemId = String(req.query.ItemId);
    data.Category = req.query.Category as TEquipmentKey;

    const inventory = await getInventory2(accountId, data.Category, "MiscItems", "Affiliations");
    const weaponIndex = inventory[data.Category].findIndex(x => String(x._id) === data.ItemId);
    if (weaponIndex === -1) {
        throw new Error(`Weapon with ${data.ItemId} not found in category ${String(req.query.Category)}`);
    }

    const weapon = inventory[data.Category][weaponIndex];
    weapon.Features ??= 0;
    weapon.Features |= eEquipmentFeatures.GILDED;
    weapon.ItemName = data.ItemName;
    weapon.XP = 0;

    if (data.Category != "OperatorAmps" && data.PolarizeSlot && data.PolarizeValue) {
        weapon.Polarity = [
            {
                Slot: data.PolarizeSlot,
                Value: data.PolarizeValue
            }
        ];
    }
    inventory[data.Category][weaponIndex] = weapon;
    const inventoryChanges: IInventoryChanges = {};
    inventoryChanges[data.Category] = [weapon.toJSON<IEquipmentClient>()];

    const affiliationMods = [];

    const recipe = ExportRecipes[data.Recipe];
    inventoryChanges.MiscItems = recipe.secretIngredients!.map(ingredient => ({
        ItemType: ingredient.ItemType,
        ItemCount: ingredient.ItemCount * -1
    }));
    addMiscItems(inventory, inventoryChanges.MiscItems);

    if (recipe.syndicateStandingChange) {
        const affiliation = inventory.Affiliations.find(x => x.Tag == recipe.syndicateStandingChange!.tag)!;
        affiliation.Standing += recipe.syndicateStandingChange.value;
        affiliationMods.push({
            Tag: recipe.syndicateStandingChange.tag,
            Standing: recipe.syndicateStandingChange.value
        });
    }

    await inventory.save();
    res.json({
        InventoryChanges: inventoryChanges,
        AffiliationMods: affiliationMods
    });
    broadcastInventoryUpdate(req);
};