The game we are working on requires that when the character makes a purchase, there is also a change in their custom data since we have our own progression system in game.
One way we could do this is to send two separate requests from the clientAPI. PurchaseItem(), followed by UpdateCharacterData(). However, this can result in an invalid state if one of the requests succeeds, but not the other.
One solution I thought of is to use a Cloudscript. The client makes a request to purchase an item by calling the cloudscript, along with sending the required custom data changes. Then, on the server side, we:
1) Get the character's inventory and virtual currency holdings
2) Get the catalog items and find the price of the item we want to buy.
3) Compare the currency the character has with the cost of the item.
4) Call GrantItemsToCharacter() if they have enough currency.
5) Update the custom data using UpdateCharacterData()
That is at least 5 serverAPI calls to perform a purchase, which includes getting the entire catalog to find the price of a single item.
However, I can't think of another way to make this as safely atomic as possible.
If there are alternatives to this approach, I'd love to hear them.