show journey distances as well

This commit is contained in:
Daniel Friesel 2018-10-05 20:41:28 +02:00
parent 6afef992f1
commit 7694e626a5
2 changed files with 53 additions and 6 deletions

View file

@ -4,7 +4,9 @@ use Mojolicious::Lite;
use Cache::File; use Cache::File;
use DateTime; use DateTime;
use DBI; use DBI;
use Geo::Distance;
use List::Util qw(first); use List::Util qw(first);
use List::MoreUtils qw(after_incl before_incl);
use Travel::Status::DE::IRIS; use Travel::Status::DE::IRIS;
use Travel::Status::DE::IRIS::Stations; use Travel::Status::DE::IRIS::Stations;
@ -233,6 +235,18 @@ sub get_departures {
} }
} }
sub get_station {
my ($station_name) = @_;
my @candidates
= Travel::Status::DE::IRIS::Stations::get_station($station_name);
if ( @candidates == 1 ) {
return $candidates[0];
}
return undef;
}
helper 'checkin' => sub { helper 'checkin' => sub {
my ( $self, $station, $train_id ) = @_; my ( $self, $station, $train_id ) = @_;
@ -450,7 +464,8 @@ helper 'get_user_travels' => sub {
type => $train_type, type => $train_type,
line => $train_line, line => $train_line,
no => $train_no, no => $train_no,
messages => [ split( qr{|}, $raw_messages ) ], messages => [ split( qr{[|]}, $raw_messages ) ],
route => [ split( qr{[|]}, $raw_route ) ],
completed => 0, completed => 0,
} }
); );
@ -464,7 +479,7 @@ helper 'get_user_travels' => sub {
if ($train_no) { if ($train_no) {
$ref->{sched_arrival} = epoch_to_dt($raw_sched_ts), $ref->{sched_arrival} = epoch_to_dt($raw_sched_ts),
$ref->{rt_arrival} = epoch_to_dt($raw_real_ts), $ref->{rt_arrival} = epoch_to_dt($raw_real_ts),
$ref->{messages} = [ split( qr{|}, $raw_messages ) ]; $ref->{messages} = [ split( qr{[|]}, $raw_messages ) ];
} }
} }
} }
@ -516,6 +531,36 @@ helper 'get_user_status' => sub {
}; };
}; };
helper 'get_travel_distance' => sub {
my ( $self, $from, $to, $route_ref ) = @_;
my $distance = 0;
my $geo = Geo::Distance->new();
my @route = after_incl { $_ eq $from } @{$route_ref};
@route = before_incl { $_ eq $to } @route;
if ( @route < 2 ) {
# I AM ERROR
return 0;
}
my $prev_station = get_station( shift @route );
if ( not $prev_station ) {
return 0;
}
for my $station_name (@route) {
if ( my $station = get_station($station_name) ) {
$distance += $geo->distance( 'kilometer', $prev_station->[3],
$prev_station->[4], $station->[3], $station->[4] );
$prev_station = $station;
}
}
return $distance;
};
helper 'navbar_class' => sub { helper 'navbar_class' => sub {
my ( $self, $path ) = @_; my ( $self, $path ) = @_;

View file

@ -49,10 +49,12 @@
<td><%= $travel->{type} %> <%= $travel->{line} // $travel->{no} %></td> <td><%= $travel->{type} %> <%= $travel->{line} // $travel->{no} %></td>
<td><%= $travel->{from_name} %> → <%= $travel->{to_name} %></td> <td><%= $travel->{from_name} %> → <%= $travel->{to_name} %></td>
% if ($travel->{rt_arrival} and $travel->{rt_departure}) { % if ($travel->{rt_arrival} and $travel->{rt_departure}) {
<td><%= ($travel->{rt_arrival}->epoch - $travel->{rt_departure}->epoch) / 60 %> min</td> <td><%= ($travel->{rt_arrival}->epoch - $travel->{rt_departure}->epoch) / 60 %> min
% } /
% else { <%= sprintf('%.f', $self->get_travel_distance($travel->{from_name}, $travel->{to_name}, $travel->{route})) %>km
<td>?</td> </td>
% } else {
<td><%= sprintf('%.f', $self->get_travel_distance($travel->{from_name}, $travel->{to_name}, $travel->{route})) %>km</td>
% } % }
</tr> </tr>
% } % }