private void ProcessUnorderedRouteLocationChange() { // The waypoints don't need to be visited in order, so we need to check if we are at any of them for (int i = 0; i < _race.Route.Waypoints.Count; i++) { if (!_race.WaypointVisited[i]) { EDWaypoint waypoint = _race.Route.Waypoints[i]; if (waypoint.WaypointHit(Location, _previousLocation)) { // We've reached a waypoint WaypointIndex = i; AddRaceHistory($"Arrived at {waypoint.Name}"); NumberOfWaypointsVisited++; _race.WaypointVisited[i] = true; // Check if there are any waypoints left to visit (if not, race is finished) if (NumberOfWaypointsVisited < _race.Route.Waypoints.Count) { return; } // If we get here, then all waypoints have been visited and so the race is finished Finished = true; FinishTime = DateTime.UtcNow; string raceTime = $"{FinishTime.Subtract(StartTime):hh\\:mm\\:ss}"; notableEvents?.AddStatusEvent("CompletedNotification", Commander, $" ({raceTime})"); AddRaceHistory($"Completed in {raceTime}"); WaypointIndex = 0; DistanceToWaypoint = 0; return; } } } }
private void ProcessLocationChange() { if (_race == null) { return; } if (!_race.WaypointsMustBeVisitedInOrder) { ProcessUnorderedRouteLocationChange(); return; } DistanceToWaypoint = EDLocation.DistanceBetween(Location, _race.Route.Waypoints[WaypointIndex].Location); int lapStartWaypoint = _race.LapStartWaypoint - 1; if (_race.Laps == 0) { TotalDistanceLeft = _race.Route.TotalDistanceLeftAtWaypoint(WaypointIndex) + DistanceToWaypoint; } else { // Total distance left needs to take into account the laps TotalDistanceLeft = _race.TotalDistanceLeftAtWaypoint(WaypointIndex, Lap) + DistanceToWaypoint; if (lapStartWaypoint < 0) { lapStartWaypoint = 0; } } if ((_race.Leader == null) || (TotalDistanceLeft < _race.Leader.TotalDistanceLeft)) { _race.Leader = this; } EDWaypoint previousWaypoint = null; if (WaypointIndex > 0) { previousWaypoint = _race.Route.Waypoints[WaypointIndex - 1]; } else if (_race.Laps > 0) { previousWaypoint = _race.Route.Waypoints[_race.Route.Waypoints.Count - 1]; } if (_race.Route.Waypoints[WaypointIndex].WaypointHit(Location, _previousLocation, previousWaypoint?.Location)) { // Commander has reached the target waypoint NumberOfWaypointsVisited++; if (_race.Laps > 0) { if (WaypointIndex != lapStartWaypoint) { AddRaceHistory($"Arrived at {_race.Route.Waypoints[WaypointIndex].Name} (lap {Lap})"); } else { // We're at the start waypoint, so have completed a lap DateTime lapEndTime = TimeStamp; LapEndTimes.Add(lapEndTime); TimeSpan thisLapTime = lapEndTime.Subtract(LapStartTime); LapTimes.Add(thisLapTime); LapStartTime = lapEndTime; string lapTime = $"{thisLapTime:hh\\:mm\\:ss}"; if (Lap == 1) { FastestLap = 1; } else if (thisLapTime < FastestLapTime()) { FastestLap = Lap; } Lap++; // We've only finished if this lap number is greater than the number of laps if (Lap > _race.Laps) { Finished = true; if (!Eliminated) { FinishTime = DateTime.UtcNow; string raceTime = $"{FinishTime.Subtract(StartTime):hh\\:mm\\:ss}"; notableEvents?.AddStatusEvent("CompletedNotification", Commander, $" ({raceTime})"); AddRaceHistory($"Completed in {raceTime}"); } WaypointIndex = 0; DistanceToWaypoint = 0; } else if (!Eliminated) { notableEvents?.AddStatusEvent("CompletedLap", Commander, $" {Lap - 1} ({lapTime})"); AddRaceHistory($"Completed lap {Lap - 1} in {lapTime}"); } if (Lap > 2 && FastestLap == Lap - 1) { notableEvents?.AddStatusEvent("FastestLapNotification", Commander, lapTime); } } } else { AddRaceHistory($"Arrived at {_race.Route.Waypoints[WaypointIndex].Name}"); } WaypointIndex++; if ((_race.Laps > 0) && (WaypointIndex > _race.LapEndWaypoint) && (Lap <= _race.Laps)) { WaypointIndex = lapStartWaypoint; } else if (WaypointIndex >= _race.Route.Waypoints.Count) { if (!Eliminated) { Finished = true; FinishTime = DateTime.UtcNow; string raceTime = $"{FinishTime.Subtract(StartTime):hh\\:mm\\:ss}"; notableEvents?.AddStatusEvent("CompletedNotification", Commander, $" ({raceTime})"); AddRaceHistory($"Completed in {raceTime}"); WaypointIndex = 0; DistanceToWaypoint = 0; } } } if (DistanceToWaypoint < _nextLogDistanceToWaypoint) { AddRaceHistory($"{(DistanceToWaypoint / 1000):F1}km to {_race.Route.Waypoints[WaypointIndex].Name}"); _nextLogDistanceToWaypoint = DistanceToWaypoint - 5000; } }