2024-01-25 14:49:45 +01:00
|
|
|
import { parseSlotPurchaseName } from "@/src/helpers/purchaseHelpers";
|
2023-06-14 02:26:19 +02:00
|
|
|
import { getSubstringFromKeyword } from "@/src/helpers/stringHelpers";
|
2024-07-03 12:30:32 +02:00
|
|
|
import {
|
|
|
|
addBooster,
|
2024-12-22 20:35:08 +01:00
|
|
|
addItem,
|
|
|
|
addMiscItems,
|
2024-07-03 12:30:32 +02:00
|
|
|
combineInventoryChanges,
|
2025-01-24 15:24:29 +01:00
|
|
|
updateCurrency,
|
2024-07-03 12:30:32 +02:00
|
|
|
updateSlots
|
|
|
|
} from "@/src/services/inventoryService";
|
2025-03-03 12:48:46 -08:00
|
|
|
import { getRandomWeightedRewardUc } from "@/src/services/rngService";
|
2024-12-22 23:28:44 +01:00
|
|
|
import { getVendorManifestByOid } from "@/src/services/serversideVendorsService";
|
2024-12-22 20:35:08 +01:00
|
|
|
import { IMiscItem } from "@/src/types/inventoryTypes/inventoryTypes";
|
2024-12-22 23:31:30 +01:00
|
|
|
import { IPurchaseRequest, IPurchaseResponse, SlotPurchase, IInventoryChanges } from "@/src/types/purchaseTypes";
|
2024-01-06 16:26:58 +01:00
|
|
|
import { logger } from "@/src/utils/logger";
|
2025-01-11 07:18:42 +01:00
|
|
|
import worldState from "@/static/fixed_responses/worldState/worldState.json";
|
2024-12-23 04:05:06 +01:00
|
|
|
import {
|
2024-12-25 23:34:14 +01:00
|
|
|
ExportBoosterPacks,
|
2025-02-24 21:46:30 -08:00
|
|
|
ExportBoosters,
|
2024-12-23 04:05:06 +01:00
|
|
|
ExportBundles,
|
|
|
|
ExportGear,
|
2025-02-24 21:46:20 -08:00
|
|
|
ExportMisc,
|
2024-12-23 04:05:06 +01:00
|
|
|
ExportResources,
|
|
|
|
ExportSyndicates,
|
|
|
|
ExportVendors,
|
|
|
|
TRarity
|
|
|
|
} from "warframe-public-export-plus";
|
2025-01-06 05:36:39 +01:00
|
|
|
import { config } from "./configService";
|
2025-01-24 15:24:29 +01:00
|
|
|
import { TInventoryDatabaseDocument } from "../models/inventoryModels/inventoryModel";
|
2023-06-14 02:26:19 +02:00
|
|
|
|
2024-12-29 21:47:18 +01:00
|
|
|
export const getStoreItemCategory = (storeItem: string): string => {
|
2023-06-14 02:26:19 +02:00
|
|
|
const storeItemString = getSubstringFromKeyword(storeItem, "StoreItems/");
|
|
|
|
const storeItemElements = storeItemString.split("/");
|
|
|
|
return storeItemElements[1];
|
|
|
|
};
|
|
|
|
|
2024-12-29 21:47:18 +01:00
|
|
|
export const getStoreItemTypesCategory = (typesItem: string): string => {
|
2023-06-14 02:26:19 +02:00
|
|
|
const typesString = getSubstringFromKeyword(typesItem, "Types");
|
|
|
|
const typeElements = typesString.split("/");
|
|
|
|
if (typesItem.includes("StoreItems")) {
|
|
|
|
return typeElements[2];
|
|
|
|
}
|
|
|
|
return typeElements[1];
|
|
|
|
};
|
|
|
|
|
2024-12-25 23:34:14 +01:00
|
|
|
export const handlePurchase = async (
|
|
|
|
purchaseRequest: IPurchaseRequest,
|
2025-01-24 15:24:29 +01:00
|
|
|
inventory: TInventoryDatabaseDocument
|
2024-12-25 23:34:14 +01:00
|
|
|
): Promise<IPurchaseResponse> => {
|
2024-01-06 16:26:58 +01:00
|
|
|
logger.debug("purchase request", purchaseRequest);
|
2023-06-14 02:26:19 +02:00
|
|
|
|
2025-01-18 07:06:07 +01:00
|
|
|
const inventoryChanges: IInventoryChanges = {};
|
2024-12-22 23:28:44 +01:00
|
|
|
if (purchaseRequest.PurchaseParams.Source == 7) {
|
|
|
|
const manifest = getVendorManifestByOid(purchaseRequest.PurchaseParams.SourceId!);
|
2025-01-18 11:11:52 +01:00
|
|
|
if (manifest) {
|
|
|
|
const ItemId = (JSON.parse(purchaseRequest.PurchaseParams.ExtraPurchaseInfoJson!) as { ItemId: string })
|
|
|
|
.ItemId;
|
|
|
|
const offer = manifest.VendorInfo.ItemManifest.find(x => x.Id.$oid == ItemId);
|
|
|
|
if (!offer) {
|
|
|
|
throw new Error(`unknown vendor offer: ${ItemId}`);
|
|
|
|
}
|
|
|
|
if (offer.ItemPrices) {
|
2025-01-24 15:24:29 +01:00
|
|
|
handleItemPrices(
|
|
|
|
inventory,
|
2025-01-18 11:11:52 +01:00
|
|
|
offer.ItemPrices,
|
|
|
|
purchaseRequest.PurchaseParams.Quantity,
|
|
|
|
inventoryChanges
|
|
|
|
);
|
|
|
|
}
|
|
|
|
purchaseRequest.PurchaseParams.Quantity *= offer.QuantityMultiplier;
|
|
|
|
} else if (!ExportVendors[purchaseRequest.PurchaseParams.SourceId!]) {
|
|
|
|
throw new Error(`unknown vendor: ${purchaseRequest.PurchaseParams.SourceId!}`);
|
2025-01-18 07:06:07 +01:00
|
|
|
}
|
2024-12-22 23:28:44 +01:00
|
|
|
}
|
|
|
|
|
2024-06-15 22:12:57 +02:00
|
|
|
const purchaseResponse = await handleStoreItemAcquisition(
|
|
|
|
purchaseRequest.PurchaseParams.StoreItem,
|
2025-01-24 15:24:29 +01:00
|
|
|
inventory,
|
2024-12-25 23:33:29 +01:00
|
|
|
purchaseRequest.PurchaseParams.Quantity
|
2024-06-15 22:12:57 +02:00
|
|
|
);
|
2025-01-18 07:06:07 +01:00
|
|
|
combineInventoryChanges(purchaseResponse.InventoryChanges, inventoryChanges);
|
2023-06-14 02:26:19 +02:00
|
|
|
|
2024-06-15 02:50:43 +02:00
|
|
|
if (!purchaseResponse) throw new Error("purchase response was undefined");
|
2023-06-14 02:26:19 +02:00
|
|
|
|
2025-01-24 15:24:29 +01:00
|
|
|
const currencyChanges = updateCurrency(
|
|
|
|
inventory,
|
2023-12-28 16:24:52 +01:00
|
|
|
purchaseRequest.PurchaseParams.ExpectedPrice,
|
2025-01-24 15:24:29 +01:00
|
|
|
purchaseRequest.PurchaseParams.UsePremium
|
2023-12-28 16:24:52 +01:00
|
|
|
);
|
2024-06-15 02:50:43 +02:00
|
|
|
purchaseResponse.InventoryChanges = {
|
2023-12-28 16:24:52 +01:00
|
|
|
...currencyChanges,
|
2024-06-15 02:50:43 +02:00
|
|
|
...purchaseResponse.InventoryChanges
|
2023-12-28 16:24:52 +01:00
|
|
|
};
|
|
|
|
|
2024-12-22 20:35:08 +01:00
|
|
|
switch (purchaseRequest.PurchaseParams.Source) {
|
2024-12-22 23:31:30 +01:00
|
|
|
case 2:
|
2025-01-17 14:43:51 +01:00
|
|
|
{
|
2024-12-22 23:31:30 +01:00
|
|
|
const syndicateTag = purchaseRequest.PurchaseParams.SyndicateTag!;
|
2025-01-17 14:43:51 +01:00
|
|
|
if (purchaseRequest.PurchaseParams.UseFreeFavor!) {
|
|
|
|
const affiliation = inventory.Affiliations.find(x => x.Tag == syndicateTag)!;
|
|
|
|
affiliation.FreeFavorsUsed ??= [];
|
2025-01-24 14:18:05 +01:00
|
|
|
const lastTitle = affiliation.FreeFavorsEarned![affiliation.FreeFavorsUsed.length];
|
|
|
|
affiliation.FreeFavorsUsed.push(lastTitle);
|
|
|
|
purchaseResponse.FreeFavorsUsed = [
|
|
|
|
{
|
|
|
|
Tag: syndicateTag,
|
|
|
|
Title: lastTitle
|
|
|
|
}
|
|
|
|
];
|
2025-01-17 14:43:51 +01:00
|
|
|
} else {
|
|
|
|
const syndicate = ExportSyndicates[syndicateTag];
|
|
|
|
if (syndicate) {
|
|
|
|
const favour = syndicate.favours.find(
|
|
|
|
x => x.storeItem == purchaseRequest.PurchaseParams.StoreItem
|
|
|
|
);
|
|
|
|
if (favour) {
|
|
|
|
const affiliation = inventory.Affiliations.find(x => x.Tag == syndicateTag);
|
|
|
|
if (affiliation) {
|
|
|
|
purchaseResponse.Standing = [
|
|
|
|
{
|
|
|
|
Tag: syndicateTag,
|
|
|
|
Standing: favour.standingCost
|
|
|
|
}
|
|
|
|
];
|
|
|
|
affiliation.Standing -= favour.standingCost;
|
|
|
|
}
|
2024-12-22 23:31:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2024-12-22 20:35:08 +01:00
|
|
|
case 7:
|
2024-12-22 23:28:44 +01:00
|
|
|
if (purchaseRequest.PurchaseParams.SourceId! in ExportVendors) {
|
|
|
|
const vendor = ExportVendors[purchaseRequest.PurchaseParams.SourceId!];
|
2024-12-22 20:35:08 +01:00
|
|
|
const offer = vendor.items.find(x => x.storeItem == purchaseRequest.PurchaseParams.StoreItem);
|
2025-01-18 11:11:52 +01:00
|
|
|
if (offer && offer.itemPrices) {
|
2025-01-24 15:24:29 +01:00
|
|
|
handleItemPrices(
|
|
|
|
inventory,
|
2025-01-18 07:06:07 +01:00
|
|
|
offer.itemPrices,
|
|
|
|
purchaseRequest.PurchaseParams.Quantity,
|
|
|
|
purchaseResponse.InventoryChanges
|
|
|
|
);
|
2024-12-22 20:35:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2024-12-22 23:28:59 +01:00
|
|
|
case 18: {
|
|
|
|
if (purchaseRequest.PurchaseParams.SourceId! != worldState.PrimeVaultTraders[0]._id.$oid) {
|
|
|
|
throw new Error("invalid request source");
|
|
|
|
}
|
|
|
|
const offer =
|
|
|
|
worldState.PrimeVaultTraders[0].Manifest.find(
|
|
|
|
x => x.ItemType == purchaseRequest.PurchaseParams.StoreItem
|
|
|
|
) ??
|
|
|
|
worldState.PrimeVaultTraders[0].EvergreenManifest.find(
|
|
|
|
x => x.ItemType == purchaseRequest.PurchaseParams.StoreItem
|
|
|
|
);
|
|
|
|
if (offer) {
|
|
|
|
if (offer.RegularPrice) {
|
|
|
|
const invItem: IMiscItem = {
|
|
|
|
ItemType: "/Lotus/Types/Items/MiscItems/SchismKey",
|
|
|
|
ItemCount: offer.RegularPrice * purchaseRequest.PurchaseParams.Quantity * -1
|
|
|
|
};
|
|
|
|
|
|
|
|
addMiscItems(inventory, [invItem]);
|
|
|
|
|
|
|
|
purchaseResponse.InventoryChanges.MiscItems ??= [];
|
2025-02-26 15:41:07 -08:00
|
|
|
purchaseResponse.InventoryChanges.MiscItems.push(invItem);
|
2025-01-06 05:36:39 +01:00
|
|
|
} else if (!config.infiniteRegalAya) {
|
2024-12-22 23:28:59 +01:00
|
|
|
inventory.PrimeTokens -= offer.PrimePrice! * purchaseRequest.PurchaseParams.Quantity;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2024-12-22 20:35:08 +01:00
|
|
|
}
|
|
|
|
|
2024-06-15 02:50:43 +02:00
|
|
|
return purchaseResponse;
|
2023-12-28 16:24:52 +01:00
|
|
|
};
|
|
|
|
|
2025-01-24 15:24:29 +01:00
|
|
|
const handleItemPrices = (
|
|
|
|
inventory: TInventoryDatabaseDocument,
|
2025-01-18 07:06:07 +01:00
|
|
|
itemPrices: IMiscItem[],
|
|
|
|
purchaseQuantity: number,
|
|
|
|
inventoryChanges: IInventoryChanges
|
2025-01-24 15:24:29 +01:00
|
|
|
): void => {
|
2025-01-18 07:06:07 +01:00
|
|
|
for (const item of itemPrices) {
|
|
|
|
const invItem: IMiscItem = {
|
|
|
|
ItemType: item.ItemType,
|
|
|
|
ItemCount: item.ItemCount * purchaseQuantity * -1
|
|
|
|
};
|
|
|
|
|
|
|
|
addMiscItems(inventory, [invItem]);
|
|
|
|
|
|
|
|
inventoryChanges.MiscItems ??= [];
|
2025-02-26 15:41:07 -08:00
|
|
|
const change = inventoryChanges.MiscItems.find(x => x.ItemType == item.ItemType);
|
2025-01-18 07:06:07 +01:00
|
|
|
if (change) {
|
|
|
|
change.ItemCount += invItem.ItemCount;
|
|
|
|
} else {
|
2025-02-26 15:41:07 -08:00
|
|
|
inventoryChanges.MiscItems.push(invItem);
|
2025-01-18 07:06:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2025-02-25 04:38:17 -08:00
|
|
|
export const handleBundleAcqusition = async (
|
|
|
|
storeItemName: string,
|
|
|
|
inventory: TInventoryDatabaseDocument,
|
|
|
|
quantity: number = 1,
|
|
|
|
inventoryChanges: IInventoryChanges = {}
|
|
|
|
): Promise<IInventoryChanges> => {
|
|
|
|
const bundle = ExportBundles[storeItemName];
|
|
|
|
logger.debug("acquiring bundle", bundle);
|
|
|
|
for (const component of bundle.components) {
|
|
|
|
combineInventoryChanges(
|
|
|
|
inventoryChanges,
|
|
|
|
(
|
|
|
|
await handleStoreItemAcquisition(
|
|
|
|
component.typeName,
|
|
|
|
inventory,
|
|
|
|
component.purchaseQuantity * quantity,
|
|
|
|
component.durability,
|
|
|
|
true
|
|
|
|
)
|
|
|
|
).InventoryChanges
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return inventoryChanges;
|
|
|
|
};
|
|
|
|
|
2024-12-25 23:33:29 +01:00
|
|
|
export const handleStoreItemAcquisition = async (
|
2024-06-15 22:12:57 +02:00
|
|
|
storeItemName: string,
|
2025-01-24 15:24:29 +01:00
|
|
|
inventory: TInventoryDatabaseDocument,
|
2024-12-25 23:33:29 +01:00
|
|
|
quantity: number = 1,
|
|
|
|
durability: TRarity = "COMMON",
|
2024-06-24 12:31:29 +02:00
|
|
|
ignorePurchaseQuantity: boolean = false
|
2024-12-22 23:31:30 +01:00
|
|
|
): Promise<IPurchaseResponse> => {
|
2024-06-15 22:12:57 +02:00
|
|
|
let purchaseResponse = {
|
|
|
|
InventoryChanges: {}
|
|
|
|
};
|
|
|
|
logger.debug(`handling acquision of ${storeItemName}`);
|
|
|
|
if (storeItemName in ExportBundles) {
|
2025-02-25 04:38:17 -08:00
|
|
|
await handleBundleAcqusition(storeItemName, inventory, quantity, purchaseResponse.InventoryChanges);
|
2024-06-15 22:12:57 +02:00
|
|
|
} else {
|
|
|
|
const storeCategory = getStoreItemCategory(storeItemName);
|
|
|
|
const internalName = storeItemName.replace("/StoreItems", "");
|
|
|
|
logger.debug(`store category ${storeCategory}`);
|
2024-06-24 12:31:29 +02:00
|
|
|
if (!ignorePurchaseQuantity) {
|
|
|
|
if (internalName in ExportGear) {
|
|
|
|
quantity *= ExportGear[internalName].purchaseQuantity || 1;
|
2024-12-23 04:05:06 +01:00
|
|
|
} else if (internalName in ExportResources) {
|
|
|
|
quantity *= ExportResources[internalName].purchaseQuantity || 1;
|
2024-06-24 12:31:29 +02:00
|
|
|
}
|
|
|
|
}
|
2024-06-15 22:12:57 +02:00
|
|
|
switch (storeCategory) {
|
2025-01-04 00:25:09 +01:00
|
|
|
default: {
|
2025-02-26 15:41:07 -08:00
|
|
|
purchaseResponse = await addItem(inventory, internalName, quantity, true);
|
2024-06-15 22:12:57 +02:00
|
|
|
break;
|
2025-01-04 00:25:09 +01:00
|
|
|
}
|
2024-06-15 22:12:57 +02:00
|
|
|
case "Types":
|
2025-01-24 15:24:29 +01:00
|
|
|
purchaseResponse = await handleTypesPurchase(internalName, inventory, quantity);
|
2024-06-15 22:12:57 +02:00
|
|
|
break;
|
|
|
|
case "Boosters":
|
2025-02-24 21:46:30 -08:00
|
|
|
purchaseResponse = handleBoostersPurchase(storeItemName, inventory, durability);
|
2024-06-15 22:12:57 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return purchaseResponse;
|
|
|
|
};
|
|
|
|
|
2023-12-28 16:24:52 +01:00
|
|
|
export const slotPurchaseNameToSlotName: SlotPurchase = {
|
|
|
|
SuitSlotItem: { name: "SuitBin", slotsPerPurchase: 1 },
|
|
|
|
TwoSentinelSlotItem: { name: "SentinelBin", slotsPerPurchase: 2 },
|
|
|
|
TwoWeaponSlotItem: { name: "WeaponBin", slotsPerPurchase: 2 },
|
|
|
|
SpaceSuitSlotItem: { name: "SpaceSuitBin", slotsPerPurchase: 1 },
|
|
|
|
TwoSpaceWeaponSlotItem: { name: "SpaceWeaponBin", slotsPerPurchase: 2 },
|
|
|
|
MechSlotItem: { name: "MechBin", slotsPerPurchase: 1 },
|
|
|
|
TwoOperatorWeaponSlotItem: { name: "OperatorAmpBin", slotsPerPurchase: 2 },
|
|
|
|
RandomModSlotItem: { name: "RandomModBin", slotsPerPurchase: 3 },
|
|
|
|
TwoCrewShipSalvageSlotItem: { name: "CrewShipSalvageBin", slotsPerPurchase: 2 },
|
|
|
|
CrewMemberSlotItem: { name: "CrewMemberBin", slotsPerPurchase: 1 }
|
|
|
|
};
|
|
|
|
|
|
|
|
// // extra = everything above the base +2 slots (depending on slot type)
|
|
|
|
// // new slot above base = extra + 1 and slots +1
|
|
|
|
// // new frame = slots -1
|
|
|
|
// // number of frames = extra - slots + 2
|
2025-01-24 15:24:29 +01:00
|
|
|
const handleSlotPurchase = (
|
2024-06-20 16:35:24 +02:00
|
|
|
slotPurchaseNameFull: string,
|
2025-01-24 15:24:29 +01:00
|
|
|
inventory: TInventoryDatabaseDocument,
|
2025-01-05 02:43:06 +01:00
|
|
|
quantity: number
|
2025-01-24 15:24:29 +01:00
|
|
|
): IPurchaseResponse => {
|
2024-01-06 16:26:58 +01:00
|
|
|
logger.debug(`slot name ${slotPurchaseNameFull}`);
|
2023-12-28 16:24:52 +01:00
|
|
|
const slotPurchaseName = parseSlotPurchaseName(
|
|
|
|
slotPurchaseNameFull.substring(slotPurchaseNameFull.lastIndexOf("/") + 1)
|
|
|
|
);
|
2024-01-06 16:26:58 +01:00
|
|
|
logger.debug(`slot purchase name ${slotPurchaseName}`);
|
2023-12-28 16:24:52 +01:00
|
|
|
|
2024-01-06 16:26:58 +01:00
|
|
|
const slotName = slotPurchaseNameToSlotName[slotPurchaseName].name;
|
2025-01-05 02:43:06 +01:00
|
|
|
const slotsPurchased = slotPurchaseNameToSlotName[slotPurchaseName].slotsPerPurchase * quantity;
|
2023-12-28 16:24:52 +01:00
|
|
|
|
2025-01-05 02:43:06 +01:00
|
|
|
updateSlots(inventory, slotName, slotsPurchased, slotsPurchased);
|
2024-01-06 16:26:58 +01:00
|
|
|
|
2025-01-05 02:43:06 +01:00
|
|
|
logger.debug(`added ${slotsPurchased} slot ${slotName}`);
|
2023-12-28 16:24:52 +01:00
|
|
|
|
2025-02-26 15:41:07 -08:00
|
|
|
const inventoryChanges: IInventoryChanges = {};
|
|
|
|
inventoryChanges[slotName] = {
|
|
|
|
count: 0,
|
|
|
|
platinum: 1,
|
|
|
|
Slots: slotsPurchased,
|
|
|
|
Extra: slotsPurchased
|
2023-12-28 16:24:52 +01:00
|
|
|
};
|
2025-02-26 15:41:07 -08:00
|
|
|
return { InventoryChanges: inventoryChanges };
|
2023-06-14 02:26:19 +02:00
|
|
|
};
|
|
|
|
|
2024-12-31 01:39:45 +01:00
|
|
|
const handleBoosterPackPurchase = async (
|
|
|
|
typeName: string,
|
2025-01-24 15:24:29 +01:00
|
|
|
inventory: TInventoryDatabaseDocument,
|
2024-12-31 01:39:45 +01:00
|
|
|
quantity: number
|
|
|
|
): Promise<IPurchaseResponse> => {
|
2024-12-25 23:34:14 +01:00
|
|
|
const pack = ExportBoosterPacks[typeName];
|
|
|
|
if (!pack) {
|
|
|
|
throw new Error(`unknown booster pack: ${typeName}`);
|
|
|
|
}
|
|
|
|
const purchaseResponse: IPurchaseResponse = {
|
|
|
|
BoosterPackItems: "",
|
|
|
|
InventoryChanges: {}
|
|
|
|
};
|
2024-12-31 01:39:45 +01:00
|
|
|
for (let i = 0; i != quantity; ++i) {
|
|
|
|
for (const weights of pack.rarityWeightsPerRoll) {
|
2025-03-03 12:48:46 -08:00
|
|
|
const result = getRandomWeightedRewardUc(pack.components, weights);
|
2024-12-31 01:39:45 +01:00
|
|
|
if (result) {
|
|
|
|
logger.debug(`booster pack rolled`, result);
|
|
|
|
purchaseResponse.BoosterPackItems +=
|
2025-03-03 12:48:46 -08:00
|
|
|
result.Item.split("/Lotus/").join("/Lotus/StoreItems/") + ',{"lvl":0};';
|
2024-12-31 01:39:45 +01:00
|
|
|
combineInventoryChanges(
|
|
|
|
purchaseResponse.InventoryChanges,
|
2025-03-03 12:48:46 -08:00
|
|
|
(await addItem(inventory, result.Item, 1)).InventoryChanges
|
2024-12-31 01:39:45 +01:00
|
|
|
);
|
|
|
|
}
|
2024-12-25 23:34:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return purchaseResponse;
|
|
|
|
};
|
|
|
|
|
2025-02-23 03:53:56 -08:00
|
|
|
const handleCreditBundlePurchase = async (
|
|
|
|
typeName: string,
|
|
|
|
inventory: TInventoryDatabaseDocument
|
|
|
|
): Promise<IPurchaseResponse> => {
|
2025-02-24 21:46:20 -08:00
|
|
|
if (typeName && typeName in ExportMisc.creditBundles) {
|
|
|
|
const creditsAmount = ExportMisc.creditBundles[typeName];
|
2025-02-23 03:53:56 -08:00
|
|
|
|
|
|
|
inventory.RegularCredits += creditsAmount;
|
|
|
|
await inventory.save();
|
|
|
|
|
|
|
|
return { InventoryChanges: { RegularCredits: creditsAmount } };
|
|
|
|
} else {
|
|
|
|
throw new Error(`unknown credit bundle: ${typeName}`);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-12-28 16:24:52 +01:00
|
|
|
//TODO: change to getInventory, apply changes then save at the end
|
2024-06-20 16:35:24 +02:00
|
|
|
const handleTypesPurchase = async (
|
|
|
|
typesName: string,
|
2025-01-24 15:24:29 +01:00
|
|
|
inventory: TInventoryDatabaseDocument,
|
2024-06-20 16:35:24 +02:00
|
|
|
quantity: number
|
2024-12-25 23:34:14 +01:00
|
|
|
): Promise<IPurchaseResponse> => {
|
2023-06-14 02:26:19 +02:00
|
|
|
const typeCategory = getStoreItemTypesCategory(typesName);
|
2024-01-06 16:26:58 +01:00
|
|
|
logger.debug(`type category ${typeCategory}`);
|
2023-06-14 02:26:19 +02:00
|
|
|
switch (typeCategory) {
|
2025-01-24 15:24:29 +01:00
|
|
|
default:
|
|
|
|
return await addItem(inventory, typesName, quantity);
|
2024-12-25 23:34:14 +01:00
|
|
|
case "BoosterPacks":
|
2025-01-24 15:24:29 +01:00
|
|
|
return handleBoosterPackPurchase(typesName, inventory, quantity);
|
2023-12-28 16:24:52 +01:00
|
|
|
case "SlotItems":
|
2025-01-24 15:24:29 +01:00
|
|
|
return handleSlotPurchase(typesName, inventory, quantity);
|
2025-02-23 03:53:56 -08:00
|
|
|
case "CreditBundles":
|
|
|
|
return handleCreditBundlePurchase(typesName, inventory);
|
2023-06-14 02:26:19 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2025-01-24 15:24:29 +01:00
|
|
|
const handleBoostersPurchase = (
|
2024-06-20 16:35:24 +02:00
|
|
|
boosterStoreName: string,
|
2025-01-24 15:24:29 +01:00
|
|
|
inventory: TInventoryDatabaseDocument,
|
2024-06-20 16:35:24 +02:00
|
|
|
durability: TRarity
|
2025-01-24 15:24:29 +01:00
|
|
|
): { InventoryChanges: IInventoryChanges } => {
|
2025-02-24 21:46:30 -08:00
|
|
|
if (!(boosterStoreName in ExportBoosters)) {
|
|
|
|
logger.error(`unknown booster type: ${boosterStoreName}`);
|
2024-06-15 22:12:57 +02:00
|
|
|
return { InventoryChanges: {} };
|
|
|
|
}
|
2023-08-31 14:29:09 +04:00
|
|
|
|
2025-02-24 21:46:30 -08:00
|
|
|
const ItemType = ExportBoosters[boosterStoreName].typeName;
|
|
|
|
const ExpiryDate = ExportMisc.boosterDurations[durability];
|
2023-08-31 14:29:09 +04:00
|
|
|
|
2025-01-24 15:24:29 +01:00
|
|
|
addBooster(ItemType, ExpiryDate, inventory);
|
2023-08-31 14:29:09 +04:00
|
|
|
|
|
|
|
return {
|
|
|
|
InventoryChanges: {
|
|
|
|
Boosters: [{ ItemType, ExpiryDate }]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|