201 lines
7.5 KiB
C#
201 lines
7.5 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
public class CupidLobby : MonoBehaviour
|
|
{
|
|
|
|
public const bool saveUsername = false;
|
|
private static string m_username = "";
|
|
|
|
[SerializeField]private GameObject MatchmakingUI;
|
|
[SerializeField]private string GameScene;
|
|
[Header("Server info (matchmaker port should match server settings.json)")]
|
|
[Tooltip("IP/hostname uses HTTP and cupidPort below. Or full URL: https://match.example.com (default 443, no :port in URL) or http://host/path.")]
|
|
[SerializeField]private string serverAddress = "xx.xx.xxx.xx";
|
|
[Tooltip("HTTP port when server address is not a full URL. Ignored for https:// or http:// URLs unless the URL includes an explicit port.")]
|
|
[SerializeField]private int cupidPort = 2612;
|
|
[Tooltip("Sent as the password query param on GET /settings (matchmaker settings.json).")]
|
|
[SerializeField]private string cupidPassword = "HelloWorld";
|
|
[Tooltip("Used when LoginManager is absent or has no token (e.g. Cupid sample scene).")]
|
|
[SerializeField]private string bearerTokenFallback = "";
|
|
|
|
public static string Username
|
|
{
|
|
get
|
|
{
|
|
if(m_username == ""){
|
|
m_username = Cupid.RandomUsername;
|
|
}
|
|
|
|
if(saveUsername){
|
|
if (PlayerPrefs.HasKey("username"))
|
|
{
|
|
return PlayerPrefs.GetString("username");
|
|
}else{
|
|
string username = Cupid.RandomUsername;
|
|
PlayerPrefs.SetString("username",username);
|
|
PlayerPrefs.Save();
|
|
|
|
return username;
|
|
}
|
|
}else{
|
|
return m_username;
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|
|
public static CupidLobby instance;
|
|
void Awake(){
|
|
instance=this;
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
string token = !string.IsNullOrEmpty(LoginManager.AuthToken)
|
|
? LoginManager.AuthToken
|
|
: bearerTokenFallback;
|
|
if (LoginManager.instance != null && LoginManager.CurrentUser != null && !string.IsNullOrEmpty(LoginManager.CurrentUser.username))
|
|
m_username = LoginManager.CurrentUser.username;
|
|
Cupid.Init(serverAddress, cupidPort, token, cupidPassword);
|
|
}
|
|
bool matchmaking = false;
|
|
public void Matchmake()
|
|
{
|
|
LoginManager.ClearMatchYourTeam();
|
|
Logger.Log("Starting matchmake as " + Username);
|
|
StartCoroutine(matchmake());
|
|
}
|
|
|
|
IEnumerator matchmake()
|
|
{
|
|
matchmaking = true;
|
|
if (string.IsNullOrEmpty(Cupid.BearerToken))
|
|
{
|
|
Logger.Log("Matchmake aborted: no bearer token");
|
|
matchmaking = false;
|
|
RefreshMatchmakingPanel();
|
|
yield break;
|
|
}
|
|
|
|
while (matchmaking)
|
|
{
|
|
RefreshMatchmakingPanel();
|
|
|
|
using (UnityWebRequest req = UnityWebRequest.Get(Cupid.CupidURI + "/"))
|
|
{
|
|
req.downloadHandler = new DownloadHandlerBuffer();
|
|
req.SetRequestHeader("Authorization", "Bearer " + Cupid.BearerToken);
|
|
yield return req.SendWebRequest();
|
|
|
|
string text = req.downloadHandler != null ? req.downloadHandler.text : "";
|
|
if (req.result != UnityWebRequest.Result.Success)
|
|
{
|
|
Logger.Log("Matchmaker poll failed: " + req.error + " body: " + text);
|
|
yield return new WaitForSeconds(1);
|
|
continue;
|
|
}
|
|
|
|
Team? teamHint = LoginManager.MatchYourTeam;
|
|
if (!teamHint.HasValue && GameManager.instance != null)
|
|
teamHint = GameManager.MyTeam;
|
|
int userId = LoginManager.CurrentUser != null ? LoginManager.CurrentUser.id : 0;
|
|
|
|
CupidRoom? room = Cupid.ParseRoom(text, userId, teamHint);
|
|
if (room == null)
|
|
{
|
|
if (text != null && text.Trim() != "0")
|
|
Logger.Log("Still waiting: " + text);
|
|
}
|
|
else
|
|
{
|
|
CupidRoom _room = (CupidRoom)room;
|
|
Logger.Log("Got into a room");
|
|
Logger.Log(text);
|
|
MatchmadeResponse env = JsonUtility.FromJson<MatchmadeResponse>(text);
|
|
MatchmadeTeamPayload branch = MatchmadeResponse.SelectTeamPayload(env, userId, teamHint);
|
|
if (branch == null || !MatchmadeResponse.TryResolveMyAndOpponent(env, branch, userId, out MatchmadePlayer myPlayer, out MatchmadePlayer opponentPlayer))
|
|
{
|
|
Debug.Log("Waiting for the other player to show up (could not resolve roster)");
|
|
yield return new WaitForSeconds(1);
|
|
continue;
|
|
}
|
|
|
|
string yt = (branch.your_team ?? "").Trim().ToLowerInvariant();
|
|
Team myTeam = yt == "blue" ? Team.Blue : Team.Red;
|
|
|
|
if (myTeam == Team.Red)
|
|
{
|
|
GameManager.RedPlayer = myPlayer;
|
|
GameManager.BluePlayer = opponentPlayer;
|
|
}
|
|
else
|
|
{
|
|
GameManager.BluePlayer = myPlayer;
|
|
GameManager.RedPlayer = opponentPlayer;
|
|
}
|
|
GameManager.MyTeam = myTeam;
|
|
|
|
int rcPrizeCoins = env.rc_prize > 0 ? env.rc_prize : branch.rc_prize;
|
|
GameManager.MatchRcPrizeCoins = rcPrizeCoins;
|
|
|
|
LevelLoadManager.instance.SetupMatchMade(myTeam, myPlayer, opponentPlayer, CoinHelper.FormatString(rcPrizeCoins) + " RC");
|
|
GameManager.ConfigureDedicatedMatchReporting(branch.match_id);
|
|
Logger.Log("Setting cupid to load game scene");
|
|
Cupid.RoomPort = _room.Port;
|
|
LoginManager.SetMatchYourTeam(_room.your_team);
|
|
matchmaking = false;
|
|
Logger.Log("Loading game scene");
|
|
LevelLoadManager.LoadLevel(GameScene);
|
|
yield break;
|
|
}
|
|
}
|
|
|
|
yield return new WaitForSeconds(1);
|
|
}
|
|
}
|
|
|
|
IEnumerator CancelMatchmake()
|
|
{
|
|
if (string.IsNullOrEmpty(Cupid.BearerToken))
|
|
{
|
|
Logger.Log("Cancel skipped: no bearer token");
|
|
yield break;
|
|
}
|
|
|
|
using (UnityWebRequest www = UnityWebRequest.Get(Cupid.CupidURI + "/cancel"))
|
|
{
|
|
www.downloadHandler = new DownloadHandlerBuffer();
|
|
www.SetRequestHeader("Authorization", "Bearer " + Cupid.BearerToken);
|
|
yield return www.SendWebRequest();
|
|
string text = www.downloadHandler != null ? www.downloadHandler.text : "";
|
|
if (www.result != UnityWebRequest.Result.Success)
|
|
{
|
|
Logger.Log("Cancel request failed: " + www.error + " body: " + text);
|
|
yield break;
|
|
}
|
|
if (text.Trim() == "1")
|
|
Logger.Log("Cancelled matchmaking success");
|
|
else
|
|
Logger.Log("Matchmaking cancellation said " + text);
|
|
}
|
|
}
|
|
|
|
public void Cancel()
|
|
{
|
|
matchmaking = false;
|
|
StartCoroutine(CancelMatchmake());
|
|
RefreshMatchmakingPanel();
|
|
}
|
|
|
|
void RefreshMatchmakingPanel()
|
|
{
|
|
MatchmakingUI.SetActive(matchmaking);
|
|
}
|
|
}
|