summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2021-01-12 22:46:16 +0100
committerDaniel Friesel <derf@finalrewind.org>2021-01-12 22:46:16 +0100
commiteeec149f96a11141f8ae6fc417de6620b821b773 (patch)
tree7c64dbbd573033f680070fde9c35f4094d1b0b46 /bin
parentd768ba93d4d78747709fbbb432130bd2bd9e454e (diff)
Consider valid day of week when handling duplicates
Diffstat (limited to 'bin')
-rwxr-xr-xbin/db-zugbildung-to-json36
1 files changed, 36 insertions, 0 deletions
diff --git a/bin/db-zugbildung-to-json b/bin/db-zugbildung-to-json
index 3010ad9..af22818 100755
--- a/bin/db-zugbildung-to-json
+++ b/bin/db-zugbildung-to-json
@@ -53,6 +53,12 @@ sub range_is_today {
return;
}
+sub weekday_is_today {
+ my ($weekdys) = @_;
+
+ return ( ${ $weekdys->[ $now->day_of_week - 1 ] } == 1 );
+}
+
my @weekdays = (qw(Mo Di Mi Do Fr Sa So));
my @months = (qw(0 I II III IV V VI VII VIII IX X XI XII));
my %weekday = map { ( $weekdays[$_] => $_ ) } ( 0 .. $#weekdays );
@@ -787,6 +793,36 @@ for my $train_no ( keys %map ) {
if ( $valid_count == 1 ) {
$smap{$train_no} = $latest_valid;
}
+ else {
+ $valid_count = 0;
+ $latest_valid = undef;
+ for my $train ( @{ $map{$train_no} } ) {
+ my $is_valid = 0;
+ for my $schedule ( @{ $train->{schedules} // [] } ) {
+ for my $valid ( @{ $schedule->{valid} // [] } ) {
+ if ( range_is_today($valid)
+ and weekday_is_today( $schedule->{weekdays} ) )
+ {
+ $is_valid = 1;
+ }
+ }
+
+ # invalid may override valid for certain days
+ for my $invalid ( @{ $schedule->{invalid} // [] } ) {
+ if ( range_is_today($invalid) ) {
+ $is_valid = 0;
+ }
+ }
+ }
+ if ($is_valid) {
+ $latest_valid = $train;
+ $valid_count++;
+ }
+ }
+ if ( $valid_count == 1 ) {
+ $smap{$train_no} = $latest_valid;
+ }
+ }
}
}