forked from OpenWF/SpaceNinjaServer
		
	improve: purchases (#161)
Co-authored-by: Sainan <Sainan@users.noreply.github.com>
This commit is contained in:
		
							parent
							
								
									d62785a883
								
							
						
					
					
						commit
						971d149122
					
				@ -27,7 +27,7 @@ export const inventorySlotsController: RequestHandler = async (req, res) => {
 | 
			
		||||
 | 
			
		||||
    //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);
 | 
			
		||||
 | 
			
		||||
    //console.log({ InventoryChanges: currencyChanges }, " added loadout changes:");
 | 
			
		||||
 | 
			
		||||
@ -93,15 +93,19 @@ export const updateSlots = async (accountId: string, slotName: SlotNames, slotAm
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
export const updateCurrency = async (price: number, usePremium: boolean, accountId: string) => {
 | 
			
		||||
    if (config.infiniteResources) {
 | 
			
		||||
        return {};
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const inventory = await getInventory(accountId);
 | 
			
		||||
 | 
			
		||||
    if (usePremium) {
 | 
			
		||||
        if (inventory.PremiumCreditsFree > 0) {
 | 
			
		||||
            inventory.PremiumCreditsFree += price;
 | 
			
		||||
            inventory.PremiumCreditsFree -= Math.min(price, inventory.PremiumCreditsFree);
 | 
			
		||||
        }
 | 
			
		||||
        inventory.PremiumCredits += price;
 | 
			
		||||
        inventory.PremiumCredits -= price;
 | 
			
		||||
    } else {
 | 
			
		||||
        inventory.RegularCredits += price;
 | 
			
		||||
        inventory.RegularCredits -= price;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const modifiedPaths = inventory.modifiedPaths();
 | 
			
		||||
 | 
			
		||||
@ -5,12 +5,15 @@ import {
 | 
			
		||||
    addBooster,
 | 
			
		||||
    addCustomization,
 | 
			
		||||
    addMechSuit,
 | 
			
		||||
    addMiscItems,
 | 
			
		||||
    addPowerSuit,
 | 
			
		||||
    addSentinel,
 | 
			
		||||
    addWeapon,
 | 
			
		||||
    getInventory,
 | 
			
		||||
    updateCurrency,
 | 
			
		||||
    updateSlots
 | 
			
		||||
} from "@/src/services/inventoryService";
 | 
			
		||||
import { IMiscItem } from "@/src/types/inventoryTypes/inventoryTypes";
 | 
			
		||||
import { IPurchaseRequest, IPurchaseResponse, SlotNameToInventoryName, SlotPurchase } from "@/src/types/purchaseTypes";
 | 
			
		||||
import { logger } from "@/src/utils/logger";
 | 
			
		||||
 | 
			
		||||
@ -44,11 +47,18 @@ export const handlePurchase = async (purchaseRequest: IPurchaseRequest, accountI
 | 
			
		||||
            inventoryChanges = await handleWeaponsPurchase(internalName, accountId);
 | 
			
		||||
            break;
 | 
			
		||||
        case "Types":
 | 
			
		||||
            inventoryChanges = await handleTypesPurchase(internalName, accountId);
 | 
			
		||||
            inventoryChanges = await handleTypesPurchase(
 | 
			
		||||
                internalName,
 | 
			
		||||
                accountId,
 | 
			
		||||
                purchaseRequest.PurchaseParams.Quantity
 | 
			
		||||
            );
 | 
			
		||||
            break;
 | 
			
		||||
        case "Boosters":
 | 
			
		||||
            inventoryChanges = await handleBoostersPurchase(internalName, accountId);
 | 
			
		||||
            break;
 | 
			
		||||
        case "Interface":
 | 
			
		||||
            inventoryChanges = await handleCustomizationPurchase(internalName, accountId);
 | 
			
		||||
            break;
 | 
			
		||||
        default:
 | 
			
		||||
            const errorMessage = `unknown store category: ${storeCategory} not implemented or new`;
 | 
			
		||||
            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
 | 
			
		||||
const handleTypesPurchase = async (typesName: string, accountId: string) => {
 | 
			
		||||
const handleTypesPurchase = async (typesName: string, accountId: string, quantity: number) => {
 | 
			
		||||
    const typeCategory = getStoreItemTypesCategory(typesName);
 | 
			
		||||
    logger.debug(`type category ${typeCategory}`);
 | 
			
		||||
    switch (typeCategory) {
 | 
			
		||||
        case "AvatarImages":
 | 
			
		||||
        case "SuitCustomizations":
 | 
			
		||||
            return await handleSuitCustomizationsPurchase(typesName, accountId);
 | 
			
		||||
            return await handleCustomizationPurchase(typesName, accountId);
 | 
			
		||||
        // case "Recipes":
 | 
			
		||||
        //     break;
 | 
			
		||||
        case "Sentinels":
 | 
			
		||||
            return await handleSentinelPurchase(typesName, accountId);
 | 
			
		||||
        case "SlotItems":
 | 
			
		||||
            return await handleSlotPurchase(typesName, accountId);
 | 
			
		||||
        case "Items":
 | 
			
		||||
            return await handleMiscItemPurchase(typesName, accountId, quantity);
 | 
			
		||||
        default:
 | 
			
		||||
            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);
 | 
			
		||||
 | 
			
		||||
    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
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user