feat: implement XPInfo #200
@ -1,11 +1,29 @@
 | 
			
		||||
import { RequestHandler } from "express";
 | 
			
		||||
import { Inventory } from "@/src/models/inventoryModels/inventoryModel";
 | 
			
		||||
import { IStatsView } from "@/src/types/statTypes";
 | 
			
		||||
import config from "@/config.json";
 | 
			
		||||
import view from "@/static/fixed_responses/view.json";
 | 
			
		||||
import allScans from "@/static/fixed_responses/allScans.json";
 | 
			
		||||
 | 
			
		||||
const viewController: RequestHandler = (_req, res) => {
 | 
			
		||||
const viewController: RequestHandler = async (req, res) => {
 | 
			
		||||
    if (!req.query.accountId) {
 | 
			
		||||
        res.status(400).json({ error: "accountId was not provided" });
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    const inventory = await Inventory.findOne({ accountOwnerId: req.query.accountId });
 | 
			
		||||
    if (!inventory) {
 | 
			
		||||
        res.status(400).json({ error: "inventory was undefined" });
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const responseJson: IStatsView = view;
 | 
			
		||||
    responseJson.Weapons = [];
 | 
			
		||||
    for (const item of inventory.XPInfo) {
 | 
			
		||||
        responseJson.Weapons.push({
 | 
			
		||||
            type: item.ItemType,
 | 
			
		||||
            xp: item.XP
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
    if (config.unlockAllScans) {
 | 
			
		||||
        responseJson.Scans = allScans;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -201,12 +201,27 @@ const addGearExpByCategory = (
 | 
			
		||||
    const category = inventory[categoryName];
 | 
			
		||||
 | 
			
		||||
    gearArray?.forEach(({ ItemId, XP }) => {
 | 
			
		||||
        const itemIndex = ItemId ? category.findIndex(item => item._id?.equals(ItemId.$oid)) : -1;
 | 
			
		||||
        const item = category[itemIndex];
 | 
			
		||||
        if (!XP) {
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (itemIndex !== -1 && item.XP != undefined) {
 | 
			
		||||
            item.XP += XP || 0;
 | 
			
		||||
        const itemIndex = ItemId ? category.findIndex(item => item._id?.equals(ItemId.$oid)) : -1;
 | 
			
		||||
        if (itemIndex !== -1) {
 | 
			
		||||
            const item = category[itemIndex];
 | 
			
		||||
            item.XP ??= 0;
 | 
			
		||||
            item.XP += XP;
 | 
			
		||||
            inventory.markModified(`${categoryName}.${itemIndex}.XP`);
 | 
			
		||||
 | 
			
		||||
            const xpinfoIndex = inventory.XPInfo.findIndex(x => x.ItemType == item.ItemType);
 | 
			
		||||
            if (xpinfoIndex !== -1) {
 | 
			
		||||
                const xpinfo = inventory.XPInfo[xpinfoIndex];
 | 
			
		||||
                xpinfo.XP += XP;
 | 
			
		||||
            } else {
 | 
			
		||||
                inventory.XPInfo.push({
 | 
			
		||||
                    ItemType: item.ItemType,
 | 
			
		||||
                    XP: XP
 | 
			
		||||
                });
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    });
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user