go/trains/cmd/train2livesplit: fix

This commit is contained in:
Luke Granger-Brown 2021-11-25 17:33:05 +00:00
parent 9cb68e94d3
commit 1f34692907

View file

@ -106,13 +106,16 @@ func fetchTrainOnce(ctx context.Context, id int) (*webapi.ServiceData, error) {
func generateLiveSplitScriptData(sd *webapi.ServiceData) (*templateVars, error) { func generateLiveSplitScriptData(sd *webapi.ServiceData) (*templateVars, error) {
asf := new(strings.Builder) asf := new(strings.Builder)
currentSplit := -1 currentSplit := -1
usingTime := func(td *webapi.TimingData) time.Time { usingTime := func(td *webapi.TimingData) (time.Time, error) {
if td.PublicScheduled != nil { if td.PublicScheduled != nil {
return *td.PublicScheduled return td.PublicScheduled.ToTime()
} }
return *td.WorkingScheduled return td.WorkingScheduled.ToTime()
}
timetableDepartureTime, err := usingTime(sd.Locations[0].DepartureTiming)
if err != nil {
return nil, fmt.Errorf("parsing departure time: %w", err)
} }
timetableDepartureTime := usingTime(sd.Locations[0].DepartureTiming)
var lastHadActual bool var lastHadActual bool
for n, l := range sd.Locations { for n, l := range sd.Locations {
var usingData *webapi.TimingData var usingData *webapi.TimingData
@ -124,7 +127,10 @@ func generateLiveSplitScriptData(sd *webapi.ServiceData) (*templateVars, error)
case l.ArrivalTiming != nil: case l.ArrivalTiming != nil:
usingData = l.ArrivalTiming usingData = l.ArrivalTiming
} }
usingTimetableTime := usingTime(usingData) usingTimetableTime, err := usingTime(usingData)
if err != nil {
return nil, fmt.Errorf("parsing time for %q: %w", l.Location.Name, err)
}
var secondsOnTimetable, secondsActual int var secondsOnTimetable, secondsActual int
secondsOnTimetable = int((usingTimetableTime.Sub(timetableDepartureTime)) / time.Second) secondsOnTimetable = int((usingTimetableTime.Sub(timetableDepartureTime)) / time.Second)
@ -135,7 +141,11 @@ func generateLiveSplitScriptData(sd *webapi.ServiceData) (*templateVars, error)
lastHadActual = false lastHadActual = false
} }
if usingData.Actual != nil { if usingData.Actual != nil {
secondsActual = int((usingData.Actual.Sub(timetableDepartureTime)) / time.Second) timeActual, err := usingData.Actual.ToTime()
if err != nil {
return nil, fmt.Errorf("parsing actual time for %q: %w", l.Location.Name, err)
}
secondsActual = int((timeActual.Sub(timetableDepartureTime)) / time.Second)
lastHadActual = true lastHadActual = true
} }
@ -159,8 +169,8 @@ this["state"].CurrentSplitIndex = %d;
return &templateVars{ return &templateVars{
Service: sd, Service: sd,
FirstLocation: &sd.Locations[0], FirstLocation: sd.Locations[0],
LastLocation: &sd.Locations[len(sd.Locations)-1], LastLocation: sd.Locations[len(sd.Locations)-1],
AddSplitFunctions: asf.String(), AddSplitFunctions: asf.String(),
}, nil }, nil
} }