chore: updateCurrency with existing inventory instance (#674)
This commit is contained in:
		
							parent
							
								
									ddb1a8d665
								
							
						
					
					
						commit
						16d98636e9
					
				@ -7,7 +7,14 @@ import { getRecipe } from "@/src/services/itemDataService";
 | 
				
			|||||||
import { IOid } from "@/src/types/commonTypes";
 | 
					import { IOid } from "@/src/types/commonTypes";
 | 
				
			||||||
import { getJSONfromString } from "@/src/helpers/stringHelpers";
 | 
					import { getJSONfromString } from "@/src/helpers/stringHelpers";
 | 
				
			||||||
import { getAccountIdForRequest } from "@/src/services/loginService";
 | 
					import { getAccountIdForRequest } from "@/src/services/loginService";
 | 
				
			||||||
import { getInventory, updateCurrency, addItem, addMiscItems, addRecipes } from "@/src/services/inventoryService";
 | 
					import {
 | 
				
			||||||
 | 
					    getInventory,
 | 
				
			||||||
 | 
					    updateCurrency,
 | 
				
			||||||
 | 
					    addItem,
 | 
				
			||||||
 | 
					    addMiscItems,
 | 
				
			||||||
 | 
					    addRecipes,
 | 
				
			||||||
 | 
					    updateCurrencyByAccountId
 | 
				
			||||||
 | 
					} from "@/src/services/inventoryService";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export interface IClaimCompletedRecipeRequest {
 | 
					export interface IClaimCompletedRecipeRequest {
 | 
				
			||||||
    RecipeIds: IOid[];
 | 
					    RecipeIds: IOid[];
 | 
				
			||||||
@ -43,9 +50,8 @@ export const claimCompletedRecipeController: RequestHandler = async (req, res) =
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (req.query.cancel) {
 | 
					    if (req.query.cancel) {
 | 
				
			||||||
        const currencyChanges = await updateCurrency(recipe.buildPrice * -1, false, accountId);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        const inventory = await getInventory(accountId);
 | 
					        const inventory = await getInventory(accountId);
 | 
				
			||||||
 | 
					        const currencyChanges = updateCurrency(inventory, recipe.buildPrice * -1, false);
 | 
				
			||||||
        addMiscItems(inventory, recipe.ingredients);
 | 
					        addMiscItems(inventory, recipe.ingredients);
 | 
				
			||||||
        await inventory.save();
 | 
					        await inventory.save();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -74,7 +80,7 @@ export const claimCompletedRecipeController: RequestHandler = async (req, res) =
 | 
				
			|||||||
        if (req.query.rush) {
 | 
					        if (req.query.rush) {
 | 
				
			||||||
            InventoryChanges = {
 | 
					            InventoryChanges = {
 | 
				
			||||||
                ...InventoryChanges,
 | 
					                ...InventoryChanges,
 | 
				
			||||||
                ...(await updateCurrency(recipe.skipBuildTimePrice, true, accountId))
 | 
					                ...(await updateCurrencyByAccountId(recipe.skipBuildTimePrice, true, accountId))
 | 
				
			||||||
            };
 | 
					            };
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        res.json({
 | 
					        res.json({
 | 
				
			||||||
 | 
				
			|||||||
@ -1,5 +1,5 @@
 | 
				
			|||||||
import { getAccountIdForRequest } from "@/src/services/loginService";
 | 
					import { getAccountIdForRequest } from "@/src/services/loginService";
 | 
				
			||||||
import { updateCurrency } from "@/src/services/inventoryService";
 | 
					import { updateCurrencyByAccountId } from "@/src/services/inventoryService";
 | 
				
			||||||
import { RequestHandler } from "express";
 | 
					import { RequestHandler } from "express";
 | 
				
			||||||
import { updateSlots } from "@/src/services/inventoryService";
 | 
					import { updateSlots } from "@/src/services/inventoryService";
 | 
				
			||||||
import { InventorySlot } from "@/src/types/inventoryTypes/inventoryTypes";
 | 
					import { InventorySlot } from "@/src/types/inventoryTypes/inventoryTypes";
 | 
				
			||||||
@ -26,7 +26,7 @@ export const inventorySlotsController: RequestHandler = async (req, res) => {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    //TODO: check which slot was purchased because pvpBonus is also possible
 | 
					    //TODO: check which slot was purchased because pvpBonus is also possible
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const currencyChanges = await updateCurrency(20, true, accountId);
 | 
					    const currencyChanges = await updateCurrencyByAccountId(20, true, accountId);
 | 
				
			||||||
    await updateSlots(accountId, InventorySlot.PVE_LOADOUTS, 1, 1);
 | 
					    await updateSlots(accountId, InventorySlot.PVE_LOADOUTS, 1, 1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    //console.log({ InventoryChanges: currencyChanges }, " added loadout changes:");
 | 
					    //console.log({ InventoryChanges: currencyChanges }, " added loadout changes:");
 | 
				
			||||||
 | 
				
			|||||||
@ -34,14 +34,7 @@ export const modularWeaponCraftingController: RequestHandler = async (req, res)
 | 
				
			|||||||
    // Give weapon
 | 
					    // Give weapon
 | 
				
			||||||
    const weapon = await addEquipment(category, data.WeaponType, accountId, data.Parts);
 | 
					    const weapon = await addEquipment(category, data.WeaponType, accountId, data.Parts);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Remove credits
 | 
					    // Remove credits & parts
 | 
				
			||||||
    const currencyChanges = await updateCurrency(
 | 
					 | 
				
			||||||
        category == "Hoverboards" || category == "MoaPets" ? 5000 : 4000,
 | 
					 | 
				
			||||||
        false,
 | 
					 | 
				
			||||||
        accountId
 | 
					 | 
				
			||||||
    );
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // Remove parts
 | 
					 | 
				
			||||||
    const miscItemChanges = [];
 | 
					    const miscItemChanges = [];
 | 
				
			||||||
    for (const part of data.Parts) {
 | 
					    for (const part of data.Parts) {
 | 
				
			||||||
        miscItemChanges.push({
 | 
					        miscItemChanges.push({
 | 
				
			||||||
@ -50,6 +43,11 @@ export const modularWeaponCraftingController: RequestHandler = async (req, res)
 | 
				
			|||||||
        });
 | 
					        });
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    const inventory = await getInventory(accountId);
 | 
					    const inventory = await getInventory(accountId);
 | 
				
			||||||
 | 
					    const currencyChanges = updateCurrency(
 | 
				
			||||||
 | 
					        inventory,
 | 
				
			||||||
 | 
					        category == "Hoverboards" || category == "MoaPets" ? 5000 : 4000,
 | 
				
			||||||
 | 
					        false
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
    addMiscItems(inventory, miscItemChanges);
 | 
					    addMiscItems(inventory, miscItemChanges);
 | 
				
			||||||
    await inventory.save();
 | 
					    await inventory.save();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -20,8 +20,9 @@ export const nameWeaponController: RequestHandler = async (req, res) => {
 | 
				
			|||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
        item.ItemName = undefined;
 | 
					        item.ItemName = undefined;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					    const currencyChanges = updateCurrency(inventory, "webui" in req.query ? 0 : 15, true);
 | 
				
			||||||
    await inventory.save();
 | 
					    await inventory.save();
 | 
				
			||||||
    res.json({
 | 
					    res.json({
 | 
				
			||||||
        InventoryChanges: await updateCurrency("webui" in req.query ? 0 : 15, true, accountId)
 | 
					        InventoryChanges: currencyChanges
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
				
			|||||||
@ -26,14 +26,13 @@ export const startRecipeController: RequestHandler = async (req, res) => {
 | 
				
			|||||||
        throw new Error(`unknown recipe ${recipeName}`);
 | 
					        throw new Error(`unknown recipe ${recipeName}`);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    await updateCurrency(recipe.buildPrice, false, accountId);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    const ingredientsInverse = recipe.ingredients.map(component => ({
 | 
					    const ingredientsInverse = recipe.ingredients.map(component => ({
 | 
				
			||||||
        ItemType: component.ItemType,
 | 
					        ItemType: component.ItemType,
 | 
				
			||||||
        ItemCount: component.ItemCount * -1
 | 
					        ItemCount: component.ItemCount * -1
 | 
				
			||||||
    }));
 | 
					    }));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const inventory = await getInventory(accountId);
 | 
					    const inventory = await getInventory(accountId);
 | 
				
			||||||
 | 
					    updateCurrency(inventory, recipe.buildPrice, false);
 | 
				
			||||||
    addMiscItems(inventory, ingredientsInverse);
 | 
					    addMiscItems(inventory, ingredientsInverse);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    //buildtime is in seconds
 | 
					    //buildtime is in seconds
 | 
				
			||||||
 | 
				
			|||||||
@ -19,7 +19,7 @@ export const upgradesController: RequestHandler = async (req, res) => {
 | 
				
			|||||||
            operation.UpgradeRequirement == "/Lotus/Types/Items/MiscItems/ModSlotUnlocker" ||
 | 
					            operation.UpgradeRequirement == "/Lotus/Types/Items/MiscItems/ModSlotUnlocker" ||
 | 
				
			||||||
            operation.UpgradeRequirement == "/Lotus/Types/Items/MiscItems/CustomizationSlotUnlocker"
 | 
					            operation.UpgradeRequirement == "/Lotus/Types/Items/MiscItems/CustomizationSlotUnlocker"
 | 
				
			||||||
        ) {
 | 
					        ) {
 | 
				
			||||||
            await updateCurrency(10, true, accountId);
 | 
					            updateCurrency(inventory, 10, true);
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
            addMiscItems(inventory, [
 | 
					            addMiscItems(inventory, [
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
 | 
				
			|||||||
@ -2,7 +2,7 @@ import { Inventory } from "@/src/models/inventoryModels/inventoryModel";
 | 
				
			|||||||
import postTutorialInventory from "@/static/fixed_responses/postTutorialInventory.json";
 | 
					import postTutorialInventory from "@/static/fixed_responses/postTutorialInventory.json";
 | 
				
			||||||
import { config } from "@/src/services/configService";
 | 
					import { config } from "@/src/services/configService";
 | 
				
			||||||
import { Types } from "mongoose";
 | 
					import { Types } from "mongoose";
 | 
				
			||||||
import { SlotNames, IInventoryChanges, IBinChanges } from "@/src/types/purchaseTypes";
 | 
					import { SlotNames, IInventoryChanges, IBinChanges, ICurrencyChanges } from "@/src/types/purchaseTypes";
 | 
				
			||||||
import {
 | 
					import {
 | 
				
			||||||
    IChallengeProgress,
 | 
					    IChallengeProgress,
 | 
				
			||||||
    IConsumable,
 | 
					    IConsumable,
 | 
				
			||||||
@ -483,47 +483,43 @@ export const updateSlots = async (
 | 
				
			|||||||
    await inventory.save();
 | 
					    await inventory.save();
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const updateCurrency = async (
 | 
					const isCurrencyTracked = (usePremium: boolean): boolean => {
 | 
				
			||||||
 | 
					    return usePremium ? !config.infinitePlatinum : !config.infiniteCredits;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export const updateCurrency = (
 | 
				
			||||||
 | 
					    inventory: IInventoryDatabaseDocument,
 | 
				
			||||||
 | 
					    price: number,
 | 
				
			||||||
 | 
					    usePremium: boolean
 | 
				
			||||||
 | 
					): ICurrencyChanges => {
 | 
				
			||||||
 | 
					    const currencyChanges: ICurrencyChanges = {};
 | 
				
			||||||
 | 
					    if (price != 0 && isCurrencyTracked(usePremium)) {
 | 
				
			||||||
 | 
					        if (usePremium) {
 | 
				
			||||||
 | 
					            if (inventory.PremiumCreditsFree > 0) {
 | 
				
			||||||
 | 
					                currencyChanges.PremiumCreditsFree = Math.min(price, inventory.PremiumCreditsFree) * -1;
 | 
				
			||||||
 | 
					                inventory.PremiumCreditsFree += currencyChanges.PremiumCreditsFree;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            currencyChanges.PremiumCredits = -price;
 | 
				
			||||||
 | 
					            inventory.PremiumCredits += currencyChanges.PremiumCredits;
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            currencyChanges.RegularCredits = -price;
 | 
				
			||||||
 | 
					            inventory.RegularCredits += currencyChanges.RegularCredits;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        logger.debug(`currency changes `, currencyChanges);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return currencyChanges;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export const updateCurrencyByAccountId = async (
 | 
				
			||||||
    price: number,
 | 
					    price: number,
 | 
				
			||||||
    usePremium: boolean,
 | 
					    usePremium: boolean,
 | 
				
			||||||
    accountId: string
 | 
					    accountId: string
 | 
				
			||||||
): Promise<IInventoryChanges> => {
 | 
					): Promise<ICurrencyChanges> => {
 | 
				
			||||||
    if (usePremium ? config.infinitePlatinum : config.infiniteCredits) {
 | 
					    if (!isCurrencyTracked(usePremium)) {
 | 
				
			||||||
        return {};
 | 
					        return {};
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					 | 
				
			||||||
    const inventory = await getInventory(accountId);
 | 
					    const inventory = await getInventory(accountId);
 | 
				
			||||||
 | 
					    const currencyChanges = updateCurrency(inventory, price, usePremium);
 | 
				
			||||||
    if (usePremium) {
 | 
					 | 
				
			||||||
        if (inventory.PremiumCreditsFree > 0) {
 | 
					 | 
				
			||||||
            inventory.PremiumCreditsFree -= Math.min(price, inventory.PremiumCreditsFree);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        inventory.PremiumCredits -= price;
 | 
					 | 
				
			||||||
    } else {
 | 
					 | 
				
			||||||
        inventory.RegularCredits -= price;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    const modifiedPaths = inventory.modifiedPaths();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    type currencyKeys = "RegularCredits" | "PremiumCredits" | "PremiumCreditsFree";
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    const currencyChanges = {} as Record<currencyKeys, number>;
 | 
					 | 
				
			||||||
    modifiedPaths.forEach(path => {
 | 
					 | 
				
			||||||
        currencyChanges[path as currencyKeys] = -price;
 | 
					 | 
				
			||||||
    });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    logger.debug(`currency changes `, { currencyChanges });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    //let changes = {} as keyof currencyKeys;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // const obj2 = modifiedPaths.reduce(
 | 
					 | 
				
			||||||
    //     (obj, key) => {
 | 
					 | 
				
			||||||
    //         obj[key as keyof currencyKeys] = price;
 | 
					 | 
				
			||||||
    //         return obj;
 | 
					 | 
				
			||||||
    //     },
 | 
					 | 
				
			||||||
    //     {} as Record<keyof currencyKeys, number>
 | 
					 | 
				
			||||||
    // );
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    await inventory.save();
 | 
					    await inventory.save();
 | 
				
			||||||
    return currencyChanges;
 | 
					    return currencyChanges;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
				
			|||||||
@ -6,7 +6,7 @@ import {
 | 
				
			|||||||
    addMiscItems,
 | 
					    addMiscItems,
 | 
				
			||||||
    combineInventoryChanges,
 | 
					    combineInventoryChanges,
 | 
				
			||||||
    getInventory,
 | 
					    getInventory,
 | 
				
			||||||
    updateCurrency,
 | 
					    updateCurrencyByAccountId,
 | 
				
			||||||
    updateSlots
 | 
					    updateSlots
 | 
				
			||||||
} from "@/src/services/inventoryService";
 | 
					} from "@/src/services/inventoryService";
 | 
				
			||||||
import { getRandomWeightedReward } from "@/src/services/rngService";
 | 
					import { getRandomWeightedReward } from "@/src/services/rngService";
 | 
				
			||||||
@ -66,7 +66,7 @@ export const handlePurchase = async (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    if (!purchaseResponse) throw new Error("purchase response was undefined");
 | 
					    if (!purchaseResponse) throw new Error("purchase response was undefined");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const currencyChanges = await updateCurrency(
 | 
					    const currencyChanges = await updateCurrencyByAccountId(
 | 
				
			||||||
        purchaseRequest.PurchaseParams.ExpectedPrice,
 | 
					        purchaseRequest.PurchaseParams.ExpectedPrice,
 | 
				
			||||||
        purchaseRequest.PurchaseParams.UsePremium,
 | 
					        purchaseRequest.PurchaseParams.UsePremium,
 | 
				
			||||||
        accountId
 | 
					        accountId
 | 
				
			||||||
 | 
				
			|||||||
@ -17,13 +17,16 @@ export interface IPurchaseParams {
 | 
				
			|||||||
    UseFreeFavor?: boolean; // for Source 2
 | 
					    UseFreeFavor?: boolean; // for Source 2
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export type IInventoryChanges = {
 | 
					export interface ICurrencyChanges {
 | 
				
			||||||
    [_ in SlotNames]?: IBinChanges;
 | 
					 | 
				
			||||||
} & {
 | 
					 | 
				
			||||||
    RegularCredits?: number;
 | 
					    RegularCredits?: number;
 | 
				
			||||||
    PremiumCredits?: number;
 | 
					    PremiumCredits?: number;
 | 
				
			||||||
    PremiumCreditsFree?: number;
 | 
					    PremiumCreditsFree?: number;
 | 
				
			||||||
} & Record<string, IBinChanges | number | object[]>;
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export type IInventoryChanges = {
 | 
				
			||||||
 | 
					    [_ in SlotNames]?: IBinChanges;
 | 
				
			||||||
 | 
					} & ICurrencyChanges &
 | 
				
			||||||
 | 
					    Record<string, IBinChanges | number | object[]>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export interface IPurchaseResponse {
 | 
					export interface IPurchaseResponse {
 | 
				
			||||||
    InventoryChanges: IInventoryChanges;
 | 
					    InventoryChanges: IInventoryChanges;
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user