UPF/Assets/Game/Scripts/Minigame/AutoConnect.cs
2022-10-06 23:45:04 +05:30

133 lines
3.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
using LightReflectiveMirror;
using UnityEngine.SceneManagement;
public class AutoConnect : MonoBehaviour
{
public static AutoConnect instance;
public bool isClient;
public static int serverPort;
public bool isRanked;
public kcp2k.KcpTransport transport;
void Start()
{
instance = this;
if(isClient && !DBmanager.LoggedIn){
SceneManager.LoadScene(0);
}
if (isRanked)
{
// if(!isRankedServer){
// lrm.serverListUpdated.AddListener(OnServerListUpdated);
// StartCoroutine(refreshList());
// }else{
// StartCoroutine(startHost());
// }
string[] args = System.Environment.GetCommandLineArgs();
string input = "";
for (int i = 0; i < args.Length; i++)
{
// Debug.Log("ARG " + i + ": " + args[i]);
if (args[i] == "-port")
{
input = args[i + 1];
}
}
ushort port = 7777;
try
{
port = ushort.Parse(input);
}
catch
{
}
if(!isClient){
Debug.Log("Setting port to " + port);
transport.Port = port;
NetworkManager.singleton.StartServer();
}else{
NetworkManager.singleton.networkAddress = RegionManager.selectedServer.ip;
transport.Port = (ushort)serverPort;
NetworkManager.singleton.StartClient();
}
}
else
{
if (isClient)
{
FindObjectOfType<NetworkManager>().networkAddress = RegionManager.selectedServer.ip;
FindObjectOfType<NetworkManager>().StartClient();
}
}
}
float t;
void Update(){
if(isRanked && !isClient){
//timer
t += Time.deltaTime;
if(t > 30 && FindObjectsOfType<SpaceshipController>().Length ==0){
//No players joined, or they left, gotta close my self
t=0;
StartCoroutine(CloseRoom());
}
}
}
IEnumerator CloseRoom(){
WWWForm form = new WWWForm();
form.AddField("port", serverPort);
WWW req = new WWW(DBmanager.phpRoot+"clear_ranked_room.php",form);
yield return req;
Application.Quit();
}
// 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();
// }
// }
// }
}