using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.Networking; using System.Linq; public class ObjectDropper : MonoBehaviour { public GameObject errorPanel; public int objectId; //used for qr code version, same object with different qrcode has different ids public string thisObjectPoints; //the point of this object public string thisObjectName; // name of the objects private bool isInplay; //play status private string objectOwner; //owner of the current object private string thisPlayer; //identifier of the current player public GameObject playerNumberObject; //indicate current player id public GameObject serverManager; //public Button deleteButton; //public List colors; //different colors indicate the ownership of different players public bool isMaster; // Start is called before the first frame update void Start() { isInplay = false; this.thisPlayer = "0"; } // Update is called once per frame void Update() { } public void objectWasScanned() { if(isInplay) return; if(isMaster) { isInplay = true; // GET OBJECT OWNER FROM SERVER string currentGameNum = serverManager.GetComponent().GetGameNumber(); StartCoroutine(GetObjectOwner(currentGameNum)); } this.thisPlayer = playerNumberObject.GetComponent().GetPlayerNumber(); if(thisPlayer == "0") return; string currentGame = serverManager.GetComponent().GetGameNumber(); if(currentGame == "0") return; StartCoroutine(IsObjectInPlay(thisPlayer, currentGame)); } //determine if the object is already in play for an existing game id public IEnumerator IsObjectInPlay(string player, string gameNumber) { string serverUrl = "https://smarthubs.media.tuwien.ac.at/api/object_in_play/" + gameNumber.ToString()+ "/" + this.objectId; //+ this.objectId UnityWebRequest www = UnityWebRequest.Get(serverUrl); yield return www.SendWebRequest(); if (www.result != UnityWebRequest.Result.Success) { Debug.Log(www.error); } else { if(www.downloadHandler.text == "False") { this.objectOwner = this.thisPlayer; this.isInplay = true; // CHANGE THE OBJECT COLOR //foreach(GameObject go in colors) //{ //if(go.name.Contains(thisPlayer)) //go.SetActive(true); //} // NOTIFY THE SERVER ABOUT THE NEW OWNER // NOPTIFY THE SERVER ABOUT THE POINTS StartCoroutine(SetObjectInPlay(thisPlayer, gameNumber)); } else { isInplay = true; // GET OBJECT OWNER FROM SERVER StartCoroutine(GetObjectOwner(gameNumber)); } } } public IEnumerator SetObjectInPlay(string player, string gameNumber) //transfer data to server { string serverUrl = "https://smarthubs.media.tuwien.ac.at/api/add_object"; WWWForm form = new WWWForm(); form.AddField("game", gameNumber); form.AddField("player", player); form.AddField("points", this.thisObjectPoints); form.AddField("oid", this.objectId); form.AddField("name", this.thisObjectName); using (UnityWebRequest www = UnityWebRequest.Post(serverUrl, form)) { yield return www.SendWebRequest(); if (www.result != UnityWebRequest.Result.Success) { //Debug.Log(www.error); // PROMPT ERROR //errorPanel.SetActive(true); } else { //Debug.Log("Form upload complete!"); } } } //function to remove the spawaned objects public void DeleteObject() { Debug.Log("server call"); if (!isInplay) return; Debug.Log("server "); string currentGame = serverManager.GetComponent().GetGameNumber(); StartCoroutine(DeleteObjectFromServer(currentGame, objectId)); // Make server call to delete points } private IEnumerator DeleteObjectFromServer(string gameNumber, int objectId) { Debug.Log("server message:"); string serverUrl = "https://smarthubs.media.tuwien.ac.at/api/delete_object"; WWWForm form = new WWWForm(); form.AddField("game", gameNumber); form.AddField("oid", objectId.ToString()); using (UnityWebRequest www = UnityWebRequest.Post(serverUrl, form)) { yield return www.SendWebRequest(); //Debug.Log("server message:" +www.result); if (www.result != UnityWebRequest.Result.Success) { Debug.Log(www.error); if (errorPanel != null) errorPanel.SetActive(true); } else { Debug.Log("Object and points successfully removed from the server."); Destroy(this.gameObject); // Destroy the GameObject after successful server response } } } //Retrieves the current owner of the object from the server and updates the object's color based on the owner. public IEnumerator GetObjectOwner(string gameNumber) { // CHANGE THE OBJECT COLOR string serverUrl = "https://smarthubs.media.tuwien.ac.at/api/get_object_owner/" + gameNumber.ToString()+ "/" + this.objectId; // + this.objectId UnityWebRequest www = UnityWebRequest.Get(serverUrl); yield return www.SendWebRequest(); if (www.result != UnityWebRequest.Result.Success) { Debug.Log(www.error); //errorPanel.SetActive(true); } else { string object_owner = www.downloadHandler.text; //foreach(GameObject go in colors) //{ //if(go.name.Contains(object_owner)) // go.SetActive(true); //} } } public void setThisPlayer(string thisPlayer) { this.thisPlayer = thisPlayer; } }