MasterCreateGame.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 MasterCreateGame : MonoBehaviour
  8. {
  9. private string gameNumber;
  10. public Text gameDisplayNumber;
  11. public GameObject selectionPanel;
  12. public GameObject errorPanel;
  13. public GameObject connectingPanel;
  14. public GameObject playerNumberInput;
  15. public GameObject locationInput;
  16. // Start is called before the first frame update
  17. void Start()
  18. {
  19. string serverUrl_nextGame = "https://smarthubs.media.tuwien.ac.at/api/get_next_game_id";
  20. StartCoroutine(getNextId(serverUrl_nextGame));
  21. // GET THE NEW GAME ID FROM SERVER
  22. // SHOVE IT INTO THE GAME NUMBER FIELD
  23. }
  24. // Update is called once per frame
  25. void Update()
  26. {
  27. }
  28. public void CreateGameHelper()
  29. {
  30. // SEND THE CREATE GAME TO SERVER
  31. string serverUrl_createGame = "https://smarthubs.media.tuwien.ac.at/api/create_game";
  32. connectingPanel.SetActive(true);
  33. StartCoroutine(CreateGame(serverUrl_createGame));
  34. connectingPanel.SetActive(false);
  35. }
  36. public IEnumerator getNextId(string serverUrl_nextGame)
  37. {
  38. UnityWebRequest www = UnityWebRequest.Get(serverUrl_nextGame);
  39. yield return www.SendWebRequest();
  40. if (www.result != UnityWebRequest.Result.Success)
  41. {
  42. Debug.Log(www.error);
  43. }
  44. else
  45. {
  46. // Show results as text
  47. //Debug.Log(www.downloadHandler.text);
  48. string next_game_id = www.downloadHandler.text;
  49. Debug.Log(next_game_id);
  50. this.gameNumber = next_game_id;
  51. // DSISPLAY GAME NUMBER
  52. this.gameDisplayNumber.text = next_game_id;
  53. }
  54. }
  55. public IEnumerator CreateGame(string ServerUrl)
  56. {
  57. string players = "4";
  58. string location = locationInput.GetComponent<InputField>().text;
  59. if(location == null)
  60. location = "";
  61. if(players == null)
  62. players = "";
  63. WWWForm form = new WWWForm();
  64. form.AddField("players", players);
  65. form.AddField("location", location);
  66. using (UnityWebRequest www = UnityWebRequest.Post(ServerUrl, form))
  67. {
  68. yield return www.SendWebRequest();
  69. if (www.result != UnityWebRequest.Result.Success)
  70. {
  71. Debug.Log(www.error);
  72. // PROMPT ERROR
  73. errorPanel.SetActive(true);
  74. }
  75. else
  76. {
  77. Debug.Log("Form upload complete!");
  78. Debug.Log(www.downloadHandler.text);
  79. // CLOSE THE PANEL
  80. this.selectionPanel.SetActive(false);
  81. }
  82. }
  83. }
  84. public string GetGameNumber()
  85. {
  86. return this.gameNumber;
  87. }
  88. }