question

iglzsr avatar image
iglzsr asked

Matchmaking Unity, PlayFab and Mirror

Hi, im trying to make a simple multiplayer card game in Unity using PlayFab for the matchmaking, and mirror for the connections. I have read this is an appropiate way to do it, but its my first multiplayer game attempt and probably there is a better way to do it.

The actual question is that following some tutorials ive been able to make a matchmaking ticket with the players, but that ticket does not have the server ip nor the port. How can I achieve that?

I'll leave the code of the class here, and the whole project on a zip.

Thanks

using System.Collections;
using UnityEngine;
using PlayFab;
using PlayFab.MultiplayerModels;
using TMPro;
using Mirror;


public class Matchmaker : MonoBehaviour
{
    [SerializeField] private GameObject playButton;
    [SerializeField] private GameObject leaveQueueButton;
    [SerializeField] private TMP_Text queueStatusText;
    [SerializeField] NetworkManager networkManager;


    private string ticketId;
    private Coroutine pollTicketCoroutine;


    private static string QueueName = "Normal";


    public void StartMatchmaking()
    {
        playButton.SetActive(false);
        queueStatusText.text = "Submitting Ticket";
        queueStatusText.gameObject.SetActive(true);


        PlayFabMultiplayerAPI.CreateMatchmakingTicket(
            new CreateMatchmakingTicketRequest
            {
                Creator = new MatchmakingPlayer
                {
                    Entity = new EntityKey
                    {
                        Id = PlayFabLogin.EntityId,
                        Type = "title_player_account",
                    },
                    Attributes = new MatchmakingPlayerAttributes
                    {
                        DataObject = new { }
                    }
                },


                GiveUpAfterSeconds = 120,


                QueueName = QueueName
            },
            OnMatchmakingTicketCreated,
            OnMatchmakingError
        );
    }


    public void LeaveQueue()
    {
        leaveQueueButton.SetActive(false);
        queueStatusText.gameObject.SetActive(false);


        PlayFabMultiplayerAPI.CancelMatchmakingTicket(
            new CancelMatchmakingTicketRequest
            {
                QueueName = QueueName,
                TicketId = ticketId
            },
            OnTicketCanceled,
            OnMatchmakingError
        );
    }


    private void OnTicketCanceled(CancelMatchmakingTicketResult result)
    {
        playButton.SetActive(true);
    }


    private void OnMatchmakingTicketCreated(CreateMatchmakingTicketResult result)
    {
        ticketId = result.TicketId;
        pollTicketCoroutine = StartCoroutine(PollTicket(result.TicketId));


        leaveQueueButton.SetActive(true);
        queueStatusText.text = "Ticket Created";
    }


    private void OnMatchmakingError(PlayFabError error)
    {
        Debug.LogError(error.GenerateErrorReport());
    }


    private IEnumerator PollTicket(string ticketId)
    {
        while (true)
        {
            PlayFabMultiplayerAPI.GetMatchmakingTicket(
                new GetMatchmakingTicketRequest
                {
                    TicketId = ticketId,
                    QueueName = QueueName
                },
                OnGetMatchMakingTicket,
                OnMatchmakingError
            );


            yield return new WaitForSeconds(1);
        }
    }


    private void OnGetMatchMakingTicket(GetMatchmakingTicketResult result)
    {
        queueStatusText.text = $"Status: {result.Status}";


        switch (result.Status)
        {
            case "Matched":
                StopCoroutine(pollTicketCoroutine);
                StartMatch(result.MatchId);
                break;
            case "Canceled":
                StopCoroutine(pollTicketCoroutine);
                leaveQueueButton.SetActive(false);
                queueStatusText.gameObject.SetActive(false);
                playButton.SetActive(true);
                break;
        }
    }


    private void StartMatch(string matchId)
    {
        queueStatusText.text = $"Starting Match";


        PlayFabMultiplayerAPI.GetMatch(
            new GetMatchRequest
            {
                MatchId = matchId,
                QueueName = QueueName
            },
            OnGetMatch,
            OnMatchmakingError
        );
    }


    private void OnGetMatch(GetMatchResult result)
    {
        queueStatusText.text = $"{result.Members[0].Entity.Id} vs {result.Members[1].Entity.Id}";
        Debug.Log("Ip del Servidor:" + result.ServerDetails.IPV4Address +":" + result.ServerDetails.Ports[0].Num);
        //configure mirror network
        var transport = Transport.activeTransport as TelepathyTransport;
        transport.port = (ushort)result.ServerDetails.Ports[0].Num;
        networkManager.networkAddress = result.ServerDetails.IPV4Address;


        Debug.Log($"Connecting to {networkManager.networkAddress}:{result.ServerDetails.Ports[0].Num}");
        Debug.Log($"Connecting to {networkManager.networkAddress}:{result.ServerDetails.Ports[0].Num}");


        networkManager.StartClient();
    }
}


Matchmaking
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Ivan Cai avatar image
Ivan Cai answered

When you create and configure the matchmaking queue, please enable the checkbox for Enable server allocation. In this way, once there is a match, the matchmaking process will allocate a server for players. Then the players can get the server's ip and port via calling GetMatch API. If you want to learn more about it, please refer to https://docs.microsoft.com/en-us/gaming/playfab/features/multiplayer/matchmaking/multiplayer-servers#activating-server-allocation-for-the-queue.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.