123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading;
- using UnityEngine;
- 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 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>(); // определение завершения загрузки
- //List<bool> StartStop = new List<bool>();
- // Start is called before the first frame update
- void Start()
- {
- WorkerMarker(4);
- WorkerMarker(356);
- //System.Random rnd = new System.Random();
- //for (int i = 0; i < 240; i++)
- //{
- // var ts = new DateTime(2020, 03, 26, 10,Convert.ToInt32(i/60), i%60);
- // //else ts = new DateTime(2020, 03, 26, 10, 17, i - 60);
- // structures.Add(new Structure
- // {
- // id = (uint)structures.Count + 1,
- // coord_x = rnd.Next(0, 13),
- // coord_y = rnd.Next(0, 10),
- // ts = ts,
- // acc_id = 356,
- // location_id = 2,
- // zone_id = 2
- // });
- // structures.Add(new Structure
- // {
- // id = (uint)structures.Count + 1,
- // coord_x = rnd.Next(0, 13),
- // coord_y = rnd.Next(0, 10),
- // ts = ts,
- // acc_id = 341,
- // location_id = 22,
- // zone_id = 2
- // });
- //}
- 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;
- }
- 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);
- }
- }
- }
- 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.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>
- /// <returns></returns>
- static IEnumerator MarkerPostion(float pause,float step, Vector3 start_pos, Vector3 end_pos, GameObject worker_marker, Toggle toggle, Structure w, int count)
- {
- yield return new WaitForSeconds(pause);
- if (worker_marker.activeSelf == true && start && starting.ElementAt(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);
- toggle.transform.GetChild(1).gameObject.GetComponent<Text>().text = $"{w.acc_id} {w.ts.ToString("hh:mm:ss")}";
- }
- }
- void Position(float pause, float step, Vector3 end_pos, GameObject worker_marker, Toggle toggle, Structure w, int count)
- {
- if (worker_marker.activeSelf == true && starting.ElementAt(count))
- {
- var start_pos = worker_marker.transform.position;
- var x_position = Mathf.Lerp(start_pos.x, end_pos.x, 0.1f);
- var y_position = Mathf.Lerp(start_pos.z, end_pos.z, 0.1f);
- //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);
- 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()
- {
- start = !start;
- if (start)
- {
- StartStopButton.transform.GetChild(0).GetComponent<Text>().text = "Остановить";
- starting.Add(true);
- var time_start = DateTime.Parse(inputField_date.text);//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));
- //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 = DateTime.Parse(inputField_date.text); // 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));
- foreach (var m in markers)
- {
- if (m.toggle.isOn)
- {
- client.CoordinatesRequest(time_start.Ticks, time_end.Ticks, 1, uint.Parse(inputField_location.text), m.acc_id);
- end_send[m.acc_id] = 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;
- }
- }
- void StartingAccPositiong(Marker m)
- {
- var worker = Workers[m.acc_id].OrderBy(wr => wr.ts).ToList();
- if (Workers.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, 0.5f, worker.ElementAt(i - 1).coord_y);
- var end_pos = new Vector3(w.coord_x, 0.5f, w.coord_y);
- for (float j = 0; j <= 1.1f; j += 0.1f) // 1.1f из-за округления float
- {
- StartCoroutine(MarkerPostion(i + offset + j, j, start_pos, end_pos, m.marker, m.toggle, w, starting.Count - 1));
- //Position(i + offset + j, j, end_pos, m.marker, m.toggle, w, starting.Count - 1);
- }
- offset += 0.1f;
- }
- }
- }
- }
|