CurrentGameConnect.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.Networking;
  6. using System.Linq;
  7. public class CurrentGameConnect : MonoBehaviour
  8. {
  9. private string currentGameNumber;
  10. public GameObject errorPanel;
  11. public GameObject connectingPanel;
  12. public GameObject numberInput;
  13. public GameObject selectionPanel;
  14. public GameObject nextPanel;
  15. // Start is called before the first frame update
  16. void Start()
  17. {
  18. currentGameNumber = "0";
  19. GameObject parent = GameObject.Find("Canvas");
  20. }
  21. // Update is called once per frame
  22. void Update()
  23. {
  24. }
  25. public void checkIfGameExistsAndNotFinished()
  26. {
  27. string gameNumber = numberInput.GetComponent<InputField>().text;
  28. // SEND THE REQUEST TO SERVER
  29. // CHECK IF GAME NUMBERS EXISTS
  30. string serverUrl_check = "https://smarthubs.media.tuwien.ac.at/api/is_game_closed/" + gameNumber;
  31. connectingPanel.SetActive(true);
  32. StartCoroutine(CheckGame(serverUrl_check, gameNumber));
  33. //CHECK IF IT IS NOT FINISHED
  34. // IF NOT
  35. //currentGameNumber = GameNumber;
  36. // ELSE ERROR WINDOW POP-UP
  37. //onnectionError.SetActive(true);
  38. }
  39. public IEnumerator CheckGame(string serverUrl, string gameNumber)
  40. {
  41. UnityWebRequest www = UnityWebRequest.Get(serverUrl);
  42. yield return www.SendWebRequest();
  43. if (www.result != UnityWebRequest.Result.Success)
  44. {
  45. Debug.Log(www.error);
  46. connectingPanel.SetActive(false);
  47. errorPanel.SetActive(true);
  48. }
  49. else
  50. {
  51. // Show results as text
  52. //Debug.Log(www.downloadHandler.text);
  53. string is_game_closed = www.downloadHandler.text;
  54. Debug.Log(is_game_closed );
  55. // DSISPLAY GAME NUMBER
  56. // this.gameDisplayNumber.text = next_game_id;
  57. if(is_game_closed == "False")
  58. {
  59. connectingPanel.SetActive(false);
  60. currentGameNumber = gameNumber;
  61. selectionPanel.SetActive(false);
  62. nextPanel.SetActive(true);
  63. }
  64. else
  65. {
  66. connectingPanel.SetActive(false);
  67. errorPanel.SetActive(true);
  68. }
  69. }
  70. }
  71. public string GetGameNumber()
  72. {
  73. return this.currentGameNumber;
  74. }
  75. }