using System;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
namespace UnityEngine.Ucg.Matchmaking
{
class MatchmakingClient
{
internal string Url { get; }
const string k_CreateRequestEndpoint = "/mmrequest";
const string k_GetAssignmentEndpoint = "/mmassigment";
const string k_ApiVersion = "1";
internal MatchmakingClient(string endpoint)
{
Url = "http://" + endpoint + "/v" + k_ApiVersion;
}
///
/// Start matchmaking for a provided request. This tells your matchmaking endpoint to add
/// the players and group data in the request to the matchmaking pool for consideration
///
/// The matchmaking request
/// An asynchronous operation that can be used in various async flow patterns.
/// The webrequest inside will contain a json success object
/// TODO: Strongly type expect contract return from successful call
internal UnityWebRequestAsyncOperation RequestMatchAsync(MatchmakingRequest request)
{
string url = Url + k_CreateRequestEndpoint;
UnityWebRequest webRequest = new UnityWebRequest(url, "POST");
webRequest.SetRequestHeader("Content-Type", "application/json");
string txtRec = JsonUtility.ToJson(request);
byte[] jsonToSend = new UTF8Encoding().GetBytes(txtRec);
webRequest.uploadHandler = new UploadHandlerRaw(jsonToSend);
webRequest.downloadHandler = new DownloadHandlerBuffer();
Debug.Log("Calling... " + url + " " + txtRec);
return webRequest.SendWebRequest();
}
///
/// Retrieve the assignment for a given player. This call will perform a long GET while listening for
/// matchmaking results
///
/// The id of a player
/// An asynchronous operation that can be used in various async flow patterns.
/// The webrequest inside will contain a json connection string object
/// TODO: Strongly type expect contract return from successful call
internal UnityWebRequestAsyncOperation GetAssignmentAsync(string id)
{
string url = Url + k_GetAssignmentEndpoint + "?id=" + id;
UnityWebRequest webRequest = new UnityWebRequest(url, "POST");
webRequest.SetRequestHeader("Content-Type", "application/json");
webRequest.downloadHandler = new DownloadHandlerBuffer();
Debug.Log("Calling... " + url);
return webRequest.SendWebRequest();
}
}
}