152 lines
3.8 KiB
C#
Raw Normal View History

2021-08-08 19:59:40 +01:00
using QSB.Events;
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);
QSBCore.UnityEvents.RunWhen(() => PlayerTransformSync.LocalInstance != null, () => QSBEventManager.FireEvent(EventNames.QSBServerState, ForceGetCurrentState()));
}
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:
QSBEventManager.FireEvent(EventNames.QSBServerState, ServerState.Credits);
break;
case OWScene.TitleScreen:
QSBEventManager.FireEvent(EventNames.QSBServerState, ServerState.NotLoaded);
break;
case OWScene.SolarSystem:
if (oldScene == OWScene.SolarSystem)
{
QSBEventManager.FireEvent(EventNames.QSBServerState, ServerState.WaitingForAllPlayersToReady);
2021-08-08 19:59:40 +01:00
}
else
{
QSBEventManager.FireEvent(EventNames.QSBServerState, ServerState.InSolarSystem);
}
2021-08-22 17:17:46 +01:00
2021-08-08 19:59:40 +01:00
break;
case OWScene.EyeOfTheUniverse:
QSBEventManager.FireEvent(EventNames.QSBServerState, 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);
QSBEventManager.FireEvent(EventNames.QSBServerState, ServerState.NotLoaded);
break;
}
}
private void OnTriggerSupernova()
{
DebugLog.DebugWrite($"TriggerSupernova");
QSBEventManager.FireEvent(EventNames.QSBServerState, 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
2021-11-20 19:49:50 +00:00
=> x.State is ClientState.WaitingForOthersToReadyInSolarSystem
or ClientState.AliveInSolarSystem
or ClientState.AliveInEye))
2021-08-08 19:59:40 +01:00
{
DebugLog.DebugWrite($"All ready!!");
QSBEventManager.FireEvent(EventNames.QSBStartLoop);
2021-11-12 21:05:58 +00:00
if (QSBSceneManager.CurrentScene == OWScene.SolarSystem)
{
QSBEventManager.FireEvent(EventNames.QSBServerState, ServerState.InSolarSystem);
}
else if (QSBSceneManager.CurrentScene == OWScene.EyeOfTheUniverse)
{
QSBEventManager.FireEvent(EventNames.QSBServerState, ServerState.InEye);
}
else
{
DebugLog.ToConsole($"Error - All players were ready in non-universe scene!?", OWML.Common.MessageType.Error);
QSBEventManager.FireEvent(EventNames.QSBServerState, ServerState.NotLoaded);
}
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
}
}
}
}
}