Spaces:
Sleeping
Sleeping
Download project/Assets/Scripts/Game/DinoResultReporter.cs from alfarouqwebserver/SWS-Smarta: direct link, hf CLI and curl.
- Browser
- Download file 2.64 kB
-
https://huggingface.co/spaces/alfarouqwebserver/SWS-Smarta/resolve/main/project/Assets/Scripts/Game/DinoResultReporter.cs
- Command line
-
hf download hf://spaces/alfarouqwebserver/SWS-Smarta/project/Assets/Scripts/Game/DinoResultReporter.cs
-
curl -L -o DinoResultReporter.cs https://huggingface.co/spaces/alfarouqwebserver/SWS-Smarta/resolve/main/project/Assets/Scripts/Game/DinoResultReporter.cs
2.64 kB
| using System; | |
| using System.Collections; | |
| using System.Collections.Generic; | |
| using System.Text; | |
| using Fusion; | |
| using UnityEngine; | |
| using UnityEngine.Networking; | |
| [] | |
| public class DinoWinEntry | |
| { | |
| public int player_id; | |
| public int wins; | |
| public string name; | |
| } | |
| [] | |
| public class DinoMatchReport | |
| { | |
| public string api_key; | |
| public string room; | |
| public string winner_name; | |
| public int winner_id; | |
| public int total_rounds; | |
| public int player_count; | |
| public DinoWinEntry[] wins; | |
| } | |
| public class DinoResultReporter : MonoBehaviour | |
| { | |
| public string serverUrl = "/game/report"; | |
| public string apiKey = ""; | |
| public bool autoReport = true; | |
| bool sent; | |
| public void ReportMatch(DinoGameManager gm) | |
| { | |
| if (!autoReport || sent || gm == null) | |
| return; | |
| if (string.IsNullOrWhiteSpace(serverUrl)) | |
| return; | |
| sent = true; | |
| StartCoroutine(SendReport(gm)); | |
| } | |
| public void ResetReporter() | |
| { | |
| sent = false; | |
| } | |
| IEnumerator SendReport(DinoGameManager gm) | |
| { | |
| NetworkRunner runner = FindObjectOfType<NetworkRunner>(); | |
| string roomName = runner != null ? runner.SessionInfo.Name : "dino-room"; | |
| List<DinoWinEntry> winList = new List<DinoWinEntry>(); | |
| DinoPlayer[] players = FindObjectsOfType<DinoPlayer>(); | |
| foreach (DinoPlayer p in players) | |
| { | |
| if (p.Object == null) | |
| continue; | |
| int id = p.Object.InputAuthority.PlayerId; | |
| int wins = 0; | |
| if (id >= 0 && id < gm.WinCounts.Length) | |
| wins = gm.WinCounts.Get(id); | |
| winList.Add(new DinoWinEntry | |
| { | |
| player_id = id, | |
| wins = wins, | |
| name = p.PlayerName.ToString() | |
| }); | |
| } | |
| DinoMatchReport payload = new DinoMatchReport | |
| { | |
| api_key = apiKey, | |
| room = roomName, | |
| winner_name = gm.WinnerName.ToString(), | |
| winner_id = gm.WinnerId, | |
| total_rounds = gm.CurrentRound, | |
| player_count = players.Length, | |
| wins = winList.ToArray() | |
| }; | |
| string json = JsonUtility.ToJson(payload); | |
| byte[] body = Encoding.UTF8.GetBytes(json); | |
| using (UnityWebRequest req = new UnityWebRequest(serverUrl, "POST")) | |
| { | |
| req.uploadHandler = new UploadHandlerRaw(body); | |
| req.downloadHandler = new DownloadHandlerBuffer(); | |
| req.SetRequestHeader("Content-Type", "application/json"); | |
| yield return req.SendWebRequest(); | |
| } | |
| } | |
| } | |