From 809f1910da538bffb0e671eb3965f28d5e60237a Mon Sep 17 00:00:00 2001 From: Derf Null Date: Fri, 7 Apr 2023 13:04:22 +0200 Subject: move mot mask calculation to a helper function --- lib/Travel/Status/DE/HAFAS.pm | 49 ++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 21 deletions(-) (limited to 'lib/Travel/Status') diff --git a/lib/Travel/Status/DE/HAFAS.pm b/lib/Travel/Status/DE/HAFAS.pm index e12a76f..da6eb47 100644 --- a/lib/Travel/Status/DE/HAFAS.pm +++ b/lib/Travel/Status/DE/HAFAS.pm @@ -250,26 +250,6 @@ sub new { $lid = 'A=1@O=' . $self->{station} . '@'; } - my $mot_mask = 2**@{ $hafas_instance{$service}{productbits} } - 1; - - my %mot_pos; - for my $i ( 0 .. $#{ $hafas_instance{$service}{productbits} } ) { - $mot_pos{ $hafas_instance{$service}{productbits}[$i] } = $i; - } - - if ( my @mots = @{ $self->{exclusive_mots} // [] } ) { - $mot_mask = 0; - for my $mot (@mots) { - $mot_mask |= 1 << $mot_pos{$mot}; - } - } - - if ( my @mots = @{ $self->{excluded_mots} // [] } ) { - for my $mot (@mots) { - $mot_mask &= ~( 1 << $mot_pos{$mot} ); - } - } - my $maxjny = $conf{results} // 30; my $duration = $conf{lookahead} // -1; @@ -289,7 +269,7 @@ sub new { { type => "PROD", mode => "INC", - value => $mot_mask + value => $self->mot_mask } ] }, @@ -411,6 +391,33 @@ sub new_p { # }}} # {{{ Internal Helpers +sub mot_mask { + my ($self) = @_; + + my $service = $self->{active_service}; + my $mot_mask = 2**@{ $hafas_instance{$service}{productbits} } - 1; + + my %mot_pos; + for my $i ( 0 .. $#{ $hafas_instance{$service}{productbits} } ) { + $mot_pos{ $hafas_instance{$service}{productbits}[$i] } = $i; + } + + if ( my @mots = @{ $self->{exclusive_mots} // [] } ) { + $mot_mask = 0; + for my $mot (@mots) { + $mot_mask |= 1 << $mot_pos{$mot}; + } + } + + if ( my @mots = @{ $self->{excluded_mots} // [] } ) { + for my $mot (@mots) { + $mot_mask &= ~( 1 << $mot_pos{$mot} ); + } + } + + return $mot_mask; +} + sub post_with_cache { my ( $self, $url ) = @_; my $cache = $self->{cache}; -- cgit v1.2.3