Sign In Register

How can we help you today?

Start a new topic
Answered

Transactions

Hi,


I'm using Unity.


Is there any possible way to put two requests into one transaction?


For example, I have a Thief and a Treasury in my game. Each object has AmountOfGold property.


So, when the Thief steals gold from Treasury, their AmountOfGold change. This change on client-side must be reflected on the server-side, in GameSpark's database.


Each object has its own SaveData() method and corresponding Event in GameSparks portal, which code writes new AmountOfGold to the Mongo DB.


So it may happen that the Thief sends its SaveData request and everything works fine -- DB is updated.

Then the Treasury does the same (but only with its own SaveData request), and something goes wrong (internet connection or whatever), -- the new AmountOfGold is not saved to DB.


So it appears, that the Thief has more money, and the Treasury still has its initial gold amount as nothing happened.


If it'd be possible to implement some kind of transation, it would simply roll back in this case.


I don't have any experience how to handle this situation correctly, so maybe you guys may give me some advice on it. Thanks.


Best Answer

Hi Aleksey,


One way to achieving this would be to have an event in cloud code handle the transaction entirely. So for example you would pass it the Id of the two parties involved in the transaction, this event would process the transaction then return up to date values of each party. This way if an internet connection problem occurred no transaction would take place at all.


Another way of handling this could be to use SetDurable as documented here https://docs.gamesparks.com/tutorials/game-engine-integrations/using-the-setdurable-parameter.html SetDurable is a function parameter in the Unity version of the GameSparks SDK. When you send a Request with .SetDurable(true) it will store your request in a queue to be sent to GameSparks in the event of your device being disconnected from the network.  This functionality enables your game to withstand random disconnects from the internet and allow a highscore to be posted after it was achieved, or a challenge turn to be taken and sent to GameSparks, once the connection has been restored.


Hope this helps

 - Steve


Answer

Hi Aleksey,


One way to achieving this would be to have an event in cloud code handle the transaction entirely. So for example you would pass it the Id of the two parties involved in the transaction, this event would process the transaction then return up to date values of each party. This way if an internet connection problem occurred no transaction would take place at all.


Another way of handling this could be to use SetDurable as documented here https://docs.gamesparks.com/tutorials/game-engine-integrations/using-the-setdurable-parameter.html SetDurable is a function parameter in the Unity version of the GameSparks SDK. When you send a Request with .SetDurable(true) it will store your request in a queue to be sent to GameSparks in the event of your device being disconnected from the network.  This functionality enables your game to withstand random disconnects from the internet and allow a highscore to be posted after it was achieved, or a challenge turn to be taken and sent to GameSparks, once the connection has been restored.


Hope this helps

 - Steve

Hi Steve,


Thank you for the detailed reply. I think I will use the 1st approach. 


SetDurable is OK, but if the following scenario happens, it may damage game experience:


Thief 1 has 1000 gold (on the client and on the server)

Thief 1 steals 500 gold from treasury (But connection lost. Client app sends durable request to tell the server about this theft)

Thief 1 has 1500 on the client-side now

Thief 1 buys some upgrade for 1300

Thief 1 has 200 now (still on the client side)


Now Thief 2 (another player) steals gold from Thief 1. But from his side Thief 1 still has 1000, so he steals all that amount.


In the end Thief 1 has -800 gold on the server.

Login to post a comment