rts-sim-module/fi/balise.go

66 lines
2.1 KiB
Go

package fi
import (
"fmt"
"joylink.club/ecs"
"joylink.club/rtsssimulation/component"
"joylink.club/rtsssimulation/entity"
)
// BaliseUpdateFixedTelegram 更新固定报文
func BaliseUpdateFixedTelegram(w ecs.World, id string, telegram []byte, userTelegram []byte) error {
result := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] {
wd := entity.GetWorldData(w)
entry, ok := wd.EntityMap[id]
if ok {
baliseFixedTelegram := component.BaliseFixedTelegramType.Get(entry)
baliseFixedTelegram.Telegram = telegram
baliseFixedTelegram.UserTelegram = userTelegram
} else {
return ecs.NewErrResult(fmt.Errorf("未找到id=%s的应答器", id))
}
return ecs.NewOkEmptyResult()
})
return result.Err
}
// BaliseUpdateVariableTelegram 更新可变报文
func BaliseUpdateVariableTelegram(w ecs.World, id string, telegram []byte, force bool) error {
result := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] {
wd := entity.GetWorldData(w)
entry, ok := wd.EntityMap[id]
if ok {
if entry.HasComponent(component.BaliseVariableTelegramType) {
if force {
entry.AddComponent(component.ForceVariableTelegram)
} else if entry.HasComponent(component.ForceVariableTelegram) {
return ecs.NewOkEmptyResult()
}
baliseVariableTelegram := component.BaliseVariableTelegramType.Get(entry)
baliseVariableTelegram.Telegram = telegram
} else {
ecs.NewErrResult(fmt.Errorf("应答器[%s]无可变报文组件", id))
}
} else {
return ecs.NewErrResult(fmt.Errorf("未找到应答器[%s]", id))
}
return ecs.NewOkEmptyResult()
})
return result.Err
}
// BaliseCancelForceVariableTelegram 取消强制可变报文
func BaliseCancelForceVariableTelegram(w ecs.World, id string) error {
request := <-ecs.Request[ecs.EmptyType](w, func() ecs.Result[ecs.EmptyType] {
wd := entity.GetWorldData(w)
entry, ok := wd.EntityMap[id]
if ok {
if entry.HasComponent(component.ForceVariableTelegram) {
entry.RemoveComponent(component.ForceVariableTelegram)
}
}
return ecs.NewOkEmptyResult()
})
return request.Err
}