Tutorial on Matchmaking in Multiplayer Games

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.

matchmaking Tutorial on Matchmaking in Multiplayer Games

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.

1
2
3
4
5
6
private void JoinRoomWithRandomPlayer()
{
    Dictionary  tableProperties = new Dictionary();
    tableProperties.Add("IsPrivateRoom", "false");
    WarpClient.GetInstance().JoinRoomWithProperties(tableProperties);
}

We will get the response in onJoinRoomDone of RoomRequestListener

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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.

1
2
3
4
5
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.

1
2
3
4
5
6
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.

twitter Tutorial on Matchmaking in Multiplayer Gamesfacebook Tutorial on Matchmaking in Multiplayer Gamesgoogle Tutorial on Matchmaking in Multiplayer Gameslinkedin Tutorial on Matchmaking in Multiplayer Gamespinterest Tutorial on Matchmaking in Multiplayer Gamesreddit Tutorial on Matchmaking in Multiplayer Gamesstumbleupon Tutorial on Matchmaking in Multiplayer Gamestumblr Tutorial on Matchmaking in Multiplayer Games

Leave a Reply

Your email address will not be published. Required fields are marked *


two + = 6

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>