67 lines
1.9 KiB
C#
67 lines
1.9 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
public class ServerAutoClose : MonoBehaviour
|
|
{
|
|
#if UNITY_SERVER
|
|
|
|
void Start()
|
|
{
|
|
Logger.Log("Starting auto close watchdog");
|
|
}
|
|
|
|
int lastObservedPlayerCount = -1;
|
|
bool hasPlayerEverJoined = false;
|
|
float noPlayersTimer = 0f;
|
|
int lastCountdownLoggedSecond = -1;
|
|
const float NoPlayersTimeoutSeconds = 65f;
|
|
|
|
void Update()
|
|
{
|
|
int activePlayers = 0;
|
|
try
|
|
{
|
|
activePlayers = CupidConnector.GetActivePlayerConnectionCount();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Logger.Log("Error getting active player connection count: " + e.Message);
|
|
return;
|
|
}
|
|
|
|
if (activePlayers != lastObservedPlayerCount)
|
|
{
|
|
Logger.Log("Active player connections: " + activePlayers);
|
|
lastObservedPlayerCount = activePlayers;
|
|
}
|
|
|
|
if (activePlayers > 0)
|
|
{
|
|
hasPlayerEverJoined = true;
|
|
noPlayersTimer = 0f;
|
|
lastCountdownLoggedSecond = -1;
|
|
}
|
|
|
|
noPlayersTimer += Time.deltaTime;
|
|
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");
|
|
// ReportDedicatedMatchRoomClosed settles /winner first when escrow is open.
|
|
GameManager.ReportDedicatedMatchRoomClosed();
|
|
Application.Quit();
|
|
}
|
|
}
|
|
|
|
#endif
|
|
}
|