123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Networking;
- using UnityEngine.UI;
- public class DisplayFinalResults : MonoBehaviour
- {
- public Text resultsDisplayText;
- public GameObject errorPanel;
- private string resultsUrl = "https://smarthubs.media.tuwien.ac.at/api/get_all_player_points/" ;
-
- public void FetchAndDisplayResults()
- {
- StartCoroutine(GetFinalResults());
- }
- public IEnumerator GetFinalResults()
- {
- string gameNumber = FindObjectOfType<MasterCreateGame>().GetGameNumber();
-
- WWWForm form = new WWWForm();
-
- resultsUrl="https://smarthubs.media.tuwien.ac.at/api/get_all_player_points/";
- resultsUrl= resultsUrl + "" + gameNumber;
- Debug.Log("display:"+ resultsUrl);
- using (UnityWebRequest www = UnityWebRequest.Get(resultsUrl))
- {
- yield return www.SendWebRequest();
- if (www.result != UnityWebRequest.Result.Success)
- {
- Debug.Log(www.error);
- if (errorPanel != null)
- {
- errorPanel.SetActive(true);
- }
- }
- else
- {
- Debug.Log("Results fetched successfully");
-
-
-
- string rawResults = www.downloadHandler.text;
- Debug.Log("Raw results: " + rawResults);
- DisplayResultsNicely(rawResults);
-
- }
- }
- }
-
-
- private void DisplayResultsNicely(string rawResults)
- {
-
-
- rawResults = rawResults.Trim('{', '}').Trim();
- string[] playerEntries = rawResults.Split(',');
- string formattedResults = "";
- foreach (string entry in playerEntries)
- {
-
- string[] keyValue = entry.Split(':');
- if (keyValue.Length == 2)
- {
- string playerNumber = keyValue[0].Trim('"');
- string playerPoints = keyValue[1].Trim('}', '"');
- formattedResults += "Player " + playerNumber + ": " + playerPoints + " points\n";
- Debug.Log("Parsed result for Player " + playerNumber + ": " + playerPoints + " points");
- }
- }
-
- resultsDisplayText.text = formattedResults;
- Debug.Log("Final Display: " + formattedResults);
- }
-
- }
|