77 lines
1.8 KiB
Go
77 lines
1.8 KiB
Go
package types
|
|
|
|
import (
|
|
"iter"
|
|
"time"
|
|
)
|
|
|
|
type Trip struct {
|
|
*Route
|
|
Stops []*StopTime `json:"stops" csv:"stops"`
|
|
RouteId string `json:"route_id" csv:"route_id"`
|
|
ServiceId string `json:"service_id" csv:"service_id"`
|
|
TripId string `json:"trip_id" csv:"trip_id"`
|
|
TripHeadsign string `json:"trip_headsign" csv:"trip_headsign"`
|
|
TripShortName string `json:"trip_short_name" csv:"trip_short_name"`
|
|
}
|
|
|
|
func (t *Trip) GetDirectPossibleDestinations(stop *Stop, when time.Time) iter.Seq[*StopTime] {
|
|
return func(yield func(*StopTime) bool) {
|
|
started := false
|
|
for _, st := range t.Stops {
|
|
if !started {
|
|
if st.StopId == stop.StopId && st.PickupType == 0 {
|
|
started = true
|
|
}
|
|
continue
|
|
}
|
|
if started && st.DropOffType == 0 && st.DepartsAfter(when) {
|
|
if !yield(st) {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (t *Trip) SetRoute(route *Route) {
|
|
t.Route = route
|
|
}
|
|
|
|
func (t *Trip) AddStopTime(stopTime *StopTime) {
|
|
if t.Stops == nil {
|
|
t.Stops = make([]*StopTime, 0)
|
|
}
|
|
// Find the index to insert based on StopSequence
|
|
idx := 0
|
|
for i, st := range t.Stops {
|
|
if stopTime.StopSequence < st.StopSequence {
|
|
idx = i
|
|
break
|
|
}
|
|
idx = i + 1
|
|
}
|
|
// Insert at the correct position
|
|
t.Stops = append(t.Stops[:idx], append([]*StopTime{stopTime}, t.Stops[idx:]...)...)
|
|
// Adjust times if necessary for next-day continuation
|
|
for i := idx; i < len(t.Stops)-1; i++ {
|
|
curr := t.Stops[i]
|
|
next := t.Stops[i+1]
|
|
if next.ArrivalTime < curr.ArrivalTime {
|
|
next.ArrivalTime += 86400
|
|
}
|
|
if next.DepartureTime < curr.DepartureTime {
|
|
next.DepartureTime += 86400
|
|
}
|
|
}
|
|
}
|
|
|
|
func (t *Trip) Has(stop *Stop) (*StopTime, bool) {
|
|
for _, st := range t.Stops {
|
|
if st.StopId == stop.StopId {
|
|
return st, true
|
|
}
|
|
}
|
|
return nil, false
|
|
}
|