From ae3a9b1e885a316e7cb5aa203c77bae38989b9e1 Mon Sep 17 00:00:00 2001 From: starfish <50672801+starfi5h@users.noreply.github.com> Date: Thu, 5 Dec 2024 18:17:37 +0800 Subject: [PATCH] Move StatisticsManager.SendBroadcastIfNeeded from real time to logic tick (60) --- NebulaNetwork/Server.cs | 10 ---------- NebulaPatcher/Patches/Dynamic/GameData_Patch.cs | 1 + .../Patches/Dynamic/UIStatisticsWindow_Patch.cs | 2 +- NebulaWorld/Statistics/StatisticsManager.cs | 6 ++++-- 4 files changed, 6 insertions(+), 13 deletions(-) diff --git a/NebulaNetwork/Server.cs b/NebulaNetwork/Server.cs index aa31d9b8a..9b42da412 100644 --- a/NebulaNetwork/Server.cs +++ b/NebulaNetwork/Server.cs @@ -43,7 +43,6 @@ public class Server : IServer public INetPacketProcessor PacketProcessor { get; set; } = new NebulaNetPacketProcessor(); private const float GAME_RESEARCH_UPDATE_INTERVAL = 2; - private const float STATISTICS_UPDATE_INTERVAL = 1; private const float LAUNCH_UPDATE_INTERVAL = 4; private const float DYSONSPHERE_UPDATE_INTERVAL = 2; private const float WARNING_UPDATE_INTERVAL = 1; @@ -54,8 +53,6 @@ public class Server : IServer private float gameResearchHashUpdateTimer; private NgrokManager ngrokManager; - private float productionStatisticsUpdateTimer; - private WebSocketServer socket; private float warningUpdateTimer; @@ -414,7 +411,6 @@ public void Update() } gameResearchHashUpdateTimer += Time.deltaTime; - productionStatisticsUpdateTimer += Time.deltaTime; dysonLaunchUpateTimer += Time.deltaTime; dysonSphereUpdateTimer += Time.deltaTime; warningUpdateTimer += Time.deltaTime; @@ -430,12 +426,6 @@ public void Update() } } - if (productionStatisticsUpdateTimer > STATISTICS_UPDATE_INTERVAL) - { - productionStatisticsUpdateTimer = 0; - Multiplayer.Session.Statistics.SendBroadcastIfNeeded(); - } - if (dysonLaunchUpateTimer > LAUNCH_UPDATE_INTERVAL) { dysonLaunchUpateTimer = 0; diff --git a/NebulaPatcher/Patches/Dynamic/GameData_Patch.cs b/NebulaPatcher/Patches/Dynamic/GameData_Patch.cs index 48d4115e4..ccdc5bb9c 100644 --- a/NebulaPatcher/Patches/Dynamic/GameData_Patch.cs +++ b/NebulaPatcher/Patches/Dynamic/GameData_Patch.cs @@ -329,6 +329,7 @@ public static void GameTick_Postfix(long time) if (Multiplayer.Session.LocalPlayer.IsHost) { Multiplayer.Session.Launch.CollectProjectile(); + Multiplayer.Session.Statistics.SendBroadcastIfNeeded(time); return; } diff --git a/NebulaPatcher/Patches/Dynamic/UIStatisticsWindow_Patch.cs b/NebulaPatcher/Patches/Dynamic/UIStatisticsWindow_Patch.cs index 76d055310..289cd3910 100644 --- a/NebulaPatcher/Patches/Dynamic/UIStatisticsWindow_Patch.cs +++ b/NebulaPatcher/Patches/Dynamic/UIStatisticsWindow_Patch.cs @@ -12,7 +12,7 @@ namespace NebulaPatcher.Patches.Dynamic; [HarmonyPatch(typeof(UIStatisticsWindow))] internal class UIStatisticsWindow_Patch { - [HarmonyPostfix] + [HarmonyPrefix] [HarmonyPatch(nameof(UIStatisticsWindow._OnOpen))] [SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "Original Function Name")] public static void _OnOpen_Postfix() diff --git a/NebulaWorld/Statistics/StatisticsManager.cs b/NebulaWorld/Statistics/StatisticsManager.cs index 1f0ef74f4..a8447dba2 100644 --- a/NebulaWorld/Statistics/StatisticsManager.cs +++ b/NebulaWorld/Statistics/StatisticsManager.cs @@ -21,6 +21,7 @@ public class StatisticsManager : IDisposable private PlanetData[] planetDataMap = new PlanetData[GameMain.data.factories.Length]; private List statisticalSnapShots = []; + private long lastUpdateTime; public bool IsStatisticsNeeded { get; set; } public long[] PowerEnergyStoredData { get; set; } @@ -92,12 +93,13 @@ public void CaptureStatisticalSnapshot() statisticalSnapShots.Add(snapshot); } - public void SendBroadcastIfNeeded() + public void SendBroadcastIfNeeded(long time) { - if (!IsStatisticsNeeded) + if (!IsStatisticsNeeded || time % 60 != 0 || lastUpdateTime == time) { return; } + lastUpdateTime = time; using (GetRequestors(out var requestors)) { if (requestors.Count <= 0)