Properly handle REFERENCE_ID for future changes

This commit is contained in:
2025-12-16 20:17:45 +08:00
parent 6e94adbed4
commit 36e077c910
4 changed files with 227 additions and 165 deletions

View File

@@ -18,6 +18,9 @@ type BusStop struct {
// RouteStops[ StationSeq ] = BusStop // RouteStops[ StationSeq ] = BusStop
RouteStops *map[int]*BusStop RouteStops *map[int]*BusStop
// AltRoutes
AltRoutes *map[string]*BusStop
i18n.Generics i18n.Generics
} }

View File

@@ -3,6 +3,7 @@ package bus
import ( import (
"fmt" "fmt"
"sort" "sort"
"strconv"
"strings" "strings"
query "github.com/tgckpg/golifehk/query" query "github.com/tgckpg/golifehk/query"
@@ -65,29 +66,71 @@ func ( this QueryResult ) Message() ( string, error ) {
} else if 1 == len(*q.SearchTerms) { } else if 1 == len(*q.SearchTerms) {
// Route listing // Route listing
st := map[string] *BusStop{} busLines := map[string]map[string]*BusStop{}
keys := [] string{}
for _, entry := range *q.Results { for _, entry := range *q.Results {
busStop := any(entry).(*BusStop) busStop := any(entry).(*BusStop)
if _, ok := st[ busStop.Direction ]; ok { busRoutes := busLines[busStop.ReferenceId]
if busRoutes == nil {
busRoutes = make(map[string]*BusStop)
busLines[busStop.ReferenceId] = busRoutes
}
if _, ok := busRoutes[busStop.Direction]; ok {
continue continue
} }
st[ busStop.Direction ] = busStop busRoutes[busStop.Direction] = busStop
keys = append( keys, busStop.Direction )
for st[ busStop.Direction ].PrevStop() != nil { for busRoutes[busStop.Direction].PrevStop() != nil {
st[ busStop.Direction ] = st[ busStop.Direction ].PrevStop() busRoutes[busStop.Direction] = busRoutes[busStop.Direction].PrevStop()
} }
} }
sort.Strings( keys ) // Sort Bus Lines
lineKeys := make([]string, 0, len(busLines))
for k := range busLines {
lineKeys = append(lineKeys, k)
}
for _, d := range keys { sort.Slice(lineKeys, func(i, j int) bool {
b := st[ d ] ai, an := splitKey(lineKeys[i])
utils.WriteMDv2Text( &sb, q.Key ) bi, bn := splitKey(lineKeys[j])
// different base → normal string compare
if ai != bi {
return ai < bi
}
// same base: base key comes first
if an == -1 && bn != -1 {
return true
}
if an != -1 && bn == -1 {
return false
}
// both have suffix → numeric compare
return an < bn
})
for _, lineKey := range lineKeys {
busRoutes := busLines[lineKey]
// Sort route directions
dirKeys := make([]string, 0, len(busRoutes))
for k := range busRoutes {
dirKeys = append(dirKeys, k)
}
sort.Strings(dirKeys)
for _, d := range dirKeys {
b := busRoutes[d]
utils.WriteMDv2Text(&sb, lineKey)
if d == "O" { if d == "O" {
sb.WriteString("↑") sb.WriteString("↑")
@@ -109,6 +152,7 @@ func ( this QueryResult ) Message() ( string, error ) {
} }
sb.WriteString("\n") sb.WriteString("\n")
} }
}
} else { } else {
return "", fmt.Errorf("%s", "Unreachable condition occured!?") return "", fmt.Errorf("%s", "Unreachable condition occured!?")
@@ -146,3 +190,17 @@ func ( this QueryResult ) Message() ( string, error ) {
return sb.String(), nil return sb.String(), nil
} }
func splitKey(s string) (base string, num int) {
parts := strings.SplitN(s, "-", 2)
if len(parts) == 1 {
return s, -1 // base key, no suffix
}
n, err := strconv.Atoi(parts[1])
if err != nil {
return s, -1 // fallback: treat as base
}
return parts[0], n
}

View File

@@ -4,7 +4,6 @@ import (
"encoding/csv" "encoding/csv"
"fmt" "fmt"
"io" "io"
"log"
"net/http" "net/http"
"path/filepath" "path/filepath"
"strconv" "strconv"
@@ -63,21 +62,16 @@ func readBusStopData(r io.Reader) (*map[string]*BusStop, error) {
} }
} }
// Ignoring special route for now as getSchedule does not reflect entryId := fmt.Sprintf("%s:%s", entry.ReferenceId, entry.StationId)
// which bus is a special route
if entry.ReferenceId != entry.RouteId {
log.Printf("Ignoring special Route: %s", entry.ReferenceId)
continue
}
if busStops[entry.StationId] != nil { if busStops[entryId] != nil {
return nil, fmt.Errorf("Duplicated entry %+v", entry) return nil, fmt.Errorf("Duplicated entry %+v", entry)
} }
routeDir, hasKey := routeStops[entry.RouteId] routeDir, hasKey := routeStops[entry.ReferenceId]
if !hasKey { if !hasKey {
routeStops[entry.RouteId] = map[string]map[int]*BusStop{} routeStops[entry.ReferenceId] = map[string]map[int]*BusStop{}
routeDir = routeStops[entry.RouteId] routeDir = routeStops[entry.ReferenceId]
} }
route, hasKey := routeDir[entry.Direction] route, hasKey := routeDir[entry.Direction]
@@ -94,7 +88,7 @@ func readBusStopData(r io.Reader) (*map[string]*BusStop, error) {
entry.RouteStops = &route entry.RouteStops = &route
entry.Reload() entry.Reload()
busStops[entry.StationId] = &entry busStops[entryId] = &entry
} }
return &busStops, nil return &busStops, nil
} }

View File

@@ -1,7 +1,9 @@
package bus package bus
import ( import (
"log"
"strings" "strings"
query "github.com/tgckpg/golifehk/query" query "github.com/tgckpg/golifehk/query"
) )
@@ -42,6 +44,11 @@ func Query( lang string, message string ) query.IQueryResult {
for _, busStopSch := range schedules.BusStops { for _, busStopSch := range schedules.BusStops {
if busStopSch.BusStopId == busStop.StationId { if busStopSch.BusStopId == busStop.StationId {
if busStop.RouteId != busStop.ReferenceId {
// There were no indicator for special routes from getSchedule API
log.Printf("Ignoring special route matches: %s", busStop.ReferenceId)
continue
}
matches[busStop] = &busStopSch matches[busStop] = &busStopSch
break break
} }