Matchmaking according to the selected category

Options
I am making a game like QuizUp ( a quiz based game ), in which users can play with other online users. I have successfully implemented the creating and joining room using the documentation available.

The problem is that I have multiple categories for quizes like General Knowlege, Games, Sports, History etc. I want to make it work such that when a user selects any of the available categories then a room is created and if any other user from other part of world selects the same category, only then a match between the two is started.

For example, I selected sports category and created a room. Another player say "x" from some other region also selected sports category and only then we can play against each other.

Currently what I have achieved is that even if two users select different categories, still they are connected to a same room and match is started between the two which is obviously not what I am trying to implement.

What I did is to create separate rooms with the name of selected category.
		PhotonNetwork.CreateRoom(categorySelected, new RoomOptions() { MaxPlayers = 2, PlayerTtl = 20000 }, null);
I have searched on google for hours and found some helpful documents but no clear answer was available. I am a newbie in photon so I might be missing something very obvious.

For reference, below is the code I am using to create and join rooms:
using System;
using Photon;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

using ExitGames.Client.Photon;

public class photon_connectscript : PunBehaviour {



    public GameObject connectbutton, loadingbar;
    public Text statetext;

	public InputField InputField;
    public string UserId;

	string categoryselectedKey = "Multiplayer_Category";
	const string NickNamePlayerPrefsKey = "NickName";


	private string categorySelected;

    private const string MainSceneName = "multiplayer_startscene";


	void Start()
	{
        loadingbar.SetActive(false);
		statetext.text = "";
		categorySelected = PlayerPrefs.GetString (categoryselectedKey, "");

		if (PlayerPrefs.HasKey (NickNamePlayerPrefsKey)) {
			InputField.text = PlayerPrefs.GetString (NickNamePlayerPrefsKey);
		} else
			InputField.text = "";
		}

	void Update(){

		if (Input.GetKey (KeyCode.Escape))
			SceneManager.LoadScene ("multiplayer_category_select");
	}


    public void ApplyUserIdAndConnect()
    {
        loadingbar.SetActive(true);
    	statetext.text = "Connecting to Game Servers ...";
    	connectbutton.SetActive(false);
     
        string nickName = "Player";
        if (this.InputField != null && !string.IsNullOrEmpty(this.InputField.text))
        {
            nickName = this.InputField.text;
			PlayerPrefs.SetString(NickNamePlayerPrefsKey,nickName);
        }
        
    
      if (PhotonNetwork.AuthValues == null)
        {
            PhotonNetwork.AuthValues = new AuthenticationValues();
        }
		PhotonNetwork.AuthValues.UserId = nickName;

		Debug.Log("Nickname: " + nickName + " userID: " + this.UserId,this);
		

        PhotonNetwork.playerName = nickName;
        PhotonNetwork.ConnectUsingSettings("0.5");
        
        // this way we can force timeouts by pausing the client (in editor)
        PhotonHandler.StopFallbackSendAckThread();
    }

    public override void OnJoinedLobby()
    {
        OnConnectedToMaster(); // this way, it does not matter if we join a lobby or not
    }

    public override void OnConnectedToMaster()
    {
        // after connect 
        this.UserId = PhotonNetwork.player.UserId;
        PhotonNetwork.JoinRandomRoom();
        }

   

    public override void OnPhotonRandomJoinFailed(object[] codeAndMsg)
    {
		Debug.Log("OnPhotonRandomJoinFailed");

		//Specify room details here.

		// ------------------ IMPORTANT: The room name is string categorySelected so that players get 
		//----------------------matched into respective categories

		PhotonNetwork.CreateRoom(categorySelected, new RoomOptions() { MaxPlayers = 2, PlayerTtl = 20000 }, null);

    }

    public override void OnJoinedRoom()
    {
    	Debug.Log("Joined room: " + PhotonNetwork.room.Name);

		// ------------------ IMPORTANT: The scene names for each category should be named as "multiplayer_mcq_general" or 
		// ------------------- "multiplayer_tf_games" etc.

		SceneManager.LoadScene(categorySelected);

    }

    public override void OnPhotonJoinRoomFailed(object[] codeAndMsg)
    {
		Debug.Log("OnPhotonJoinRoomFailed");
      
    }

    public override void OnConnectionFail(DisconnectCause cause)
    {
        statetext.text = "Cannot connect. Please check internet connection";
        connectbutton.SetActive(true);
    }
	
	public override void OnPhotonPlayerActivityChanged(PhotonPlayer otherPlayer)
	{
		Debug.Log("OnPhotonPlayerActivityChanged() for "+otherPlayer.NickName+" IsInactive: "+otherPlayer.IsInactive);
	}

}

Any kind of help Will be highly appreciated.

Thanks

Comments

  • Hi @Usama,

    if you haven't done this already, I would recommend you taking a look at the Matchmaking Guide. It has an example about how to use custom room properties for matchmaking which I think will help you. You can find this in the section 'Not So Random Matchmaking'.
  • Usama
    Usama
    edited February 2018
    Options
    Hi @Christian_Simon
    Thanks for the quick reply. I have already implemented Matchmaking Guide and it works fine but there is still one issue I am facing.

    In customRoomProperties I am using only one property like below:
    
    RoomOptions roomOptions = new RoomOptions ();
    		roomOptions.MaxPlayers = 2;
    		roomOptions.CustomRoomProperties = new ExitGames.Client.Photon.Hashtable();
    		roomOptions.CustomRoomProperties.Add ("CT", categorySelected);
    		PhotonNetwork.JoinOrCreateRoom ("MCQ", roomOptions, null);
    
    Here CT refers to Category and categorySelected is a variable String which changes according to which category user has selected. For example if user Selected "general Knowlege" category, then categorySelected will be GK and so on.

    Using this code I am able to create a separate room with custom properties and max 2 players at a time.

    But, here is the problem. Suppose two players are already playing in a room for category lets Say "sports". Now if a third user selects the same category i.e. "sports" and tries to create a room with this property, the Photon will give an error because another room with same name and same properties already exists.

    How can I solve this problem? I hope I am being clear with what I am trying to achieve

    Thanks in advance
  • The room name has to be unique. Means there can be only one room with the same name at a time. If the room name doesn't represent the category in any way, you can try to use some kind of a random room name for example.