If room is full create a new room

Hello I am new at photon, I am trying to create a new room if the existing room or rooms are full with a max of 2 players, I have a button with the function FindOpponent that creates a new room if it doesn't exist or joins a room, what I want to do is that when the room is full (with the max players that is 2) I want to create a new room, and to join it too, some help and advice please?
using Photon.Pun;
using Photon.Realtime;
using UnityEngine;
using UnityEngine.UI;

namespace PhotonMenu.Menus {

    public class MainMenu : MonoBehaviourPunCallbacks
    {
        [SerializeField] private GameObject playerNameInput = null;
        [SerializeField] private GameObject findOpponentPanel = null;
        [SerializeField] private GameObject waitingStatusPanel = null;
        [SerializeField] private Text waitingStatusText = null;

        private bool isConnecting = false;

        private const string GameVersion = "0.1";
        private const int MaxPlayerPerRoom = 2;

        private void Awake() => PhotonNetwork.AutomaticallySyncScene = true;

        public void FindOpponent() {

            findOpponentPanel.SetActive(false);
            waitingStatusPanel.SetActive(true);

            isConnecting = true;

            if (PhotonNetwork.IsConnected)
                {
                    PhotonNetwork.JoinRandomRoom();
                }
                else
                {
                    PhotonNetwork.GameVersion = GameVersion;
                    PhotonNetwork.ConnectUsingSettings();
                }
            
            //waitingStatusText.text = "Entrando a la Sala...";
        }


        public override void OnConnectedToMaster()
        {
            Debug.Log("Connected To Master");
         
                if (isConnecting) {
                PhotonNetwork.JoinRandomRoom();
                    
                }
            
        }

        public override void OnDisconnected(DisconnectCause cause)
        {
            waitingStatusPanel.SetActive(false);
            findOpponentPanel.SetActive(false);
            playerNameInput.SetActive(true);

            Debug.Log($"Disconnected due to: {cause}");
        }

        public override void OnJoinRandomFailed(short returnCode, string message)
        {
            
            Debug.Log("No clients are waiting for opponent, creating a new room");
            PhotonNetwork.JoinOrCreateRoom("Global", new RoomOptions { MaxPlayers = MaxPlayerPerRoom }, TypedLobby.Default);
            
        }

        public override void OnJoinedRoom()
        {
            
            Debug.Log("Client successfully joined a room");
            int playerCount = PhotonNetwork.CurrentRoom.PlayerCount;
            PhotonNetwork.LoadLevel("Scene_Main");
      
            //if (playerCount != MaxPlayerPerRoom)
            //{
            //    waitingStatusText.text = "Waiting for Opponent";
            //    Debug.Log("Client is waiting for an opponent");
            //}
            //else
            //{
            //    waitingStatusText.text = "Opponent Found";
            //    Debug.Log("Match is ready to begin");
            //}
        }
        public override void OnPlayerEnteredRoom(Player newPlayer)
        {
            if (PhotonNetwork.IsMasterClient)
            {
                if (PhotonNetwork.CurrentRoom.PlayerCount == PhotonNetwork.CurrentRoom.MaxPlayers)
                {
                    PhotonNetwork.CurrentRoom.IsOpen = false;
                    PhotonNetwork.CurrentRoom.IsVisible = false;

                    PhotonNetwork.LoadLevel("Scene_Main");
                }
            }
        }
        //public override void OnPlayerEnteredRoom(Player newPlayer)
        //{
        //    //if (PhotonNetwork.CurrentRoom.PlayerCount >= 1) {
        //    //    //PhotonNetwork.CurrentRoom.IsOpen = false;

        //    //    waitingStatusText.text = "Opponent Found";
        //    //    Debug.Log("Match is ready to begin");

        //        PhotonNetwork.LoadLevel("Scene_Main");
        //    //}
        //}

    }
}
Sign In or Register to comment.