2
0

RouteEventLogger.cs 4.8 KB

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