Commit ae8462fc authored by Birte Kristina Friesel's avatar Birte Kristina Friesel
Browse files

exclusive _and_ inclusive mots \o/

parent de432ead
Loading
Loading
Loading
Loading
+28 −22
Original line number Diff line number Diff line
@@ -10,8 +10,6 @@ use List::MoreUtils qw(uniq);
use List::Util qw(first max);
use Travel::Status::DE::HAFAS;

my %train_type;

my ( $date, $time );
my $arrivals    = 0;
my $ignore_late = 0;
@@ -19,7 +17,7 @@ my $types = q{};
my $language;
my $developer_mode;
my ( $list_services, $service, $hafas_url );
my @excluded_mots;
my ( @excluded_mots, @exclusive_mots );

my @output;

@@ -49,30 +47,13 @@ if ($list_services) {
	exit 0;
}

for my $type ( split( qr{,}, $types ) ) {
	if ( substr( $type, 0, 1 ) eq q{!} ) {
		$train_type{ substr( $type, 1 ) } = 0;
		push( @excluded_mots, substr( $type, 1 ) );
	}
	else {
		$train_type{$type} = 1;
	}
}

if ( $train_type{help} or $train_type{list} or $train_type{'?'} ) {
	my @mots
	  = @{ Travel::Status::DE::HAFAS::get_service($service)->{productbits} };
	@mots = grep { $_ ne 'x' } @mots;
	@mots = uniq @mots;
	@mots = sort @mots;
	say join( "\n", @mots );
	exit 0;
}
parse_mot_options();

my $status = Travel::Status::DE::HAFAS->new(
	date           => $date,
	language       => $language,
	excluded_mots  => \@excluded_mots,
	exclusive_mots => \@exclusive_mots,
	station        => shift || show_help(1),
	time           => $time,
	mode           => $arrivals ? 'arr' : 'dep',
@@ -97,6 +78,31 @@ sub show_version {
	exit 0;
}

sub parse_mot_options {

	my $default_yes = 1;
	my $help;

	for my $type ( split( qr{,}, $types ) ) {
		if ( $type eq 'help' or $type eq 'list' or $type eq '?' ) {
			my @mots
			  = @{ Travel::Status::DE::HAFAS::get_service($service)
				  ->{productbits} };
			@mots = grep { $_ ne 'x' } @mots;
			@mots = uniq @mots;
			@mots = sort @mots;
			say join( "\n", @mots );
			exit 0;
		}
		elsif ( substr( $type, 0, 1 ) eq q{!} ) {
			push( @excluded_mots, substr( $type, 1 ) );
		}
		else {
			push( @exclusive_mots, $type );
		}
	}
}

sub display_result {
	my (@lines) = @_;

+25 −8
Original line number Diff line number Diff line
@@ -61,6 +61,7 @@ sub new {
	my $ref = {
		active_service => $service,
		developer_mode => $conf{developer_mode},
		exclusive_mots => $conf{exclusive_mots},
		excluded_mots  => $conf{excluded_mots},
		post           => {
			input => $conf{station},
@@ -115,26 +116,42 @@ sub set_productfilter {
	my ($self) = @_;

	my $service     = $self->{active_service};
	my $mot_default = '1';

	if ( not $service or not exists $hafas_instance{$service}{productbits} ) {
		return;
	}

	$self->{post}{productsFilter}
	  = '1' x ( scalar @{ $hafas_instance{$service}{productbits} } );

	if ( $self->{excluded_mots} and @{ $self->{excluded_mots} } ) {
	my %mot_pos;
	for my $i ( 0 .. $#{ $hafas_instance{$service}{productbits} } ) {
		$mot_pos{ $hafas_instance{$service}{productbits}[$i] } = $i;
	}

	if ( $self->{exclusive_mots} and @{ $self->{exclusive_mots} } ) {
		$mot_default = '0';
	}

	$self->{post}{productsFilter}
	  = $mot_default x ( scalar @{ $hafas_instance{$service}{productbits} } );

	if ( $self->{exclusive_mots} and @{ $self->{exclusive_mots} } ) {
		for my $mot ( @{ $self->{exclusive_mots} } ) {
			if ( exists $mot_pos{$mot} ) {
				substr( $self->{post}{productsFilter}, $mot_pos{$mot}, 1, '1' );
			}
		}
	}

	if ( $self->{excluded_mots} and @{ $self->{excluded_mots} } ) {
		for my $mot ( @{ $self->{excluded_mots} } ) {
			if ( exists $mot_pos{$mot} ) {
				substr( $self->{post}{productsFilter}, $mot_pos{$mot}, 1 ) = 0;
				substr( $self->{post}{productsFilter}, $mot_pos{$mot}, 1, '0' );
			}
		}
	}

	say $self->{post}{productsFilter};

}

sub check_input_error {
@@ -198,7 +215,7 @@ sub results {
			push( @messages, $n->getAttribute('header') );
		}

		substr( $date, 6, 0 ) = '20';
		substr( $date, 6, 0, '20' );

		$info      //= q{};
		$routeinfo //= q{};