Free up to 1MM monthly messages. No credit card required.
We’ve now covered both building a multiplayer game lobby with a chatroom and the different ways we can use matchmaking to connect two different users. Here’s what we’ve covered so far:
Matchmaking two random users is effective, but most modern games have skill based matchmaking systems that incorporate past experience, meaning that users are matched by their skill. Every user should have a rank or level that represents their skill. Users with a similar skill level play against eachother, so a high level user isn’t matched up against a n00b.
You’ll first need to sign up for a PubNub account. Once you sign up, you can get your unique PubNub keys in the PubNub Developer Portal. Once you have, clone the GitHub repository, and enter your unique PubNub keys on the PubNub initialization, for example:
Remember when we added the custom
uuid field to
pubnub.init()? We can attach extra “state” information in a similar way when a user subscribes to a channel.
Note: The state API is used to get or set key/value pairs specific to a subscriber uuid. State information is supplied as a JSON object of key/value pairs.
All we need to do is specify the
state property in our
pubnub.subscribe() call. Unlink the
uuid field and
state can be an object.
Even better, we can access user
state directly from the same
pubnub.here_now() call we used in the last post with PubNub Presence. All we need to do is set
state: true in the
data in the code above would look like:
In this part of our matchmaking algorithm, let’s begin by adding a
skill property to our users
state when they join. We’re going to continue working on the code from the previous posts.
We’ll start by using
Math.random() to generate a random skill (1 – 3) for the current client.
We’ll also display the skill level next to our own username in a Bootstrap
my_skill as a property of our
pubnub.subscribe as was mentioned earlier.
Now when the client subscribes, it will have a random skill level attached to it’s
state object. When we call
pubnub.here_now() we ask for the
state to be populated as well.
The callback will return a list of users and all of their
state objects including their
skilllevels. We can use this information to find a more accurate match.
Similar to our last post, we filter ourselves out of the list of users.
This time instead of removing items from the returned array of users, we build a new array. We loop through all the online users. If their skill level is the same as
my_skill we add them to the array of potential
Once we have a list of similarly skilled users, we find a random user from the array to match the other user against.
We can show the skill level of each user in the list of online users. The same
state object we defined earlier is returned as the
data property of the user object when the
presenceevent is fired from
We use Bootstrap’s list-group badge feature to easily add the skill level next to each username in a stylish way.
Check out the full example below, or check out the skill base matchmaking algorithm demo CodePen here:
In our next post, we’ll show you how to enable users to challenge each other. Stay tuned!
Take a look at the top trends that are the most effective in attracting customers and reducing churn, and how you can incorporate...
Comparing the major game engines: Unity vs Unreal Engine vs Corona SDK vs GameMaker Studio, including the benefits and cons of...
Sockets (aka socket programming) enable programs to send and receive data, bi-directionally, at any given moment. This tutorial...