We are very excited to have introduced AppWarp to our growing line of cloud products. You can build a range of multiplayer games from turn based games such as poker, scrabble and pool to advanced real-time games like football, car racing and counter strike. Within the App42 cloud ecosystem, we also provide APIs for common gaming app requirements such as user authentication, storage and leader boards.
This means you can build your entire app with just client side code and App42 will manage all your server side needs! Isn’t that just great?
In this post, we will give an overview on how you can create a real-time multiplayer game taking the classic Tic-Tac-Toe game that everyone is familiar with as an example. The concepts described can be easily incorporated and extended for other games. We will only illustrate the key API calls that will need to be made here – complete Android source code for this game is available for download.
Basic Game Setup
1. Creating a game room
The primary requirement for any real-time, multiplayer game is an online room where users can come and interact with each other. Creating such a virtual room is a single API call in which you can specify the room’s static information (name, owner and maximum users allowed to join). In our case, let’s say a user with name “Dan” is logged in to the AppWarp server. The API call would be
WarpClientInstance.createRoom(“Dan’s Room”, “Dan”, 2);
Once a room has been created, onCreateRoomDone will be invoked on the Zone listener interface with the id (eg: “4891564”) of the room created. You can now add your custom logic to the game by initializing the room’s custom information data. In our case this will contain a simple JSON representing the room’s state.
WarpClientInstance.setCustomRoomData(“4891564”, “board:eeeeeeee , turn:Dan, level:beginner”);
This will associate the given JSON with the room allowing users who join the room later to draw the UI based on the custom Information.
Once the room has been setup, you can go ahead and join the room by simply calling
WarpClientInstance.joinRoom(“4891564”);
2. Finding a room to join
Most users of your multi-player game will not need to create a new room in order to start playing. They will want to see a list of available rooms and simply join one of them to start playing. This can be done as follows
WarpClientInstance.getAllRooms();
The response of this will give you the room ids of all the rooms that are there on the server. You can then query live information for each room and build a list of available rooms for the user. This can be retrieved by calling
WarpClientInstance.getLiveRoomInfo(“4891564”);
The response of this will tell you the live custom data associated with the room and the number of users joined in the room. In our example we will list only those rooms with a single joined user in the list of available game rooms for joining. Upon user selection, you can go ahead and join the room
WarpClientInstance.joinRoom(“4891564”);
Upon joining the room, it is recommended you also subscribe to the room. This will mean that you will start receiving updates from the room such as chat message, user join/leave notifications and custom updates sent by other users. This is important for Game Play described in the next section and can be done by making the following API call
WarpClientInstance.subscribeRoom(“4891564”);
3. Game Play
Once multiple users have joined a room, your game play can start. For this you will find the the updatePeers API very useful. This API gives you the ability to broadcast a custom update message to all the users that have subscribed to the room. This can be used to send updates when a user moves piece on a board game or to send the local player’s coordinates to other players in the room (in a racing or combat game for example). In our case we will do the following when Dan selects a tile to move on the tic-tac-toe board. Assuming he selected the first tile on the board.
WarpClientInstance.sendUpdatePeers(“board”, “xeeeeeeee”);
Depending on your requirement, you may also want to update the customData set on the room (to allow users who join late to sync with the current state of the game).
To receive such custom updates, a user must have subscribed to the game as described in the earlier section. Upon receipt of an update event, you can update the UI of your game to reflect the move made by the remote player/opponent.
@Override
public void onUpdatePeersReceived(UpdateEvent event) {
byte[] update = event.getUpdate();
//parse update as JSON and change UI state.
}
Once your game is over, it is recommended you delete the room. This is useful as it avoids leaving stale rooms in your game zone, thus reducing the number of rooms you queried in section 2 (for finding a suitable room).
Advanced Features
Once you have a basic game play complete, you will want to add other advanced features described next.
1. Chat
Users who have joined a room and are playing a game can send real-time chat messages to each other. This makes your game engaging and fun for your users. To receive chat messages from a room, the user must be subscribed to that room as described earlier. A user can send a chat message to a room in which he/she is currently joined simply by calling
WarpClientInstance.sendChat(“Hi fellows!”);
You don’t need to specify the room id here as AppWarp server knows the user’s location and a user can be joined only in one room at a time. If the user has not joined a room yet, this message will not be delivered anywhere and an error will be returned in the resulting response callback.
2. Spectator
This is a useful feature if you are building a social game and want to allow users to watch games being played by their friends for example. You can do this by simply subscribing to the desired room (as described earlier) without actually joining it. This will allow your user to view updates from the room and build the UI similar to the joined users. The difference being that such spectators can’t participate in the game room (i.e. send updatePeers or Chat messages).
3. See online friends
If you are building a social game, it is very useful to show your game’s user his/her online buddies. This can be accomplished in 2 ways. One is to fetch all the users that are online by simply calling
WarpClientInstance.getOnlineUsers();
The result of which is given in onGetOnlineUsers of your zonelistener. This will contain the user names of all the users that are online at the moment. You can then match this with your user’s friend list. Another way is to query each user from your buddy list to see if it’s online. This can be done by
WarpClientInstance.getLiveUserInfo(“Selena”);
If user “Selena” is indeed online, the result of the request will be successful and will also tell you which room Selena is currently playing in (or the Lobby if she is waiting).
Integrating with the App42 ecosystem
Now that you have completed your game play, you would need to complete your application by adding other required functionality like user authentication, user session management, leader boards etc. You are free to do these out of band using other providers or your own implementation. However it is recommended that you use App42 cloud APIs for this as they are extremely powerful, simple to use and you remain within the same ecosystem (re-use your AppWarp API key), simplifying your development.
1. Authentication
It is important to note that AppWarp server does not manage user accounts. It simply requires that every user who is concurrently connected to your game must have a unique username. We recommend you use the simple App42 cloud User API to fulfil this requirement and other user account management tasks. Here is a simple code snippet for signing in.
App42Response response = userService.authenticate(“Dan”, “**password**”);
If(response.isResponseSuccess()){
WarpClientInstance.JoinZone(“Dan”);
}
2. Scoring and Rewards
Most multiplayer games incorporate leader boards in their games. This is a common way of making games more engaging and keeping your users interested. AppWarp recommends you use App42 cloud gaming API for managing user scores, leader boards and rewards.
3. Storage
In some cases you want to save certain data related to your app, for example to save user profile photos, game replays, chat history etc. It can be cumbersome to maintain this on the user’s device and a cloud solution works best. For such and other custom storage requirements you can use App42 cloud storage APIs.
Leave A Reply