From b1348c7d4a4f8432859c36217d87b20fdc359cb2 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sun, 11 Oct 2020 19:38:01 +0200 Subject: move in_transit queries to an InTransit model class --- lib/Travelynx/Helper/IRIS.pm | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'lib/Travelynx/Helper') diff --git a/lib/Travelynx/Helper/IRIS.pm b/lib/Travelynx/Helper/IRIS.pm index 3b98a51..a8ca6c3 100644 --- a/lib/Travelynx/Helper/IRIS.pm +++ b/lib/Travelynx/Helper/IRIS.pm @@ -68,4 +68,41 @@ sub get_departures { } } +sub route_diff { + my ( $self, $train ) = @_; + my @json_route; + my @route = $train->route; + my @sched_route = $train->sched_route; + + my $route_idx = 0; + my $sched_idx = 0; + + while ( $route_idx <= $#route and $sched_idx <= $#sched_route ) { + if ( $route[$route_idx] eq $sched_route[$sched_idx] ) { + push( @json_route, [ $route[$route_idx], {}, undef ] ); + $route_idx++; + $sched_idx++; + } + + # this branch is inefficient, but won't be taken frequently + elsif ( not( grep { $_ eq $route[$route_idx] } @sched_route ) ) { + push( @json_route, [ $route[$route_idx], {}, 'additional' ], ); + $route_idx++; + } + else { + push( @json_route, [ $sched_route[$sched_idx], {}, 'cancelled' ], ); + $sched_idx++; + } + } + while ( $route_idx <= $#route ) { + push( @json_route, [ $route[$route_idx], {}, 'additional' ], ); + $route_idx++; + } + while ( $sched_idx <= $#sched_route ) { + push( @json_route, [ $sched_route[$sched_idx], {}, 'cancelled' ], ); + $sched_idx++; + } + return @json_route; +} + 1; -- cgit v1.2.3