// Calls the matchmaking client to do matchmaking against the backend and then connects to the game server with TCP
    public IEnumerator DoMatchMakingAndConnect()
    {
        Debug.Log("Request matchmaking...");
        GameObject.FindObjectOfType <UIManager>().SetTextBox("Requesting matchmaking...");
        yield return(null);

        var matchMakingRequestInfo = this.matchmakingClient.RequestMatchMaking();

        Debug.Log("TicketId: " + matchMakingRequestInfo.TicketId);

        if (matchMakingRequestInfo != null)
        {
            bool matchmakingDone = false;
            int  tries           = 0;
            while (!matchmakingDone)
            {
                Debug.Log("Checking match status...");
                GameObject.FindObjectOfType <UIManager>().SetTextBox("Checking match status...");
                yield return(null);

                this.matchStatusInfo = this.matchmakingClient.RequestMatchStatus(matchMakingRequestInfo.TicketId);
                if (matchStatusInfo.PlayerSessionId.Equals("NotPlacedYet"))
                {
                    Debug.Log("Still waiting for placement");
                    GameObject.FindObjectOfType <UIManager>().SetTextBox("Still waiting for placement...");
                    yield return(new WaitForSeconds(1.0f));
                }
                else
                {
                    Debug.Log("Matchmaking done!");
                    GameObject.FindObjectOfType <UIManager>().SetTextBox("Matchmaking done! Connecting to server...");
                    yield return(null);

                    matchmakingDone = true;

                    // Matchmaking done, connect to the servers
                    Connect();
                }
                tries++;

                // Return null if we failed after 20 tries
                if (tries >= 20)
                {
                    GameObject.FindObjectOfType <UIManager>().SetTextBox("Aborting matchmaking, no match done on 20 seconds");
                    Debug.Log("Aborting matchmaking, no match done on 20 seconds");
                    yield return(null);

                    break;
                }
                yield return(null);
            }
        }
        else
        {
            GameObject.FindObjectOfType <UIManager>().SetTextBox("Matchmaking failed! Not connected.");
            Debug.Log("Matchmaking request failed!");
        }

        yield return(null);
    }
示例#2
0
 // Checks the status of a matchmaking request ticket
 public MatchStatusInfo RequestMatchStatus(string ticketId)
 {
     try
     {
         //Make the signed request and wait for max 10 seconds to complete
         var response = Task.Run(() => this.SendSignedGetRequest(this.client.apiEndpoint + "requestmatchstatus?ticketId=" + ticketId));
         response.Wait(10000);
         string          jsonResponse = response.Result;
         MatchStatusInfo info         = JsonUtility.FromJson <MatchStatusInfo>(jsonResponse);
         return(info);
     }
     catch (Exception e)
     {
         Debug.Log(e.Message);
         return(null);
     }
 }