From a087ad571d40e90baf1dfbfe6393fa4bb5f057d1 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sat, 26 Dec 2020 08:20:20 +0100 Subject: normalize direction when showing the wagon view --- lib/DBInfoscreen/Controller/Wagenreihung.pm | 50 +++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 9 deletions(-) (limited to 'lib/DBInfoscreen/Controller') diff --git a/lib/DBInfoscreen/Controller/Wagenreihung.pm b/lib/DBInfoscreen/Controller/Wagenreihung.pm index 04f193d..9567913 100644 --- a/lib/DBInfoscreen/Controller/Wagenreihung.pm +++ b/lib/DBInfoscreen/Controller/Wagenreihung.pm @@ -107,6 +107,14 @@ sub wagenreihung { ); } + my $wref = { + e => $exit_side ? substr( $exit_side, 0, 1 ) : '', + tt => $wr->train_type, + tn => $train, + s => $wr->station_name, + p => $wr->platform + }; + if ( $wr->has_bad_wagons ) { # create fake positions as the correct ones are not available @@ -117,16 +125,40 @@ sub wagenreihung { $pos += 4; } } + elsif ( defined $wr->direction and scalar $wr->wagons > 2 ) { + + # wagenlexikon images only know one orientation. They assume + # that the second class (i.e., the wagon with the lowest + # wagon number) is in the leftmost carriage(s). We define the + # wagon with the lowest start_percent value to be leftmost + # and invert the direction passed on to $wref if it is not + # the wagon with the lowest wagon number. + + my @wagons = $wr->wagons; + + # skip first wagon as it may be a locomotive + my $wn1 = $wagons[1]->number; + my $wn2 = $wagons[2]->number; + my $wp1 = $wagons[1]{position}{start_percent}; + my $wp2 = $wagons[2]{position}{start_percent}; + + if ( $wn1 =~ m{^\d+$} and $wn2 =~ m{^\d+$} ) { + + # We need to perform normalization in two cases: + # * wagon 1 is leftmost and its number is higher than wagon 2 + # * wagon 1 is rightmost and its number is lower than wagon 2 + # (-> the leftmost wagon has the highest number) + if ( ( $wp1 < $wp2 and $wn1 > $wn2 ) + or ( $wp1 > $wp2 and $wn1 < $wn2 ) ) + { + $wref->{d} = 100 - $wr->direction; + } + else { + $wref->{d} = $wr->direction; + } + } + } - my $wref = { - - #d => $wr->direction, - #e => $exit_side ? substr( $exit_side, 0, 1 ) : '', - tt => $wr->train_type, - tn => $train, - s => $wr->station_name, - p => $wr->platform - }; $wref = b64_encode( encode_json($wref) ); $self->render( -- cgit v1.2.3