RouteEventLogger.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using JetBrains.Annotations;
  5. using Logging.Base;
  6. using Logging.Data;
  7. using Routes;
  8. using UnityEngine;
  9. namespace Logging.Events
  10. {
  11. public enum RouteEvent
  12. {
  13. None,
  14. Left,
  15. Right,
  16. Straight
  17. }
  18. public readonly struct RouteEventData : ISerializable
  19. {
  20. private readonly long timestamp;
  21. private readonly RouteEvent eventRoute;
  22. public RouteEventData(long timestamp, RouteEvent eventRoute)
  23. {
  24. this.timestamp = timestamp;
  25. this.eventRoute = eventRoute;
  26. }
  27. public KeyValuePair<long, string[]> Serialize() => new KeyValuePair<long, string[]>(timestamp, new[]
  28. {
  29. eventRoute.ToString()
  30. });
  31. }
  32. public class RouteEventLogger : SensorDataLogger<RouteEventData>
  33. {
  34. public override string Key => "route_events";
  35. public GameObject routes;
  36. [CanBeNull] private Route activeRoute;
  37. private bool activeRouteAvailable;
  38. private IEnumerable<Turn> turns;
  39. public override void Start()
  40. {
  41. base.Start();
  42. activeRoute = routes.GetComponentsInChildren<Route>().FirstOrDefault(r => r.isActiveAndEnabled);
  43. if (activeRoute == null) return;
  44. activeRouteAvailable = true;
  45. turns = activeRoute.items.Select(item => item.turn);
  46. }
  47. private void Update()
  48. {
  49. var e = activeRouteAvailable ? EventForActiveRoute() : RouteEvent.None;
  50. /*if (e != RouteEvent.None)
  51. {
  52. print($"RouteEvent: {e.ToString()}");
  53. }*/
  54. Log(new RouteEventData(Helpers.RoundToLong(Time.time * 1000f), e));
  55. }
  56. private RouteEvent EventForActiveRoute()
  57. {
  58. var currentTurn = turns.FirstOrDefault(t => t.BikeTriggerState == TriggerState.Inside);
  59. if (currentTurn == null)
  60. {
  61. return RouteEvent.None;
  62. }
  63. switch (currentTurn.comingFrom)
  64. {
  65. case RoadDirection.West:
  66. switch (currentTurn.goingTo)
  67. {
  68. case RoadDirection.North:
  69. return RouteEvent.Left;
  70. case RoadDirection.East:
  71. return RouteEvent.Straight;
  72. case RoadDirection.South:
  73. return RouteEvent.Right;
  74. default:
  75. return RouteEvent.None;
  76. }
  77. case RoadDirection.North:
  78. switch (currentTurn.goingTo)
  79. {
  80. case RoadDirection.East:
  81. return RouteEvent.Left;
  82. case RoadDirection.South:
  83. return RouteEvent.Straight;
  84. case RoadDirection.West:
  85. return RouteEvent.Right;
  86. default:
  87. return RouteEvent.None;
  88. }
  89. case RoadDirection.East:
  90. switch (currentTurn.goingTo)
  91. {
  92. case RoadDirection.North:
  93. return RouteEvent.Right;
  94. case RoadDirection.South:
  95. return RouteEvent.Left;
  96. case RoadDirection.West:
  97. return RouteEvent.Straight;
  98. default:
  99. return RouteEvent.None;
  100. }
  101. case RoadDirection.South:
  102. switch (currentTurn.goingTo)
  103. {
  104. case RoadDirection.North:
  105. return RouteEvent.Straight;
  106. case RoadDirection.East:
  107. return RouteEvent.Right;
  108. case RoadDirection.West:
  109. return RouteEvent.Left;
  110. default:
  111. return RouteEvent.None;
  112. }
  113. default:
  114. return RouteEvent.None;
  115. }
  116. }
  117. public override IEnumerable<RouteEventData> ReadLog(IEnumerable<IEnumerable<string>> lines)
  118. {
  119. throw new NotImplementedException();
  120. }
  121. }
  122. }