Z_get_RW_Objects.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. using UnityEngine.Networking;
  6. using System.Linq;
  7. using ARLocation;
  8. using Newtonsoft.Json;
  9. public class Z_get_RW_Objects : MonoBehaviour
  10. {
  11. private float elapsed = 0.0f;
  12. private float period = 1.5f;
  13. public GameObject gpsPrefab;
  14. public GameObject serverManager;
  15. public GameObject playerNumberObject;
  16. private string playerNumber;
  17. public List<GameObject> objectsPlay;
  18. private string serverUrl;
  19. // Start is called before the first frame update
  20. void Start()
  21. {
  22. this.serverUrl = "https://smarthubs.media.tuwien.ac.at/api/get_all_objects_in_game/";
  23. }
  24. // Update is called once per frame
  25. void Update()
  26. {
  27. elapsed += Time.deltaTime;
  28. if (elapsed >= period) {
  29. elapsed = 0.0f;
  30. string gn = serverManager.GetComponent<CurrentGameConnect>().GetGameNumber();
  31. string pn = playerNumberObject.GetComponent<CurrentPlayerPicker>().GetPlayerNumber();
  32. if(gn == null)
  33. return;
  34. if(pn == null)
  35. return;
  36. StartCoroutine(PlayedObjects(this.serverUrl, gn, pn));
  37. }
  38. }
  39. public IEnumerator PlayedObjects(string serverUrl, string gameN, string playerN)
  40. {
  41. serverUrl += gameN;
  42. this.playerNumber = playerN;
  43. UnityWebRequest www = UnityWebRequest.Get(serverUrl);
  44. yield return www.SendWebRequest();
  45. if (www.result != UnityWebRequest.Result.Success)
  46. {
  47. Debug.Log(www.error);
  48. }
  49. else
  50. {
  51. //var result = www.downloadHandler.text.Split(new[] { '},' })
  52. // .Select(s => s.Split(new[] { ':' }))
  53. // .ToDictionary(k => k[0].Replace("\"","" ).Replace("}","").Replace("{", "").Trim(), v => v[1].Replace("\"", "").Replace("}", "").Trim());
  54. //var result_list = result.Values.ToList();
  55. var values = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string,string>>>(www.downloadHandler.text);
  56. foreach (var uni in values)
  57. {
  58. //you can print values here or add to a list or ...
  59. string owner= uni.Value["owner"];
  60. string latitude = uni.Value["latitude"];
  61. string longitude = uni.Value["longitude"];
  62. string object_name = uni.Value["object_name"];
  63. if(owner != playerNumber)
  64. {
  65. foreach(GameObject obj in objectsPlay) {
  66. if(object_name.Contains(obj.name))
  67. {
  68. GameObject placedObject = Instantiate(obj);
  69. // SET COLOR
  70. placedObject.GetComponent<Z_color_Select>().SetObjectColor(playerNumber);
  71. // SET GPS COORDINATES
  72. var loc = new Location()
  73. {
  74. Latitude = Convert.ToDouble(latitude),
  75. Longitude = Convert.ToDouble(longitude),
  76. Altitude = 0.5f,
  77. AltitudeMode = AltitudeMode.GroundRelative
  78. };
  79. var opts = new PlaceAtLocation.PlaceAtOptions()
  80. {
  81. HideObjectUntilItIsPlaced = true,
  82. MaxNumberOfLocationUpdates = 2,
  83. MovementSmoothing = 0.1f,
  84. UseMovingAverage = false
  85. };
  86. PlaceAtLocation.AddPlaceAtComponent(placedObject, loc, opts);
  87. }
  88. }
  89. }
  90. }
  91. }
  92. }
  93. }