chore: updateCurrency with existing inventory instance

This commit is contained in:
Sainan 2024-12-29 22:12:45 +01:00
parent 230c0303b1
commit ad2b8b74ce
9 changed files with 63 additions and 60 deletions

View File

@ -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({

View File

@ -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:");

View File

@ -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();

View File

@ -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
});
};

View File

@ -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

View File

@ -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, [
{

View File

@ -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<IInventoryChanges> => {
if (usePremium ? config.infinitePlatinum : config.infiniteCredits) {
): Promise<ICurrencyChanges> => {
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<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>
// );
const currencyChanges = updateCurrency(inventory, price, usePremium);
await inventory.save();
return currencyChanges;
};

View File

@ -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

View File

@ -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<string, IBinChanges | number | object[]>;
}
export type IInventoryChanges = {
[_ in SlotNames]?: IBinChanges;
} & ICurrencyChanges &
Record<string, IBinChanges | number | object[]>;
export interface IPurchaseResponse {
InventoryChanges: IInventoryChanges;