chore: do addItem on inventory document, not accountId (#699)

This commit is contained in:
Sainan 2025-01-04 00:25:09 +01:00 committed by GitHub
parent 7a6ffd94dc
commit 74ed098692
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 181 additions and 156 deletions

View File

@ -80,11 +80,12 @@ export const claimCompletedRecipeController: RequestHandler = async (req, res) =
...(await updateCurrencyByAccountId(recipe.skipBuildTimePrice, true, accountId)) ...(await updateCurrencyByAccountId(recipe.skipBuildTimePrice, true, accountId))
}; };
} }
res.json({ const inventory = await getInventory(accountId);
InventoryChanges: { InventoryChanges = {
...InventoryChanges, ...InventoryChanges,
...(await addItem(accountId, recipe.resultType, recipe.num)).InventoryChanges ...(await addItem(inventory, recipe.resultType, recipe.num)).InventoryChanges
} };
}); await inventory.save();
res.json({ InventoryChanges });
} }
}; };

View File

@ -4,6 +4,7 @@ import { getInventory, addMiscItems, addEquipment } from "@/src/services/invento
import { IMiscItem, TFocusPolarity, TEquipmentKey } from "@/src/types/inventoryTypes/inventoryTypes"; import { IMiscItem, TFocusPolarity, TEquipmentKey } from "@/src/types/inventoryTypes/inventoryTypes";
import { logger } from "@/src/utils/logger"; import { logger } from "@/src/utils/logger";
import { ExportFocusUpgrades } from "warframe-public-export-plus"; import { ExportFocusUpgrades } from "warframe-public-export-plus";
import { IEquipmentClient } from "@/src/types/inventoryTypes/commonInventoryTypes";
export const focusController: RequestHandler = async (req, res) => { export const focusController: RequestHandler = async (req, res) => {
const accountId = await getAccountIdForRequest(req); const accountId = await getAccountIdForRequest(req);
@ -102,8 +103,10 @@ export const focusController: RequestHandler = async (req, res) => {
"/Lotus/Weapons/Sentients/OperatorAmplifiers/SentTrainingAmplifier/SentAmpTrainingChassis", "/Lotus/Weapons/Sentients/OperatorAmplifiers/SentTrainingAmplifier/SentAmpTrainingChassis",
"/Lotus/Weapons/Sentients/OperatorAmplifiers/SentTrainingAmplifier/SentAmpTrainingBarrel" "/Lotus/Weapons/Sentients/OperatorAmplifiers/SentTrainingAmplifier/SentAmpTrainingBarrel"
]; ];
const result = await addEquipment("OperatorAmps", request.StartingWeaponType, accountId, parts); const inventory = await getInventory(accountId);
res.json(result); const inventoryChanges = addEquipment(inventory, "OperatorAmps", request.StartingWeaponType, parts);
await inventory.save();
res.json((inventoryChanges.OperatorAmps as IEquipmentClient[])[0]);
break; break;
} }
case FocusOperation.UnbindUpgrade: { case FocusOperation.UnbindUpgrade: {

View File

@ -1,5 +1,5 @@
import { getAccountIdForRequest } from "@/src/services/loginService"; import { getAccountIdForRequest } from "@/src/services/loginService";
import { updateCurrencyByAccountId } from "@/src/services/inventoryService"; import { getInventory, updateCurrency } 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,8 +26,10 @@ 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 updateCurrencyByAccountId(20, true, accountId); const inventory = await getInventory(accountId);
await updateSlots(accountId, InventorySlot.PVE_LOADOUTS, 1, 1); const currencyChanges = updateCurrency(inventory, 20, true);
updateSlots(inventory, InventorySlot.PVE_LOADOUTS, 1, 1);
await inventory.save();
//console.log({ InventoryChanges: currencyChanges }, " added loadout changes:"); //console.log({ InventoryChanges: currencyChanges }, " added loadout changes:");

View File

@ -34,9 +34,10 @@ export const modularWeaponCraftingController: RequestHandler = async (req, res)
throw new Error(`unknown modular weapon type: ${data.WeaponType}`); throw new Error(`unknown modular weapon type: ${data.WeaponType}`);
} }
const category = modularWeaponTypes[data.WeaponType]; const category = modularWeaponTypes[data.WeaponType];
const inventory = await getInventory(accountId);
// Give weapon // Give weapon
const weapon = await addEquipment(category, data.WeaponType, accountId, data.Parts); const weapon = addEquipment(inventory, category, data.WeaponType, data.Parts);
// Remove credits & parts // Remove credits & parts
const miscItemChanges = []; const miscItemChanges = [];
@ -46,7 +47,6 @@ export const modularWeaponCraftingController: RequestHandler = async (req, res)
ItemCount: -1 ItemCount: -1
}); });
} }
const inventory = await getInventory(accountId);
const currencyChanges = updateCurrency( const currencyChanges = updateCurrency(
inventory, inventory,
category == "Hoverboards" || category == "MoaPets" ? 5000 : 4000, category == "Hoverboards" || category == "MoaPets" ? 5000 : 4000,

View File

@ -1,7 +1,7 @@
import { getAccountIdForRequest } from "@/src/services/loginService"; import { getAccountIdForRequest } from "@/src/services/loginService";
import { ItemType, toAddItemRequest } from "@/src/helpers/customHelpers/addItemHelpers"; import { ItemType, toAddItemRequest } from "@/src/helpers/customHelpers/addItemHelpers";
import { getWeaponType } from "@/src/services/itemDataService"; import { getWeaponType } from "@/src/services/itemDataService";
import { addPowerSuit, addEquipment } from "@/src/services/inventoryService"; import { addPowerSuit, addEquipment, getInventory } from "@/src/services/inventoryService";
import { RequestHandler } from "express"; import { RequestHandler } from "express";
const addItemController: RequestHandler = async (req, res) => { const addItemController: RequestHandler = async (req, res) => {
@ -10,14 +10,18 @@ const addItemController: RequestHandler = async (req, res) => {
switch (request.type) { switch (request.type) {
case ItemType.Powersuit: { case ItemType.Powersuit: {
const powersuit = await addPowerSuit(request.InternalName, accountId); const inventory = await getInventory(accountId);
res.json(powersuit); const inventoryChanges = addPowerSuit(inventory, request.InternalName);
await inventory.save();
res.json(inventoryChanges);
return; return;
} }
case ItemType.Weapon: { case ItemType.Weapon: {
const inventory = await getInventory(accountId);
const weaponType = getWeaponType(request.InternalName); const weaponType = getWeaponType(request.InternalName);
const weapon = await addEquipment(weaponType, request.InternalName, accountId); const inventoryChanges = addEquipment(inventory, weaponType, request.InternalName);
res.json(weapon); await inventory.save();
res.json(inventoryChanges);
break; break;
} }
default: default:

View File

@ -105,13 +105,12 @@ export const getInventory = async (accountOwnerId: string): Promise<TInventoryDa
}; };
export const addItem = async ( export const addItem = async (
accountId: string, inventory: TInventoryDatabaseDocument,
typeName: string, typeName: string,
quantity: number = 1 quantity: number = 1
): Promise<{ InventoryChanges: IInventoryChanges }> => { ): Promise<{ InventoryChanges: IInventoryChanges }> => {
// Strict typing // Strict typing
if (typeName in ExportRecipes) { if (typeName in ExportRecipes) {
const inventory = await getInventory(accountId);
const recipeChanges = [ const recipeChanges = [
{ {
ItemType: typeName, ItemType: typeName,
@ -119,7 +118,6 @@ export const addItem = async (
} satisfies ITypeCount } satisfies ITypeCount
]; ];
addRecipes(inventory, recipeChanges); addRecipes(inventory, recipeChanges);
await inventory.save();
return { return {
InventoryChanges: { InventoryChanges: {
Recipes: recipeChanges Recipes: recipeChanges
@ -127,11 +125,9 @@ export const addItem = async (
}; };
} }
if (typeName in ExportResources) { if (typeName in ExportResources) {
const inventory = await getInventory(accountId);
if (ExportResources[typeName].productCategory == "Ships") { if (ExportResources[typeName].productCategory == "Ships") {
const oid = await createShip(new Types.ObjectId(accountId), typeName); const oid = await createShip(inventory.accountOwnerId, typeName);
inventory.Ships.push(oid); inventory.Ships.push(oid);
await inventory.save();
return { return {
InventoryChanges: { InventoryChanges: {
Ships: [ Ships: [
@ -143,11 +139,8 @@ export const addItem = async (
} }
}; };
} else if (ExportResources[typeName].productCategory == "CrewShips") { } else if (ExportResources[typeName].productCategory == "CrewShips") {
return { const inventoryChanges = addCrewShip(inventory, typeName);
InventoryChanges: { return { InventoryChanges: inventoryChanges };
CrewShips: [await addCrewShip(typeName, accountId)]
}
};
} else { } else {
const miscItemChanges = [ const miscItemChanges = [
{ {
@ -156,7 +149,6 @@ export const addItem = async (
} satisfies IMiscItem } satisfies IMiscItem
]; ];
addMiscItems(inventory, miscItemChanges); addMiscItems(inventory, miscItemChanges);
await inventory.save();
return { return {
InventoryChanges: { InventoryChanges: {
MiscItems: miscItemChanges MiscItems: miscItemChanges
@ -165,21 +157,14 @@ export const addItem = async (
} }
} }
if (typeName in ExportCustoms) { if (typeName in ExportCustoms) {
return { const inventoryChanges = addSkin(inventory, typeName);
InventoryChanges: { return { InventoryChanges: inventoryChanges };
WeaponSkins: [await addSkin(typeName, accountId)]
}
};
} }
if (typeName in ExportFlavour) { if (typeName in ExportFlavour) {
return { const inventoryChanges = addCustomization(inventory, typeName);
InventoryChanges: { return { InventoryChanges: inventoryChanges };
FlavourItems: [await addCustomization(typeName, accountId)]
}
};
} }
if (typeName in ExportUpgrades || typeName in ExportArcanes) { if (typeName in ExportUpgrades || typeName in ExportArcanes) {
const inventory = await getInventory(accountId);
const changes = [ const changes = [
{ {
ItemType: typeName, ItemType: typeName,
@ -187,7 +172,6 @@ export const addItem = async (
} }
]; ];
addMods(inventory, changes); addMods(inventory, changes);
await inventory.save();
return { return {
InventoryChanges: { InventoryChanges: {
RawUpgrades: changes RawUpgrades: changes
@ -195,7 +179,6 @@ export const addItem = async (
}; };
} }
if (typeName in ExportGear) { if (typeName in ExportGear) {
const inventory = await getInventory(accountId);
const consumablesChanges = [ const consumablesChanges = [
{ {
ItemType: typeName, ItemType: typeName,
@ -203,7 +186,6 @@ export const addItem = async (
} satisfies IConsumable } satisfies IConsumable
]; ];
addConsumables(inventory, consumablesChanges); addConsumables(inventory, consumablesChanges);
await inventory.save();
return { return {
InventoryChanges: { InventoryChanges: {
Consumables: consumablesChanges Consumables: consumablesChanges
@ -216,8 +198,8 @@ export const addItem = async (
case "Powersuits": case "Powersuits":
switch (typeName.substr(1).split("/")[2]) { switch (typeName.substr(1).split("/")[2]) {
default: { default: {
const inventoryChanges = await addPowerSuit(typeName, accountId); const inventoryChanges = addPowerSuit(inventory, typeName);
await updateSlots(accountId, InventorySlot.SUITS, 0, 1); updateSlots(inventory, InventorySlot.SUITS, 0, 1);
return { return {
InventoryChanges: { InventoryChanges: {
...inventoryChanges, ...inventoryChanges,
@ -230,22 +212,22 @@ export const addItem = async (
}; };
} }
case "Archwing": { case "Archwing": {
const spaceSuit = await addSpaceSuit(typeName, accountId); const inventoryChanges = addSpaceSuit(inventory, typeName);
await updateSlots(accountId, InventorySlot.SPACESUITS, 0, 1); updateSlots(inventory, InventorySlot.SPACESUITS, 0, 1);
return { return {
InventoryChanges: { InventoryChanges: {
...inventoryChanges,
SpaceSuitBin: { SpaceSuitBin: {
count: 1, count: 1,
platinum: 0, platinum: 0,
Slots: -1 Slots: -1
}, }
SpaceSuits: [spaceSuit]
} }
}; };
} }
case "EntratiMech": { case "EntratiMech": {
const inventoryChanges = await addMechSuit(typeName, accountId); const inventoryChanges = addMechSuit(inventory, typeName);
await updateSlots(accountId, InventorySlot.MECHSUITS, 0, 1); updateSlots(inventory, InventorySlot.MECHSUITS, 0, 1);
return { return {
InventoryChanges: { InventoryChanges: {
...inventoryChanges, ...inventoryChanges,
@ -261,18 +243,17 @@ export const addItem = async (
break; break;
case "Weapons": { case "Weapons": {
const weaponType = getWeaponType(typeName); const weaponType = getWeaponType(typeName);
const weapon = await addEquipment(weaponType, typeName, accountId); const inventoryChanges = addEquipment(inventory, weaponType, typeName);
await updateSlots(accountId, InventorySlot.WEAPONS, 0, 1); updateSlots(inventory, InventorySlot.WEAPONS, 0, 1);
return { return {
InventoryChanges: { InventoryChanges: {
WeaponBin: { count: 1, platinum: 0, Slots: -1 }, ...inventoryChanges,
[weaponType]: [weapon] WeaponBin: { count: 1, platinum: 0, Slots: -1 }
} }
}; };
} }
case "Objects": { case "Objects": {
// /Lotus/Objects/Tenno/Props/TnoLisetTextProjector (Note Beacon) // /Lotus/Objects/Tenno/Props/TnoLisetTextProjector (Note Beacon)
const inventory = await getInventory(accountId);
const changes = [ const changes = [
{ {
ItemType: typeName, ItemType: typeName,
@ -280,7 +261,6 @@ export const addItem = async (
} satisfies IMiscItem } satisfies IMiscItem
]; ];
addShipDecorations(inventory, changes); addShipDecorations(inventory, changes);
await inventory.save();
return { return {
InventoryChanges: { InventoryChanges: {
ShipDecorations: changes ShipDecorations: changes
@ -290,8 +270,8 @@ export const addItem = async (
case "Types": case "Types":
switch (typeName.substr(1).split("/")[2]) { switch (typeName.substr(1).split("/")[2]) {
case "Sentinels": { case "Sentinels": {
const inventoryChanges = await addSentinel(typeName, accountId); const inventoryChanges = addSentinel(inventory, typeName);
await updateSlots(accountId, InventorySlot.SENTINELS, 0, 1); updateSlots(inventory, InventorySlot.SENTINELS, 0, 1);
return { return {
InventoryChanges: { InventoryChanges: {
...inventoryChanges, ...inventoryChanges,
@ -302,7 +282,6 @@ export const addItem = async (
case "Items": { case "Items": {
switch (typeName.substr(1).split("/")[3]) { switch (typeName.substr(1).split("/")[3]) {
case "ShipDecos": { case "ShipDecos": {
const inventory = await getInventory(accountId);
const changes = [ const changes = [
{ {
ItemType: typeName, ItemType: typeName,
@ -310,7 +289,6 @@ export const addItem = async (
} satisfies IMiscItem } satisfies IMiscItem
]; ];
addShipDecorations(inventory, changes); addShipDecorations(inventory, changes);
await inventory.save();
return { return {
InventoryChanges: { InventoryChanges: {
ShipDecorations: changes ShipDecorations: changes
@ -318,7 +296,6 @@ export const addItem = async (
}; };
} }
default: { default: {
const inventory = await getInventory(accountId);
const miscItemChanges = [ const miscItemChanges = [
{ {
ItemType: typeName, ItemType: typeName,
@ -326,7 +303,6 @@ export const addItem = async (
} satisfies IMiscItem } satisfies IMiscItem
]; ];
addMiscItems(inventory, miscItemChanges); addMiscItems(inventory, miscItemChanges);
await inventory.save();
return { return {
InventoryChanges: { InventoryChanges: {
MiscItems: miscItemChanges MiscItems: miscItemChanges
@ -338,7 +314,6 @@ export const addItem = async (
case "Game": case "Game":
if (typeName.substr(1).split("/")[3] == "Projections") { if (typeName.substr(1).split("/")[3] == "Projections") {
// Void Relics, e.g. /Lotus/Types/Game/Projections/T2VoidProjectionGaussPrimeDBronze // Void Relics, e.g. /Lotus/Types/Game/Projections/T2VoidProjectionGaussPrimeDBronze
const inventory = await getInventory(accountId);
const miscItemChanges = [ const miscItemChanges = [
{ {
ItemType: typeName, ItemType: typeName,
@ -346,7 +321,6 @@ export const addItem = async (
} satisfies IMiscItem } satisfies IMiscItem
]; ];
addMiscItems(inventory, miscItemChanges); addMiscItems(inventory, miscItemChanges);
await inventory.save();
return { return {
InventoryChanges: { InventoryChanges: {
MiscItems: miscItemChanges MiscItems: miscItemChanges
@ -363,13 +337,13 @@ export const addItem = async (
}; };
//TODO: maybe genericMethod for all the add methods, they share a lot of logic //TODO: maybe genericMethod for all the add methods, they share a lot of logic
export const addSentinel = async (sentinelName: string, accountId: string): Promise<IInventoryChanges> => { export const addSentinel = (
const inventoryChanges: IInventoryChanges = {}; inventory: TInventoryDatabaseDocument,
sentinelName: string,
inventoryChanges: IInventoryChanges = {}
): IInventoryChanges => {
if (ExportSentinels[sentinelName]?.defaultWeapon) { if (ExportSentinels[sentinelName]?.defaultWeapon) {
inventoryChanges.SentinelWeapons = [ addSentinelWeapon(inventory, ExportSentinels[sentinelName].defaultWeapon, inventoryChanges);
await addSentinelWeapon(ExportSentinels[sentinelName].defaultWeapon, accountId)
];
} }
const modsToGive: IRawUpgrade[] = []; const modsToGive: IRawUpgrade[] = [];
@ -387,96 +361,109 @@ export const addSentinel = async (sentinelName: string, accountId: string): Prom
} }
} }
const inventory = await getInventory(accountId);
addMods(inventory, modsToGive); addMods(inventory, modsToGive);
const sentinelIndex = inventory.Sentinels.push({ ItemType: sentinelName, Configs: configs, XP: 0 }); const sentinelIndex = inventory.Sentinels.push({ ItemType: sentinelName, Configs: configs, XP: 0 }) - 1;
const changedInventory = await inventory.save(); inventoryChanges.Sentinels ??= [];
inventoryChanges.Sentinels = [changedInventory.Sentinels[sentinelIndex - 1].toJSON()]; (inventoryChanges.Sentinels as IEquipmentClient[]).push(
inventory.Sentinels[sentinelIndex].toJSON<IEquipmentClient>()
);
return inventoryChanges; return inventoryChanges;
}; };
export const addSentinelWeapon = async (typeName: string, accountId: string): Promise<IEquipmentClient> => { export const addSentinelWeapon = (
const inventory = await getInventory(accountId); inventory: TInventoryDatabaseDocument,
const sentinelIndex = inventory.SentinelWeapons.push({ ItemType: typeName }); typeName: string,
const changedInventory = await inventory.save(); inventoryChanges: IInventoryChanges
return changedInventory.SentinelWeapons[sentinelIndex - 1].toJSON<IEquipmentClient>(); ): void => {
const index = inventory.SentinelWeapons.push({ ItemType: typeName }) - 1;
inventoryChanges.SentinelWeapons ??= [];
(inventoryChanges.SentinelWeapons as IEquipmentClient[]).push(
inventory.SentinelWeapons[index].toJSON<IEquipmentClient>()
);
}; };
export const addPowerSuit = async (powersuitName: string, accountId: string): Promise<IInventoryChanges> => { export const addPowerSuit = (
const inventoryChanges: IInventoryChanges = {}; inventory: TInventoryDatabaseDocument,
powersuitName: string,
inventoryChanges: IInventoryChanges = {}
): IInventoryChanges => {
const specialItems = getExalted(powersuitName); const specialItems = getExalted(powersuitName);
if (specialItems) { if (specialItems) {
for await (const specialItem of specialItems) { for (const specialItem of specialItems) {
await addSpecialItem(specialItem, accountId, inventoryChanges); addSpecialItem(inventory, specialItem, inventoryChanges);
} }
} }
const inventory = await getInventory(accountId); const suitIndex = inventory.Suits.push({ ItemType: powersuitName, Configs: [], UpgradeVer: 101, XP: 0 }) - 1;
const suitIndex = inventory.Suits.push({ ItemType: powersuitName, Configs: [], UpgradeVer: 101, XP: 0 }); inventoryChanges.Suits ??= [];
const changedInventory = await inventory.save(); (inventoryChanges.Suits as IEquipmentClient[]).push(inventory.Suits[suitIndex].toJSON<IEquipmentClient>());
inventoryChanges.Suits = [changedInventory.Suits[suitIndex - 1].toJSON()];
return inventoryChanges; return inventoryChanges;
}; };
export const addMechSuit = async (mechsuitName: string, accountId: string): Promise<IInventoryChanges> => { export const addMechSuit = (
const inventoryChanges: IInventoryChanges = {}; inventory: TInventoryDatabaseDocument,
mechsuitName: string,
inventoryChanges: IInventoryChanges = {}
): IInventoryChanges => {
const specialItems = getExalted(mechsuitName); const specialItems = getExalted(mechsuitName);
if (specialItems) { if (specialItems) {
for await (const specialItem of specialItems) { for (const specialItem of specialItems) {
await addSpecialItem(specialItem, accountId, inventoryChanges); addSpecialItem(inventory, specialItem, inventoryChanges);
} }
} }
const inventory = await getInventory(accountId); const suitIndex = inventory.MechSuits.push({ ItemType: mechsuitName, Configs: [], UpgradeVer: 101, XP: 0 }) - 1;
const suitIndex = inventory.MechSuits.push({ ItemType: mechsuitName, Configs: [], UpgradeVer: 101, XP: 0 }); inventoryChanges.MechSuits ??= [];
const changedInventory = await inventory.save(); (inventoryChanges.MechSuits as IEquipmentClient[]).push(inventory.MechSuits[suitIndex].toJSON<IEquipmentClient>());
inventoryChanges.MechSuits = [changedInventory.MechSuits[suitIndex - 1].toJSON()];
return inventoryChanges; return inventoryChanges;
}; };
export const addSpecialItem = async ( export const addSpecialItem = (
inventory: TInventoryDatabaseDocument,
itemName: string, itemName: string,
accountId: string,
inventoryChanges: IInventoryChanges inventoryChanges: IInventoryChanges
): Promise<void> => { ): void => {
const inventory = await getInventory(accountId);
if (inventory.SpecialItems.find(x => x.ItemType == itemName)) { if (inventory.SpecialItems.find(x => x.ItemType == itemName)) {
return; return;
} }
const specialItemIndex = inventory.SpecialItems.push({ const specialItemIndex =
inventory.SpecialItems.push({
ItemType: itemName, ItemType: itemName,
Configs: [], Configs: [],
Features: 1, Features: 1,
UpgradeVer: 101, UpgradeVer: 101,
XP: 0 XP: 0
}); }) - 1;
const changedInventory = await inventory.save();
inventoryChanges.SpecialItems ??= []; inventoryChanges.SpecialItems ??= [];
(inventoryChanges.SpecialItems as object[]).push(changedInventory.SpecialItems[specialItemIndex - 1].toJSON()); (inventoryChanges.SpecialItems as IEquipmentClient[]).push(
inventory.SpecialItems[specialItemIndex].toJSON<IEquipmentClient>()
);
}; };
export const addSpaceSuit = async (spacesuitName: string, accountId: string): Promise<IEquipmentClient> => { export const addSpaceSuit = (
const inventory = await getInventory(accountId); inventory: TInventoryDatabaseDocument,
const suitIndex = inventory.SpaceSuits.push({ ItemType: spacesuitName, Configs: [], UpgradeVer: 101, XP: 0 }); spacesuitName: string,
const changedInventory = await inventory.save(); inventoryChanges: IInventoryChanges = {}
return changedInventory.SpaceSuits[suitIndex - 1].toJSON<IEquipmentClient>(); ): IInventoryChanges => {
const suitIndex = inventory.SpaceSuits.push({ ItemType: spacesuitName, Configs: [], UpgradeVer: 101, XP: 0 }) - 1;
inventoryChanges.SpaceSuits ??= [];
(inventoryChanges.SpaceSuits as IEquipmentClient[]).push(
inventory.SpaceSuits[suitIndex].toJSON<IEquipmentClient>()
);
return inventoryChanges;
}; };
export const updateSlots = async ( export const updateSlots = (
accountId: string, inventory: TInventoryDatabaseDocument,
slotName: SlotNames, slotName: SlotNames,
slotAmount: number, slotAmount: number,
extraAmount: number extraAmount: number
): Promise<void> => { ): void => {
const inventory = await getInventory(accountId);
inventory[slotName].Slots += slotAmount; inventory[slotName].Slots += slotAmount;
if (inventory[slotName].Extra === undefined) { if (inventory[slotName].Extra === undefined) {
inventory[slotName].Extra = extraAmount; inventory[slotName].Extra = extraAmount;
} else { } else {
inventory[slotName].Extra += extraAmount; inventory[slotName].Extra += extraAmount;
} }
await inventory.save();
}; };
const isCurrencyTracked = (usePremium: boolean): boolean => { const isCurrencyTracked = (usePremium: boolean): boolean => {
@ -548,44 +535,61 @@ export const updateTheme = async (data: IThemeUpdateRequest, accountId: string):
await inventory.save(); await inventory.save();
}; };
export const addEquipment = async ( export const addEquipment = (
inventory: TInventoryDatabaseDocument,
category: TEquipmentKey, category: TEquipmentKey,
type: string, type: string,
accountId: string, modularParts: string[] | undefined = undefined,
modularParts: string[] | undefined = undefined inventoryChanges: IInventoryChanges = {}
): Promise<IEquipmentClient> => { ): IInventoryChanges => {
const inventory = await getInventory(accountId); const index =
inventory[category].push({
const index = inventory[category].push({
ItemType: type, ItemType: type,
Configs: [], Configs: [],
XP: 0, XP: 0,
ModularParts: modularParts ModularParts: modularParts
}); }) - 1;
const changedInventory = await inventory.save(); inventoryChanges[category] ??= [];
return changedInventory[category][index - 1].toJSON() as object as IEquipmentClient; (inventoryChanges[category] as IEquipmentClient[]).push(inventory[category][index].toJSON<IEquipmentClient>());
return inventoryChanges;
}; };
export const addCustomization = async (customizatonName: string, accountId: string): Promise<IFlavourItem> => { export const addCustomization = (
const inventory = await getInventory(accountId); inventory: TInventoryDatabaseDocument,
const flavourItemIndex = inventory.FlavourItems.push({ ItemType: customizatonName }) - 1; customizationName: string,
const changedInventory = await inventory.save(); inventoryChanges: IInventoryChanges = {}
return changedInventory.FlavourItems[flavourItemIndex].toJSON(); ): IInventoryChanges => {
const flavourItemIndex = inventory.FlavourItems.push({ ItemType: customizationName }) - 1;
inventoryChanges.FlavourItems ??= [];
(inventoryChanges.FlavourItems as IFlavourItem[]).push(
inventory.FlavourItems[flavourItemIndex].toJSON<IFlavourItem>()
);
return inventoryChanges;
}; };
export const addSkin = async (typeName: string, accountId: string): Promise<IWeaponSkinClient> => { export const addSkin = (
const inventory = await getInventory(accountId); inventory: TInventoryDatabaseDocument,
typeName: string,
inventoryChanges: IInventoryChanges = {}
): IInventoryChanges => {
const index = inventory.WeaponSkins.push({ ItemType: typeName }) - 1; const index = inventory.WeaponSkins.push({ ItemType: typeName }) - 1;
const changedInventory = await inventory.save(); inventoryChanges.WeaponSkins ??= [];
return changedInventory.WeaponSkins[index].toJSON() as object as IWeaponSkinClient; (inventoryChanges.WeaponSkins as IWeaponSkinClient[]).push(
inventory.WeaponSkins[index].toJSON<IWeaponSkinClient>()
);
return inventoryChanges;
}; };
const addCrewShip = async (typeName: string, accountId: string) => { const addCrewShip = (
const inventory = await getInventory(accountId); inventory: TInventoryDatabaseDocument,
typeName: string,
inventoryChanges: IInventoryChanges = {}
): IInventoryChanges => {
const index = inventory.CrewShips.push({ ItemType: typeName }) - 1; const index = inventory.CrewShips.push({ ItemType: typeName }) - 1;
const changedInventory = await inventory.save(); inventoryChanges.CrewShips ??= [];
return changedInventory.CrewShips[index].toJSON(); (inventoryChanges.CrewShips as object[]).push(inventory.CrewShips[index].toJSON());
return inventoryChanges;
}; };
const addGearExpByCategory = ( const addGearExpByCategory = (
@ -864,7 +868,7 @@ export const addBooster = async (ItemType: string, time: number, accountId: stri
existingBooster.ExpiryDate = Math.max(existingBooster.ExpiryDate, currentTime) + time; existingBooster.ExpiryDate = Math.max(existingBooster.ExpiryDate, currentTime) + time;
inventory.markModified(`Boosters.${itemIndex}.ExpiryDate`); inventory.markModified(`Boosters.${itemIndex}.ExpiryDate`);
} else { } else {
Boosters.push({ ItemType, ExpiryDate: currentTime + time }) - 1; Boosters.push({ ItemType, ExpiryDate: currentTime + time });
} }
await inventory.save(); await inventory.save();

View File

@ -204,9 +204,12 @@ export const handleStoreItemAcquisition = async (
} }
} }
switch (storeCategory) { switch (storeCategory) {
default: default: {
purchaseResponse = await addItem(accountId, internalName, quantity); const inventory = await getInventory(accountId);
purchaseResponse = await addItem(inventory, internalName, quantity);
await inventory.save();
break; break;
}
case "Types": case "Types":
purchaseResponse = await handleTypesPurchase(internalName, accountId, quantity); purchaseResponse = await handleTypesPurchase(internalName, accountId, quantity);
break; break;
@ -248,7 +251,9 @@ const handleSlotPurchase = async (
const slotName = slotPurchaseNameToSlotName[slotPurchaseName].name; const slotName = slotPurchaseNameToSlotName[slotPurchaseName].name;
const slotsPerPurchase = slotPurchaseNameToSlotName[slotPurchaseName].slotsPerPurchase; const slotsPerPurchase = slotPurchaseNameToSlotName[slotPurchaseName].slotsPerPurchase;
await updateSlots(accountId, slotName, slotsPerPurchase, slotsPerPurchase); const inventory = await getInventory(accountId);
updateSlots(inventory, slotName, slotsPerPurchase, slotsPerPurchase);
await inventory.save();
logger.debug(`added ${slotsPerPurchase} slot ${slotName}`); logger.debug(`added ${slotsPerPurchase} slot ${slotName}`);
@ -277,6 +282,7 @@ const handleBoosterPackPurchase = async (
BoosterPackItems: "", BoosterPackItems: "",
InventoryChanges: {} InventoryChanges: {}
}; };
const inventory = await getInventory(accountId);
for (let i = 0; i != quantity; ++i) { for (let i = 0; i != quantity; ++i) {
for (const weights of pack.rarityWeightsPerRoll) { for (const weights of pack.rarityWeightsPerRoll) {
const result = getRandomWeightedReward(pack.components, weights); const result = getRandomWeightedReward(pack.components, weights);
@ -286,11 +292,12 @@ const handleBoosterPackPurchase = async (
result.type.split("/Lotus/").join("/Lotus/StoreItems/") + ',{"lvl":0};'; result.type.split("/Lotus/").join("/Lotus/StoreItems/") + ',{"lvl":0};';
combineInventoryChanges( combineInventoryChanges(
purchaseResponse.InventoryChanges, purchaseResponse.InventoryChanges,
(await addItem(accountId, result.type, result.itemCount)).InventoryChanges (await addItem(inventory, result.type, result.itemCount)).InventoryChanges
); );
} }
} }
} }
await inventory.save();
return purchaseResponse; return purchaseResponse;
}; };
@ -303,8 +310,12 @@ const handleTypesPurchase = async (
const typeCategory = getStoreItemTypesCategory(typesName); const typeCategory = getStoreItemTypesCategory(typesName);
logger.debug(`type category ${typeCategory}`); logger.debug(`type category ${typeCategory}`);
switch (typeCategory) { switch (typeCategory) {
default: default: {
return await addItem(accountId, typesName, quantity); const inventory = await getInventory(accountId);
const resp = await addItem(inventory, typesName, quantity);
await inventory.save();
return resp;
}
case "BoosterPacks": case "BoosterPacks":
return await handleBoosterPackPurchase(typesName, accountId, quantity); return await handleBoosterPackPurchase(typesName, accountId, quantity);
case "SlotItems": case "SlotItems":