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> Workers = new Dictionary>(); List structures = new List(); public static List TestStructures = new List(); List dates = new List(); List locations = new List(); 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 maps; static List starting = new List(); // для остановок public static Dictionary end_send = new Dictionary(); // определение завершения загрузки bool real_time = false; int interval = 1; float nextTime = 0; //List StartStop = new List(); // 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 = 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 = "Stop"; else ButtonPlay.transform.GetChild(0).GetComponent().text = "Play"; //Time.SetActive(true); } public static List markers = new List(); public class Marker { public Toggle toggle { get; set; } public GameObject marker { get; set; } public uint acc_id { get; set; } } /// /// Создание маркера для сотрудника /// /// 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 = 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 = $"{acc_id}"; worker_marker.GetComponent().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 }); } /// /// Смена положения маркера /// /// Задержка /// Шаг до нового положения /// Начальное положение /// Финальное положение /// Маркер сотрудника /// Данные из БД /// Номер процесса/запуска /// Номер записи из БД /// Всего записей из БД /// 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 = $"{w.acc_id} x={end_pos.x} y={end_pos.z}"; toggle.transform.GetChild(1).gameObject.GetComponent().text = $"{w.acc_id} {w.ts.ToString("hh:mm:ss")} {progress + 1} из {all}"; if(progress + 1 == all) { sStartStopButton.transform.GetChild(0).GetComponent().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 = $"{w.acc_id} x={end_pos.x} y={end_pos.z}"; toggle.transform.GetChild(1).gameObject.GetComponent().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; /// /// Запуск отрисовки /// public void ButtonStart() { if (int.Parse(StartHour.GetComponent().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 = "Некорректный формат даты"; start = false; } var time_start = date; //new DateTime(dates.ElementAt(DropdownData.value).Ticks); time_start = time_start.AddHours(int.Parse(StartHour.GetComponent().text)); time_start = time_start.AddMinutes(int.Parse(StartMin.GetComponent().text)); time_start = time_start.AddSeconds(int.Parse(StartSec.GetComponent().text)); Debug.Log(time_start); //var time_start = new DateTime(1, 1, 1, int.Parse(StartHour.GetComponent().text), int.Parse(StartMin.GetComponent().text), int.Parse(StartSec.GetComponent().text)); //var time_end = new DateTime(1, 1, 1, int.Parse(EndHour.GetComponent().text), int.Parse(EndMin.GetComponent().text), int.Parse(EndSec.GetComponent().text)); var time_end = date; //new DateTime(dates.ElementAt(DropdownData.value).Ticks); time_end = time_end.AddHours(int.Parse(EndHour.GetComponent().text)); time_end = time_end.AddMinutes(int.Parse(EndMin.GetComponent().text)); time_end = time_end.AddSeconds(int.Parse(EndSec.GetComponent().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 = "Остановить"; } catch (Exception e) { ErrorDialog.SetActive(true); ErrorDialog.transform.GetChild(0).GetComponent().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 = "Отобразить"; 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; } } } }