golifehk/datasources/mtr/bus/QueryResult.go

135 lines
3.9 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"
2022-09-16 20:33:47 +00:00
query "github.com/tgckpg/golifehk/query"
utils "github.com/tgckpg/golifehk/utils"
2022-09-14 13:21:39 +00:00
)
type QueryResult struct {
2022-09-16 20:33:47 +00:00
Schedules *map[*BusStop] *BusStopBuses
2022-09-14 13:21:39 +00:00
Lang string
Error error
2022-09-16 20:33:47 +00:00
Query *query.QueryObject
2022-09-15 13:38:04 +00:00
}
2022-09-15 12:00:49 +00:00
func writeShortRoute( lang *string, sb *strings.Builder, b *BusStop ) {
if b.PrevStop() != nil {
2022-09-25 10:48:22 +00:00
utils.WriteMDv2Text( sb, (*b.PrevStop().Name)[ *lang ] )
2022-09-15 13:38:04 +00:00
sb.WriteString( " \\> " )
2022-09-15 12:00:49 +00:00
}
2022-09-15 13:38:04 +00:00
sb.WriteString( "*" )
2022-09-25 10:48:22 +00:00
utils.WriteMDv2Text( sb, (*b.Name)[ *lang ] )
2022-09-15 13:38:04 +00:00
sb.WriteString( "*" )
2022-09-15 12:00:49 +00:00
if b.NextStop() != nil {
2022-09-15 13:38:04 +00:00
sb.WriteString( " \\> " )
2022-09-25 10:48:22 +00:00
utils.WriteMDv2Text( sb, (*b.NextStop().Name)[ *lang ] )
2022-09-15 12:00:49 +00:00
}
sb.WriteString( "\n" )
}
2022-09-16 20:33:47 +00:00
func ( this QueryResult ) Message() ( string, error ) {
2022-09-14 13:21:39 +00:00
2022-09-16 20:33:47 +00:00
if this.Error != nil {
return "", this.Error
2022-09-14 13:21:39 +00:00
}
sb := strings.Builder{}
2022-09-16 20:33:47 +00:00
if this.Schedules == nil {
2022-09-14 13:21:39 +00:00
2022-09-16 20:33:47 +00:00
q := *this.Query
if q.Key == "" {
sort.Sort( query.ByKey( *q.Results ) )
for _, entry := range *q.Results {
busStop := any( entry ).( *BusStop )
2022-09-25 10:48:22 +00:00
utils.WriteMDv2Text( &sb, busStop.RouteId )
2022-09-15 12:00:49 +00:00
sb.WriteString( " " )
2022-09-16 20:33:47 +00:00
writeShortRoute( &this.Lang, &sb, busStop )
}
} else if 1 == len( *q.SearchTerms ) && 0 < len( *q.Results ) {
// Route listing
st := map[string] *BusStop{}
keys := [] string{}
for _, entry := range *q.Results {
busStop := any( entry ).( *BusStop )
if _, ok := st[ busStop.Direction ]; ok {
continue
}
st[ busStop.Direction ] = busStop
keys = append( keys, busStop.Direction )
for st[ busStop.Direction ].PrevStop() != nil {
st[ busStop.Direction ] = st[ busStop.Direction ].PrevStop()
}
2022-09-15 12:00:49 +00:00
}
2022-09-16 20:33:47 +00:00
sort.Strings( keys )
for _, d := range keys {
b := st[ d ]
sb.WriteString( q.Key )
2022-09-25 10:48:22 +00:00
if d == "O" {
sb.WriteString( "↑" )
} else if d == "I" {
sb.WriteString( "↓" )
} else {
sb.WriteString( "\\?" )
}
2022-09-15 12:00:49 +00:00
sb.WriteString( "\n " )
2022-09-14 13:21:39 +00:00
2022-09-15 12:00:49 +00:00
for {
2022-09-25 10:48:22 +00:00
utils.WriteMDv2Text( &sb, (*b.Name)[ this.Lang ] )
2022-09-15 12:00:49 +00:00
b = b.NextStop()
if b == nil {
break
}
2022-09-14 13:21:39 +00:00
2022-09-15 13:38:04 +00:00
sb.WriteString( " \\> " )
2022-09-15 12:00:49 +00:00
}
sb.WriteString( "\n" )
}
2022-09-16 20:33:47 +00:00
} else if 1 < len( *q.SearchTerms ) && len( *q.Results ) == 0 {
terms := make( []string, len(*q.SearchTerms), len(*q.SearchTerms) )
for i, term := range *q.SearchTerms {
terms[i] = term.Org
}
return "", fmt.Errorf( "Not Found: \"%s\"", strings.Join( terms, "\", \"" ) )
2022-09-15 12:00:49 +00:00
} else {
2022-09-16 20:33:47 +00:00
return "", fmt.Errorf( "%s", "Unreachable condition occured!?" )
2022-09-14 13:21:39 +00:00
}
2022-09-15 12:00:49 +00:00
} else {
2022-09-16 20:33:47 +00:00
if 0 < len( *this.Schedules ) {
for busStop, buses := range *this.Schedules {
writeShortRoute( &this.Lang, &sb, busStop )
2022-09-15 13:38:04 +00:00
for _, bus := range buses.Buses {
sb.WriteString( " \\* " )
if bus.ETAText == "" {
2022-09-25 10:48:22 +00:00
utils.WriteMDv2Text( &sb, bus.ETDText )
2022-09-15 13:38:04 +00:00
} else {
2022-09-25 10:48:22 +00:00
utils.WriteMDv2Text( &sb, bus.ETAText )
2022-09-15 13:38:04 +00:00
}
sb.WriteString( "\n" )
2022-09-15 12:00:49 +00:00
}
sb.WriteString( "\n" )
2022-09-14 14:54:51 +00:00
}
2022-09-15 13:38:04 +00:00
} else {
2022-09-25 10:48:22 +00:00
utils.WriteMDv2Text( &sb, "Schedules are empty...perhaps Out of Service Time?" )
2022-09-14 13:21:39 +00:00
}
}
2022-09-16 20:33:47 +00:00
return sb.String(), nil
2022-09-14 13:21:39 +00:00
}