UPF/Assets/Game/Scripts/Minigame/AutoConnect.cs
2022-09-11 15:54:15 +05:30

62 lines
1.8 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
using LightReflectiveMirror;
public class AutoConnect : MonoBehaviour
{
public bool isClient;
public static bool isRankedServer;
public static string serverName;
public bool isRanked;
public LightReflectiveMirrorTransport lrm;
void Start()
{
if(isRanked){
if(!isRankedServer){
lrm.serverListUpdated.AddListener(OnServerListUpdated);
StartCoroutine(refreshList());
}else{
StartCoroutine(startHost());
}
}else{
if(isClient){
FindObjectOfType<NetworkManager>().networkAddress = RegionManager.selectedServer.ip;
FindObjectOfType<NetworkManager>().StartClient();
}
}
}
IEnumerator startHost(){
while(!lrm.isConnectedToRelay){
yield return new WaitForSeconds(1);
}
lrm.serverName = serverName;
lrm.isPublicServer=true;
NetworkManager.singleton.StartHost();
}
IEnumerator refreshList(){
while(!lrm.isConnectedToRelay){
yield return new WaitForSeconds(1);
}
while(!NetworkManager.singleton.isNetworkActive){
yield return new WaitForSeconds(1);
lrm.RequestServerList();
}
}
void OnServerListUpdated(){
Debug.Log("Got new server list of " + lrm.relayServerList.Count);
foreach(Room room in lrm.relayServerList){
if(room.serverName == serverName){
Debug.Log("Found server for me! Joining now");
NetworkManager.singleton.networkAddress = room.serverId;
NetworkManager.singleton.StartClient();
}
}
}
}