User.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using UnityEngine;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using UnityEngine.UI;
  8. public class User
  9. {
  10. enum GetUserFlags { CompanyID, UserID };
  11. public uint id { get; set; }
  12. public string name { get; set; }
  13. public bool online { get; set; }
  14. public Toggle toggle { get; set; }
  15. public GameObject toggle_user { get; set; }
  16. public GameObject marker { get; set; }
  17. public LineRenderer marker_line { get; set; }
  18. //public uint acc_id { get; set; }
  19. //public bool starting { get; set; }
  20. public bool broadcast { get; set; }
  21. public static void UsersReceive(byte[] bytedata)
  22. {
  23. var player = PlayerController.instance;
  24. player.users = new Dictionary<uint, User>();
  25. int read = 5;
  26. while (read < bytedata.Length)
  27. {
  28. var id = BitConverter.ToUInt32(bytedata, read);
  29. var online = bytedata[read + 4];
  30. var lenname = bytedata[read + 5];
  31. var name = Encoding.UTF8.GetString(bytedata, read + 6, lenname);
  32. read += 6 + lenname;
  33. Debug.Log($"user received id {id} lenname {lenname} name {name} online {online}");
  34. player.users.Add(id, new User { id = id, name = name, online = online!=0 });
  35. WorkerMarker(player.users[id], Color.green);
  36. }
  37. player.users_load = true;
  38. }
  39. public static void SendGetUsers()
  40. {
  41. SendGetUsersForCompany(Client.instance.company_id);
  42. }
  43. //companyId: 1 = Тайшет, 2 = Тестовая, 3 = Братское
  44. public static void SendGetUsersForCompany(uint companyId)
  45. {
  46. List<byte> list = new List<byte>();
  47. list.Add(47);
  48. list.Add((byte)GetUserFlags.CompanyID);
  49. list.AddRange(BitConverter.GetBytes(companyId));
  50. Client.SendEnqueue(list.ToArray());
  51. }
  52. public static void SendGetUser(uint userId)
  53. {
  54. List<byte> list = new List<byte>();
  55. list.Add(47);
  56. list.Add((byte)GetUserFlags.UserID);
  57. list.AddRange(BitConverter.GetBytes(userId));
  58. Client.SendEnqueue(list.ToArray());
  59. }
  60. public static User Find(uint id)
  61. {
  62. User user;
  63. PlayerController.instance.users.TryGetValue(id, out user);
  64. return user;
  65. }
  66. public static void LogInOut(byte[] bytedata)
  67. {
  68. var accid = BitConverter.ToUInt32(bytedata, 1);
  69. var cmd = bytedata[5];
  70. var u = User.Find(accid);
  71. if (u != null)
  72. {
  73. var b = cmd != 0;
  74. u.toggle_user.SetActive(b);
  75. u.marker.SetActive(b);
  76. }
  77. else
  78. {
  79. Debug.LogError($"LogInOut accid {accid} not found");
  80. SendGetUser(accid);
  81. }
  82. Debug.Log($"LogInOut accid {accid} cmd {cmd}");
  83. }
  84. public static void CoordinatesReceive(byte[] bytedata)
  85. {
  86. //var coordleng = bytedata.Length - 11;
  87. var accid = BitConverter.ToUInt32(bytedata, 5);
  88. var locid = BitConverter.ToUInt32(bytedata, 9);
  89. var player = PlayerController.instance;
  90. //WorkerController.TestStructures.Clear();
  91. //print("len " + BitConverter.ToUInt32(bytedata, 1));
  92. var worker = new List<Structure>();
  93. for (var read = 13; read < bytedata.Length; read += 20)
  94. {
  95. var id = BitConverter.ToUInt32(bytedata, read);
  96. var x = BitConverter.ToSingle(bytedata, read + 4);
  97. var y = BitConverter.ToSingle(bytedata, read + 8);
  98. var ticks = BitConverter.ToInt64(bytedata, read + 12);
  99. //print($"coord accid {accid} locid {locid} {id} x {x} y{y} ticks {ticks}");
  100. //WorkerController.markers.ElementAt(1).marker.transform.position = new Vector3(x, 0.5f, y);
  101. //WorkerController.TestStructures
  102. worker.Add(new Structure
  103. {
  104. id = id,
  105. coord_x = x,
  106. coord_y = y,
  107. ts = new DateTime(ticks),
  108. acc_id = accid,
  109. location_id = locid,
  110. zone_id = 1
  111. });
  112. }
  113. player.Workers[accid] = worker.OrderBy(w => w.ts).ToList();
  114. player.end_send[accid] = true;
  115. }
  116. /// <summary>
  117. /// Создание маркера для сотрудника
  118. /// </summary>
  119. /// <param name="acc_id"></param>
  120. public static void WorkerMarker(User user, Color color/*, GameObject WorkersList, Action<uint> BroadcastStart*/)
  121. {
  122. var toggle_user = UnityEngine.Object.Instantiate(Resources.Load("GameObjects/ToggleUser", typeof(GameObject))) as GameObject;
  123. toggle_user.name = $"{user.id}";
  124. var toggle = toggle_user.transform.GetChild(0).GetComponent<Toggle>();
  125. toggle.transform.GetChild(1).gameObject.GetComponent<Text>().text = $"{user.id} {user.name}";
  126. //toggle_user.transform.SetParent(WorkersList.transform);
  127. toggle.isOn = false;
  128. //var camera_button = toggle_user.transform.GetChild(1).GetComponent<Button>();
  129. //camera_button.onClick.AddListener(() => BroadcastStart(user.id));
  130. var marker = UnityEngine.Object.Instantiate(Resources.Load("GameObjects/Capsule", typeof(GameObject))) as GameObject;
  131. marker.name = $"marker_{user.id}";
  132. marker.transform.GetChild(0).transform.GetChild(0).transform.GetChild(0).GetComponent<Text>().text = $"{user.id}";
  133. marker.transform.SetParent(GameObject.Find("Markers").transform);
  134. marker.GetComponent<Renderer>().material.color = color; // UnityEngine.Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
  135. marker.GetComponent<LabelObjectScript>().UserId = user.id;
  136. var color_line = UnityEngine.Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
  137. var marker_line = UnityEngine.Object.Instantiate(Resources.Load("GameObjects/Line", typeof(LineRenderer))) as LineRenderer;
  138. marker_line.name = $"marker_line_{user.id}";
  139. marker_line.startColor = color_line;
  140. marker_line.endColor = Color.white;
  141. marker_line.material.color = color_line;
  142. marker_line.transform.SetParent(GameObject.Find("Markers").transform);
  143. user.marker = marker;
  144. user.toggle = toggle;
  145. user.marker_line = marker_line;
  146. user.toggle_user = toggle_user;
  147. }
  148. public void SetParam(GameObject WorkersList, Action<uint> BroadcastStart)
  149. {
  150. toggle_user.transform.SetParent(WorkersList.transform);
  151. var camera_button = toggle_user.transform.GetChild(1).GetComponent<Button>();
  152. camera_button.onClick.AddListener(() => BroadcastStart(id));
  153. }
  154. }