From 5edee6bc6f89bd292aff76358a0f8c0f4f31a388 Mon Sep 17 00:00:00 2001
From: "hieda_kyuko@hpr" <ugo58956@protonmail.com>
Date: Thu, 22 May 2025 15:39:29 +0200
Subject: [PATCH] Sort departures in timetable + Raptor fixes

---
 lib/App/FrameUtils.pm | 56 ++++++++++++++++++++++++++++++++++++-------
 lib/App/Navi.pm       | 11 ++++++---
 src/oud2_parser.pl    |  7 +++---
 3 files changed, 58 insertions(+), 16 deletions(-)

diff --git a/lib/App/FrameUtils.pm b/lib/App/FrameUtils.pm
index 8ed5bec..5026e68 100644
--- a/lib/App/FrameUtils.pm
+++ b/lib/App/FrameUtils.pm
@@ -53,6 +53,7 @@ sub hassya_hyou
 	my $rosen_id = $_[0];
 	my $rosen = MasterUtils::get_rosen_frame $master, $rosen_id;
 	my @ressyas = @{ $_[1] };
+	my @sorted_ressyas = ();
 	my $is_down = $_[3];
 	my $target_absolute = MasterUtils::get_eki_position $master, $rosen_id, $_[2];
 	my $target_relative = $is_down
@@ -61,28 +62,65 @@ sub hassya_hyou
 	
 	foreach my $ressya (@ressyas)
 	{
-	    my %data = %{ $ressya };
-	    my @jikoku = @{ $data{'jikoku'} };
+	    my @jikoku = @{ $ressya->{'jikoku'} };
 	    my $pos = 0;
 	    while ($pos != $target_relative) { shift @jikoku; $pos++; }
 	    if (!defined $jikoku[0]) { next; }
 	    
-	    my %ts = %{ $jikoku[0] };
-	    my $time = $ts{'hatsu'};
+	    my $ts = $jikoku[0];
+	    my $time = $ts->{'hatsu'};
 	    # Timestamp has no dep time (only arrival time for ex)
 	    if (!defined $time) { next; }
 	    # This service does not stop here
-	    if ($ts{'mode'} != 1) { next; }
+	    if ($ts->{'mode'} != 1) { next; }
+
+	    my $corrected_time = $time < 300
+		? $time + 2400
+		: $time;
+	    my %frame = ( time => $corrected_time, ressya => $ressya );
+	    # Add this service into a new sorted list
+	    $pos = 0;
+	    if ($#sorted_ressyas < 0)
+	    {
+		push @sorted_ressyas, \%frame;
+	    }
+	    else
+	    {
+		# Days start at 3:00
+		while (defined $sorted_ressyas[$pos] &&
+		       $sorted_ressyas[$pos]->{'time'} < $corrected_time)
+		{
+		    $pos++;
+		}
+		splice @sorted_ressyas, $pos, 0, \%frame;
+	    }
+	}
+
+	my $current_hour = -1;
+	foreach my $frame (@sorted_ressyas)
+	{
+	    my $time = $frame->{'time'};
+	    $time = $time > 2400
+		? $time - 2400
+		: $time;
+	    my $hour = int $time / 100;
+	    if ($hour != $current_hour)
+	    {
+		print "== $hour ==\n";
+		$current_hour = $hour;
+	    }
+	    my $ressya = $frame->{'ressya'};
+	    my @jikoku = @{ $ressya->{'jikoku'} };
 	    # Format: time (tab) destination (tab) type (with eventual name)
-	    my $syubetsu = get_syubetsu $master, $rosen_id, $data{'syubetsu'};
+	    my $syubetsu = get_syubetsu $master, $rosen_id, $ressya->{'syubetsu'};
 	    print( (TimeUtils::format_time_simple $time)
 		   . "\tfor "
-		   . (get_destination $master, $rosen_id, \@jikoku, $target_relative, $is_down)
+		   . (get_destination $master, $rosen_id, \@jikoku, 0, $is_down)
 		   . "\t$syubetsu")
 		;
-	    if (defined $data{'meisyo'})
+	    if (defined $ressya->{'meisyo'})
 	    {
-		print " $data{'meisyo'}";
+		print " $ressya->{'meisyo'}";
 	    }
 	    print "\n";
 	}
diff --git a/lib/App/Navi.pm b/lib/App/Navi.pm
index f52eedd..bf7c114 100644
--- a/lib/App/Navi.pm
+++ b/lib/App/Navi.pm
@@ -36,8 +36,7 @@ sub Raptor_simple
     # Use ~0 as a substitute to infty, not that travel times would exceed this anyway
     my %taus;
     my %taustar;
-    my @marked;
-    
+    my @marked;    
 
     # We add two more hashes:
     # one to keep track of the last used service to arrive at a station
@@ -45,6 +44,10 @@ sub Raptor_simple
     # and one to save the station used to board this last used service
     my %from;
 
+    # We also save the number of times a station was visited
+    # This may need a demonstration but we consider that the minimal time is already reached if the station is processed two times the number of routes it is served by.
+    my %visited;
+
     my sub earliest_trip
     {
 	my $k = $_[0];
@@ -84,6 +87,7 @@ sub Raptor_simple
 	$taus{$key} = \@a;
 	$taustar{$key} = ~0;
 	$earliest{$key} = ();
+	$visited{$key} = 0;
     }
     
     $taus{$from_id}[0] = $dep_time;
@@ -121,12 +125,13 @@ sub Raptor_simple
 			}
 		    }
 		}
-		if (!$substituted)
+		if (!$substituted && $visited{$p} < 2 * scalar @rosens)
 		{
 		    push @Q, [$r, $d, $p];
 		}
 	    }
 	    Log::d "Unmarking $p_display ($p)";
+	    $visited{$p}++;
 	}
 	@marked = ();
 
diff --git a/src/oud2_parser.pl b/src/oud2_parser.pl
index e07a51d..96dab04 100644
--- a/src/oud2_parser.pl
+++ b/src/oud2_parser.pl
@@ -259,7 +259,7 @@ foreach my $file (@trains)
     catch ($e) { print "Malformation in .oud file.\n$e\n"; }
     print "\n";
 
-    print $recce->show_progress();
+    # print $recce->show_progress();
 
     my %frame = %{ ${$recce->value()} };
     $frame{'id'} = $basename;
@@ -342,6 +342,5 @@ foreach my $frame (values %{ $master{'rosen'} })
     $frame->{'eki'} = \@new_ekis;
 }
 
-# MasterUtils::get_routes \%master, 'konpoku_9';
-# FrameUtils::hassya_hyou \%master, 'konpoku_9';
-Navi::Raptor_simple \%master, 'sekihoku1_0', 'senmo_6', 1330;
+# FrameUtils::hassya_hyou \%master, 'sekihoku1_28';
+Navi::Raptor_simple \%master, 'sibetu_9', 'sibetu_34', 800;
-- 
GitLab