AppWarp Gaming Engine provides a comprehensive set of match making APIs to developers. Using these you can add modes in your game where players can play against random opponents or their friends. In this post we will illustrate how to do these using the multiplayer Catapult Wars sample game that I described in my previous post.
While creating a room, we need to add a room-property “IsPrivateRoom”. If this flag is true, it means the room is created for players known to each other and not for random game play. If its false, then random players can join it.
Process to start/join a game against a random player used in Catapult Wars :
First off, the client will call the JoinRoom with properties API (“IsPrivateRoom”, “false”). If Join Room is successful – we are inside the random room and ready to play. If this join fails, it means that there is currently no random room available and the client needs to create a new room, join that and wait for another player to join in.
private void JoinRoomWithRandomPlayer()
{
Dictionary tableProperties = new Dictionary();
tableProperties.Add("IsPrivateRoom", "false");
WarpClient.GetInstance().JoinRoomWithProperties(tableProperties);
}
We will get the response in onJoinRoomDone of RoomRequestListener
public void onJoinRoomDone(RoomEvent eventObj)
{
if (eventObj.getResult() == WarpResponseResultCode.SUCCESS)
{
// reset the local global properties as we are starting a new game play session
GlobalContext.tableProperties["Player1Score"]=0;
GlobalContext.tableProperties["Player2Score"]=0;
GlobalContext.tableProperties["WindX"]=0;
GlobalContext.tableProperties["WindY"]=0;
GlobalContext.tableProperties["fireNumber"]=1;
GlobalContext.GameRoomId = eventObj.getData().getId();
WarpClient.GetInstance().SubscribeRoom(GlobalContext.GameRoomId);
// get live information to fetch the name of the opponent if already inside
WarpClient.GetInstance().GetLiveRoomInfo(GlobalContext.GameRoomId);
}
else
{
try
{
if (GlobalContext.tableProperties["IsPrivateRoom"].ToString().Equals("true",StringComparison.InvariantCultureIgnoreCase))
{
// failed to join a private room
Deployment.Current.Dispatcher.BeginInvoke(newShowResultCallback(mShowResultCallback), "Sorry, the given user is not available");
}
else
{
// failed to join a random room, create a new one
WarpClient.GetInstance().CreateRoom(GlobalContext.roomName, GlobalContext.localUsername, 2, GlobalContext.tableProperties);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
}
When we get the response of “CreateRoom” in onCreateRoomDone method we will go ahead and join it.
public void onCreateRoomDone(RoomEvent eventObj)
{
GlobalContext.GameRoomId = eventObj.getData().getId();
WarpClient.GetInstance().JoinRoom(eventObj.getData().getId());
}
Process to start a game with a friend in Catapult Wars (MainPage.xaml.cs, ZoneRequestListener.cs)
User can start a game with a known player (friend) if he knows his username used to connect with AppWarp.
First we will need to make a request to get the current online status and location of the friend.
private void JoinRoomWithRemoteUserID()
{
AddListeners();
//here we are looking for location-ID by his username
WarpClient.GetInstance().GetLiveUserInfo(remoteUsername.Text);
}
We will get the response in onGetLiveUserInfoDone of ZoneRequestListener
public void onGetLiveUserInfoDone(LiveUserInfoEvent eventObj)
{
if ((eventObj.getResult() == WarpResponseResultCode.SUCCESS) && (eventObj.getLocationId()!=null))
{
// Join the room where the friend is playing
GlobalContext.GameRoomId = eventObj.getLocationId();
WarpClient.GetInstance().JoinRoom(GlobalContext.GameRoomId);
}
else
{
// remote user is either off line or has not joined any room yet. Create one and wait for him.
GlobalContext.tableProperties["IsPrivateRoom"]="true";
WarpClient.GetInstance().CreateRoom(GlobalContext.localUsername, GlobalContext.localUsername, 2, GlobalContext.tableProperties);
}
}
Source Code can be downloaded or viewed from our Git repo. If you have any questions or need any further assistance, please feel free to write us at support@shephertz.com.
Leave A Reply