ping reporting
This commit is contained in:
@@ -614,6 +614,12 @@ public class LoginManager : MonoBehaviour
|
||||
StartCoroutine(PurchaseRcCoroutine(packId, onComplete));
|
||||
}
|
||||
|
||||
/// <summary>POST /auth/ping-report with <c>{"match_id":123,"ping":42}</c>. Callback (success, errorOrNull, reportOrNull).</summary>
|
||||
public void ReportPing(int matchId, int ping, Action<bool, string, PingReportData> onComplete)
|
||||
{
|
||||
StartCoroutine(ReportPingCoroutine(matchId, ping, onComplete));
|
||||
}
|
||||
|
||||
IEnumerator PurchaseRcCoroutine(string packId, Action<bool, string> onComplete)
|
||||
{
|
||||
if (string.IsNullOrEmpty(AuthToken))
|
||||
@@ -687,6 +693,59 @@ public class LoginManager : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator ReportPingCoroutine(int matchId, int ping, Action<bool, string, PingReportData> onComplete)
|
||||
{
|
||||
if (string.IsNullOrEmpty(AuthToken))
|
||||
{
|
||||
onComplete?.Invoke(false, "Not signed in", null);
|
||||
yield break;
|
||||
}
|
||||
|
||||
string json = "{\"match_id\":" + matchId + ",\"ping\":" + ping + "}";
|
||||
string url = AuthBaseUrl + "/auth/ping-report";
|
||||
|
||||
using (var request = new UnityWebRequest(url, UnityWebRequest.kHttpVerbPOST))
|
||||
{
|
||||
byte[] bodyRaw = Encoding.UTF8.GetBytes(json);
|
||||
request.uploadHandler = new UploadHandlerRaw(bodyRaw);
|
||||
request.downloadHandler = new DownloadHandlerBuffer();
|
||||
request.SetRequestHeader("Content-Type", "application/json");
|
||||
request.SetRequestHeader("Authorization", "Bearer " + AuthToken);
|
||||
|
||||
yield return request.SendWebRequest();
|
||||
|
||||
string text = request.downloadHandler != null ? request.downloadHandler.text : "";
|
||||
|
||||
if (request.result != UnityWebRequest.Result.Success &&
|
||||
request.result != UnityWebRequest.Result.ProtocolError)
|
||||
{
|
||||
onComplete?.Invoke(false, string.IsNullOrEmpty(request.error) ? "Network error" : request.error, null);
|
||||
yield break;
|
||||
}
|
||||
|
||||
if (request.responseCode < 200 || request.responseCode >= 300)
|
||||
{
|
||||
onComplete?.Invoke(false, string.IsNullOrEmpty(text) ? "Request failed (" + request.responseCode + ")" : text, null);
|
||||
yield break;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(text))
|
||||
{
|
||||
onComplete?.Invoke(false, "Empty response from server", null);
|
||||
yield break;
|
||||
}
|
||||
|
||||
PingReportApiResponse resp = JsonUtility.FromJson<PingReportApiResponse>(text);
|
||||
if (resp != null && resp.ok && resp.report != null)
|
||||
onComplete?.Invoke(true, null, resp.report);
|
||||
else
|
||||
{
|
||||
string err = resp != null && !string.IsNullOrEmpty(resp.error) ? resp.error : "Ping report failed";
|
||||
onComplete?.Invoke(false, err, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator FetchUserDataCoroutine(Action<bool, string, UserData> onComplete, bool applyToStatic)
|
||||
{
|
||||
if (string.IsNullOrEmpty(AuthToken))
|
||||
@@ -739,4 +798,5 @@ public class LoginManager : MonoBehaviour
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
[Serializable]
|
||||
public class PingReportApiResponse
|
||||
{
|
||||
public bool ok;
|
||||
public string error;
|
||||
public PingReportData report;
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 76e4e69960816204d8a01d0e534e0bf4
|
||||
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
[Serializable]
|
||||
public class PingReportData
|
||||
{
|
||||
public int id;
|
||||
public int user_id;
|
||||
public int match_id;
|
||||
public int ping;
|
||||
public string ip_address;
|
||||
public string created_at;
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b3f6089d3b18cb34482d7de0cd8bca51
|
||||
Reference in New Issue
Block a user