sounds and screens with improved matchmaker
This commit is contained in:
@@ -1,22 +1,24 @@
|
||||
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 string GAME_NAME = "soccar";
|
||||
public const bool saveUsername = false;
|
||||
private static string m_username = "";
|
||||
|
||||
[SerializeField]private GameObject MatchmakingUI;
|
||||
[SerializeField]private string GameScene;
|
||||
[Header("Server info")]
|
||||
[Header("Server info (matchmaker port should match server settings.json)")]
|
||||
[SerializeField]private string serverAddress = "xx.xx.xxx.xx";
|
||||
[SerializeField]private int cupidPort = 1601;
|
||||
[SerializeField]private string password = "xyz@123";
|
||||
[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
|
||||
{
|
||||
@@ -47,11 +49,17 @@ public class CupidLobby : MonoBehaviour
|
||||
|
||||
void Start()
|
||||
{
|
||||
Cupid.Init(serverAddress, cupidPort, password);
|
||||
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());
|
||||
}
|
||||
@@ -59,63 +67,101 @@ public class CupidLobby : MonoBehaviour
|
||||
IEnumerator matchmake()
|
||||
{
|
||||
matchmaking = true;
|
||||
if (string.IsNullOrEmpty(Cupid.BearerToken))
|
||||
{
|
||||
Logger.Log("Matchmake aborted: no bearer token");
|
||||
matchmaking = false;
|
||||
RefreshMatchmakingPanel();
|
||||
yield break;
|
||||
}
|
||||
|
||||
while (matchmaking)
|
||||
{
|
||||
RefreshMatchmakingPanel();
|
||||
|
||||
WWW req = new WWW(Cupid.CupidURI + "/?password=" + password + "&&username=" + Username + $"&&game_name={GAME_NAME}");
|
||||
yield return req;
|
||||
// Debug.Log(req.text);
|
||||
CupidRoom? room = Cupid.ParseRoom(req.text);
|
||||
if(room == null){
|
||||
Logger.Log("No room : " + req.text);
|
||||
}else{
|
||||
CupidRoom _room = (CupidRoom)room;
|
||||
Logger.Log("Got into a room");
|
||||
Logger.Log(req.text);
|
||||
Logger.Log("Setting cupid to load game scene");
|
||||
Cupid.RoomPort = _room.Port;
|
||||
matchmaking=false;
|
||||
Logger.Log("Loading game scene");
|
||||
SceneManager.LoadScene(GameScene);
|
||||
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}");
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// string[] data = req.text.Split(',');
|
||||
// if(data.Length ==2){
|
||||
// Logger.Log(req.text);
|
||||
// if(data[0] == "1"){
|
||||
// //Game started
|
||||
// Logger.Log("Setting cupid room to " + data[1]);
|
||||
|
||||
// Cupid.RoomPort = int.Parse(data[1]);
|
||||
// Logger.Log("Loading scene " + GameScene);
|
||||
|
||||
// SceneManager.LoadScene(GameScene);
|
||||
// matchmaking=false;
|
||||
// break;
|
||||
// }else{
|
||||
// //Game not started gotta continue
|
||||
// }
|
||||
// int gamePort = -1;
|
||||
// try{
|
||||
// gamePort = int.Parse(data[1]);
|
||||
// }catch(Exception e){
|
||||
// Logger.Log("Couldn't parse game port: " + req.text);
|
||||
// }
|
||||
// }
|
||||
|
||||
yield return new WaitForSeconds(1);
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator CancelMatchmake(){
|
||||
WWW www = new WWW(Cupid.CupidURI + "/cancel?password=" + password + "&&username=" + Username);
|
||||
yield return www;
|
||||
if(www.text == "1"){
|
||||
Logger.Log("Cancelled matchmaking success");
|
||||
}else{
|
||||
Logger.Log("Matchmaking cancellation said " + www.text);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user