1
0

RouteEventLogger.cs 4.7 KB

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