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
+60
View File
@@ -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
}
}
}
}