diff options
| author | Birte Kristina Friesel <derf@finalrewind.org> | 2025-12-27 15:12:17 +0100 |
|---|---|---|
| committer | Birte Kristina Friesel <derf@finalrewind.org> | 2025-12-27 15:12:17 +0100 |
| commit | 3acb1b379b9be3c1cc252d7a55f1ec34240e48f6 (patch) | |
| tree | 14c6af48601b7de74036b92979616ff98e70c58c /lib | |
| parent | b3300e3a92d0ac3a4b029b5f9c48a08c87a58ba1 (diff) | |
EFA suggestions: go back to a single template
Diffstat (limited to 'lib')
| -rwxr-xr-x | lib/Travelynx/Controller/Traveling.pm | 36 | ||||
| -rw-r--r-- | lib/Travelynx/Helper/EFA.pm | 93 |
2 files changed, 66 insertions, 63 deletions
diff --git a/lib/Travelynx/Controller/Traveling.pm b/lib/Travelynx/Controller/Traveling.pm index 4b0c341..e48dd2b 100755 --- a/lib/Travelynx/Controller/Traveling.pm +++ b/lib/Travelynx/Controller/Traveling.pm @@ -943,34 +943,28 @@ sub station { @results = map { $_->[0] } sort { $b->[1] <=> $a->[1] } map { [ $_, $_->datetime->epoch ] } $status->results; - $status = { - station_eva => $status->stop->id_num, - station_name => $status->stop->full_name, - related_stations => [], - }; my $backend_id = $self->stations->get_backend_id( efa => $efa_service ); my @destinations = $self->journeys->get_connection_targets( uid => $uid, backend_id => $backend_id, - eva => $status->{station_eva}, + eva => $status->stop->id_num, ); - for my $dep (@results) { - destination: for my $dest (@destinations) { - for my $stop ( $dep->route_post ) { - if ( $stop->full_name eq $dest->{name} ) { - push( @suggestions, - [ $dep, $dest, $stop->arr ] ); - next destination; - } - } - } - } + @suggestions = $self->efa->grep_suggestions( + status => $status, + destinations => \@destinations + ); + @suggestions = sort { $a->[0]{sort_ts} <=> $b->[0]{sort_ts} } + grep { + $_->[0]{sort_ts} >= $now - 300 + and $_->[0]{sort_ts} <= $now + 1800 + } @suggestions; - @suggestions = map { $_->[0] } - sort { $a->[1] <=> $b->[1] } - grep { $_->[1] >= $now - 300 and $_->[1] <= $now + 1800 } - map { [ $_, $_->[0]->datetime->epoch ] } @suggestions; + $status = { + station_eva => $status->stop->id_num, + station_name => $status->stop->full_name, + related_stations => [], + }; } elsif ($motis_service) { @results = map { $_->[0] } diff --git a/lib/Travelynx/Helper/EFA.pm b/lib/Travelynx/Helper/EFA.pm index a280388..2f3ff1f 100644 --- a/lib/Travelynx/Helper/EFA.pm +++ b/lib/Travelynx/Helper/EFA.pm @@ -48,10 +48,53 @@ sub get_departures_p { ); } +sub grep_suggestions { + my ( $self, %opt ) = @_; + my $status = $opt{status}; + my $destinations = $opt{destinations}; + my $max_per_dest = $opt{max_per_dest}; + + my @suggestions; + my %via_count; + for my $dep ( $status->results ) { + destination: for my $dest ( @{$destinations} ) { + for my $stop ( $dep->route_post ) { + if ( $stop->full_name eq $dest->{name} ) { + if ( not $dep->is_cancelled ) { + $via_count{ $dep->stop_id_num } += 1; + } + if ( $max_per_dest + and $via_count{ $dep->stop_id_num } > $max_per_dest ) + { + next destination; + } + my $dep_json = { + id => $dep->id, + ts => ( $dep->sched_datetime // $dep->datetime )->epoch, + sort_ts => $dep->datetime->epoch, + is_cancelled => $dep->is_cancelled, + stop_id_num => $dep->stop_id_num, + sched_hhmm => $dep->sched_datetime->strftime('%H:%M'), + rt_hhmm => $dep->datetime->strftime('%H:%M'), + delay => $dep->delay, + platform => $dep->platform, + type => $dep->type, + line => $dep->line, + }; + push( @suggestions, + [ $dep_json, $dest, $stop->arr->strftime('%H:%M') ] ); + next destination; + } + } + } + } + return @suggestions; +} + sub get_connections_p { my ( $self, %opt ) = @_; my $promise = Mojo::Promise->new; - my @destinations = @{ $opt{destinations} }; + my $destinations = $opt{destinations}; $self->get_departures_p( service => $opt{service}, @@ -62,47 +105,13 @@ sub get_connections_p { )->then( sub { my ($status) = @_; - my @suggestions; - my %via_count; - for my $dep ( $status->results ) { - destination: for my $dest (@destinations) { - for my $stop ( $dep->route_post ) { - if ( $stop->full_name eq $dest->{name} ) { - if ( not $dep->is_cancelled ) { - $via_count{ $dep->stop_id_num } += 1; - } - if ( $via_count{ $dep->stop_id_num } > 2 ) { - next destination; - } - my $dep_json = { - id => $dep->id, - ts => ( $dep->sched_datetime // $dep->datetime ) - ->epoch, - is_cancelled => $dep->is_cancelled, - stop_id_num => $dep->stop_id_num, - sched_hhmm => - $dep->sched_datetime->strftime('%H:%M'), - rt_hhmm => $dep->datetime->strftime('%H:%M'), - delay => $dep->delay, - platform => $dep->platform, - type => $dep->type, - line => $dep->line, - }; - push( - @suggestions, - [ - $dep_json, $dest, - $stop->arr->strftime('%H:%M') - ] - ); - next destination; - } - } - } - } - @suggestions = map { $_->[0] } - sort { $a->[1] <=> $b->[1] } - map { [ $_, $_->[0]->{ts} ] } @suggestions; + my @suggestions = $self->grep_suggestions( + status => $status, + destinations => $destinations, + max_per_dest => 2 + ); + @suggestions + = sort { $a->[0]{sort_ts} <=> $b->[0]{sort_ts} } @suggestions; $promise->resolve( \@suggestions ); return; } |
