reconnect, 30% fixes from test feedback
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Mirror;
|
||||
using UnityEngine;
|
||||
|
||||
@@ -9,7 +10,7 @@ public class CupidConnector : MonoBehaviour
|
||||
public kcp2k.KcpTransport transport;
|
||||
void Start()
|
||||
{
|
||||
#if UNITY_SERVER
|
||||
#if UNITY_SERVER
|
||||
//Server code
|
||||
string[] args = Environment.GetCommandLineArgs();
|
||||
int matchId = 0;
|
||||
@@ -20,7 +21,6 @@ public class CupidConnector : MonoBehaviour
|
||||
if (args[i].Contains("-port") && i + 1 < args.Length)
|
||||
{
|
||||
Cupid.RoomPort = int.Parse(args[i + 1]);
|
||||
Logger.SetFileName(Cupid.RoomPort.ToString());
|
||||
}
|
||||
if (i + 1 < args.Length)
|
||||
{
|
||||
@@ -34,6 +34,12 @@ public class CupidConnector : MonoBehaviour
|
||||
}
|
||||
}
|
||||
GameManager.ConfigureDedicatedMatchReporting(matchId);
|
||||
#if UNITY_EDITOR
|
||||
Logger.Log("Editor mode, skipping arg check");
|
||||
Cupid.RoomPort = 7777;
|
||||
matchId = 228;
|
||||
|
||||
#else
|
||||
if(Cupid.RoomPort < 0){
|
||||
Logger.Log("Invalid port, Did you pass the -port arguement?");
|
||||
return;
|
||||
@@ -42,12 +48,21 @@ public class CupidConnector : MonoBehaviour
|
||||
Logger.Log("Invalid match id, Did you pass the -matchId arguement?");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
Logger.SetFileName(matchId.ToString());
|
||||
|
||||
// Keep this component alive across server scene loads so timeout coroutine keeps running.
|
||||
DontDestroyOnLoad(gameObject);
|
||||
|
||||
transport.Port = (ushort)Cupid.RoomPort;
|
||||
Logger.Log($"Starting server at port {Cupid.RoomPort}");
|
||||
//StartCoroutine(PlayerTimeoutLoop());
|
||||
NetworkManager.singleton.StartServer();
|
||||
#else
|
||||
#else
|
||||
//Client code
|
||||
if(Cupid.RoomPort <0){
|
||||
if (Cupid.RoomPort < 0)
|
||||
{
|
||||
Logger.Log("Invalid Room port");
|
||||
return;
|
||||
}
|
||||
@@ -55,37 +70,85 @@ public class CupidConnector : MonoBehaviour
|
||||
NetworkManager.singleton.networkAddress = Cupid.ServerAddress;
|
||||
Logger.Log($"Starting client at ${Cupid.ServerAddress}:{Cupid.RoomPort}");
|
||||
NetworkManager.singleton.StartClient();
|
||||
#endif
|
||||
#endif
|
||||
|
||||
}
|
||||
#if UNITY_SERVER
|
||||
float t=0;
|
||||
int _t = 0;
|
||||
void Update(){
|
||||
if(t < 60){
|
||||
t += Time.deltaTime;
|
||||
}else{
|
||||
if(NetworkServer.connections.Count <= 0){
|
||||
Logger.Log("Closing port " + Cupid.RoomPort + " due to no players");
|
||||
|
||||
|
||||
const float NoPlayersTimeoutSeconds = 45f;
|
||||
float noPlayersTimer = 0f;
|
||||
bool hasPlayerEverJoined = false;
|
||||
int lastCountdownLoggedSecond = -1;
|
||||
int lastObservedPlayerCount = -1;
|
||||
|
||||
public static int GetActivePlayerConnectionCount()
|
||||
{
|
||||
if (NetworkServer.connections == null || NetworkServer.connections.Count == 0)
|
||||
return 0;
|
||||
|
||||
// Mirror can keep dictionary entries around briefly; count only live/authenticated clients.
|
||||
return NetworkServer.connections.Values.Count(conn =>
|
||||
conn != null &&
|
||||
conn.isAuthenticated &&
|
||||
conn.identity != null);
|
||||
}
|
||||
|
||||
IEnumerator PlayerTimeoutLoop()
|
||||
{
|
||||
Logger.Log("Player timeout loop started");
|
||||
while (true)
|
||||
{
|
||||
int activePlayers = 0;
|
||||
try{
|
||||
activePlayers = GetActivePlayerConnectionCount();
|
||||
if (activePlayers != lastObservedPlayerCount)
|
||||
{
|
||||
Logger.Log("Active player connections: " + activePlayers);
|
||||
lastObservedPlayerCount = activePlayers;
|
||||
}
|
||||
}catch(Exception e){
|
||||
Logger.Log("Error getting active player connection count: " + e.Message);
|
||||
}
|
||||
if (activePlayers > 0)
|
||||
{
|
||||
hasPlayerEverJoined = true;
|
||||
noPlayersTimer = 0f;
|
||||
lastCountdownLoggedSecond = -1;
|
||||
yield return new WaitForSecondsRealtime(1f);
|
||||
continue;
|
||||
}
|
||||
|
||||
noPlayersTimer += 1f;
|
||||
int secondsRemaining = Mathf.CeilToInt(NoPlayersTimeoutSeconds - noPlayersTimer);
|
||||
if (secondsRemaining > 0 && secondsRemaining % 10 == 0 && secondsRemaining != lastCountdownLoggedSecond)
|
||||
{
|
||||
lastCountdownLoggedSecond = secondsRemaining;
|
||||
Logger.Log("Server will be closed due to no players in, " + secondsRemaining);
|
||||
}
|
||||
|
||||
if (noPlayersTimer >= NoPlayersTimeoutSeconds)
|
||||
{
|
||||
if (hasPlayerEverJoined)
|
||||
Logger.Log("All players left, exiting");
|
||||
else
|
||||
Logger.Log("No players joined, exiting");
|
||||
GameManager.ReportDedicatedMatchRoomClosed();
|
||||
Application.Quit();
|
||||
}
|
||||
}
|
||||
if((int)t !=_t){
|
||||
_t = (int)t;
|
||||
Logger.Log(NetworkServer.connections.Count.ToString());
|
||||
yield return new WaitForSecondsRealtime(1f);
|
||||
|
||||
}
|
||||
}
|
||||
#else
|
||||
void Update(){
|
||||
// Debug.Log(NetworkServer.connections.Count);
|
||||
}
|
||||
#endif
|
||||
|
||||
void OnValidate(){
|
||||
if(GetComponent<NetworkManager>()!=null){
|
||||
GetComponent<NetworkManager>().headlessStartMode= HeadlessStartOptions.DoNothing;
|
||||
|
||||
void OnValidate()
|
||||
{
|
||||
if (GetComponent<NetworkManager>() != null)
|
||||
{
|
||||
GetComponent<NetworkManager>().headlessStartMode = HeadlessStartOptions.DoNothing;
|
||||
}
|
||||
if(transport == null){
|
||||
if (transport == null)
|
||||
{
|
||||
transport = GetComponent<kcp2k.KcpTransport>();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user