chore: require an inventory document to use addItem

purchase 50 arcane packs - before: 2715.070 ms
purchase 50 arcane packs - after: 183.988 ms
This commit is contained in:
Sainan 2025-01-03 07:34:02 +01:00
parent ef4364760a
commit 542a295cfb
3 changed files with 23 additions and 46 deletions

View File

@ -83,11 +83,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

@ -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,9 +139,7 @@ export const addItem = async (
} }
}; };
} else if (ExportResources[typeName].productCategory == "CrewShips") { } else if (ExportResources[typeName].productCategory == "CrewShips") {
const inventory = await getInventory(accountId);
const inventoryChanges = addCrewShip(inventory, typeName); const inventoryChanges = addCrewShip(inventory, typeName);
await inventory.save();
return { InventoryChanges: inventoryChanges }; return { InventoryChanges: inventoryChanges };
} else { } else {
const miscItemChanges = [ const miscItemChanges = [
@ -155,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
@ -164,19 +157,14 @@ export const addItem = async (
} }
} }
if (typeName in ExportCustoms) { if (typeName in ExportCustoms) {
const inventory = await getInventory(accountId);
const inventoryChanges = addSkin(inventory, typeName); const inventoryChanges = addSkin(inventory, typeName);
await inventory.save();
return { InventoryChanges: inventoryChanges }; return { InventoryChanges: inventoryChanges };
} }
if (typeName in ExportFlavour) { if (typeName in ExportFlavour) {
const inventory = await getInventory(accountId);
const inventoryChanges = addCustomization(inventory, typeName); const inventoryChanges = addCustomization(inventory, typeName);
await inventory.save();
return { InventoryChanges: inventoryChanges }; return { InventoryChanges: inventoryChanges };
} }
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,
@ -184,7 +172,6 @@ export const addItem = async (
} }
]; ];
addMods(inventory, changes); addMods(inventory, changes);
await inventory.save();
return { return {
InventoryChanges: { InventoryChanges: {
RawUpgrades: changes RawUpgrades: changes
@ -192,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,
@ -200,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
@ -213,10 +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 inventory = await getInventory(accountId);
const inventoryChanges = addPowerSuit(inventory, typeName); const inventoryChanges = addPowerSuit(inventory, typeName);
updateSlots(inventory, InventorySlot.SUITS, 0, 1); updateSlots(inventory, InventorySlot.SUITS, 0, 1);
await inventory.save();
return { return {
InventoryChanges: { InventoryChanges: {
...inventoryChanges, ...inventoryChanges,
@ -229,10 +212,8 @@ export const addItem = async (
}; };
} }
case "Archwing": { case "Archwing": {
const inventory = await getInventory(accountId);
const inventoryChanges = addSpaceSuit(inventory, typeName); const inventoryChanges = addSpaceSuit(inventory, typeName);
updateSlots(inventory, InventorySlot.SPACESUITS, 0, 1); updateSlots(inventory, InventorySlot.SPACESUITS, 0, 1);
await inventory.save();
return { return {
InventoryChanges: { InventoryChanges: {
...inventoryChanges, ...inventoryChanges,
@ -245,10 +226,8 @@ export const addItem = async (
}; };
} }
case "EntratiMech": { case "EntratiMech": {
const inventory = await getInventory(accountId);
const inventoryChanges = addMechSuit(inventory, typeName); const inventoryChanges = addMechSuit(inventory, typeName);
updateSlots(inventory, InventorySlot.MECHSUITS, 0, 1); updateSlots(inventory, InventorySlot.MECHSUITS, 0, 1);
await inventory.save();
return { return {
InventoryChanges: { InventoryChanges: {
...inventoryChanges, ...inventoryChanges,
@ -263,11 +242,9 @@ export const addItem = async (
} }
break; break;
case "Weapons": { case "Weapons": {
const inventory = await getInventory(accountId);
const weaponType = getWeaponType(typeName); const weaponType = getWeaponType(typeName);
const inventoryChanges = addEquipment(inventory, weaponType, typeName); const inventoryChanges = addEquipment(inventory, weaponType, typeName);
updateSlots(inventory, InventorySlot.WEAPONS, 0, 1); updateSlots(inventory, InventorySlot.WEAPONS, 0, 1);
await inventory.save();
return { return {
InventoryChanges: { InventoryChanges: {
...inventoryChanges, ...inventoryChanges,
@ -277,7 +254,6 @@ export const addItem = async (
} }
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,
@ -285,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
@ -295,10 +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 inventory = await getInventory(accountId);
const inventoryChanges = addSentinel(inventory, typeName); const inventoryChanges = addSentinel(inventory, typeName);
updateSlots(inventory, InventorySlot.SENTINELS, 0, 1); updateSlots(inventory, InventorySlot.SENTINELS, 0, 1);
await inventory.save();
return { return {
InventoryChanges: { InventoryChanges: {
...inventoryChanges, ...inventoryChanges,
@ -309,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,
@ -317,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
@ -325,7 +296,6 @@ export const addItem = async (
}; };
} }
default: { default: {
const inventory = await getInventory(accountId);
const miscItemChanges = [ const miscItemChanges = [
{ {
ItemType: typeName, ItemType: typeName,
@ -333,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
@ -345,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,
@ -353,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

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;
@ -279,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);
@ -288,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;
}; };
@ -305,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":