Skip to content

Commit 68e7a5b

Browse files
committed
don't show final stop name twice
1 parent 3ccb0fc commit 68e7a5b

2 files changed

Lines changed: 31 additions & 11 deletions

File tree

app/lib/mapforge/trains/tools.rb

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ def self.next_departures(stops, now)
6262
.sort_by { |stop| stop[:departure] }
6363
end
6464

65+
# The next train that ends at a station. It has no departure, so #next_departures never sees
66+
# it, and no destination either, because the station it is standing in is the destination.
67+
def self.next_arrival(stops, now)
68+
stops.select { |stop| stop[:departure].nil? && stop[:arrival] && stop[:arrival] >= now }
69+
.min_by { |stop| stop[:arrival] }
70+
end
71+
6572
# How many minutes late a stop is. The live time and the planned one have to be the same event:
6673
# comparing an arrival against a planned departure would report the stop time as being early.
6774
def self.delay(stop)
@@ -101,17 +108,21 @@ def self.delay_colors(minutes)
101108
# route_setup left in label-title:
102109
#
103110
# 12:20 (12:18) → Gräfenberg (+2)
104-
# 12:35 → Nürnberg Nordost
111+
# 12:35
105112
#
106113
# Planned departure, planned arrival in brackets where the train does not start here, terminus,
107-
# and how late it currently is.
114+
# and how late it currently is. A train that ends here has no departure and no destination but
115+
# this station, whose name is above the board already, so its line is the planned arrival alone.
108116
def self.board_label(stops, now)
109-
next_departures(stops, now).map { |stop|
110-
minutes = ((stop[:departure] - stop[:planned]) / 60).round
117+
lines = next_departures(stops, now).map { |stop|
111118
arrival = stop[:planned_arrival]
112-
"#{hhmm(stop[:planned])}#{" (#{hhmm(arrival)})" if arrival} " \
113-
"→ #{stop[:destination]}#{" (+#{minutes})" unless minutes.zero?}"
114-
}.join("\n")
119+
[ stop[:planned], "#{hhmm(stop[:planned])}#{" (#{hhmm(arrival)})" if arrival}#{stop[:destination]}",
120+
((stop[:departure] - stop[:planned]) / 60).round ]
121+
}
122+
ends_here = next_arrival(stops, now)
123+
lines << [ ends_here[:planned], hhmm(ends_here[:planned]), delay(ends_here) ] if ends_here
124+
lines.sort_by(&:first).map { |_time, text, minutes| "#{text}#{" (+#{minutes})" unless minutes.zero?}" }
125+
.join("\n")
115126
end
116127

117128
# Every time here comes from the DB API and belongs to a German platform display, so it is shown

spec/lib/mapforge/trains/tools_spec.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,25 +151,34 @@ def properties(minutes, at: start)
151151
# 10:00 UTC is 12:00 in Berlin.
152152
let(:noon) { Time.utc(2026, 8, 1, 10, 0) }
153153

154+
def board_label(stops, time) = described_class.board_label(stops, time)
155+
154156
it "puts the planned arrival in brackets and the delay behind the destination" do
155157
stops = [ { destination: "Gräfenberg", planned_arrival: noon + 8.minutes,
156158
planned: noon + 10.minutes, departure: noon + 12.minutes } ]
157159

158-
expect(described_class.board_label(stops, noon)).to eq("12:10 (12:08) → Gräfenberg (+2)")
160+
expect(board_label(stops, noon)).to eq("12:10 (12:08) → Gräfenberg (+2)")
159161
end
160162

161163
it "leaves out the brackets where the train starts here, and the delay where there is none" do
162164
stops = [ { destination: "Gräfenberg", planned: noon + 10.minutes, departure: noon + 10.minutes } ]
163165

164-
expect(described_class.board_label(stops, noon)).to eq("12:10 → Gräfenberg")
166+
expect(board_label(stops, noon)).to eq("12:10 → Gräfenberg")
167+
end
168+
169+
it "shows a train that ends here as its arrival alone, without a destination" do
170+
stops = [ { planned_arrival: noon + 35.minutes, planned: noon + 35.minutes, arrival: noon + 37.minutes },
171+
{ destination: "Nürnberg", planned: noon + 40.minutes, departure: noon + 40.minutes } ]
172+
173+
expect(board_label(stops, noon)).to eq("12:35 (+2)\n12:40 → Nürnberg")
165174
end
166175

167176
it "is one line per destination, earliest first, and empty once nothing is due" do
168177
stops = [ { destination: "Gräfenberg", planned: noon + 20.minutes, departure: noon + 20.minutes },
169178
{ destination: "Nürnberg", planned: noon + 5.minutes, departure: noon + 5.minutes } ]
170179

171-
expect(described_class.board_label(stops, noon)).to eq("12:05 → Nürnberg\n12:20 → Gräfenberg")
172-
expect(described_class.board_label(stops, noon + 1.hour)).to eq("")
180+
expect(board_label(stops, noon)).to eq("12:05 → Nürnberg\n12:20 → Gräfenberg")
181+
expect(board_label(stops, noon + 1.hour)).to eq("")
173182
end
174183
end
175184
end

0 commit comments

Comments
 (0)