DisplayFinalResults.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Networking;
  5. using UnityEngine.UI;
  6. public class DisplayFinalResults : MonoBehaviour
  7. {
  8. public Text resultsDisplayText; // Assign this in the inspector to a UI Text element where results will be displayed
  9. public GameObject errorPanel; // A panel to display in case of errors
  10. private string resultsUrl = "https://smarthubs.media.tuwien.ac.at/api/get_all_player_points/" ; //server api to show
  11. // display the results, such as at the end of a game.
  12. public void FetchAndDisplayResults()
  13. {
  14. StartCoroutine(GetFinalResults());
  15. }
  16. public IEnumerator GetFinalResults()
  17. {
  18. string gameNumber = FindObjectOfType<MasterCreateGame>().GetGameNumber();
  19. // Assuming the server expects a game ID to fetch the results
  20. WWWForm form = new WWWForm();
  21. //form.AddField("game_id", gameNumber);
  22. resultsUrl="https://smarthubs.media.tuwien.ac.at/api/get_all_player_points/";
  23. resultsUrl= resultsUrl + "" + gameNumber;
  24. Debug.Log("display:"+ resultsUrl);
  25. using (UnityWebRequest www = UnityWebRequest.Get(resultsUrl)) //use get instead of post, since no modification needed in server side
  26. {
  27. yield return www.SendWebRequest();
  28. if (www.result != UnityWebRequest.Result.Success)
  29. {
  30. Debug.Log(www.error);
  31. if (errorPanel != null)
  32. {
  33. errorPanel.SetActive(true); // Show error panel if there is an error
  34. }
  35. }
  36. else
  37. {
  38. Debug.Log("Results fetched successfully");
  39. // Assuming the server returns results in a simple text format
  40. //resultsDisplayText.text = www.downloadHandler.text; // Display the results
  41. // Parse and display the results
  42. string rawResults = www.downloadHandler.text; // Rraw jason text from the server
  43. Debug.Log("Raw results: " + rawResults);
  44. DisplayResultsNicely(rawResults);
  45. }
  46. }
  47. }
  48. private void DisplayResultsNicely(string rawResults)
  49. {
  50. // Trim curly braces and split the string into key-value pairs
  51. //rawResults = rawResults.Trim('{', '}');
  52. rawResults = rawResults.Trim('{', '}').Trim();
  53. string[] playerEntries = rawResults.Split(',');
  54. string formattedResults = "";
  55. foreach (string entry in playerEntries)
  56. {
  57. // Split each key-value pair
  58. string[] keyValue = entry.Split(':');
  59. if (keyValue.Length == 2)
  60. {
  61. string playerNumber = keyValue[0].Trim('"'); // Remove extra quotes
  62. string playerPoints = keyValue[1].Trim('}', '"'); // Ensure no extra curly braces
  63. formattedResults += "Player " + playerNumber + ": " + playerPoints + " points\n";
  64. Debug.Log("Parsed result for Player " + playerNumber + ": " + playerPoints + " points");
  65. }
  66. }
  67. // Display the formatted results
  68. resultsDisplayText.text = formattedResults;
  69. Debug.Log("Final Display: " + formattedResults);
  70. }
  71. }