improve: purchases (#161)

Co-authored-by: Sainan <Sainan@users.noreply.github.com>
This commit is contained in:
Sainan 2024-05-09 01:27:32 +02:00 committed by GitHub
parent d62785a883
commit 971d149122
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 42 additions and 8 deletions

View File

@ -27,7 +27,7 @@ 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 updateCurrency(-20, true, accountId); const currencyChanges = await updateCurrency(20, true, accountId);
await updateSlots(accountId, SlotNameToInventoryName.LOADOUT, 1, 1); await updateSlots(accountId, SlotNameToInventoryName.LOADOUT, 1, 1);
//console.log({ InventoryChanges: currencyChanges }, " added loadout changes:"); //console.log({ InventoryChanges: currencyChanges }, " added loadout changes:");

View File

@ -93,15 +93,19 @@ export const updateSlots = async (accountId: string, slotName: SlotNames, slotAm
}; };
export const updateCurrency = async (price: number, usePremium: boolean, accountId: string) => { export const updateCurrency = async (price: number, usePremium: boolean, accountId: string) => {
if (config.infiniteResources) {
return {};
}
const inventory = await getInventory(accountId); const inventory = await getInventory(accountId);
if (usePremium) { if (usePremium) {
if (inventory.PremiumCreditsFree > 0) { if (inventory.PremiumCreditsFree > 0) {
inventory.PremiumCreditsFree += price; inventory.PremiumCreditsFree -= Math.min(price, inventory.PremiumCreditsFree);
} }
inventory.PremiumCredits += price; inventory.PremiumCredits -= price;
} else { } else {
inventory.RegularCredits += price; inventory.RegularCredits -= price;
} }
const modifiedPaths = inventory.modifiedPaths(); const modifiedPaths = inventory.modifiedPaths();

View File

@ -5,12 +5,15 @@ import {
addBooster, addBooster,
addCustomization, addCustomization,
addMechSuit, addMechSuit,
addMiscItems,
addPowerSuit, addPowerSuit,
addSentinel, addSentinel,
addWeapon, addWeapon,
getInventory,
updateCurrency, updateCurrency,
updateSlots updateSlots
} from "@/src/services/inventoryService"; } from "@/src/services/inventoryService";
import { IMiscItem } from "@/src/types/inventoryTypes/inventoryTypes";
import { IPurchaseRequest, IPurchaseResponse, SlotNameToInventoryName, SlotPurchase } from "@/src/types/purchaseTypes"; import { IPurchaseRequest, IPurchaseResponse, SlotNameToInventoryName, SlotPurchase } from "@/src/types/purchaseTypes";
import { logger } from "@/src/utils/logger"; import { logger } from "@/src/utils/logger";
@ -44,11 +47,18 @@ export const handlePurchase = async (purchaseRequest: IPurchaseRequest, accountI
inventoryChanges = await handleWeaponsPurchase(internalName, accountId); inventoryChanges = await handleWeaponsPurchase(internalName, accountId);
break; break;
case "Types": case "Types":
inventoryChanges = await handleTypesPurchase(internalName, accountId); inventoryChanges = await handleTypesPurchase(
internalName,
accountId,
purchaseRequest.PurchaseParams.Quantity
);
break; break;
case "Boosters": case "Boosters":
inventoryChanges = await handleBoostersPurchase(internalName, accountId); inventoryChanges = await handleBoostersPurchase(internalName, accountId);
break; break;
case "Interface":
inventoryChanges = await handleCustomizationPurchase(internalName, accountId);
break;
default: default:
const errorMessage = `unknown store category: ${storeCategory} not implemented or new`; const errorMessage = `unknown store category: ${storeCategory} not implemented or new`;
logger.error(errorMessage); logger.error(errorMessage);
@ -163,18 +173,21 @@ const handlePowersuitPurchase = async (powersuitName: string, accountId: string)
}; };
//TODO: change to getInventory, apply changes then save at the end //TODO: change to getInventory, apply changes then save at the end
const handleTypesPurchase = async (typesName: string, accountId: string) => { const handleTypesPurchase = async (typesName: string, accountId: string, quantity: number) => {
const typeCategory = getStoreItemTypesCategory(typesName); const typeCategory = getStoreItemTypesCategory(typesName);
logger.debug(`type category ${typeCategory}`); logger.debug(`type category ${typeCategory}`);
switch (typeCategory) { switch (typeCategory) {
case "AvatarImages":
case "SuitCustomizations": case "SuitCustomizations":
return await handleSuitCustomizationsPurchase(typesName, accountId); return await handleCustomizationPurchase(typesName, accountId);
// case "Recipes": // case "Recipes":
// break; // break;
case "Sentinels": case "Sentinels":
return await handleSentinelPurchase(typesName, accountId); return await handleSentinelPurchase(typesName, accountId);
case "SlotItems": case "SlotItems":
return await handleSlotPurchase(typesName, accountId); return await handleSlotPurchase(typesName, accountId);
case "Items":
return await handleMiscItemPurchase(typesName, accountId, quantity);
default: default:
throw new Error(`unknown Types category: ${typeCategory} not implemented or new`); throw new Error(`unknown Types category: ${typeCategory} not implemented or new`);
} }
@ -193,7 +206,7 @@ const handleSentinelPurchase = async (sentinelName: string, accountId: string) =
}; };
}; };
const handleSuitCustomizationsPurchase = async (customizationName: string, accountId: string) => { const handleCustomizationPurchase = async (customizationName: string, accountId: string) => {
const customization = await addCustomization(customizationName, accountId); const customization = await addCustomization(customizationName, accountId);
return { return {
@ -228,3 +241,20 @@ const handleBoostersPurchase = async (boosterStoreName: string, accountId: strin
} }
}; };
}; };
const handleMiscItemPurchase = async (uniqueName: string, accountId: string, quantity: number) => {
const inventory = await getInventory(accountId);
const miscItemChanges = [
{
ItemType: uniqueName,
ItemCount: quantity
} satisfies IMiscItem
];
addMiscItems(inventory, miscItemChanges);
await inventory.save();
return {
InventoryChanges: {
MiscItems: miscItemChanges
}
};
};