question

Alexey Sudarev avatar image
Alexey Sudarev asked

So many request to server

Hello. I'm trying to do function "Buy All Cars". The more cars the more requests. How can I optimize updating car data in my inventory?

handlers.BuyAllCars = function(args) {
    var inventory = server.GetUserInventory({PlayFabId: currentPlayerId}).Inventory;
    var carsCatalog = server.GetCatalogItems({CatalogVersion: "cars"}).Catalog;
    
    var inventoryCars = [];
    
    for(var i = 0; i < inventory.length; i++) {
        if(inventory[i].CatalogVersion != "cars") continue;
        
        inventoryCars.push(inventory[i]);
    }
    
    var buyCars = [];
    
    for(var i = 0; i < carsCatalog.length; i++) {
        var exist = false;
        
        for(var j = 0; j < inventoryCars.length; j++) {
            if(carsCatalog[i].ItemId == inventoryCars[j].ItemId) {
                exist = true;
                break;
            }
        }
        
        if(exist == true) continue;
        
        buyCars.push(carsCatalog[i].ItemId);
    }
    
    var grantItems = server.GrantItemsToUser(
    {
        CatalogVersion: "cars",
        PlayFabId: currentPlayerId,
        ItemIds: buyCars
    });
    
    for(var i = 0; i < carsCatalog.length; i++) {
        for(var j = 0; j < grantItems.ItemGrantResults.length; j++) {
            if(carsCatalog[i].ItemId != grantItems.ItemGrantResults[j].ItemId) continue;
            
            var unencodedData = JSON.parse(carsCatalog[i].CustomData);
            var newData = {};


            newData["engine"] = unencodedData.engine;
            newData["petrol"] = unencodedData.petrol;
            
            server.UpdateUserInventoryItemCustomData({
                Data: newData,
                ItemInstanceId: grantItems.ItemGrantResults[j].ItemInstanceId,
                PlayFabId: currentPlayerId
            });               
        }
    }
}
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Citrus Yan avatar image
Citrus Yan answered

As the number of cars gets bigger, inevitably, the requests will also increase in your case. Sooner or later, you’ll exceed the CloudScript-related limits, mainly the ones on “Cloud Script execution time (API call)” and “Cloud Script execution API requests issued” (you find them in [Game Manager] -> [Title Settings] -> [Limits] ). Therefore, I would suggest you migrate this logic into your own custom server or the new CloudScript using Azure Functions feature that PlayFab offers, please navigate to the link to learn more.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.