WorkerController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. public class WorkerController : MonoBehaviour
  9. {
  10. public static Dictionary<uint, List<Structure>> Workers = new Dictionary<uint, List<Structure>>();
  11. List<Structure> structures = new List<Structure>();
  12. public static List<Structure> TestStructures = new List<Structure>();
  13. List<DateTime> dates = new List<DateTime>();
  14. List<uint> locations = new List<uint>();
  15. public GameObject WorkersList; // scroll content
  16. public Dropdown DropdownLocation;
  17. public Dropdown DropdownData;
  18. public InputField inputField_date;
  19. public InputField inputField_location;
  20. public GameObject StartHour;
  21. public GameObject StartMin;
  22. public GameObject StartSec;
  23. public GameObject EndHour;
  24. public GameObject EndMin;
  25. public GameObject EndSec;
  26. public GameObject StartStopButton;
  27. static Client client = new Client();
  28. public List<GameObject> maps;
  29. static List<bool> starting = new List<bool>(); // для остановок
  30. public static Dictionary<uint, bool> end_send = new Dictionary<uint, bool>(); // определение завершения загрузки
  31. //List<bool> StartStop = new List<bool>();
  32. // Start is called before the first frame update
  33. void Start()
  34. {
  35. WorkerMarker(4);
  36. WorkerMarker(356);
  37. //System.Random rnd = new System.Random();
  38. //for (int i = 0; i < 240; i++)
  39. //{
  40. // var ts = new DateTime(2020, 03, 26, 10,Convert.ToInt32(i/60), i%60);
  41. // //else ts = new DateTime(2020, 03, 26, 10, 17, i - 60);
  42. // structures.Add(new Structure
  43. // {
  44. // id = (uint)structures.Count + 1,
  45. // coord_x = rnd.Next(0, 13),
  46. // coord_y = rnd.Next(0, 10),
  47. // ts = ts,
  48. // acc_id = 356,
  49. // location_id = 2,
  50. // zone_id = 2
  51. // });
  52. // structures.Add(new Structure
  53. // {
  54. // id = (uint)structures.Count + 1,
  55. // coord_x = rnd.Next(0, 13),
  56. // coord_y = rnd.Next(0, 10),
  57. // ts = ts,
  58. // acc_id = 341,
  59. // location_id = 22,
  60. // zone_id = 2
  61. // });
  62. //}
  63. foreach (var s in structures.Select(s => s.acc_id).Distinct())
  64. WorkerMarker(s);
  65. foreach (var s in structures.Select(s => s.ts.Date).Distinct())
  66. {
  67. dates.Add(s);
  68. DropdownData.options.Add(new Dropdown.OptionData(s.ToString("dd.MM.yyyy")));
  69. }
  70. //foreach (var s in structures.Select(s => s.location_id).Distinct())
  71. //{
  72. // locations.Add(s);
  73. DropdownLocation.options.Add(new Dropdown.OptionData("22 Офис Ижевск"));
  74. DropdownLocation.options.Add(new Dropdown.OptionData("25"));
  75. inputField_date.text = DateTime.Now.ToString("dd.MM.yyyy");
  76. //}
  77. }
  78. int active_location = 0;
  79. // Update is called once per frame
  80. void Update()
  81. {
  82. if (active_location != DropdownLocation.value)
  83. {
  84. maps.ElementAt(active_location).SetActive(false);
  85. maps.ElementAt(DropdownLocation.value).SetActive(true);
  86. active_location = DropdownLocation.value;
  87. }
  88. foreach (var m in markers)
  89. {
  90. m.marker.SetActive(m.toggle.isOn);
  91. if (m.toggle.isOn && Workers.ContainsKey(m.acc_id) && starting.Last() && end_send[m.acc_id])
  92. {
  93. end_send[m.acc_id] = false;
  94. StartingAccPositiong(m);
  95. }
  96. }
  97. }
  98. public static List<Marker> markers = new List<Marker>();
  99. public class Marker
  100. {
  101. public Toggle toggle { get; set; }
  102. public GameObject marker { get; set; }
  103. public uint acc_id { get; set; }
  104. }
  105. /// <summary>
  106. /// Создание маркера для сотрудника
  107. /// </summary>
  108. /// <param name="acc_id"></param>
  109. void WorkerMarker(uint acc_id)
  110. {
  111. var worker_toggle = Instantiate(Resources.Load("GameObjects/Toggle", typeof(Toggle))) as Toggle;
  112. worker_toggle.name = acc_id.ToString();
  113. worker_toggle.transform.GetChild(1).gameObject.GetComponent<Text>().text = acc_id.ToString();
  114. worker_toggle.transform.SetParent(WorkersList.transform);
  115. var worker_marker = Instantiate(Resources.Load("GameObjects/Capsule", typeof(GameObject))) as GameObject;
  116. worker_marker.name = "marker_" + acc_id.ToString();
  117. worker_marker.GetComponent<Renderer>().material.color = UnityEngine.Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
  118. markers.Add(new Marker { acc_id = acc_id, marker = worker_marker, toggle = worker_toggle });
  119. }
  120. /// <summary>
  121. /// Смена положения маркера
  122. /// </summary>
  123. /// <param name="pause">Задержка</param>
  124. /// <param name="step">Шаг до нового положения</param>
  125. /// <param name="start_pos">Начальное положение</param>
  126. /// <param name="end_pos">Финальное положение</param>
  127. /// <param name="worker_marker">Маркер сотрудника</param>
  128. /// <returns></returns>
  129. static IEnumerator MarkerPostion(float pause,float step, Vector3 start_pos, Vector3 end_pos, GameObject worker_marker, Toggle toggle, Structure w, int count)
  130. {
  131. yield return new WaitForSeconds(pause);
  132. if (worker_marker.activeSelf == true && start && starting.ElementAt(count))
  133. {
  134. var x_position = Mathf.Lerp(start_pos.x, end_pos.x, step);
  135. var y_position = Mathf.Lerp(start_pos.z, end_pos.z, step);
  136. //Debug.Log($"marker name={worker_marker.name} time={pause} step={step} sec x={x_position} y={y_position} x0={start_pos.x} y0={start_pos.z} x1={end_pos.x} y1={end_pos.z}");
  137. worker_marker.transform.position = new Vector3(x_position, 0.5f, y_position);
  138. toggle.transform.GetChild(1).gameObject.GetComponent<Text>().text = $"{w.acc_id} {w.ts.ToString("hh:mm:ss")}";
  139. }
  140. }
  141. void Position(float pause, float step, Vector3 end_pos, GameObject worker_marker, Toggle toggle, Structure w, int count)
  142. {
  143. if (worker_marker.activeSelf == true && starting.ElementAt(count))
  144. {
  145. var start_pos = worker_marker.transform.position;
  146. var x_position = Mathf.Lerp(start_pos.x, end_pos.x, 0.1f);
  147. var y_position = Mathf.Lerp(start_pos.z, end_pos.z, 0.1f);
  148. //Debug.Log($"marker name={worker_marker.name} time={pause} step={step} sec x={x_position} y={y_position} x0={start_pos.x} y0={start_pos.z} x1={end_pos.x} y1={end_pos.z}");
  149. worker_marker.transform.position = new Vector3(x_position, 0.5f, y_position);
  150. toggle.transform.GetChild(1).gameObject.GetComponent<Text>().text = $"{w.acc_id} {w.ts.ToString("hh:mm:ss")}";
  151. }
  152. }
  153. //static IEnumerator RequestPostion(long ts, long te, uint loc, uint account_id)
  154. //{
  155. // client.CoordinatesRequest(ts, te, 1, loc);
  156. // yield return new WaitForSeconds(5);
  157. // foreach (var t in TestStructures)
  158. // Debug.Log(t);
  159. //}
  160. static bool start = false;
  161. /// <summary>
  162. /// Запуск отрисовки
  163. /// Остановить нельзя наверное
  164. /// </summary>
  165. public void ButtonStart()
  166. {
  167. start = !start;
  168. if (start)
  169. {
  170. StartStopButton.transform.GetChild(0).GetComponent<Text>().text = "Остановить";
  171. starting.Add(true);
  172. var time_start = DateTime.Parse(inputField_date.text);//new DateTime(dates.ElementAt(DropdownData.value).Ticks);
  173. time_start = time_start.AddHours(int.Parse(StartHour.GetComponent<InputField>().text));
  174. time_start = time_start.AddMinutes(int.Parse(StartMin.GetComponent<InputField>().text));
  175. time_start = time_start.AddSeconds(int.Parse(StartSec.GetComponent<InputField>().text));
  176. //var time_start = new DateTime(1, 1, 1, int.Parse(StartHour.GetComponent<InputField>().text), int.Parse(StartMin.GetComponent<InputField>().text), int.Parse(StartSec.GetComponent<InputField>().text));
  177. //var time_end = new DateTime(1, 1, 1, int.Parse(EndHour.GetComponent<InputField>().text), int.Parse(EndMin.GetComponent<InputField>().text), int.Parse(EndSec.GetComponent<InputField>().text));
  178. var time_end = DateTime.Parse(inputField_date.text); // new DateTime(dates.ElementAt(DropdownData.value).Ticks);
  179. time_end = time_end.AddHours(int.Parse(EndHour.GetComponent<InputField>().text));
  180. time_end = time_end.AddMinutes(int.Parse(EndMin.GetComponent<InputField>().text));
  181. time_end = time_end.AddSeconds(int.Parse(EndSec.GetComponent<InputField>().text));
  182. // client.account_id = 356;
  183. //StartCoroutine(RequestPostion(time_start.Ticks, time_end.Ticks, 22, 356));
  184. foreach (var m in markers)
  185. {
  186. if (m.toggle.isOn)
  187. {
  188. client.CoordinatesRequest(time_start.Ticks, time_end.Ticks, 1, uint.Parse(inputField_location.text), m.acc_id);
  189. end_send[m.acc_id] = false;
  190. }
  191. }
  192. // var workers = structures.Where(w => w.ts >= time_start && w.ts <= time_end && w.location_id == int.Parse(DropdownLocation.options[DropdownLocation.value].text.ToString())).ToList();
  193. //foreach (var m in markers)
  194. // if (m.toggle.isOn)
  195. // StartingAccPositiong(m);
  196. }
  197. else
  198. {
  199. StartStopButton.transform.GetChild(0).GetComponent<Text>().text = "Отобразить";
  200. starting[starting.Count - 1] = false;
  201. }
  202. }
  203. void StartingAccPositiong(Marker m)
  204. {
  205. var worker = Workers[m.acc_id].OrderBy(wr => wr.ts).ToList();
  206. if (Workers.Any())
  207. {
  208. var offset = 0.1f;
  209. for (int i = 0; i < worker.Count; i++)
  210. {
  211. var w = worker.ElementAt(i);
  212. var start_pos = m.marker.transform.position;
  213. if (i != 0) start_pos = new Vector3(worker.ElementAt(i - 1).coord_x, 0.5f, worker.ElementAt(i - 1).coord_y);
  214. var end_pos = new Vector3(w.coord_x, 0.5f, w.coord_y);
  215. for (float j = 0; j <= 1.1f; j += 0.1f) // 1.1f из-за округления float
  216. {
  217. StartCoroutine(MarkerPostion(i + offset + j, j, start_pos, end_pos, m.marker, m.toggle, w, starting.Count - 1));
  218. //Position(i + offset + j, j, end_pos, m.marker, m.toggle, w, starting.Count - 1);
  219. }
  220. offset += 0.1f;
  221. }
  222. }
  223. }
  224. }