Browse Source

Перемещение маркеров с реальными данными

Виктор Шейко 5 years ago
parent
commit
5c98edc867
3 changed files with 724 additions and 184 deletions
  1. 600 112
      Assets/Scenes/Location.unity
  2. 111 65
      Assets/Scripts/Controllers/WorkerController.cs
  3. 13 7
      Assets/Scripts/Net/Client.cs

File diff suppressed because it is too large
+ 600 - 112
Assets/Scenes/Location.unity


+ 111 - 65
Assets/Scripts/Controllers/WorkerController.cs

@@ -2,11 +2,13 @@
 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>();
@@ -15,24 +17,35 @@ public class WorkerController : MonoBehaviour
     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 StartStop;
+    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()
     {
-        System.Random rnd = new System.Random();
+        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
         //    {
@@ -56,31 +69,48 @@ public class WorkerController : MonoBehaviour
         //    });
         //}            
 
-        foreach(var s in structures.Select(s => s.acc_id).Distinct())
-        WorkerMarker(s);
+        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")));
+            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($"{s}"));
-        }
+        //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()
     {
-       foreach (var m in markers) 
-        m.marker.SetActive(m.toggle.isOn);
+        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);
+            }
+        }
     }
 
-    List<Marker> markers = new List<Marker>();
-    class Marker
+    public static List<Marker> markers = new List<Marker>();
+    public class Marker
     {
         public Toggle toggle { get; set; }
         public GameObject marker { get; set; }
@@ -115,33 +145,40 @@ public class WorkerController : MonoBehaviour
     /// <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)
+    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)
+        if (worker_marker.activeSelf == true && start && starting.ElementAt(count))
         {
-            //float smoothTime = 0.2f;
-            //float yVelocity = 0.5f;
-            //float x_position = Mathf.SmoothDamp(start_pos.x, pos.x, ref yVelocity, smoothTime);
-            //float y_position = Mathf.SmoothDamp(start_pos.z, pos.z, ref yVelocity, smoothTime);
             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")}";
-            //worker_marker.transform.position = pos;    
         }
     }
 
-    static IEnumerator RequestPostion(long ts, long te, uint loc)
+    void Position(float pause, float step, Vector3 end_pos, GameObject worker_marker, Toggle toggle, Structure w, int count)
     {
-        client.CoordinatesRequest(ts, te, 1, loc);
-         yield return new WaitForSeconds(5);
-        foreach (var t in TestStructures)
-            Debug.Log(t);
+        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>
     /// Запуск отрисовки
@@ -152,53 +189,62 @@ public class WorkerController : MonoBehaviour
         start = !start;
         if (start)
         {
-            StartStop.transform.GetChild(0).GetComponent<Text>().text = "Остановить";
-            var time_start = new DateTime(2020, 03, 26);//new DateTime(dates.ElementAt(DropdownData.value).Ticks);
+            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 = new DateTime(2020, 03, 26); // new DateTime(dates.ElementAt(DropdownData.value).Ticks);
-            //var tet = .Parse(EndHour.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;
-            //client.CoordinatesRequest(time_start.Ticks, time_end.Ticks, 1, 25);
-            //foreach (var t in TestStructures)
-            //    Debug.Log(t);
-
-            //##### 
-            StartCoroutine(RequestPostion(time_start.Ticks, time_end.Ticks, 1));
-
-            //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();
-            //if (workers.Any())
-            //    foreach (var m in markers)
-            //    {
-            //        if (m.toggle.isOn)
-            //        {
-            //            var worker = workers.Where(w => w.acc_id == m.acc_id).ToList();
-            //            var start_pos = m.marker.transform.position;
-
-            //            var offset = 0.1f;
-            //            for (int i = 0; i < worker.Count; i++)
-            //            {
-            //                var w = worker.ElementAt(i);
-            //                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));
-            //                }
-            //                offset += 0.1f;
-            //            }
-            //        }
-            //    }
-        } else
+            // 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
         {
-            StartStop.transform.GetChild(0).GetComponent<Text>().text = "Отобразить";
+            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;
+            }
         }
     }
 }

+ 13 - 7
Assets/Scripts/Net/Client.cs

@@ -135,16 +135,20 @@ public class Client : MonoBehaviour
         //var coordleng = bytedata.Length - 11;
         var accid = BitConverter.ToUInt32(bytedata, 3);
         var locid = BitConverter.ToUInt32(bytedata, 7);
-        WorkerController.TestStructures.Clear();
+        //WorkerController.TestStructures.Clear();
+        var worker = new List<Structure>();
         for (var read = 11; read < bytedata.Length; read += 20)
         {
             var id = BitConverter.ToUInt32(bytedata, read);
             var x = BitConverter.ToInt32(bytedata, read+4);
             var y = BitConverter.ToInt32(bytedata, read+8);
             var ticks = BitConverter.ToInt64(bytedata, read+12);
-            print($"coord accid {accid} locid {locid} {id} x {x} y{y} ticks {ticks}");            
+            print($"coord accid {accid} locid {locid} {id} x {x} y{y} ticks {ticks}");
 
-            WorkerController.TestStructures.Add(new Structure
+            //WorkerController.markers.ElementAt(1).marker.transform.position = new Vector3(x, 0.5f, y); 
+
+            //WorkerController.TestStructures
+            worker.Add(new Structure
             {
                 id = id,
                 coord_x = x,
@@ -155,6 +159,8 @@ public class Client : MonoBehaviour
                 zone_id = 1
             });
         }
+        WorkerController.Workers[accid] = worker;
+        WorkerController.end_send[accid] = true;
     }
 
     public void Myping(byte[] bytedata)
@@ -276,9 +282,9 @@ public class Client : MonoBehaviour
         }
 
         account_id = accid;
-        var ts = DateTime.Now.Ticks-1000000000000;
-        var te = DateTime.Now.Ticks;
-        CoordinatesRequest(ts, te, 1, 1);
+        //var ts = DateTime.Now.Ticks-1000000000000;
+        //var te = DateTime.Now.Ticks;
+        //CoordinatesRequest(ts, te, 1, 1);
     }
 
     #region system functions
@@ -751,7 +757,7 @@ public class Client : MonoBehaviour
     /// <summary>
     /// Запрос координат с сервера, timeStart, timeEnd == DateTime.Ticks
     /// </summary>
-    public void CoordinatesRequest(long timeStart, long timeEnd, byte resolution, uint locationID)
+    public void CoordinatesRequest(long timeStart, long timeEnd, byte resolution, uint locationID, uint account_id)
     {
         login_sent = true;
         Debug.Log("SendRequestCoordinates");