diff --git a/src/controllers/api/claimCompletedRecipeController.ts b/src/controllers/api/claimCompletedRecipeController.ts index ce096f74..237a5b87 100644 --- a/src/controllers/api/claimCompletedRecipeController.ts +++ b/src/controllers/api/claimCompletedRecipeController.ts @@ -7,7 +7,14 @@ import { getRecipe } from "@/src/services/itemDataService"; import { IOid } from "@/src/types/commonTypes"; import { getJSONfromString } from "@/src/helpers/stringHelpers"; 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 { RecipeIds: IOid[]; @@ -43,9 +50,8 @@ export const claimCompletedRecipeController: RequestHandler = async (req, res) = } if (req.query.cancel) { - const currencyChanges = await updateCurrency(recipe.buildPrice * -1, false, accountId); - const inventory = await getInventory(accountId); + const currencyChanges = updateCurrency(inventory, recipe.buildPrice * -1, false); addMiscItems(inventory, recipe.ingredients); await inventory.save(); @@ -74,7 +80,7 @@ export const claimCompletedRecipeController: RequestHandler = async (req, res) = if (req.query.rush) { InventoryChanges = { ...InventoryChanges, - ...(await updateCurrency(recipe.skipBuildTimePrice, true, accountId)) + ...(await updateCurrencyByAccountId(recipe.skipBuildTimePrice, true, accountId)) }; } res.json({ diff --git a/src/controllers/api/inventorySlotsController.ts b/src/controllers/api/inventorySlotsController.ts index 8515577d..0c95c89d 100644 --- a/src/controllers/api/inventorySlotsController.ts +++ b/src/controllers/api/inventorySlotsController.ts @@ -1,5 +1,5 @@ import { getAccountIdForRequest } from "@/src/services/loginService"; -import { updateCurrency } from "@/src/services/inventoryService"; +import { updateCurrencyByAccountId } from "@/src/services/inventoryService"; import { RequestHandler } from "express"; import { updateSlots } from "@/src/services/inventoryService"; 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 - const currencyChanges = await updateCurrency(20, true, accountId); + const currencyChanges = await updateCurrencyByAccountId(20, true, accountId); await updateSlots(accountId, InventorySlot.PVE_LOADOUTS, 1, 1); //console.log({ InventoryChanges: currencyChanges }, " added loadout changes:"); diff --git a/src/controllers/api/modularWeaponCraftingController.ts b/src/controllers/api/modularWeaponCraftingController.ts index 217197c3..f77fc7d9 100644 --- a/src/controllers/api/modularWeaponCraftingController.ts +++ b/src/controllers/api/modularWeaponCraftingController.ts @@ -34,14 +34,7 @@ export const modularWeaponCraftingController: RequestHandler = async (req, res) // Give weapon const weapon = await addEquipment(category, data.WeaponType, accountId, data.Parts); - // Remove credits - const currencyChanges = await updateCurrency( - category == "Hoverboards" || category == "MoaPets" ? 5000 : 4000, - false, - accountId - ); - - // Remove parts + // Remove credits & parts const miscItemChanges = []; for (const part of data.Parts) { miscItemChanges.push({ @@ -50,6 +43,11 @@ export const modularWeaponCraftingController: RequestHandler = async (req, res) }); } const inventory = await getInventory(accountId); + const currencyChanges = updateCurrency( + inventory, + category == "Hoverboards" || category == "MoaPets" ? 5000 : 4000, + false + ); addMiscItems(inventory, miscItemChanges); await inventory.save(); diff --git a/src/controllers/api/nameWeaponController.ts b/src/controllers/api/nameWeaponController.ts index 94795d60..2a3f4236 100644 --- a/src/controllers/api/nameWeaponController.ts +++ b/src/controllers/api/nameWeaponController.ts @@ -20,8 +20,9 @@ export const nameWeaponController: RequestHandler = async (req, res) => { } else { item.ItemName = undefined; } + const currencyChanges = updateCurrency(inventory, "webui" in req.query ? 0 : 15, true); await inventory.save(); res.json({ - InventoryChanges: await updateCurrency("webui" in req.query ? 0 : 15, true, accountId) + InventoryChanges: currencyChanges }); }; diff --git a/src/controllers/api/startRecipeController.ts b/src/controllers/api/startRecipeController.ts index 04ad7a65..20693509 100644 --- a/src/controllers/api/startRecipeController.ts +++ b/src/controllers/api/startRecipeController.ts @@ -26,14 +26,13 @@ export const startRecipeController: RequestHandler = async (req, res) => { throw new Error(`unknown recipe ${recipeName}`); } - await updateCurrency(recipe.buildPrice, false, accountId); - const ingredientsInverse = recipe.ingredients.map(component => ({ ItemType: component.ItemType, ItemCount: component.ItemCount * -1 })); const inventory = await getInventory(accountId); + updateCurrency(inventory, recipe.buildPrice, false); addMiscItems(inventory, ingredientsInverse); //buildtime is in seconds diff --git a/src/controllers/api/upgradesController.ts b/src/controllers/api/upgradesController.ts index 0e7c2381..5f85af34 100644 --- a/src/controllers/api/upgradesController.ts +++ b/src/controllers/api/upgradesController.ts @@ -19,7 +19,7 @@ export const upgradesController: RequestHandler = async (req, res) => { operation.UpgradeRequirement == "/Lotus/Types/Items/MiscItems/ModSlotUnlocker" || operation.UpgradeRequirement == "/Lotus/Types/Items/MiscItems/CustomizationSlotUnlocker" ) { - await updateCurrency(10, true, accountId); + updateCurrency(inventory, 10, true); } else { addMiscItems(inventory, [ { diff --git a/src/services/inventoryService.ts b/src/services/inventoryService.ts index 381da0c5..5b0ce1cc 100644 --- a/src/services/inventoryService.ts +++ b/src/services/inventoryService.ts @@ -2,7 +2,7 @@ import { Inventory } from "@/src/models/inventoryModels/inventoryModel"; import postTutorialInventory from "@/static/fixed_responses/postTutorialInventory.json"; import { config } from "@/src/services/configService"; import { Types } from "mongoose"; -import { SlotNames, IInventoryChanges, IBinChanges } from "@/src/types/purchaseTypes"; +import { SlotNames, IInventoryChanges, IBinChanges, ICurrencyChanges } from "@/src/types/purchaseTypes"; import { IChallengeProgress, IConsumable, @@ -483,47 +483,43 @@ export const updateSlots = async ( 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, usePremium: boolean, accountId: string -): Promise => { - if (usePremium ? config.infinitePlatinum : config.infiniteCredits) { +): Promise => { + if (!isCurrencyTracked(usePremium)) { return {}; } - const inventory = await getInventory(accountId); - - 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; - 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 - // ); - + const currencyChanges = updateCurrency(inventory, price, usePremium); await inventory.save(); return currencyChanges; }; diff --git a/src/services/purchaseService.ts b/src/services/purchaseService.ts index c3a16cf0..8496934d 100644 --- a/src/services/purchaseService.ts +++ b/src/services/purchaseService.ts @@ -6,7 +6,7 @@ import { addMiscItems, combineInventoryChanges, getInventory, - updateCurrency, + updateCurrencyByAccountId, updateSlots } from "@/src/services/inventoryService"; import { getRandomWeightedReward } from "@/src/services/rngService"; @@ -66,7 +66,7 @@ export const handlePurchase = async ( if (!purchaseResponse) throw new Error("purchase response was undefined"); - const currencyChanges = await updateCurrency( + const currencyChanges = await updateCurrencyByAccountId( purchaseRequest.PurchaseParams.ExpectedPrice, purchaseRequest.PurchaseParams.UsePremium, accountId diff --git a/src/types/purchaseTypes.ts b/src/types/purchaseTypes.ts index 83e5fb0d..f2b1ff9b 100644 --- a/src/types/purchaseTypes.ts +++ b/src/types/purchaseTypes.ts @@ -17,13 +17,16 @@ export interface IPurchaseParams { UseFreeFavor?: boolean; // for Source 2 } -export type IInventoryChanges = { - [_ in SlotNames]?: IBinChanges; -} & { +export interface ICurrencyChanges { RegularCredits?: number; PremiumCredits?: number; PremiumCreditsFree?: number; -} & Record; +} + +export type IInventoryChanges = { + [_ in SlotNames]?: IBinChanges; +} & ICurrencyChanges & + Record; export interface IPurchaseResponse { InventoryChanges: IInventoryChanges;