summaryrefslogtreecommitdiff
path: root/lib/Travelynx/Helper/IRIS.pm
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2020-10-11 19:38:01 +0200
committerDaniel Friesel <derf@finalrewind.org>2020-10-11 19:38:01 +0200
commitb1348c7d4a4f8432859c36217d87b20fdc359cb2 (patch)
treee5cff2c62caa46bd0929362f510b9d3b2fd59e7b /lib/Travelynx/Helper/IRIS.pm
parentb6330217f086360d865163e29285ea2bf6c79320 (diff)
move in_transit queries to an InTransit model class
Diffstat (limited to 'lib/Travelynx/Helper/IRIS.pm')
-rw-r--r--lib/Travelynx/Helper/IRIS.pm37
1 files changed, 37 insertions, 0 deletions
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;