123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- 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<GameObject> 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<CurrentGameConnect>().GetGameNumber();
- StartCoroutine(GetObjectOwner(currentGameNum));
- }
- this.thisPlayer = playerNumberObject.GetComponent<CurrentPlayerPicker>().GetPlayerNumber();
- if(thisPlayer == "0")
- return;
- string currentGame = serverManager.GetComponent<CurrentGameConnect>().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<CurrentGameConnect>().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;
- }
- }
|