diff options
author | Daniel Friesel <derf@finalrewind.org> | 2022-01-25 21:25:29 +0100 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2022-01-25 21:25:29 +0100 |
commit | b4f2978792b1abb8ec994aa96f993641d6bdfb53 (patch) | |
tree | c4f3ad255c647926ffac26e1e539b61262fe07ad /lib/DBInfoscreen | |
parent | 2c75f4a4f7b29f7ef174b79c704678804701ac36 (diff) |
refactor json_route_diff into Stationboard controller
Diffstat (limited to 'lib/DBInfoscreen')
-rw-r--r-- | lib/DBInfoscreen/Controller/Stationboard.pm | 81 |
1 files changed, 71 insertions, 10 deletions
diff --git a/lib/DBInfoscreen/Controller/Stationboard.pm b/lib/DBInfoscreen/Controller/Stationboard.pm index 7e16502..a8e90fc 100644 --- a/lib/DBInfoscreen/Controller/Stationboard.pm +++ b/lib/DBInfoscreen/Controller/Stationboard.pm @@ -199,6 +199,69 @@ sub log_api_access { return; } +sub json_route_diff { + my ( $route, $sched_route ) = @_; + my @json_route; + my @route = @{$route}; + my @sched_route = @{$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, { name => $route[$route_idx] } ); + $route_idx++; + $sched_idx++; + } + + # this branch is inefficient, but won't be taken frequently + elsif ( not( $route[$route_idx] ~~ \@sched_route ) ) { + push( + @json_route, + { + name => $route[$route_idx], + isAdditional => 1 + } + ); + $route_idx++; + } + else { + push( + @json_route, + { + name => $sched_route[$sched_idx], + isCancelled => 1 + } + ); + $sched_idx++; + } + } + while ( $route_idx <= $#route ) { + push( + @json_route, + { + name => $route[$route_idx], + isAdditional => 1, + isCancelled => 0 + } + ); + $route_idx++; + } + while ( $sched_idx <= $#sched_route ) { + push( + @json_route, + { + name => $sched_route[$sched_idx], + isAdditional => 0, + isCancelled => 1 + } + ); + $sched_idx++; + } + return @json_route; +} + sub get_results_for { my ( $station, %opt ) = @_; my $data; @@ -461,15 +524,13 @@ sub format_iris_result_info { sub render_train { my ( $self, $result, $departure, $station_name, $template ) = @_; - $departure->{links} = []; - $departure->{route_pre_diff} = [ - $self->json_route_diff( - [ $result->route_pre ], - [ $result->sched_route_pre ] - ) - ]; + $departure->{links} = []; + $departure->{route_pre_diff} + = [ + json_route_diff( [ $result->route_pre ], [ $result->sched_route_pre ] ) + ]; $departure->{route_post_diff} = [ - $self->json_route_diff( + json_route_diff( [ $result->route_post ], [ $result->sched_route_post ] ) @@ -1138,8 +1199,8 @@ sub handle_result { } if ( $template eq 'json' ) { - my @json_route = $self->json_route_diff( [ $result->route ], - [ $result->sched_route ] ); + my @json_route + = json_route_diff( [ $result->route ], [ $result->sched_route ] ); if ( $apiver eq '1' or $apiver eq '2' ) { |