56 lines
2.0 KiB
C#
Raw Normal View History

2021-12-05 14:06:43 +00:00
using QSB.Events;
using QSB.Menus;
using QSB.Utility;
namespace QSB.SaveSync.Events
{
// only to be sent from host
internal class GameStateEvent : QSBEvent<GameStateMessage>
{
public override bool RequireWorldObjectsReady => false;
2021-12-05 23:35:33 +00:00
public override void SetupListener() => GlobalMessenger<uint>.AddListener(EventNames.QSBGameDetails, Handler);
public override void CloseListener() => GlobalMessenger<uint>.RemoveListener(EventNames.QSBGameDetails, Handler);
2021-12-05 14:06:43 +00:00
2021-12-05 23:35:33 +00:00
private void Handler(uint toId) => SendEvent(CreateMessage(toId));
2021-12-05 14:06:43 +00:00
2021-12-05 23:35:33 +00:00
private GameStateMessage CreateMessage(uint toId) => new()
2021-12-05 14:06:43 +00:00
{
AboutId = LocalPlayerId,
2021-12-07 15:47:46 +00:00
ForId = toId,
2021-12-05 14:06:43 +00:00
InSolarSystem = QSBSceneManager.CurrentScene == OWScene.SolarSystem,
InEye = QSBSceneManager.CurrentScene == OWScene.EyeOfTheUniverse,
2021-12-12 00:00:46 +00:00
LaunchCodesGiven = PlayerData.KnowsLaunchCodes(),
2021-12-05 14:06:43 +00:00
LoopCount = StandaloneProfileManager.SharedInstance.currentProfileGameSave.loopCount,
2021-12-05 23:35:33 +00:00
KnownFrequencies = StandaloneProfileManager.SharedInstance.currentProfileGameSave.knownFrequencies,
KnownSignals = StandaloneProfileManager.SharedInstance.currentProfileGameSave.knownSignals
2021-12-05 14:06:43 +00:00
};
public override void OnReceiveRemote(bool isHost, GameStateMessage message)
{
2021-12-12 10:34:55 +00:00
if (QSBSceneManager.CurrentScene != OWScene.TitleScreen)
{
DebugLog.ToConsole($"Error - Treid to handle GameStateEvent when not in TitleScreen!", OWML.Common.MessageType.Error);
return;
}
PlayerData.ResetGame();
2021-12-05 14:06:43 +00:00
var gameSave = StandaloneProfileManager.SharedInstance.currentProfileGameSave;
gameSave.loopCount = message.LoopCount;
gameSave.knownFrequencies = message.KnownFrequencies;
2021-12-05 23:35:33 +00:00
gameSave.knownSignals = message.KnownSignals;
2021-12-12 00:00:46 +00:00
PlayerData.SetPersistentCondition("LAUNCH_CODES_GIVEN", message.LaunchCodesGiven);
2021-12-05 14:06:43 +00:00
PlayerData.SaveCurrentGame();
if (message.InEye != (QSBSceneManager.CurrentScene == OWScene.EyeOfTheUniverse)
|| message.InSolarSystem != (QSBSceneManager.CurrentScene == OWScene.SolarSystem))
{
MenuManager.Instance.JoinGame(message.InEye, message.InSolarSystem);
}
}
}
}