205 lines
5.4 KiB
C#
Executable File
205 lines
5.4 KiB
C#
Executable File
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using LightReflectiveMirror;
|
|
using TMPro;
|
|
using UnityEngine.UI;
|
|
using System;
|
|
using Mirror;
|
|
using UnityEngine.SceneManagement;
|
|
using System.Threading.Tasks;
|
|
using Debug = CustomLogger.Debug;
|
|
|
|
public class MatchMaker : MonoBehaviour
|
|
{
|
|
public TMP_Text timerTxt;
|
|
public Button btn_cancel;
|
|
public TMP_Text playerCountTxt;
|
|
float timer;
|
|
void Start()
|
|
{
|
|
if(!DBmanager.LoggedIn){
|
|
SceneManager.LoadScene(0);
|
|
}
|
|
btn_cancel.onClick.AddListener(Cancel);
|
|
AudioManager.instnace.SetMusic(2);
|
|
|
|
// StartCoroutine(StartMatchmaking());
|
|
|
|
}
|
|
|
|
private void OnClientConnected()
|
|
{
|
|
Debug.Log("Hey look someone connected!");
|
|
}
|
|
|
|
// Update is called once per frame
|
|
float t=0;
|
|
float t2=100;
|
|
float connectTimeout = 0;
|
|
bool loadedScene;
|
|
|
|
int sgpPlayers;
|
|
int euPlayers;
|
|
int usPlayers;
|
|
void Update()
|
|
{
|
|
timer+=Time.deltaTime;
|
|
timerTxt.text = SceneData.SecondsToText(timer);
|
|
|
|
if(t < 1){
|
|
t += Time.deltaTime;
|
|
}else{
|
|
t=0;
|
|
StartCoroutine(FetchMatchmake());
|
|
}
|
|
|
|
if(t2 < 5){
|
|
t2+= Time.deltaTime;
|
|
|
|
}else{
|
|
t2 = 0;
|
|
StartCoroutine(FetchPlayerCount("SGP"));
|
|
StartCoroutine(FetchPlayerCount("USA"));
|
|
StartCoroutine(FetchPlayerCount("EU"));
|
|
}
|
|
}
|
|
|
|
IEnumerator FetchPlayerCount(string region){
|
|
WWWForm form = new WWWForm();
|
|
form.AddField("region", region);
|
|
|
|
WWW req = new WWW(DBmanager.phpRoot+"get_players_online.php", form);
|
|
yield return req;
|
|
|
|
if(region == "SGP"){
|
|
sgpPlayers = int.Parse(req.text);
|
|
}else if(region == "USA"){
|
|
usPlayers = int.Parse(req.text);
|
|
}else if(region == "EU"){
|
|
euPlayers = int.Parse(req.text);
|
|
}
|
|
|
|
UpdatePlayerCount();
|
|
}
|
|
|
|
void UpdatePlayerCount(){
|
|
playerCountTxt.text = $"EU: {euPlayers}\nSGP: {sgpPlayers}\nUSA: {usPlayers}";
|
|
}
|
|
int port =-1;
|
|
IEnumerator FetchMatchmake(){
|
|
#region old
|
|
// WWWForm form = new WWWForm();
|
|
// form.AddField("name", DBmanager.username);
|
|
// form.AddField("region", RegionManager.selectedServer.name);
|
|
|
|
// WWW req = new WWW(DBmanager.phpRoot+"matchmake.php", form);
|
|
// yield return req;
|
|
// Debug.Log(req.text);
|
|
|
|
// try{
|
|
// int port = int.Parse(req.text);
|
|
// if(port > 5000){
|
|
// Debug.Log("Got the port, Lets go!");
|
|
// AutoConnect.serverPort = port;
|
|
// LoadingScreen.instance.LoadLevel("MinigameRanked");
|
|
// }
|
|
// }catch{
|
|
|
|
// }
|
|
#endregion
|
|
|
|
WWW req = new WWW($"http://{RegionManager.selectedServer.ip}:1601/?password=xyz@123&&username=" + DBmanager.username);
|
|
yield return req;
|
|
|
|
Debug.Log("Matchmaker: "+req.text);
|
|
if(req.text.Contains("{")){
|
|
MatchmakerResponse response = JsonUtility.FromJson<MatchmakerResponse>(req.text);
|
|
// if()
|
|
if(port < 0){
|
|
confirmRoom(response);
|
|
}else if(port > 0){
|
|
Debug.Log("Got the port, Lets go!");
|
|
AutoConnect.serverPort = response.Port;
|
|
LoadingScreen.instance.LoadLevel("MinigameRanked");
|
|
Debug.Log(response.ToString());
|
|
|
|
port = -1;
|
|
}
|
|
}
|
|
}
|
|
MatchmakerResponse init_response;
|
|
int responseCounter =0;
|
|
void confirmRoom(MatchmakerResponse response){
|
|
if(responseCounter == 0){
|
|
init_response = response;
|
|
}
|
|
responseCounter++;
|
|
|
|
if(responseCounter > 5){
|
|
responseCounter=0;
|
|
if(response.Players.Length == 2){
|
|
//Confirm room
|
|
port = response.Port;
|
|
}else{
|
|
port = -1;
|
|
}
|
|
}
|
|
}
|
|
|
|
// IEnumerator StartMatchmaking(){
|
|
// StartCoroutine(StartLooking());
|
|
|
|
// yield return new WaitForSeconds(1);
|
|
|
|
|
|
// lrm.serverName=DBmanager.username;
|
|
// NetworkManager.singleton.StartHost();
|
|
|
|
// }
|
|
// bool foundServer =false;
|
|
// IEnumerator StartLooking(){
|
|
// int counter =0;
|
|
// while(counter < 3){
|
|
// yield return new WaitForSeconds(1);
|
|
// lrm.RequestServerList();
|
|
// // counter++;
|
|
// }
|
|
// }
|
|
|
|
// void OnServerListUpdated(){
|
|
// Debug.Log("Found " + lrm.relayServerList.Count + " Servers");
|
|
// foreach(Room room in lrm.relayServerList){
|
|
// Debug.Log(room.serverName);
|
|
// if(!foundServer && room.serverName.Length>0 && room.serverName != DBmanager.username){
|
|
// foundServer=true;
|
|
// NetworkManager.singleton.networkAddress = room.serverName;
|
|
// NetworkManager.singleton.StartClient();
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
void Cancel(){
|
|
LoadingScreen.instance.LoadLevel("GameScene");
|
|
}
|
|
|
|
|
|
}
|
|
|
|
[Serializable]
|
|
public class MatchmakerResponse{
|
|
public MatchmakerPlayerEntry[] Players;
|
|
public int Port;
|
|
public long InitTime;
|
|
|
|
public override string ToString()
|
|
{
|
|
return JsonUtility.ToJson(this);
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public class MatchmakerPlayerEntry{
|
|
public string Name;
|
|
public long LastSeen;
|
|
} |