From bd79fee02f9526c2881b84f604ae0d92c8395181 Mon Sep 17 00:00:00 2001 From: holmityd Date: Thu, 31 Aug 2023 12:50:48 +0400 Subject: [PATCH] Booster purchase fix --- src/services/inventoryService.ts | 19 ++++++------------- src/services/purchaseService.ts | 22 +++++++++++++++++++--- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/services/inventoryService.ts b/src/services/inventoryService.ts index 39ca196f..19900497 100644 --- a/src/services/inventoryService.ts +++ b/src/services/inventoryService.ts @@ -186,30 +186,23 @@ export const missionInventoryUpdate = async (data: MissionInventoryUpdate, accou await inventory.save(); }; -export const addBooster = async (boosterName: string, accountId: string): Promise => { - const match = boosterName.match(/(\d+)Day/); - if (!match) return; - - const extractedDigit = Number(match[1]); - const ItemType = boosterName.replace(`${extractedDigit}Day`, ""); - const plusTime = 86400 * extractedDigit; - const currentTime = Math.floor(Date.now() / 1000); +export const addBooster = async (ItemType: string, time: number, accountId: string): Promise => { + const currentTime = Math.floor(Date.now() / 1000) - 129600; // booster time getting more without 129600, probably defence logic, idk const inventory = await getInventory(accountId); const { Boosters } = inventory; - let itemIndex = Boosters.findIndex(i => i.ItemType === ItemType); + const itemIndex = Boosters.findIndex(i => i.ItemType === ItemType); if (itemIndex !== -1) { const existingBooster = Boosters[itemIndex]; - existingBooster.ExpiryDate = Math.max(existingBooster.ExpiryDate, currentTime) + plusTime; + existingBooster.ExpiryDate = Math.max(existingBooster.ExpiryDate, currentTime) + time; inventory.markModified(`Boosters.${itemIndex}.ExpiryDate`); } else { - itemIndex = Boosters.push({ ItemType, ExpiryDate: currentTime + plusTime }) - 1; + Boosters.push({ ItemType, ExpiryDate: currentTime + time }) - 1; } - const changedInventory = await inventory.save(); - return changedInventory.Boosters[itemIndex].toJSON(); + await inventory.save(); }; export { createInventory, addPowerSuit }; diff --git a/src/services/purchaseService.ts b/src/services/purchaseService.ts index 5c002a0b..8b88cd86 100644 --- a/src/services/purchaseService.ts +++ b/src/services/purchaseService.ts @@ -112,12 +112,28 @@ const handleSuitCustomizationsPurchase = async (customizationName: string, accou }; }; -const handleBoostersPurchase = async (boosterName: string, accountId: string) => { - const addedBooster = await addBooster(boosterName, accountId); +const boosterCollection = [ + "/Lotus/Types/Boosters/ResourceAmountBooster", + "/Lotus/Types/Boosters/AffinityBooster", + "/Lotus/Types/Boosters/ResourceDropChanceBooster", + "/Lotus/Types/Boosters/CreditBooster" +]; + +const handleBoostersPurchase = async (boosterStoreName: string, accountId: string) => { + const match = boosterStoreName.match(/(\d+)Day/); + if (!match) return; + + const extractedDigit = Number(match[1]); + const ItemType = boosterCollection.find(i => + boosterStoreName.includes(i.split("/").pop()!.replace("Booster", "")) + )!; + const ExpiryDate = extractedDigit * 86400; + + await addBooster(ItemType, ExpiryDate, accountId); return { InventoryChanges: { - Boosters: [addedBooster] + Boosters: [{ ItemType, ExpiryDate }] } }; };