From 77c2761a97b21471f8252cc26925b904cf2cdf43 Mon Sep 17 00:00:00 2001 From: Sainan Date: Mon, 17 Jun 2024 16:38:26 +0200 Subject: [PATCH] fix: not being able to purchase boosters (#316) --- package-lock.json | 8 +++---- package.json | 2 +- src/services/purchaseService.ts | 37 +++++++++++++++++++++------------ 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/package-lock.json b/package-lock.json index 113407a0..a6da5d4d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,7 +13,7 @@ "express": "^5.0.0-beta.3", "mongoose": "^8.1.1", "warframe-items": "^1.1262.74", - "warframe-public-export-plus": "^0.2.3", + "warframe-public-export-plus": "^0.2.4", "warframe-riven-info": "^0.1.0", "winston": "^3.11.0", "winston-daily-rotate-file": "^4.7.1" @@ -3909,9 +3909,9 @@ } }, "node_modules/warframe-public-export-plus": { - "version": "0.2.3", - "resolved": "https://registry.npmjs.org/warframe-public-export-plus/-/warframe-public-export-plus-0.2.3.tgz", - "integrity": "sha512-Bl4gb3f1LIdGXLEOJg2XTIFYqrialdTIvVhDqDzVJIRfii0PKsy9jsr9vqM14tWz7oVpQMeCUyvisDkkXijTSg==" + "version": "0.2.4", + "resolved": "https://registry.npmjs.org/warframe-public-export-plus/-/warframe-public-export-plus-0.2.4.tgz", + "integrity": "sha512-Kh2+4p0qirTNUfHLr/nUJ1y/kH9mKnig28dwgpPaWt41ZfGzA+/qrpqI3DHYUvpd4wTCii/HItIy6tZtpTsy4Q==" }, "node_modules/warframe-riven-info": { "version": "0.1.0", diff --git a/package.json b/package.json index 9da3bcca..7ac70817 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "express": "^5.0.0-beta.3", "mongoose": "^8.1.1", "warframe-items": "^1.1262.74", - "warframe-public-export-plus": "^0.2.3", + "warframe-public-export-plus": "^0.2.4", "warframe-riven-info": "^0.1.0", "winston": "^3.11.0", "winston-daily-rotate-file": "^4.7.1" diff --git a/src/services/purchaseService.ts b/src/services/purchaseService.ts index b8a1c1ea..0f96055c 100644 --- a/src/services/purchaseService.ts +++ b/src/services/purchaseService.ts @@ -3,7 +3,7 @@ import { getSubstringFromKeyword } from "@/src/helpers/stringHelpers"; import { addItem, addBooster, updateCurrency, updateSlots } from "@/src/services/inventoryService"; import { IPurchaseRequest, SlotPurchase } from "@/src/types/purchaseTypes"; import { logger } from "@/src/utils/logger"; -import { ExportBundles } from "warframe-public-export-plus"; +import { ExportBundles, TRarity } from "warframe-public-export-plus"; export const getStoreItemCategory = (storeItem: string) => { const storeItemString = getSubstringFromKeyword(storeItem, "StoreItems/"); @@ -26,7 +26,8 @@ export const handlePurchase = async (purchaseRequest: IPurchaseRequest, accountI const purchaseResponse = await handleStoreItemAcquisition( purchaseRequest.PurchaseParams.StoreItem, accountId, - purchaseRequest.PurchaseParams.Quantity + purchaseRequest.PurchaseParams.Quantity, + "COMMON" ); if (!purchaseResponse) throw new Error("purchase response was undefined"); @@ -48,7 +49,8 @@ export const handlePurchase = async (purchaseRequest: IPurchaseRequest, accountI const handleStoreItemAcquisition = async ( storeItemName: string, accountId: string, - quantity: number + quantity: number, + durability: TRarity ): Promise<{ InventoryChanges: object }> => { let purchaseResponse = { InventoryChanges: {} @@ -60,7 +62,12 @@ const handleStoreItemAcquisition = async ( for (const component of bundle.components) { purchaseResponse = { ...purchaseResponse, - ...(await handleStoreItemAcquisition(component.typeName, accountId, component.purchaseQuantity)) + ...(await handleStoreItemAcquisition( + component.typeName, + accountId, + component.purchaseQuantity, + component.durability + )) }; } } else { @@ -75,7 +82,7 @@ const handleStoreItemAcquisition = async ( purchaseResponse = await handleTypesPurchase(internalName, accountId, quantity); break; case "Boosters": - purchaseResponse = await handleBoostersPurchase(internalName, accountId); + purchaseResponse = await handleBoostersPurchase(internalName, accountId, durability); break; } } @@ -144,17 +151,21 @@ const boosterCollection = [ "/Lotus/Types/Boosters/CreditBooster" ]; -const handleBoostersPurchase = async (boosterStoreName: string, accountId: string) => { - const match = boosterStoreName.match(/(\d+)Day/); - if (!match) { +const boosterDuration: Record = { + COMMON: 3 * 86400, + UNCOMMON: 7 * 86400, + RARE: 30 * 86400, + LEGENDARY: 90 * 86400 +}; + +const handleBoostersPurchase = async (boosterStoreName: string, accountId: string, durability: TRarity) => { + const ItemType = boosterStoreName.replace("StoreItem", ""); + if (!boosterCollection.find(x => x == ItemType)) { + logger.error(`unknown booster type: ${ItemType}`); return { InventoryChanges: {} }; } - const extractedDigit = Number(match[1]); - const ItemType = boosterCollection.find(i => - boosterStoreName.includes(i.split("/").pop()!.replace("Booster", "")) - )!; - const ExpiryDate = extractedDigit * 86400; + const ExpiryDate = boosterDuration[durability]; await addBooster(ItemType, ExpiryDate, accountId);