summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBirte Kristina Friesel <derf@finalrewind.org>2025-12-27 15:12:17 +0100
committerBirte Kristina Friesel <derf@finalrewind.org>2025-12-27 15:12:17 +0100
commit3acb1b379b9be3c1cc252d7a55f1ec34240e48f6 (patch)
tree14c6af48601b7de74036b92979616ff98e70c58c
parentb3300e3a92d0ac3a4b029b5f9c48a08c87a58ba1 (diff)
EFA suggestions: go back to a single template
-rwxr-xr-xlib/Travelynx/Controller/Traveling.pm36
-rw-r--r--lib/Travelynx/Helper/EFA.pm93
-rw-r--r--templates/_suggestions_efa.html.ep56
-rw-r--r--templates/departures.html.ep2
4 files changed, 67 insertions, 120 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;
}
diff --git a/templates/_suggestions_efa.html.ep b/templates/_suggestions_efa.html.ep
deleted file mode 100644
index bca5d74..0000000
--- a/templates/_suggestions_efa.html.ep
+++ /dev/null
@@ -1,56 +0,0 @@
-<ul class="collection departures connections">
- % for my $res (@{$suggestions}) {
- % my ($dep, $dest, $via_arr) = @{$res};
- % my $row_class = '';
- % my $link_class = 'action-checkin';
- % if ($dep->is_cancelled) {
- % $row_class = 'cancelled';
- % $link_class = 'action-cancelled-from';
- % }
- % if ($checkin_from) {
- <li class="collection-item <%= $row_class %> <%= $link_class %>"
- data-efa="<%= $efa %>"
- data-station="<%= $dep->stop_id_num %>"
- data-train="<%= $dep->id %>"
- data-ts="<%= ($dep->sched_datetime // $dep->datetime)->epoch %>"
- data-dest="<%= $dest->{name} %>">
- % }
- % else {
- <li class="collection-item <%= $row_class %>">
- % }
- <a class="dep-time" href="#">
- % if ($dep->is_cancelled) {
- %= $dep->sched_datetime->strftime('%H:%M')
- % }
- % else {
- %= $dep->datetime->strftime('%H:%M')
- % }
- % if ($via_arr) {
- → <%= $via_arr->strftime('%H:%M') %>
- % }
- % if ($dep->delay) {
- %= sprintf('(%+d)', $dep->delay)
- % }
- </a>
- <span class="connect-platform-wrapper">
- % if ($dep->platform) {
- <span>
- % if (($dep->type // q{}) =~ m{ ast | bus | ruf }ix) {
- Steig
- % }
- % else {
- Gleis
- % }
- %= $dep->platform
- </span>
- % }
- <span class="dep-line <%= ($dep->type // q{}) =~ tr{a-zA-Z_-}{}cdr %>">
- %= $dep->line
- </span>
- </span>
- <span class="dep-dest">
- %= $dest->{name}
- </span>
- </li>
- % }
-</ul>
diff --git a/templates/departures.html.ep b/templates/departures.html.ep
index b1390b3..5d5e5af 100644
--- a/templates/departures.html.ep
+++ b/templates/departures.html.ep
@@ -90,7 +90,7 @@
%= include '_suggestions_dbris', suggestions => stash('suggestions'), checkin_from => $eva;
% }
% elsif ($efa) {
- %= include '_suggestions_efa', suggestions => stash('suggestions'), checkin_from => $eva;
+ %= include '_connections_efa', suggestions => stash('suggestions'), checkin_from => $eva;
% }
% else {
%= include '_suggestions_iris', suggestions => stash('suggestions'), checkin_from => $eva;