AppWarp’s support for realtime turn-based games eliminates the client-side effort required to do timer management, turn management and state management. All such management is done by AppWarp Cloud. Server sends the notifications to client when a move is made or a player’s turn expires. Previously the turns were allotted in a fixed order (in the order in which the user joins the room) and could not be altered from the client but now developer has the power to take this decision with the new or updated APIs available. This is a modification to older APIs and are backward compatible. Though, developers can still use the predefined turn logic.
Following are the new modified APIs
- startGame
- sendMove
A new API has also been added
- setNextTurn
All these API are discussed as follows
startGame(boolean isDefaultLogic, String nextTurn)
startGame now has two new parameters. The first one is isDefaultLogic which describes whether the developer wants us to implement the default turn logic or will he be supplying his own turn logic. isDefaulLogic if set as false will turn off the default logic. The other parameter is nextTurn. This describes the name of the player who is going to have the first turn. If nextTurn is undefined, the caller of startGame method becomes the first player to make a turn. Both parameters are optional making it not only backward compatible but also allowing developers to opt out for setting custom turn logic instead using the predefined logic.
sendMove(String moveData, String nextTurn)
sendMove now accepts one more parameter other than moveData and i.e. nextTurn. This nextTurn parameter is very important because this is what allows developers to set custom turn logic. While making a move, the player will tell who will have the next turn. Obviously this has to be set programmatically by the logic defined in the game’s code. Just like startGame, the nextTurn parameter is optional. The developer has to use it only if isDefaultLogic was set to false in startGame method.
setNextTurn(String nextTurn)
setNextTurn is a completely new API. The next turn has to be passed in sendMove method. But what if the player who has the current turn fails to call the sendMove method? Who will have the next turn? Whenever there is a condition where client fails to set the next turn, our server chooses one client and sends him onNextTurnRequest notification in Notify Listener. Here, the developer has to call this setNextTurn method with the name of the player whose next turn has to be set. Again, this has to be set programmatically by the logic defined in the game’s code just like sendMove. onNextTurnRequest will contain the name of the last player helping developers to decide the next turn.
All the listeners related to startGame and sendMove have remained unchanged.
Steps to Implement Custom Turn Logic
- Call startGame with isDefaultLogic false and optional name for first player.
WarpClient.getInstance().startGame(false, “user1”);
- On onGameStarted callback, the player with first turn has to call sendMove method with move data and next turn. To decide the next turn, the developer should locally maintain a list of players and update the list when anyone joins or leaves the room. Additionally, the developer can also call getLiveRoomInfo to get the list of players in the room. Once the next turn has been decided, call the sendMove
WarpClient.getInstance().sendMove(“hey”, “user3”);
- In case the player failed to call sendMove data; for example loss of connection or time turned out, one random client will be chosen by our Server’s Logic. That player will receive onNextTurnRequest callback in NotifyListern. Here call the setNextTurn method
public void onNextTurnRequest(String lastTurn){ WarpClient.getInstance().setNextTurn(“user5”); }
Sample
We have developed a sample to showcase our new APIs. Have a look here https://github.com/SuyashMShepHertz/AppWarp_NewTurnAPIs_Sample
Conclusion
Developers can now set custom logic for selecting the next turn in turn-based games while using AppWarp. The new APIs are not only easy to implement but also backward compatible. The latest SDK i.e. version 1.10 contains all these new APIs.
Leave A Reply