summaryrefslogtreecommitdiff
path: root/lib/Travelynx
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2019-11-16 21:24:35 +0100
committerDaniel Friesel <derf@finalrewind.org>2019-11-16 21:24:35 +0100
commitcea4b71ca3c422a220dae24ddb77fcbec4d7e1a9 (patch)
treefd7b323f1d75ca00aba317b913086a9765c85376 /lib/Travelynx
parent4f2eac9cefd40028916ed5ca1953c2ff566b0b18 (diff)
history map: skip duplicates earlier
Diffstat (limited to 'lib/Travelynx')
-rwxr-xr-xlib/Travelynx/Controller/Traveling.pm36
1 files changed, 25 insertions, 11 deletions
diff --git a/lib/Travelynx/Controller/Traveling.pm b/lib/Travelynx/Controller/Traveling.pm
index 310c8bc..fee1e79 100755
--- a/lib/Travelynx/Controller/Traveling.pm
+++ b/lib/Travelynx/Controller/Traveling.pm
@@ -5,6 +5,7 @@ use DateTime;
use DateTime::Format::Strptime;
use List::Util qw(uniq);
use List::UtilsBy qw(uniq_by);
+use List::MoreUtils qw(first_index);
use Travel::Status::DE::IRIS::Stations;
sub homepage {
@@ -428,22 +429,35 @@ sub map_history {
grep { exists $location->{$_} } @stations;
my @station_pairs;
+ my %seen;
for my $journey (@journeys) {
- my @route = map { $_->[0] } @{ $journey->{route} };
+
+ my @route = map { $_->[0] } @{ $journey->{route} };
+ my $from_index = first_index { $_ eq $journey->{from_name} } @route;
+ my $to_index = first_index { $_ eq $journey->{to_name} } @route;
+
+ if ( $from_index == -1 or $to_index == -1 ) {
+ next;
+ }
+
+ @route = @route[ $from_index .. $to_index ];
+
+ my $key = join( '|', @route );
+
+ if ( $seen{$key} ) {
+ next;
+ }
+
+ $seen{$key} = 1;
+
+ # direction does not matter at the moment
+ $seen{ join( '|', reverse @route ) } = 1;
+
my $prev_station = shift @route;
- my $within = 0;
for my $station (@route) {
- if ( $prev_station eq $journey->{from_name} ) {
- $within = 1;
- }
- if ($within) {
- push( @station_pairs, [ $prev_station, $station ] );
- }
+ push( @station_pairs, [ $prev_station, $station ] );
$prev_station = $station;
- if ( $station eq $journey->{to_name} ) {
- $within = 0;
- }
}
}