ping reporting

This commit is contained in:
2026-06-02 14:47:32 +05:30
parent d62bf9e06b
commit 2651068b50
12 changed files with 2756 additions and 62 deletions
+33
View File
@@ -8,6 +8,7 @@ using UnityEngine.InputSystem;
#endif
public class NetPlayer : NetworkBehaviour
{
const float PingReportIntervalSeconds = 30f;
[SyncVar]
public int userId;
@@ -99,6 +100,7 @@ public class NetPlayer : NetworkBehaviour
bool isDragging;
Vector2 lastDragScreenPosition;
bool serverDragActive;
Coroutine pingReportCoroutine;
#region EMOTES
public void SendTextEmote(string txtName){
@@ -158,10 +160,16 @@ public class NetPlayer : NetworkBehaviour
if(isLocalPlayer){
localPlayer = this;
SetupInputPanel();
pingReportCoroutine = StartCoroutine(CoReportPingPeriodically());
}
}
void OnDestroy(){
if (pingReportCoroutine != null)
{
StopCoroutine(pingReportCoroutine);
pingReportCoroutine = null;
}
if(localPlayer == this){
localPlayer = null;
}
@@ -555,4 +563,29 @@ public class NetPlayer : NetworkBehaviour
GameOverCanvas.instance.OnRematchCancelled();
}
IEnumerator CoReportPingPeriodically()
{
var wait = new WaitForSecondsRealtime(PingReportIntervalSeconds);
while (isLocalPlayer)
{
yield return wait;
if (!NetworkClient.active || !NetworkClient.isConnected)
continue;
if (LoginManager.instance == null || string.IsNullOrEmpty(LoginManager.AuthToken))
continue;
int matchId = GameManager.DedicatedMatchId;
if (matchId <= 0)
continue;
int pingMs = Mathf.Max(0, Mathf.RoundToInt((float)(NetworkTime.rtt * 1000.0)));
LoginManager.instance.ReportPing(matchId, pingMs, (ok, err, report) =>
{
if (!ok && !string.IsNullOrEmpty(err))
Logger.Log("Ping report failed: " + err);
});
}
}
}