Skip to content

Commit

Permalink
Upgrade routes stored with v2 format
Browse files Browse the repository at this point in the history
  • Loading branch information
sandermvanvliet committed Dec 29, 2023
1 parent 3526385 commit 36234b4
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 8 deletions.
3 changes: 0 additions & 3 deletions src/RoadCaptain.Adapters/RoadCaptain.Adapters.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@
<None Update="segments-watopia.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="segments-watopia.bin">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="turns-france.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
96 changes: 93 additions & 3 deletions src/RoadCaptain.Adapters/RouteStoreToDisk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,99 @@ internal PlannedRoute DeserializeAndUpgrade(string serialized)
seq.NextSegmentId == "watopia-bambino-fondo-004-after-before")
.ToList();

foreach (var sequuence in matchingSequences)
foreach (var sequence in matchingSequences)
{
sequuence.TurnToNextSegment = TurnDirection.Right;
sequence.TurnToNextSegment = TurnDirection.Right;
}

// persisted route v2 is definitely pre-0.7.0.9
{
// With version 0.7.0.7 we've added new segments to Watopia
// and split a few which we need to correct for.

void SplitSequence(List<SegmentSequence> newSequence, string segmentId, SegmentSequence sequence, int index)
{
if (sequence.Direction == SegmentDirection.AtoB)
{
newSequence.Add(
new SegmentSequence(
$"{segmentId}-before",
sequence.Type,
sequence.Direction,
index+1));
newSequence[^1].TurnToNextSegment = TurnDirection.GoStraight;
newSequence[^1].NextSegmentId = $"{segmentId}-after";

newSequence.Add(
new SegmentSequence(
$"{segmentId}-after",
sequence.Type,
sequence.Direction,
index+2));
newSequence[^1].TurnToNextSegment = sequence.TurnToNextSegment;
newSequence[^1].NextSegmentId = sequence.NextSegmentId;
}
else
{
newSequence.Add(
new SegmentSequence(
$"{segmentId}-after",
sequence.Type,
sequence.Direction,
index+1));
newSequence[^1].TurnToNextSegment = TurnDirection.GoStraight;
newSequence[^1].NextSegmentId = $"{segmentId}-before";

newSequence.Add(
new SegmentSequence(
$"{segmentId}-before",
sequence.Type,
sequence.Direction,
index+2));
newSequence[^1].TurnToNextSegment = sequence.TurnToNextSegment;
newSequence[^1].NextSegmentId = sequence.NextSegmentId;
}
}

var newSequence = new List<SegmentSequence>();
var index = 0;
var tainted = false;

foreach (var sequence in plannedRoute.RouteSegmentSequence)
{
if (sequence.SegmentId == "watopia-bambino-fondo-003-after-after-before")
{
// 1: watopia-bambino-fondo-003-after-after-before is split
SplitSequence(newSequence, "watopia-bambino-fondo-003-after-after-before", sequence, index);
index += 2;
tainted = true;
}
else if (sequence.SegmentId == "watopia-tempus-fugit-001")
{
// 2: watopia-tempus-fugit-001 is split
SplitSequence(newSequence, "watopia-tempus-fugit-001", sequence, index);
index += 2;
tainted = true;
}
else if (sequence.SegmentId == "watopia-bambino-fondo-004-after-before")
{
// 3: watopia-bambino-fondo-004-after-before is split
SplitSequence(newSequence, "watopia-bambino-fondo-004-after-before", sequence, index);
index += 2;
tainted = true;
}
else
{
sequence.Index = index++;
newSequence.Add(sequence);
}
}

if (tainted)
{
plannedRoute.RouteSegmentSequence.Clear();
plannedRoute.RouteSegmentSequence.AddRange(newSequence);
}
}
}
else if (schemaVersion == PersistedRouteVersion3.Version)
Expand Down Expand Up @@ -285,7 +375,7 @@ internal PlannedRoute DeserializeAndUpgrade(string serialized)

if (routeVersion < new Version(0, 7, 0, 7))
{
// With version 0.7.0.9 we've added new segments to Watopia
// With version 0.7.0.7 we've added new segments to Watopia
// and split a few which we need to correct for.

void SplitSequence(List<SegmentSequence> newSequence, string segmentId, SegmentSequence sequence, int index)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,15 @@ public void GivenSpawnPointAndValidNextSegmentAlternate_SuccessIsReturned()
GivenWorldAndSport("watopia", SportType.Cycling);

_viewModel.CallAddSegmentToRoute(GetSegmentById("watopia-beach-island-loop-001")); // This segment only has AtoB as a valid spawn direction
_viewModel.CallAddSegmentToRoute(GetSegmentById("watopia-bambino-fondo-004-after-before"));
_viewModel.CallAddSegmentToRoute(GetSegmentById("watopia-bambino-fondo-004-after-before-before"));

_lastStatusBarLevel
.Should()
.Be("info");

_lastStatusBarMessage
.Should()
.Be("Added segment Volcano circuit 1");
.Be("Added segment Volcano circuit 1-before");
}

[Fact]
Expand Down

0 comments on commit 36234b4

Please sign in to comment.