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;
- public string thisObjectPoints;
- public string thisObjectName;
- private bool isInplay;
- private string objectOwner;
- private string thisPlayer;
- public GameObject playerNumberObject;
- public GameObject serverManager;
-
-
- public bool isMaster;
-
- void Start()
- {
- isInplay = false;
- this.thisPlayer = "0";
- }
-
- void Update()
- {
-
-
- }
- public void objectWasScanned()
- {
- if(isInplay)
- return;
- if(isMaster)
- {
- isInplay = true;
-
- 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));
- }
-
- public IEnumerator IsObjectInPlay(string player, string gameNumber)
- {
- string serverUrl = "https://smarthubs.media.tuwien.ac.at/api/object_in_play/" + gameNumber.ToString()+ "/" + 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;
-
-
-
-
-
-
-
-
- StartCoroutine(SetObjectInPlay(thisPlayer, gameNumber));
- }
- else
- {
- isInplay = true;
-
- StartCoroutine(GetObjectOwner(gameNumber));
-
- }
- }
- }
- public IEnumerator SetObjectInPlay(string player, string gameNumber)
- {
- 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)
- {
-
-
-
- }
- else
- {
-
-
- }
- }
- }
-
- public void DeleteObject()
- {
- Debug.Log("server call");
- if (!isInplay)
- return;
- Debug.Log("server ");
- string currentGame = serverManager.GetComponent<CurrentGameConnect>().GetGameNumber();
- StartCoroutine(DeleteObjectFromServer(currentGame, objectId));
- }
- 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();
-
- 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);
- }
- }
- }
-
-
- public IEnumerator GetObjectOwner(string gameNumber)
- {
-
- string serverUrl = "https://smarthubs.media.tuwien.ac.at/api/get_object_owner/" + gameNumber.ToString()+ "/" + this.objectId;
- UnityWebRequest www = UnityWebRequest.Get(serverUrl);
- yield return www.SendWebRequest();
- if (www.result != UnityWebRequest.Result.Success)
- {
- Debug.Log(www.error);
-
- }
- else
- {
- string object_owner = www.downloadHandler.text;
-
-
-
-
-
- }
- }
- public void setThisPlayer(string thisPlayer)
- {
- this.thisPlayer = thisPlayer;
- }
- }
|