ObjectDropper.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 ObjectDropper : MonoBehaviour
  8. {
  9. public GameObject errorPanel;
  10. public int objectId; //used for qr code version, same object with different qrcode has different ids
  11. public string thisObjectPoints; //the point of this object
  12. public string thisObjectName; // name of the objects
  13. private bool isInplay; //play status
  14. private string objectOwner; //owner of the current object
  15. private string thisPlayer; //identifier of the current player
  16. public GameObject playerNumberObject; //indicate current player id
  17. public GameObject serverManager;
  18. //public Button deleteButton;
  19. //public List<GameObject> colors; //different colors indicate the ownership of different players
  20. public bool isMaster;
  21. // Start is called before the first frame update
  22. void Start()
  23. {
  24. isInplay = false;
  25. this.thisPlayer = "0";
  26. }
  27. // Update is called once per frame
  28. void Update()
  29. {
  30. }
  31. public void objectWasScanned()
  32. {
  33. if(isInplay)
  34. return;
  35. if(isMaster)
  36. {
  37. isInplay = true;
  38. // GET OBJECT OWNER FROM SERVER
  39. string currentGameNum = serverManager.GetComponent<CurrentGameConnect>().GetGameNumber();
  40. StartCoroutine(GetObjectOwner(currentGameNum));
  41. }
  42. this.thisPlayer = playerNumberObject.GetComponent<CurrentPlayerPicker>().GetPlayerNumber();
  43. if(thisPlayer == "0")
  44. return;
  45. string currentGame = serverManager.GetComponent<CurrentGameConnect>().GetGameNumber();
  46. if(currentGame == "0")
  47. return;
  48. StartCoroutine(IsObjectInPlay(thisPlayer, currentGame));
  49. }
  50. //determine if the object is already in play for an existing game id
  51. public IEnumerator IsObjectInPlay(string player, string gameNumber)
  52. {
  53. string serverUrl = "https://smarthubs.media.tuwien.ac.at/api/object_in_play/" + gameNumber.ToString()+ "/" + this.objectId; //+ this.objectId
  54. UnityWebRequest www = UnityWebRequest.Get(serverUrl);
  55. yield return www.SendWebRequest();
  56. if (www.result != UnityWebRequest.Result.Success)
  57. {
  58. Debug.Log(www.error);
  59. }
  60. else
  61. {
  62. if(www.downloadHandler.text == "False")
  63. {
  64. this.objectOwner = this.thisPlayer;
  65. this.isInplay = true;
  66. // CHANGE THE OBJECT COLOR
  67. //foreach(GameObject go in colors)
  68. //{
  69. //if(go.name.Contains(thisPlayer))
  70. //go.SetActive(true);
  71. //}
  72. // NOTIFY THE SERVER ABOUT THE NEW OWNER
  73. // NOPTIFY THE SERVER ABOUT THE POINTS
  74. StartCoroutine(SetObjectInPlay(thisPlayer, gameNumber));
  75. }
  76. else
  77. {
  78. isInplay = true;
  79. // GET OBJECT OWNER FROM SERVER
  80. StartCoroutine(GetObjectOwner(gameNumber));
  81. }
  82. }
  83. }
  84. public IEnumerator SetObjectInPlay(string player, string gameNumber) //transfer data to server
  85. {
  86. string serverUrl = "https://smarthubs.media.tuwien.ac.at/api/add_object";
  87. WWWForm form = new WWWForm();
  88. form.AddField("game", gameNumber);
  89. form.AddField("player", player);
  90. form.AddField("points", this.thisObjectPoints);
  91. form.AddField("oid", this.objectId);
  92. form.AddField("name", this.thisObjectName);
  93. using (UnityWebRequest www = UnityWebRequest.Post(serverUrl, form))
  94. {
  95. yield return www.SendWebRequest();
  96. if (www.result != UnityWebRequest.Result.Success)
  97. {
  98. //Debug.Log(www.error);
  99. // PROMPT ERROR
  100. //errorPanel.SetActive(true);
  101. }
  102. else
  103. {
  104. //Debug.Log("Form upload complete!");
  105. }
  106. }
  107. }
  108. //function to remove the spawaned objects
  109. public void DeleteObject()
  110. {
  111. Debug.Log("server call");
  112. if (!isInplay)
  113. return;
  114. Debug.Log("server ");
  115. string currentGame = serverManager.GetComponent<CurrentGameConnect>().GetGameNumber();
  116. StartCoroutine(DeleteObjectFromServer(currentGame, objectId)); // Make server call to delete points
  117. }
  118. private IEnumerator DeleteObjectFromServer(string gameNumber, int objectId)
  119. {
  120. Debug.Log("server message:");
  121. string serverUrl = "https://smarthubs.media.tuwien.ac.at/api/delete_object";
  122. WWWForm form = new WWWForm();
  123. form.AddField("game", gameNumber);
  124. form.AddField("oid", objectId.ToString());
  125. using (UnityWebRequest www = UnityWebRequest.Post(serverUrl, form))
  126. {
  127. yield return www.SendWebRequest();
  128. //Debug.Log("server message:" +www.result);
  129. if (www.result != UnityWebRequest.Result.Success)
  130. {
  131. Debug.Log(www.error);
  132. if (errorPanel != null)
  133. errorPanel.SetActive(true);
  134. }
  135. else
  136. {
  137. Debug.Log("Object and points successfully removed from the server.");
  138. Destroy(this.gameObject); // Destroy the GameObject after successful server response
  139. }
  140. }
  141. }
  142. //Retrieves the current owner of the object from the server and updates the object's color based on the owner.
  143. public IEnumerator GetObjectOwner(string gameNumber)
  144. {
  145. // CHANGE THE OBJECT COLOR
  146. string serverUrl = "https://smarthubs.media.tuwien.ac.at/api/get_object_owner/" + gameNumber.ToString()+ "/" + this.objectId; // + this.objectId
  147. UnityWebRequest www = UnityWebRequest.Get(serverUrl);
  148. yield return www.SendWebRequest();
  149. if (www.result != UnityWebRequest.Result.Success)
  150. {
  151. Debug.Log(www.error);
  152. //errorPanel.SetActive(true);
  153. }
  154. else
  155. {
  156. string object_owner = www.downloadHandler.text;
  157. //foreach(GameObject go in colors)
  158. //{
  159. //if(go.name.Contains(object_owner))
  160. // go.SetActive(true);
  161. //}
  162. }
  163. }
  164. public void setThisPlayer(string thisPlayer)
  165. {
  166. this.thisPlayer = thisPlayer;
  167. }
  168. }