181 lines
6.5 KiB
C#
181 lines
6.5 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
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)")]
|
|
[SerializeField]private string serverAddress = "xx.xx.xxx.xx";
|
|
[SerializeField]private int cupidPort = 2612;
|
|
[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;
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
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;
|
|
}
|
|
|
|
CupidRoom? room = Cupid.ParseRoom(text);
|
|
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 matchmadeResponse = JsonUtility.FromJson<MatchmadeResponse>(text);
|
|
if(matchmadeResponse.Players.Length < 2){
|
|
Debug.Log("Waiting for the other player to show up");
|
|
yield return new WaitForSeconds(1);
|
|
continue;
|
|
}
|
|
Team myTeam = matchmadeResponse.your_team == "red" ? Team.Red : Team.Blue;
|
|
|
|
MatchmadePlayer myPlayer = matchmadeResponse.Players.First(p => p.UserId == LoginManager.CurrentUser.id);
|
|
MatchmadePlayer opponentPlayer = matchmadeResponse.Players.First(p => p.UserId != LoginManager.CurrentUser.id);
|
|
|
|
if(myTeam==Team.Red){
|
|
GameManager.RedPlayer = myPlayer;
|
|
GameManager.BluePlayer = opponentPlayer;
|
|
}else{
|
|
GameManager.BluePlayer = myPlayer;
|
|
GameManager.RedPlayer = opponentPlayer;
|
|
}
|
|
|
|
// LevelLoadManager.instance.SetupMatchMade(myTeam, myPlayer.Name, opponentPlayer.Name, $"L10 {myPlayer.l10_wins}-{myPlayer.l10_losses}", $"L10 {opponentPlayer.l10_wins}-{opponentPlayer.l10_losses}");
|
|
LevelLoadManager.instance.SetupMatchMade(myTeam, myPlayer, opponentPlayer);
|
|
GameManager.ConfigureDedicatedMatchReporting(matchmadeResponse.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);
|
|
}
|
|
}
|