From 385b4f690e7707d51b0fd6b97f56d1a21cf698ec Mon Sep 17 00:00:00 2001 From: kerboul Date: Sun, 17 May 2026 21:55:01 +0200 Subject: [PATCH] fix: stats jamais envoyees - OnDisconnected avant Cleanup + cache du nom joueur NetworkManager: inverser ordre OnDisconnected/Cleanup pour que les listeners aient encore acces a LocalPlayerName au moment du callback. StatsTracker: mettre en cache le nom a la connexion comme fallback supplementaire. Co-Authored-By: Claude Sonnet 4.6 --- game/Assets/Scripts/Network/NetworkManager.cs | 2 +- game/Assets/Scripts/Stats/StatsTracker.cs | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/game/Assets/Scripts/Network/NetworkManager.cs b/game/Assets/Scripts/Network/NetworkManager.cs index fcdaefa..c245846 100644 --- a/game/Assets/Scripts/Network/NetworkManager.cs +++ b/game/Assets/Scripts/Network/NetworkManager.cs @@ -334,8 +334,8 @@ public class NetworkManager : MonoBehaviour private void OnRoomLeave(int code) { Debug.Log($"[Network] Left room (code: {code})"); + OnDisconnected?.Invoke(); // before Cleanup so listeners still have LocalPlayerName Cleanup(); - OnDisconnected?.Invoke(); } private void Cleanup() diff --git a/game/Assets/Scripts/Stats/StatsTracker.cs b/game/Assets/Scripts/Stats/StatsTracker.cs index b23d8bc..def13ff 100644 --- a/game/Assets/Scripts/Stats/StatsTracker.cs +++ b/game/Assets/Scripts/Stats/StatsTracker.cs @@ -32,6 +32,7 @@ public class StatsTracker : MonoBehaviour private Vector3 _lastPos; private bool _trackingActive; + private string _cachedName = ""; private PlayerController _pc; private Rigidbody _rb; @@ -119,6 +120,7 @@ public class StatsTracker : MonoBehaviour private void OnConnected() { + _cachedName = NetworkManager.Instance?.LocalPlayerName ?? ""; _lastPos = transform.position; _trackingActive = true; } @@ -163,9 +165,13 @@ public class StatsTracker : MonoBehaviour private void SendStats() { + // Prefer live name, fall back to cached (useful on disconnect where name is cleared) var nm = NetworkManager.Instance; - if (nm == null || string.IsNullOrEmpty(nm.LocalPlayerName)) return; - StartCoroutine(DoSendStats(nm.LocalPlayerName)); + string name = (nm != null && !string.IsNullOrEmpty(nm.LocalPlayerName)) + ? nm.LocalPlayerName + : _cachedName; + if (string.IsNullOrEmpty(name)) return; + StartCoroutine(DoSendStats(name)); } private IEnumerator DoSendStats(string playerName)