Matchmaking recipe

You could solve this in many different ways. It usually depends on how you’ve designed your game and what user experience you want players to have.

The following recipe shows a simple method for a generic game so you can start fast.

When you’re comfortable with this method you’ll be able to add more complexity to it and adapt it to the needs of your game.

Match Random Users

In order to play with random players, we need a list of players that want to play against other random players.

  1. Make a list by creating a collection at the Dashboard where all users can read and write the entities it will contain.

    data-structure-for-turn-based-game

  2. When a user wants to play with a random user we need to:

    1. Check whether there are any users to play with in the collection.
    2. If you find one, delete the user who is waiting for a rival and then create a game.
    3. If not, add the user to the collection to wait for someone to play with.

Algorithm

Now we have a simple working algorithm.

// We search for someone in the collection that is not “ME” (the current user)
void OnRandomGame() {

  GamedoniaData.Search("randomcollection", "{'uid':{$ne:'currentuserid'}}", 1, OnObtainRandomGame);
}

void OnObtainGame(bool success, IList data) {

	if (success) {

		// If there is no one in the collection, we opt-in ourselves
		if ( (data == null) || (data.Count==0) ) {

			// Create the basic info
			Dictionary<string,object> randomGame = new Dictionary<string, object>();
			randomGame.Add('nickname',GamedoniaUsers.me.profile["nickname"].ToString());
			randomGame.Add('uid', GamedoniaUsers.me._id);

			// Save my info to the collection
			GamedoniaData.Create("randomcollection", randomGame, OnSignForRandomGame);

		} else {

			// If there is someone waiting, we create the Game
			Game newGame = new Game();
			newGame.setUser1(((IDictionary) data[0])['uid'].ToString()); // The first person found on the collection
			newGame.setUser2(GamedoniaUsers.me._id); // Myself
			newGame.setState('Playing');

			// Save the game into the games collection
			GamedoniaData.Create("currentgames", newGame, OnCreateGame);
		}
	}
}

Other alternatives

Once you’re used to this method, you may want to try a few variations to add a little more complexity:

  • All users are added to the random collection by default, but they can opt-out of it by a setting option.
  • When a user opts-in for the first time, the fact of starting a random game won’t delete him from the collection.