penguin/golifehk

@golifehk bot in Telegram

datasources/kmb/RouteStop.go

raw ยท 1874 bytes

package kmb

import (
	"strings"

	query "github.com/tgckpg/golifehk/query"
)

type RouteStop struct {
	BusStop     *BusStop
	RouteId     string `json:"route"`
	ServiceType string `json:"service_type"`
	Direction   string `json:"bound"`
	StationSeq  int    `json:"seq,string"`
	StationId   string `json:"stop"`

	RouteStops *map[int]*RouteStop
	query.Words
	query.NoGeoLocation
}

type RouteStops struct {
	Type        string       `json:"type"`
	Version     string       `json:"version"`
	DateCreated string       `json:"generated_timestamp"`
	RouteStops  []*RouteStop `json:"data"`
}

func (routeStop RouteStop) PrevStop() *RouteStop {
	if v, hasKey := (*routeStop.RouteStops)[routeStop.StationSeq-1]; hasKey {
		return v
	}
	return nil
}

func (routeStop RouteStop) NextStop() *RouteStop {
	if v, hasKey := (*routeStop.RouteStops)[routeStop.StationSeq+1]; hasKey {
		return v
	}
	return nil
}

func isUpperHex16(s string) bool {
	if len(s) != 16 {
		return false
	}

	for i := 0; i < 16; i++ {
		c := s[i]
		if (c < '0' || c > '9') && (c < 'A' || c > 'F') {
			return false
		}
	}

	return true
}

func (this *RouteStop) Test(val string) bool {

	if isUpperHex16(val) {
		return (this.BusStop.BusStopId == val)
	}

	data := this.SearchData
	if data == nil {
		return false
	}

	for _, v := range *data {
		if strings.Contains(*v, val) {
			return true
		}
	}
	return false
}

func (this *RouteStop) Reload() {
	this.Key = &this.RouteId
	this.SearchData = this.BusStop.SearchData
}

type ByRoute []*RouteStop

func (a ByRoute) Len() int      { return len(a) }
func (a ByRoute) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByRoute) Less(i, j int) bool {
	_a := *a[i]
	_b := *a[j]
	if _a.RouteId == _b.RouteId {
		if _a.Direction == _b.Direction {
			return _a.ServiceType < _b.ServiceType
		}
		return _a.Direction < _b.Direction
	}
	return _a.RouteId < _b.RouteId
}