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-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:
|
2021-09-25 09:49:03 +01:00
|
|
|
|
DebugLog.DebugWrite($"SERVER LOAD CREDITS");
|
2021-08-08 19:59:40 +01:00
|
|
|
|
QSBEventManager.FireEvent(EventNames.QSBServerState, ServerState.Credits);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case OWScene.TitleScreen:
|
2021-09-25 09:49:03 +01:00
|
|
|
|
DebugLog.DebugWrite($"SERVER LOAD TITLE SCREEN");
|
2021-08-08 19:59:40 +01:00
|
|
|
|
QSBEventManager.FireEvent(EventNames.QSBServerState, ServerState.NotLoaded);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case OWScene.SolarSystem:
|
2021-09-25 09:49:03 +01:00
|
|
|
|
DebugLog.DebugWrite($"SERVER LOAD SOLARSYSTEM");
|
2021-08-08 19:59:40 +01:00
|
|
|
|
if (oldScene == OWScene.SolarSystem)
|
|
|
|
|
{
|
2021-09-25 09:49:03 +01:00
|
|
|
|
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:
|
2021-09-25 09:49:03 +01:00
|
|
|
|
DebugLog.DebugWrite($"EYE");
|
|
|
|
|
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");
|
2021-09-25 09:49:03 +01:00
|
|
|
|
QSBEventManager.FireEvent(EventNames.QSBServerState, ServerState.WaitingForAllPlayersToDie);
|
2021-08-08 19:59:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ServerState ForceGetCurrentState()
|
|
|
|
|
{
|
2021-09-25 09:49:03 +01:00
|
|
|
|
DebugLog.DebugWrite($"ForceGetCurrentState");
|
|
|
|
|
|
2021-08-08 19:59:40 +01:00
|
|
|
|
var currentScene = LoadManager.GetCurrentScene();
|
|
|
|
|
var lastScene = LoadManager.GetPreviousScene();
|
|
|
|
|
|
|
|
|
|
switch (currentScene)
|
|
|
|
|
{
|
|
|
|
|
case OWScene.SolarSystem:
|
2021-09-25 09:49:03 +01:00
|
|
|
|
DebugLog.DebugWrite($"- SolarSystem");
|
2021-08-08 19:59:40 +01:00
|
|
|
|
return ServerState.InSolarSystem;
|
|
|
|
|
case OWScene.EyeOfTheUniverse:
|
2021-09-25 09:49:03 +01:00
|
|
|
|
DebugLog.DebugWrite($"- Eye");
|
2021-08-08 19:59:40 +01:00
|
|
|
|
return ServerState.InEye;
|
|
|
|
|
default:
|
2021-09-25 09:49:03 +01:00
|
|
|
|
DebugLog.DebugWrite($"- Not Loaded");
|
2021-08-08 19:59:40 +01:00
|
|
|
|
return ServerState.NotLoaded;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Update()
|
|
|
|
|
{
|
|
|
|
|
if (!QSBCore.IsHost)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-25 09:49:03 +01:00
|
|
|
|
if (_currentState == ServerState.WaitingForAllPlayersToReady)
|
2021-08-08 19:59:40 +01:00
|
|
|
|
{
|
2021-10-20 17:41:03 +01:00
|
|
|
|
if (QSBPlayerManager.PlayerList.All(x => x.State == ClientState.WaitingForOthersToReadyInSolarSystem)
|
|
|
|
|
|| QSBPlayerManager.PlayerList.All(x => x.State == ClientState.AliveInSolarSystem))
|
2021-08-08 19:59:40 +01:00
|
|
|
|
{
|
|
|
|
|
DebugLog.DebugWrite($"All ready!!");
|
|
|
|
|
QSBEventManager.FireEvent(EventNames.QSBStartLoop);
|
|
|
|
|
QSBEventManager.FireEvent(EventNames.QSBServerState, ServerState.InSolarSystem);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|