From c4be4fa6be17d0a1674ddb98ff49e454b28cff67 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Wed, 22 Dec 2021 17:38:08 -0800 Subject: [PATCH] PlayerInformation --- QSB/AuthoritySync/AuthQueueMessage.cs | 1 + QSB/Events/EventNames.cs | 1 - QSB/Messaging/QSBMessageManager.cs | 3 +- QSB/Player/Events/PlayerInformationEvent.cs | 43 --------- QSB/Player/Events/PlayerInformationMessage.cs | 88 ++++++++++++++----- QSB/Player/Events/PlayerReadyMessage.cs | 2 +- QSB/Player/Events/RequestStateResyncEvent.cs | 4 +- QSB/Player/PlayerInfo.cs | 6 +- QSB/Player/QSBPlayerManager.cs | 23 ----- 9 files changed, 75 insertions(+), 96 deletions(-) delete mode 100644 QSB/Player/Events/PlayerInformationEvent.cs diff --git a/QSB/AuthoritySync/AuthQueueMessage.cs b/QSB/AuthoritySync/AuthQueueMessage.cs index 79050c3f..939ba6d7 100644 --- a/QSB/AuthoritySync/AuthQueueMessage.cs +++ b/QSB/AuthoritySync/AuthQueueMessage.cs @@ -5,6 +5,7 @@ using QuantumUNET.Transport; namespace QSB.AuthoritySync { + // always sent to host public class AuthQueueMessage : QSBEnumMessage { private QNetworkInstanceId NetId; diff --git a/QSB/Events/EventNames.cs b/QSB/Events/EventNames.cs index 28177319..8a481df9 100644 --- a/QSB/Events/EventNames.cs +++ b/QSB/Events/EventNames.cs @@ -36,7 +36,6 @@ public const string QSBConversation = nameof(QSBConversation); public const string QSBConversationStartEnd = nameof(QSBConversationStartEnd); public const string QSBChangeAnimType = nameof(QSBChangeAnimType); - public const string QSBPlayerInformation = nameof(QSBPlayerInformation); public const string QSBRevealFact = nameof(QSBRevealFact); public const string QSBSocketStateChange = nameof(QSBSocketStateChange); public const string QSBMultiStateChange = nameof(QSBMultiStateChange); diff --git a/QSB/Messaging/QSBMessageManager.cs b/QSB/Messaging/QSBMessageManager.cs index 965b45e9..af5c0e91 100644 --- a/QSB/Messaging/QSBMessageManager.cs +++ b/QSB/Messaging/QSBMessageManager.cs @@ -123,7 +123,8 @@ namespace QSB.Messaging if (!player.IsReady && player.PlayerId != QSBPlayerManager.LocalPlayerId && player.State is ClientState.AliveInSolarSystem or ClientState.AliveInEye or ClientState.DeadInSolarSystem - && msg is not QSBEventRelay { Event: PlayerInformationEvent or RequestStateResyncEvent or ServerStateEvent } + && msg is not QSBEventRelay { Event: RequestStateResyncEvent or ServerStateEvent } + and not PlayerInformationMessage and not PlayerReadyMessage) { DebugLog.ToConsole($"Warning - Got message {msg} from player {msg.From}, but they were not ready. Asking for state resync, just in case.", MessageType.Warning); diff --git a/QSB/Player/Events/PlayerInformationEvent.cs b/QSB/Player/Events/PlayerInformationEvent.cs deleted file mode 100644 index 5dda07bc..00000000 --- a/QSB/Player/Events/PlayerInformationEvent.cs +++ /dev/null @@ -1,43 +0,0 @@ -using OWML.Common; -using QSB.Events; -using QSB.Utility; - -namespace QSB.Player.Events -{ - public class PlayerInformationEvent : QSBEvent - { - public override bool RequireWorldObjectsReady => false; - - public override void SetupListener() => GlobalMessenger.AddListener(EventNames.QSBPlayerInformation, Handler); - public override void CloseListener() => GlobalMessenger.RemoveListener(EventNames.QSBPlayerInformation, Handler); - - private void Handler() => SendEvent(CreateMessage(QSBPlayerManager.LocalPlayer)); - - private PlayerInformationMessage CreateMessage(PlayerInfo player) => new() - { - AboutId = player.PlayerId, - PlayerName = player.Name, - IsReady = player.IsReady, - FlashlightActive = player.FlashlightActive, - SuitedUp = player.SuitedUp, - ProbeLauncherEquipped = player.ProbeLauncherEquipped, - SignalscopeEquipped = player.SignalscopeEquipped, - TranslatorEquipped = player.TranslatorEquipped, - ProbeActive = player.ProbeActive, - ClientState = player.State - }; - - public override void OnReceiveRemote(bool server, PlayerInformationMessage message) - { - RequestStateResyncEvent._waitingForEvent = false; - if (QSBPlayerManager.PlayerExists(message.AboutId)) - { - QSBPlayerManager.HandleFullStateMessage(message); - } - else - { - DebugLog.ToConsole($"Warning - got player information message about player that doesnt exist!", MessageType.Warning); - } - } - } -} diff --git a/QSB/Player/Events/PlayerInformationMessage.cs b/QSB/Player/Events/PlayerInformationMessage.cs index a088a33f..53c2705d 100644 --- a/QSB/Player/Events/PlayerInformationMessage.cs +++ b/QSB/Player/Events/PlayerInformationMessage.cs @@ -1,33 +1,36 @@ -using QSB.ClientServerStateSync; +using OWML.Common; +using QSB.ClientServerStateSync; +using QSB.Events; using QSB.Messaging; +using QSB.Utility; using QuantumUNET.Transport; namespace QSB.Player.Events { - public class PlayerInformationMessage : PlayerMessage + public class PlayerInformationMessage : QSBMessage { - public string PlayerName { get; set; } - public bool IsReady { get; set; } - public bool FlashlightActive { get; set; } - public bool SuitedUp { get; set; } - public bool ProbeLauncherEquipped { get; set; } - public bool SignalscopeEquipped { get; set; } - public bool TranslatorEquipped { get; set; } - public bool ProbeActive { get; set; } - public ClientState ClientState { get; set; } + private string PlayerName; + private bool IsReady; + private bool FlashlightActive; + private bool SuitedUp; + private bool ProbeLauncherEquipped; + private bool SignalscopeEquipped; + private bool TranslatorEquipped; + private bool ProbeActive; + private ClientState ClientState; - public override void Deserialize(QNetworkReader reader) + public PlayerInformationMessage() { - base.Deserialize(reader); - PlayerName = reader.ReadString(); - IsReady = reader.ReadBoolean(); - FlashlightActive = reader.ReadBoolean(); - SuitedUp = reader.ReadBoolean(); - ProbeLauncherEquipped = reader.ReadBoolean(); - SignalscopeEquipped = reader.ReadBoolean(); - TranslatorEquipped = reader.ReadBoolean(); - ProbeActive = reader.ReadBoolean(); - ClientState = (ClientState)reader.ReadInt32(); + var player = QSBPlayerManager.LocalPlayer; + PlayerName = player.Name; + IsReady = player.IsReady; + FlashlightActive = player.FlashlightActive; + SuitedUp = player.SuitedUp; + ProbeLauncherEquipped = player.ProbeLauncherEquipped; + SignalscopeEquipped = player.SignalscopeEquipped; + TranslatorEquipped = player.TranslatorEquipped; + ProbeActive = player.ProbeActive; + ClientState = player.State; } public override void Serialize(QNetworkWriter writer) @@ -43,5 +46,46 @@ namespace QSB.Player.Events writer.Write(ProbeActive); writer.Write((int)ClientState); } + + public override void Deserialize(QNetworkReader reader) + { + base.Deserialize(reader); + PlayerName = reader.ReadString(); + IsReady = reader.ReadBoolean(); + FlashlightActive = reader.ReadBoolean(); + SuitedUp = reader.ReadBoolean(); + ProbeLauncherEquipped = reader.ReadBoolean(); + SignalscopeEquipped = reader.ReadBoolean(); + TranslatorEquipped = reader.ReadBoolean(); + ProbeActive = reader.ReadBoolean(); + ClientState = (ClientState)reader.ReadInt32(); + } + + public override void OnReceiveRemote() + { + RequestStateResyncEvent._waitingForEvent = false; + if (QSBPlayerManager.PlayerExists(From)) + { + var player = QSBPlayerManager.GetPlayer(From); + player.Name = PlayerName; + player.IsReady = IsReady; + player.FlashlightActive = FlashlightActive; + player.SuitedUp = SuitedUp; + player.ProbeLauncherEquipped = ProbeLauncherEquipped; + player.SignalscopeEquipped = SignalscopeEquipped; + player.TranslatorEquipped = TranslatorEquipped; + player.ProbeActive = ProbeActive; + if (QSBPlayerManager.LocalPlayer.IsReady && player.IsReady) + { + player.UpdateObjectsFromStates(); + } + + player.State = ClientState; + } + else + { + DebugLog.ToConsole($"Warning - got player information message about player that doesnt exist!", MessageType.Warning); + } + } } } diff --git a/QSB/Player/Events/PlayerReadyMessage.cs b/QSB/Player/Events/PlayerReadyMessage.cs index 248a26ac..3628f6ed 100644 --- a/QSB/Player/Events/PlayerReadyMessage.cs +++ b/QSB/Player/Events/PlayerReadyMessage.cs @@ -27,7 +27,7 @@ namespace QSB.Player.Events { DebugLog.DebugWrite($"[SERVER] Get ready event from {From} (ready = {Value})", MessageType.Success); QSBPlayerManager.GetPlayer(From).IsReady = Value; - QSBEventManager.FireEvent(EventNames.QSBPlayerInformation); + new PlayerInformationMessage().Send(); } private void HandleClient() diff --git a/QSB/Player/Events/RequestStateResyncEvent.cs b/QSB/Player/Events/RequestStateResyncEvent.cs index 108fc7f6..d128ecf1 100644 --- a/QSB/Player/Events/RequestStateResyncEvent.cs +++ b/QSB/Player/Events/RequestStateResyncEvent.cs @@ -65,7 +65,7 @@ namespace QSB.Player.Events if (isHost) { ServerStateManager.Instance.FireChangeServerStateEvent(ServerStateManager.Instance.GetServerState()); - QSBEventManager.FireEvent(EventNames.QSBPlayerInformation); + new PlayerInformationMessage().Send(); if (WorldObjectManager.AllObjectsReady) { @@ -75,7 +75,7 @@ namespace QSB.Player.Events // if client, send player and client states else { - QSBEventManager.FireEvent(EventNames.QSBPlayerInformation); + new PlayerInformationMessage().Send(); } if (WorldObjectManager.AllObjectsReady) diff --git a/QSB/Player/PlayerInfo.cs b/QSB/Player/PlayerInfo.cs index 2b6b4ee9..b1b5573b 100644 --- a/QSB/Player/PlayerInfo.cs +++ b/QSB/Player/PlayerInfo.cs @@ -4,10 +4,10 @@ using QSB.Animation.Player.Thrusters; using QSB.Audio; using QSB.CampfireSync.WorldObjects; using QSB.ClientServerStateSync; -using QSB.Events; using QSB.ItemSync.WorldObjects.Items; using QSB.Player.TransformSync; -using QSB.QuantumSync; +using QSB.Messaging; +using QSB.Player.Events; using QSB.RoastingSync; using QSB.Tools; using QSB.Tools.FlashlightTool; @@ -231,7 +231,7 @@ namespace QSB.Player SuitedUp = Locator.GetPlayerBody().GetComponent().IsWearingSuit(true); } - QSBEventManager.FireEvent(EventNames.QSBPlayerInformation); + new PlayerInformationMessage().Send(); } private QSBTool GetToolByType(ToolType type) diff --git a/QSB/Player/QSBPlayerManager.cs b/QSB/Player/QSBPlayerManager.cs index 59a5361c..e9c637d1 100644 --- a/QSB/Player/QSBPlayerManager.cs +++ b/QSB/Player/QSBPlayerManager.cs @@ -1,6 +1,5 @@ using OWML.Common; using QSB.ItemSync.WorldObjects.Items; -using QSB.Player.Events; using QSB.Player.TransformSync; using QSB.Tools.FlashlightTool; using QSB.Utility; @@ -77,25 +76,6 @@ namespace QSB.Player public static bool PlayerExists(uint id) => id != uint.MaxValue && PlayerList.Any(x => x.PlayerId == id); - public static void HandleFullStateMessage(PlayerInformationMessage message) - { - var player = GetPlayer(message.AboutId); - player.Name = message.PlayerName; - player.IsReady = message.IsReady; - player.FlashlightActive = message.FlashlightActive; - player.SuitedUp = message.SuitedUp; - player.ProbeLauncherEquipped = message.ProbeLauncherEquipped; - player.SignalscopeEquipped = message.SignalscopeEquipped; - player.TranslatorEquipped = message.TranslatorEquipped; - player.ProbeActive = message.ProbeActive; - if (LocalPlayer.IsReady && player.IsReady) - { - player.UpdateObjectsFromStates(); - } - - player.State = message.ClientState; - } - public static IEnumerable GetSyncObjects() where T : PlayerSyncObject => PlayerSyncObjects.OfType().Where(x => x != null); @@ -106,9 +86,6 @@ namespace QSB.Player public static void RemoveSyncObject(PlayerSyncObject obj) => PlayerSyncObjects.Remove(obj); - public static bool IsBelongingToLocalPlayer(uint id) => id == LocalPlayerId || - PlayerSyncObjects.Any(x => x != null && x.AttachedNetId == id && x.IsLocalPlayer); - public static List GetPlayersWithCameras(bool includeLocalCamera = true) { var cameraList = PlayerList.Where(x => x.Camera != null && x.PlayerId != LocalPlayerId).ToList();