168 lines
3.9 KiB
C#
Raw Normal View History

2021-12-23 17:07:29 -08:00
using QSB.ClientServerStateSync.Messages;
using QSB.DeathSync.Messages;
using QSB.Messaging;
2021-08-08 19:59:40 +01:00
using QSB.Player;
using QSB.Player.TransformSync;
using QSB.Utility;
using System.Linq;
using UnityEngine;
namespace QSB.ClientServerStateSync
{
2021-08-09 11:49:58 +01:00
internal class ServerStateManager : MonoBehaviour
2021-08-08 19:59:40 +01:00
{
public static ServerStateManager Instance { get; private set; }
public event ChangeStateEvent OnChangeState;
public delegate void ChangeStateEvent(ServerState newState);
private ServerState _currentState;
2021-10-21 17:21:54 +01:00
private bool _blockNextCheck;
2021-08-08 19:59:40 +01:00
2021-08-22 16:54:48 +01:00
private void Awake()
2021-08-08 19:59:40 +01:00
=> Instance = this;
private void Start()
{
if (!QSBCore.IsHost)
{
return;
}
2021-08-22 17:17:46 +01:00
2021-08-08 19:59:40 +01:00
QSBSceneManager.OnSceneLoaded += OnSceneLoaded;
GlobalMessenger.AddListener("TriggerSupernova", OnTriggerSupernova);
2021-12-22 18:27:12 -08:00
QSBCore.UnityEvents.RunWhen(() => PlayerTransformSync.LocalInstance != null,
() => new ServerStateMessage(ForceGetCurrentState()).Send());
2021-08-08 19:59:40 +01:00
}
private void OnDestroy()
{
QSBSceneManager.OnSceneLoaded -= OnSceneLoaded;
GlobalMessenger.RemoveListener("TriggerSupernova", OnTriggerSupernova);
}
2021-12-22 18:37:24 -08:00
public void SendChangeServerStateMessage(ServerState newState)
2021-12-20 11:49:03 +00:00
{
ChangeServerState(newState);
2021-12-22 18:27:12 -08:00
new ServerStateMessage(newState).Send();
2021-12-20 11:49:03 +00:00
}
2021-08-08 19:59:40 +01:00
public void ChangeServerState(ServerState newState)
{
if (_currentState == newState)
{
return;
}
2021-08-22 17:17:46 +01:00
2021-08-08 19:59:40 +01:00
_currentState = newState;
OnChangeState?.Invoke(newState);
}
2021-08-22 16:54:48 +01:00
public ServerState GetServerState()
2021-08-08 19:59:40 +01:00
=> _currentState;
private void OnSceneLoaded(OWScene oldScene, OWScene newScene, bool inUniverse)
{
switch (newScene)
{
case OWScene.Credits_Fast:
case OWScene.Credits_Final:
case OWScene.PostCreditsScene:
2021-12-22 18:37:24 -08:00
SendChangeServerStateMessage(ServerState.Credits);
2021-08-08 19:59:40 +01:00
break;
case OWScene.TitleScreen:
2021-12-22 18:37:24 -08:00
SendChangeServerStateMessage(ServerState.NotLoaded);
2021-08-08 19:59:40 +01:00
break;
case OWScene.SolarSystem:
if (oldScene == OWScene.SolarSystem)
{
2021-12-22 18:37:24 -08:00
SendChangeServerStateMessage(ServerState.WaitingForAllPlayersToReady);
2021-08-08 19:59:40 +01:00
}
else
{
2021-12-22 18:37:24 -08:00
SendChangeServerStateMessage(ServerState.InSolarSystem);
2021-08-08 19:59:40 +01:00
}
2021-08-22 17:17:46 +01:00
2021-08-08 19:59:40 +01:00
break;
case OWScene.EyeOfTheUniverse:
2021-12-22 18:37:24 -08:00
SendChangeServerStateMessage(ServerState.WaitingForAllPlayersToReady);
2021-08-08 19:59:40 +01:00
break;
case OWScene.None:
case OWScene.Undefined:
default:
DebugLog.ToConsole($"Warning - newScene is {newScene}!", OWML.Common.MessageType.Warning);
2021-12-22 18:37:24 -08:00
SendChangeServerStateMessage(ServerState.NotLoaded);
2021-08-08 19:59:40 +01:00
break;
}
}
private void OnTriggerSupernova()
{
2021-12-22 19:06:35 -08:00
if (QSBSceneManager.CurrentScene == OWScene.SolarSystem)
2021-08-08 19:59:40 +01:00
{
SendChangeServerStateMessage(ServerState.WaitingForAllPlayersToDie);
2021-08-08 19:59:40 +01:00
}
}
private ServerState ForceGetCurrentState()
{
var currentScene = LoadManager.GetCurrentScene();
switch (currentScene)
{
case OWScene.SolarSystem:
return ServerState.InSolarSystem;
case OWScene.EyeOfTheUniverse:
return ServerState.InEye;
default:
return ServerState.NotLoaded;
}
}
private void Update()
{
if (!QSBCore.IsHost)
{
return;
}
2021-10-21 17:21:54 +01:00
if (_blockNextCheck)
{
_blockNextCheck = false;
return;
}
if (_currentState == ServerState.WaitingForAllPlayersToReady)
2021-08-08 19:59:40 +01:00
{
2021-11-12 21:05:58 +00:00
if (QSBPlayerManager.PlayerList.All(x
=> x.State is ClientState.WaitingForOthersToBeReady
2021-12-24 22:33:29 -08:00
or ClientState.AliveInSolarSystem
or ClientState.AliveInEye))
2021-08-08 19:59:40 +01:00
{
DebugLog.DebugWrite($"All ready!!");
2021-12-22 22:26:53 -08:00
new StartLoopMessage().Send();
2021-11-12 21:05:58 +00:00
if (QSBSceneManager.CurrentScene == OWScene.SolarSystem)
{
2021-12-22 18:37:24 -08:00
SendChangeServerStateMessage(ServerState.InSolarSystem);
2021-11-12 21:05:58 +00:00
}
else if (QSBSceneManager.CurrentScene == OWScene.EyeOfTheUniverse)
{
2021-12-22 18:37:24 -08:00
SendChangeServerStateMessage(ServerState.InEye);
2021-11-12 21:05:58 +00:00
}
else
{
DebugLog.ToConsole($"Error - All players were ready in non-universe scene!?", OWML.Common.MessageType.Error);
2021-12-22 18:37:24 -08:00
SendChangeServerStateMessage(ServerState.NotLoaded);
2021-11-12 21:05:58 +00:00
}
2021-11-25 15:32:34 +00:00
2021-10-21 17:21:54 +01:00
_blockNextCheck = true;
2021-08-08 19:59:40 +01:00
}
}
}
}
2021-12-24 22:33:29 -08:00
}