103 lines
2.7 KiB
C#
Raw Normal View History

using Mirror;
using OWML.Common;
2021-12-25 22:53:36 -08:00
using QSB.Menus;
2021-12-22 22:44:31 -08:00
using QSB.Messaging;
using QSB.Utility;
2021-12-23 17:07:29 -08:00
using System.Collections.Generic;
2021-12-05 14:06:43 +00:00
2022-03-02 19:46:33 -08:00
namespace QSB.SaveSync.Messages;
/// <summary>
/// always sent by host
/// </summary>
internal class GameStateMessage : QSBMessage
2021-12-05 14:06:43 +00:00
{
2022-03-02 19:46:33 -08:00
private bool WarpedToTheEye;
private float SecondsRemainingOnWarp;
private bool LaunchCodesGiven;
private int LoopCount;
private bool[] KnownFrequencies;
private Dictionary<int, bool> KnownSignals;
2022-05-01 15:24:10 +01:00
private bool ReducedFrights;
2022-03-02 19:46:33 -08:00
public GameStateMessage(uint toId)
{
2022-03-02 19:46:33 -08:00
To = toId;
var gameSave = PlayerData._currentGameSave;
WarpedToTheEye = gameSave.warpedToTheEye;
SecondsRemainingOnWarp = gameSave.secondsRemainingOnWarp;
LaunchCodesGiven = PlayerData.KnowsLaunchCodes();
LoopCount = gameSave.loopCount;
KnownFrequencies = gameSave.knownFrequencies;
KnownSignals = gameSave.knownSignals;
2022-05-01 15:24:10 +01:00
ReducedFrights = PlayerData.GetReducedFrights();
2022-03-02 19:46:33 -08:00
}
public override void Serialize(NetworkWriter writer)
{
base.Serialize(writer);
writer.Write(WarpedToTheEye);
writer.Write(SecondsRemainingOnWarp);
writer.Write(LaunchCodesGiven);
writer.Write(LoopCount);
2022-03-02 19:46:33 -08:00
writer.Write(KnownFrequencies);
2021-12-25 18:57:20 -08:00
2022-03-02 19:46:33 -08:00
writer.Write(KnownSignals.Count);
foreach (var (name, discovered) in KnownSignals)
2021-12-05 14:06:43 +00:00
{
2022-03-02 19:46:33 -08:00
writer.Write(name);
writer.Write(discovered);
2021-12-05 14:06:43 +00:00
}
2022-05-01 15:24:10 +01:00
writer.Write(ReducedFrights);
2022-03-02 19:46:33 -08:00
}
public override void Deserialize(NetworkReader reader)
{
base.Deserialize(reader);
WarpedToTheEye = reader.Read<bool>();
SecondsRemainingOnWarp = reader.Read<float>();
LaunchCodesGiven = reader.Read<bool>();
LoopCount = reader.Read<int>();
KnownFrequencies = reader.Read<bool[]>();
2021-12-05 14:06:43 +00:00
2022-03-02 19:46:33 -08:00
var signalsLength = reader.Read<int>();
KnownSignals = new Dictionary<int, bool>(signalsLength);
for (var i = 0; i < signalsLength; i++)
2021-12-22 22:44:31 -08:00
{
2022-03-02 19:46:33 -08:00
var key = reader.Read<int>();
var value = reader.Read<bool>();
KnownSignals.Add(key, value);
}
2022-05-01 15:24:10 +01:00
ReducedFrights = reader.Read<bool>();
2022-03-02 19:46:33 -08:00
}
2021-12-22 22:44:31 -08:00
2022-03-02 19:46:33 -08:00
public override void OnReceiveRemote()
{
if (QSBSceneManager.CurrentScene != OWScene.TitleScreen)
{
2022-03-02 19:46:33 -08:00
DebugLog.ToConsole($"Error - Tried to handle GameStateEvent when not in TitleScreen!", MessageType.Error);
return;
}
2022-03-02 19:46:33 -08:00
PlayerData.ResetGame();
2021-12-22 22:44:31 -08:00
2022-03-02 19:46:33 -08:00
var gameSave = PlayerData._currentGameSave;
gameSave.loopCount = LoopCount;
gameSave.knownFrequencies = KnownFrequencies;
gameSave.knownSignals = KnownSignals;
gameSave.warpedToTheEye = WarpedToTheEye;
gameSave.secondsRemainingOnWarp = SecondsRemainingOnWarp;
2021-12-22 22:44:31 -08:00
2022-03-02 19:46:33 -08:00
PlayerData.SetPersistentCondition("LAUNCH_CODES_GIVEN", LaunchCodesGiven);
2022-05-01 15:24:10 +01:00
PlayerData._settingsSave.reducedFrights = ReducedFrights;
2021-12-22 22:44:31 -08:00
2022-03-02 19:46:33 -08:00
PlayerData.SaveCurrentGame();
2021-12-22 22:44:31 -08:00
2022-04-05 16:45:26 -07:00
MenuManager.Instance.LoadGame(WarpedToTheEye);
2021-12-05 14:06:43 +00:00
}
2021-12-22 22:44:31 -08:00
}