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 <noreply@anthropic.com>
This commit is contained in:
2026-05-17 21:55:01 +02:00
parent b3651f8027
commit 385b4f690e
2 changed files with 9 additions and 3 deletions

View File

@@ -334,8 +334,8 @@ public class NetworkManager : MonoBehaviour
private void OnRoomLeave(int code) private void OnRoomLeave(int code)
{ {
Debug.Log($"[Network] Left room (code: {code})"); Debug.Log($"[Network] Left room (code: {code})");
OnDisconnected?.Invoke(); // before Cleanup so listeners still have LocalPlayerName
Cleanup(); Cleanup();
OnDisconnected?.Invoke();
} }
private void Cleanup() private void Cleanup()

View File

@@ -32,6 +32,7 @@ public class StatsTracker : MonoBehaviour
private Vector3 _lastPos; private Vector3 _lastPos;
private bool _trackingActive; private bool _trackingActive;
private string _cachedName = "";
private PlayerController _pc; private PlayerController _pc;
private Rigidbody _rb; private Rigidbody _rb;
@@ -119,6 +120,7 @@ public class StatsTracker : MonoBehaviour
private void OnConnected() private void OnConnected()
{ {
_cachedName = NetworkManager.Instance?.LocalPlayerName ?? "";
_lastPos = transform.position; _lastPos = transform.position;
_trackingActive = true; _trackingActive = true;
} }
@@ -163,9 +165,13 @@ public class StatsTracker : MonoBehaviour
private void SendStats() private void SendStats()
{ {
// Prefer live name, fall back to cached (useful on disconnect where name is cleared)
var nm = NetworkManager.Instance; var nm = NetworkManager.Instance;
if (nm == null || string.IsNullOrEmpty(nm.LocalPlayerName)) return; string name = (nm != null && !string.IsNullOrEmpty(nm.LocalPlayerName))
StartCoroutine(DoSendStats(nm.LocalPlayerName)); ? nm.LocalPlayerName
: _cachedName;
if (string.IsNullOrEmpty(name)) return;
StartCoroutine(DoSendStats(name));
} }
private IEnumerator DoSendStats(string playerName) private IEnumerator DoSendStats(string playerName)