rts-sim-module/entity/turnout.go

153 lines
6.0 KiB
Go

package entity
import (
"fmt"
"strings"
"unsafe"
"joylink.club/ecs"
"joylink.club/rtsssimulation/component"
"joylink.club/rtsssimulation/repository"
"joylink.club/rtsssimulation/repository/model/proto"
)
// 新建道岔实体
func NewTurnoutEntity(w ecs.World, uid string, worldData *component.WorldData) *ecs.Entry {
entry, ok := worldData.EntityMap[uid]
if !ok {
entry = w.Entry(w.Create(component.TurnoutTag, component.UidType, component.TurnoutPositionType))
component.UidType.SetValue(entry, component.Uid{Id: uid})
worldData.EntityMap[uid] = entry
}
return entry
}
// 加载道岔实体
func LoadTurnouts(w ecs.World) error {
data := GetWorldData(w)
turnouts := data.Repo.TurnoutList()
for _, turnout := range turnouts {
entry := NewTurnoutEntity(w, turnout.Id(), data)
var err error
switch turnout.SwitchMachineType() {
case proto.Turnout_ZDJ9_Single:
err = LoadTurnoutZdj9One(w, turnout, entry)
case proto.Turnout_ZDJ9_Double:
err = LoadTurnoutZdj9Two(w, turnout, entry, data.EntityMap)
default:
return fmt.Errorf("id=%s的道岔没有转辙机型号数据", turnout.Id())
}
if err != nil {
return err
}
}
return nil
}
// 加载道岔ZDJ9单机转辙机
func LoadTurnoutZdj9One(w ecs.World, turnout *repository.Turnout, entry *ecs.Entry) error {
panic("道岔ZDJ9单机转辙机加载未实现")
}
// 加载道岔ZDJ9双机转辙机
func LoadTurnoutZdj9Two(w ecs.World, turnout *repository.Turnout, entry *ecs.Entry, entityMap map[string]*ecs.Entry) error {
// 加载转辙机
entrys := ecs.Entries(w, w.CreateMany(2, component.ZzjStateType, component.FixedPositionTransformType))
// 给道岔添加转辙机引用组件
entry.AddComponent(component.TurnoutZzjType, unsafe.Pointer(&component.TurnoutZzj{
ZzjList: entrys,
}))
// 继电器组合电路
groups := turnout.RelayGroups()
size := len(groups)
if size == 3 { // 有继电器组合,加载继电器实体和电路相关组件
zdj9TwoElectronic := &component.Zdj9TwoElectronic{}
for _, group := range groups {
elecs := group.Components()
if group.Code() == "TDC" {
for _, elec := range elecs {
relay := elec.(*repository.Relay)
if relay.Code() == "DCJ" {
zdj9TwoElectronic.TDC_DCJ = NewRelayEntity(w, relay, entityMap)
} else if relay.Code() == "FCJ" {
zdj9TwoElectronic.TDC_FCJ = NewRelayEntity(w, relay, entityMap)
} else if relay.Code() == "YCJ" {
zdj9TwoElectronic.TDC_YCJ = NewRelayEntity(w, relay, entityMap)
} else if relay.Code() == "ZDBJ" {
zdj9TwoElectronic.TDC_ZDBJ = NewRelayEntity(w, relay, entityMap)
} else if relay.Code() == "ZFBJ" {
zdj9TwoElectronic.TDC_ZFBJ = NewRelayEntity(w, relay, entityMap)
} else {
return fmt.Errorf("未知的道岔[%s]的组合[%s]继电器: %s", turnout.Id(), group.Code(), relay.Code())
}
}
} else if group.Code() == "TDFJ1" {
for _, elec := range elecs {
// TODO:数据修复后删除
if elec.Code() == "" {
continue
}
if elec.Code() == "1DQJ" {
zdj9TwoElectronic.TDFJ1_1DQJ = NewRelayEntity(w, elec.(*repository.Relay), entityMap)
} else if elec.Code() == "BHJ" {
zdj9TwoElectronic.TDFJ1_BHJ = NewRelayEntity(w, elec.(*repository.Relay), entityMap)
} else if elec.Code() == "2DQJ" {
zdj9TwoElectronic.TDFJ1_2DQJ = NewRelayEntity(w, elec.(*repository.Relay), entityMap)
} else if elec.Code() == "1DQJF" {
zdj9TwoElectronic.TDFJ1_1DQJF = NewRelayEntity(w, elec.(*repository.Relay), entityMap)
} else if elec.Code() == "DBQ" { // 断相保护器
zdj9TwoElectronic.TDFJ1_DBQ = NewDBQEntity(w, elec.Id(), entityMap)
} else if elec.Code() == "DBJ" {
zdj9TwoElectronic.TDFJ1_DBJ = NewRelayEntity(w, elec.(*repository.Relay), entityMap)
} else if elec.Code() == "FBJ" {
zdj9TwoElectronic.TDFJ1_FBJ = NewRelayEntity(w, elec.(*repository.Relay), entityMap)
} else if elec.Code() == "QDJ" {
zdj9TwoElectronic.TDFJ1_QDJ = NewRelayEntity(w, elec.(*repository.Relay), entityMap)
} else if elec.Code() == "ZBHJ" {
zdj9TwoElectronic.TDFJ1_ZBHJ = NewRelayEntity(w, elec.(*repository.Relay), entityMap)
} else {
return fmt.Errorf("未知的道岔[%s]的组合[%s]继电器: %s", turnout.Id(), group.Code(), elec.Code())
}
}
} else if group.Code() == "TDFJ2" {
for _, elec := range elecs {
// TODO:数据修复后删除
if elec.Code() == "" {
continue
}
if elec.Code() == "1DQJ" {
zdj9TwoElectronic.TDFJ2_1DQJ = NewRelayEntity(w, elec.(*repository.Relay), entityMap)
} else if elec.Code() == "BHJ" {
zdj9TwoElectronic.TDFJ2_BHJ = NewRelayEntity(w, elec.(*repository.Relay), entityMap)
} else if elec.Code() == "2DQJ" {
zdj9TwoElectronic.TDFJ2_2DQJ = NewRelayEntity(w, elec.(*repository.Relay), entityMap)
} else if elec.Code() == "1DQJF" {
zdj9TwoElectronic.TDFJ2_1DQJF = NewRelayEntity(w, elec.(*repository.Relay), entityMap)
} else if elec.Code() == "DBQ" { // 断相保护器
zdj9TwoElectronic.TDFJ2_DBQ = NewDBQEntity(w, elec.Id(), entityMap)
} else if elec.Code() == "DBJ" {
zdj9TwoElectronic.TDFJ2_DBJ = NewRelayEntity(w, elec.(*repository.Relay), entityMap)
} else if elec.Code() == "FBJ" {
zdj9TwoElectronic.TDFJ2_FBJ = NewRelayEntity(w, elec.(*repository.Relay), entityMap)
} else {
return fmt.Errorf("未知的道岔[%s]的组合[%s]继电器: %s", turnout.Id(), group.Code(), elec.Code())
}
}
} else {
return fmt.Errorf("未知的道岔[%s]组合类型:%s", turnout.Id(), group.Code())
}
}
nils := zdj9TwoElectronic.CheckNilReference()
if len(nils) > 0 {
return fmt.Errorf("道岔[%s]ZDJ9双机继电器组合数据异常,缺失:[%s]", turnout.Id(), strings.Join(nils, ","))
} else {
// 给道岔添加电路组件
entry.AddComponent(component.Zdj9TwoElectronicType, unsafe.Pointer(zdj9TwoElectronic))
}
} else if size > 0 && size < 3 {
return fmt.Errorf("id=[%s]的道岔是ZDJ9双机牵引,继电器组合类型应为3个,现有%d个", turnout.Id(), size)
}
return nil
}