penguin/golifehk

@golifehk bot in Telegram

datasources/mtr/bus/BusStop.go

raw ยท 1578 bytes

package bus

import (
	"fmt"
	i18n "github.com/tgckpg/golifehk/i18n"
	query "github.com/tgckpg/golifehk/query"
)

type BusStop struct {
	RouteId     string
	ReferenceId string
	Direction   string
	StationSeq  int
	StationId   string
	Name_zh     string
	Name_en     string

	// RouteStops[ StationSeq ] = BusStop
	RouteStops *map[int]*BusStop

	// AltRoutes
	AltRoutes *map[string]*BusStop

	i18n.Generics
	query.GeoLocation
}

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

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

func (this *BusStop) Reload() {
	i18n_Name := map[string]string{}
	i18n_Name["en"] = this.Name_en
	i18n_Name["zh-Hant"] = this.Name_zh

	searchData := []*string{}
	searchData = append(searchData, &this.Name_en)
	searchData = append(searchData, &this.Name_zh)

	this.Name = &i18n_Name
	this.Key = &this.RouteId
	this.SearchData = &searchData
}

func (this BusStop) Register(registers map[string]struct{}) bool {
	key := fmt.Sprintf("%s,%s", this.StationId, this.ReferenceId)
	if _, ok := registers[key]; ok {
		return false
	}
	registers[key] = struct{}{}
	return true
}

type ByRoute []*BusStop

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 {
		return _a.Direction < _b.Direction
	}
	return _a.RouteId < _b.RouteId
}