123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using UnityEngine.UI;
- public class WorkerController : MonoBehaviour
- {
- public static Dictionary<uint, List<Structure>> Workers = new Dictionary<uint, List<Structure>>();
- List<Structure> structures = new List<Structure>();
- public static List<Structure> TestStructures = new List<Structure>();
- List<DateTime> dates = new List<DateTime>();
- List<uint> locations = new List<uint>();
- public GameObject WorkersList; // scroll content
- public Dropdown DropdownLocation;
- public Dropdown DropdownData;
- public InputField inputField_date;
- public InputField inputField_location;
- public GameObject StartHour;
- public GameObject StartMin;
- public GameObject StartSec;
- public GameObject EndHour;
- public GameObject EndMin;
- public GameObject EndSec;
- public GameObject StartStopButton;
- static GameObject sStartStopButton;
- public GameObject ErrorDialog;
- public GameObject ButtonPlay;
- //public GameObject Time;
- static Client client = new Client();
- public List<GameObject> maps;
- static List<bool> starting = new List<bool>(); // для остановок
- public static Dictionary<uint, bool> end_send = new Dictionary<uint, bool>(); // определение завершения загрузки
- bool real_time = false;
- int interval = 1;
- float nextTime = 0;
- //List<bool> StartStop = new List<bool>();
- // Start is called before the first frame update
- void Start()
- {
- WorkerMarker(4);
- WorkerMarker(356);
- sStartStopButton = StartStopButton;
- foreach (var s in structures.Select(s => s.acc_id).Distinct())
- WorkerMarker(s);
- foreach (var s in structures.Select(s => s.ts.Date).Distinct())
- {
- dates.Add(s);
- DropdownData.options.Add(new Dropdown.OptionData(s.ToString("dd.MM.yyyy")));
- }
- //foreach (var s in structures.Select(s => s.location_id).Distinct())
- //{
- // locations.Add(s);
- DropdownLocation.options.Add(new Dropdown.OptionData("22 Офис Ижевск"));
- DropdownLocation.options.Add(new Dropdown.OptionData("25"));
- inputField_date.text = DateTime.Now.ToString("dd.MM.yyyy");
- //}
- }
- int active_location = 0;
- // Update is called once per frame
- void Update()
- {
- if (active_location != DropdownLocation.value)
- {
- maps.ElementAt(active_location).SetActive(false);
- maps.ElementAt(DropdownLocation.value).SetActive(true);
- active_location = DropdownLocation.value;
- switch (DropdownLocation.value)
- {
- case 0:
- inputField_location.text = "22";
- break;
- case 1:
- inputField_location.text = "25";
- break;
- }
- }
- foreach (var m in markers)
- {
- m.marker.SetActive(m.toggle.isOn);
- if (m.toggle.isOn && Workers.ContainsKey(m.acc_id) && starting.Last() && end_send[m.acc_id])
- {
- end_send[m.acc_id] = false;
- StartingAccPositiong(m);
- }
- }
- if (real_time && !inputField_location.text.Equals("") && Time.time >= nextTime)
- {
- var dateNow = DateTime.UtcNow.AddHours(3);
- //Time.GetComponent<Text>().text = dateNow.ToString();
- starting.Add(true);
- var location_id = uint.Parse(inputField_location.text);
- foreach (var m in markers)
- {
- if (m.toggle.isOn)
- {
- client.CoordinatesRequest(dateNow.AddSeconds(-2).Ticks, dateNow.Ticks, 1, location_id, m.acc_id);
-
- start = true;
- //end_send[m.acc_id] = false;
- }
- }
- nextTime += interval;
- }
- }
- public void Play()
- {
- real_time = !real_time;
- if(real_time) ButtonPlay.transform.GetChild(0).GetComponent<Text>().text = "Stop";
- else ButtonPlay.transform.GetChild(0).GetComponent<Text>().text = "Play";
- //Time.SetActive(true);
- }
- public static List<Marker> markers = new List<Marker>();
- public class Marker
- {
- public Toggle toggle { get; set; }
- public GameObject marker { get; set; }
- public uint acc_id { get; set; }
- }
- /// <summary>
- /// Создание маркера для сотрудника
- /// </summary>
- /// <param name="acc_id"></param>
- void WorkerMarker(uint acc_id)
- {
- var worker_toggle = Instantiate(Resources.Load("GameObjects/Toggle", typeof(Toggle))) as Toggle;
- worker_toggle.name = acc_id.ToString();
- worker_toggle.transform.GetChild(1).gameObject.GetComponent<Text>().text = acc_id.ToString();
- worker_toggle.transform.SetParent(WorkersList.transform);
- var worker_marker = Instantiate(Resources.Load("GameObjects/Capsule", typeof(GameObject))) as GameObject;
- worker_marker.name = "marker_" + acc_id.ToString();
- worker_marker.transform.GetChild(0).transform.GetChild(0).transform.GetChild(0).GetComponent<Text>().text = $"{acc_id}";
- worker_marker.GetComponent<Renderer>().material.color = UnityEngine.Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
- markers.Add(new Marker { acc_id = acc_id, marker = worker_marker, toggle = worker_toggle });
- }
- /// <summary>
- /// Смена положения маркера
- /// </summary>
- /// <param name="pause">Задержка</param>
- /// <param name="step">Шаг до нового положения</param>
- /// <param name="start_pos">Начальное положение</param>
- /// <param name="end_pos">Финальное положение</param>
- /// <param name="worker_marker">Маркер сотрудника</param>
- /// <param name="w">Данные из БД</param>
- /// <param name="count">Номер процесса/запуска</param>
- /// <param name="progress">Номер записи из БД</param>
- /// <param name="all">Всего записей из БД</param>
- /// <returns></returns>
- static IEnumerator MarkerPostion(float pause,float step, Vector3 start_pos, Vector3 end_pos, GameObject worker_marker, Toggle toggle, Structure w, int count, int progress, int all)
- {
- yield return new WaitForSeconds(pause);
- if (worker_marker.activeSelf == true && start && starting[count])
- {
- var x_position = Mathf.Lerp(start_pos.x, end_pos.x, step);
- var y_position = Mathf.Lerp(start_pos.z, end_pos.z, step);
- //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}");
- worker_marker.transform.position = new Vector3(x_position, 0.5f, y_position);
- worker_marker.transform.GetChild(0).transform.GetChild(0).transform.GetChild(0).GetComponent<Text>().text = $"{w.acc_id} x={end_pos.x} y={end_pos.z}";
- toggle.transform.GetChild(1).gameObject.GetComponent<Text>().text = $"{w.acc_id} {w.ts.ToString("hh:mm:ss")} {progress + 1} из {all}";
- if(progress + 1 == all)
- {
- sStartStopButton.transform.GetChild(0).GetComponent<Text>().text = "Отобразить";
- starting[count] = false;
- start = false;
- }
- }
- }
- void Position(float step, Vector3 start_pos, Vector3 end_pos, GameObject worker_marker, Toggle toggle, Structure w, int count)
- {
- if (worker_marker.activeSelf == true && start && starting[count])
- {
- var x_position = Mathf.Lerp(start_pos.x, end_pos.x, Time.time - interval);
- var y_position = Mathf.Lerp(start_pos.z, end_pos.z, Time.time - interval);
- //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}");
- worker_marker.transform.position = new Vector3(x_position, 0.5f, y_position);
- worker_marker.transform.GetChild(0).transform.GetChild(0).transform.GetChild(0).GetComponent<Text>().text = $"{w.acc_id} x={end_pos.x} y={end_pos.z}";
- toggle.transform.GetChild(1).gameObject.GetComponent<Text>().text = $"{w.acc_id} {w.ts.ToString("hh:mm:ss")}";
- }
- }
- //static IEnumerator RequestPostion(long ts, long te, uint loc, uint account_id)
- //{
- // client.CoordinatesRequest(ts, te, 1, loc);
- // yield return new WaitForSeconds(5);
- // foreach (var t in TestStructures)
- // Debug.Log(t);
- //}
- static bool start = false;
- /// <summary>
- /// Запуск отрисовки
- /// </summary>
- public void ButtonStart()
- {
- if (int.Parse(StartHour.GetComponent<InputField>().text) == 99) SceneManager.LoadScene("Editor");
- else start = !start;
- if (start)
- {
- starting.Add(true);
- var date = DateTime.Now; var error_date = false;
- try
- {
- date = DateTime.Parse(inputField_date.text);
- }
- catch (Exception e)
- {
- ErrorDialog.SetActive(true);
- error_date = true;
- var text = ErrorDialog.transform.GetChild(0).GetComponent<Text>().text = "Некорректный формат даты";
- start = false;
- }
- var time_start = date; //new DateTime(dates.ElementAt(DropdownData.value).Ticks);
- time_start = time_start.AddHours(int.Parse(StartHour.GetComponent<InputField>().text));
- time_start = time_start.AddMinutes(int.Parse(StartMin.GetComponent<InputField>().text));
- time_start = time_start.AddSeconds(int.Parse(StartSec.GetComponent<InputField>().text));
- Debug.Log(time_start);
- //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));
- //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));
- var time_end = date; //new DateTime(dates.ElementAt(DropdownData.value).Ticks);
- time_end = time_end.AddHours(int.Parse(EndHour.GetComponent<InputField>().text));
- time_end = time_end.AddMinutes(int.Parse(EndMin.GetComponent<InputField>().text));
- time_end = time_end.AddSeconds(int.Parse(EndSec.GetComponent<InputField>().text));
- // client.account_id = 356;
- //StartCoroutine(RequestPostion(time_start.Ticks, time_end.Ticks, 22, 356));
- if (!error_date)
- {
- try
- {
- var location_id = uint.Parse(inputField_location.text);
- foreach (var m in markers)
- {
- if (m.toggle.isOn)
- {
- client.CoordinatesRequest(time_start.Ticks, time_end.Ticks, 1, location_id, m.acc_id);
- //end_send[m.acc_id] = false;
- }
- }
- StartStopButton.transform.GetChild(0).GetComponent<Text>().text = "Остановить";
- }
- catch (Exception e)
- {
- ErrorDialog.SetActive(true);
- ErrorDialog.transform.GetChild(0).GetComponent<Text>().text = "Введите идентификатор локации";
- start = false;
- }
- }
- // 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();
- //foreach (var m in markers)
- // if (m.toggle.isOn)
- // StartingAccPositiong(m);
- }
- else
- {
- StartStopButton.transform.GetChild(0).GetComponent<Text>().text = "Отобразить";
- starting[starting.Count - 1] = false;
- }
- }
- public void ErrorDialogClose()
- {
- ErrorDialog.SetActive(false);
- }
- void StartingAccPositiong(Marker m)
- {
- var worker = Workers[m.acc_id].OrderBy(wr => wr.ts).ToList();
- if (worker.Any())
- {
- var offset = 0.1f;
- for (int i = 0; i < worker.Count; i++)
- {
- var w = worker.ElementAt(i);
- var start_pos = m.marker.transform.position;
- if (i != 0) start_pos = new Vector3(worker.ElementAt(i-1).coord_x /100f, 0.5f, worker.ElementAt(i-1).coord_y/100f);
-
- var end_pos = new Vector3(w.coord_x / 100f, 0.5f, w.coord_y / 100f);
- for (float j = 0; j <= 1.1f; j += 0.1f) // 1.1f из-за округления float
- {
- if(real_time) Position(j, start_pos, end_pos, m.marker, m.toggle, w, starting.Count - 1);
- else
- StartCoroutine(MarkerPostion(i + offset + j, j, start_pos, end_pos, m.marker, m.toggle, w, starting.Count - 1, i, worker.Count));
- }
- offset += 0.1f;
- }
- }
- }
- }
|