Hey!
I’m using Unity economy to hold my game’s inventory. I am making a card game where each player has only 5 cards. For that reason, players can save premade hands (in cloud save) and then select them before a game, and the backend (cloud code) has to filter which hands are available for choosing at the moment of the game, depending on whether those hands can be used with the current ruleset, if they are currently in the players collection AND the player has not put those cards for sell (there are player owned shops).
The problem I’m having with the code, is that I seem to be getting only a page of the items in the player inventory. So I tried setting the request limit to 0 to see if it would just not page the thing, and it didn’t work. I tried 111 (all inventory items currently in game. This would still NOT be practical as a player can have multiple copies of a card, thus having more than the 111 cards currently in game, but still, fit for testing purposes), and this gave me a 400 Bad Request. IS there any way to avoid the result from being paged altogether? Or at least any way for me to go through the pages? This is really hindering my progress.
Here’s my script:
const { DataApi } = require('@unity-services/cloud-save-1.3');
const { InventoryApi, ConfigurationApi } = require("@unity-services/economy-2.4");
module.exports = async ({ params, context, logger }) => {
try {
const { projectId, playerId, accessToken } = context;
// Initialize the necessary Unity API clients
const cloudSaveAPI = new DataApi(context);
const inventoryApi = new InventoryApi(context);
const configurationApi = new ConfigurationApi(context);
//fetch parameters
var acceptedDecks = params.acceptedDecks;
var acceptedLevels = params.acceptedLevels;
//Get player saved hands and filter those whose cards are uneligible or have unavailable cards
var responseRaw = await cloudSaveAPI.getItems(projectId, playerId, "Hands");
var hands = responseRaw.data.results[0]?.value;
if (hands === undefined) {
hands = [];
}
var inventoryValidHandsWithMetada = await verifyAndMapHandCardsAgainstInventory(hands, configurationApi, inventoryApi, projectId, playerId, logger);
var eligibleHands = [];
if (acceptedDecks === undefined || acceptedDecks.length === 0 || acceptedLevels === undefined || acceptedLevels.length === 0) {
eligibleHands = inventoryValidHandsWithMetada;
} else {
}
return eligibleHands;
} catch (e) {
logger.error("failed to fetch cards: " + e);
}
};
async function verifyAndMapHandCardsAgainstInventory(hands, configurationApi, inventoryApi, projectId, playerId, logger) {
const inventoryApiGetPlayerInventoryRequest = { playerId: playerId, projectId: projectId, limit: 110 }
const getPlayerConfigRequest = { playerId: playerId, projectId: projectId }
//Get player inventory
const playerInventory = await inventoryApi.getPlayerInventory(inventoryApiGetPlayerInventoryRequest);
//Get player config and filter only for inventory items
var config = await configurationApi.getPlayerConfiguration(getPlayerConfigRequest);
const inventoryConfig = config.data.results.filter((configItem) => {
return configItem.type === "INVENTORY_ITEM";
});
//map inventory items with their configuraion
const itemsWithCustomData = playerInventory.data.results.map((inventoryItem) => {
const inventoryItemConfig = inventoryConfig.find((configItem) => {
return configItem.id === inventoryItem.inventoryItemId;
})
inventoryItem.customData = inventoryItemConfig.customData;
return inventoryItem.customData;
});
//Filter hands that have cards that are not in the collection or are currently being sold
logger.debug("Hands to verify: " + JSON.stringify(hands));
verifiedHands = hands.filter((hand) => {
var allCardsAreContained = true;
logger.debug("filtering hand: " + JSON.stringify(hand.handCardsId));
hand.handCardsId.forEach(cardId => {
const foundItem = itemsWithCustomData.find((item) => {
return item.id == cardId && !item.isBeingSold;
})
logger.debug("found item: " + JSON.stringify(foundItem));
if (foundItem === undefined) {
allCardsAreContained = false;
}
});
return allCardsAreContained;
});
logger.debug("found cards: " + JSON.stringify(verifiedHands));
return verifiedHands;
}