golifehk/datasources/mtr/bus/QueryResult.go

97 lines
2.6 KiB
Go
Raw Normal View History

2022-09-14 13:21:39 +00:00
package bus
import (
"fmt"
2022-09-15 12:00:49 +00:00
"sort"
2022-09-14 13:21:39 +00:00
"strings"
)
type QueryResult struct {
Schedules *map[BusStop] *BusStopBuses
Lang string
Error error
2022-09-15 12:00:49 +00:00
Query *QueryObject
2022-09-14 13:21:39 +00:00
}
2022-09-15 12:00:49 +00:00
func writeShortRoute( lang *string, sb *strings.Builder, b *BusStop ) {
if b.PrevStop() != nil {
sb.WriteString( (*b.PrevStop().Name)[ *lang ] )
sb.WriteString( " > " )
}
sb.WriteString( "**" )
sb.WriteString( (*b.Name)[ *lang ] )
sb.WriteString( "**" )
if b.NextStop() != nil {
sb.WriteString( " > " )
sb.WriteString( (*b.NextStop().Name)[ *lang ] )
}
sb.WriteString( "\n" )
}
type ByRouteId []BusStop
func (a ByRouteId) Len() int { return len(a) }
func (a ByRouteId) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByRouteId) Less(i, j int) bool { return a[i].RouteId < a[j].RouteId }
2022-09-14 13:21:39 +00:00
func ( result QueryResult ) Message() string {
if result.Error != nil {
return fmt.Sprintf( "%s", result.Error )
}
sb := strings.Builder{}
2022-09-15 12:00:49 +00:00
if result.Schedules == nil {
2022-09-14 13:21:39 +00:00
2022-09-15 12:00:49 +00:00
query := *result.Query
if query.Route == "" {
sort.Sort( ByRouteId( *query.BusStops ) )
for _, busStop := range *query.BusStops {
sb.WriteString( busStop.RouteId )
sb.WriteString( " " )
writeShortRoute( &result.Lang, &sb, &busStop )
}
} else if query.BusStops == nil && query.RouteStarts != nil {
for d, b := range *query.RouteStarts {
sb.WriteString( query.Route )
sb.WriteString( "-" )
sb.WriteString( d )
sb.WriteString( "\n " )
2022-09-14 13:21:39 +00:00
2022-09-15 12:00:49 +00:00
for {
sb.WriteString( (*b.Name)[ result.Lang ] )
b = b.NextStop()
if b == nil {
break
}
2022-09-14 13:21:39 +00:00
2022-09-15 12:00:49 +00:00
sb.WriteString( " > " )
}
sb.WriteString( "\n" )
}
} else {
sb.WriteString( "Unreachable condition occured!?" )
2022-09-14 13:21:39 +00:00
}
2022-09-15 12:00:49 +00:00
} else {
for busStop, buses := range *result.Schedules {
writeShortRoute( &result.Lang, &sb, &busStop )
for _, bus := range buses.Buses {
sb.WriteString( " * " )
if bus.ETAText == "" {
sb.WriteString( bus.ETDText )
} else {
sb.WriteString( bus.ETAText )
}
sb.WriteString( "\n" )
2022-09-14 14:54:51 +00:00
}
2022-09-14 13:21:39 +00:00
sb.WriteString( "\n" )
}
}
return sb.String()
}