diff --git a/util/blueprint/blueprint.go b/util/blueprint/blueprint.go index 451473b..5f2bb3f 100644 --- a/util/blueprint/blueprint.go +++ b/util/blueprint/blueprint.go @@ -2,6 +2,7 @@ package blueprint import ( "fmt" + "sync/atomic" ) type Blueprint struct { @@ -9,9 +10,12 @@ type Blueprint struct { graphPool GraphPool blueprintModule IBlueprintModule + mapGraph map[int64]IGraph + seedID int64 + cancelTimer func(*uint64)bool } -func (bm *Blueprint) Init(execDefFilePath string, graphFilePath string, blueprintModule IBlueprintModule) error { +func (bm *Blueprint) Init(execDefFilePath string, graphFilePath string, blueprintModule IBlueprintModule,cancelTimer func(*uint64)bool) error { err := bm.execPool.Load(execDefFilePath) if err != nil { return err @@ -28,10 +32,65 @@ func (bm *Blueprint) Init(execDefFilePath string, graphFilePath string, blueprin return err } + bm.cancelTimer = cancelTimer bm.blueprintModule = blueprintModule + bm.mapGraph = make(map[int64]IGraph,128) return nil } -func (bm *Blueprint) Create(graphName string, graphID int64) IGraph { - return bm.graphPool.Create(graphName, graphID) +func (bm *Blueprint) Create(graphName string) int64 { + if graphName == "" { + return 0 + } + + graphID := atomic.AddInt64(&bm.seedID, 1) + bm.mapGraph[graphID] = bm.graphPool.Create(graphName, graphID) + return graphID } + +func (bm *Blueprint) TriggerEvent(graphID int64, eventID int64, args ...any) error{ + graph := bm.mapGraph[graphID] + if graph == nil { + return fmt.Errorf("can not find graph:%d", graphID) + } + + _,err:= graph.Do(eventID, args...) + return err +} + +func (bm *Blueprint) Do(graphID int64, entranceID int64, args ...any) (Port_Array,error){ + graph := bm.mapGraph[graphID] + if graph == nil { + return nil,fmt.Errorf("can not find graph:%d", graphID) + } + + return graph.Do(entranceID, args...) +} + +func (bm *Blueprint) ReleaseGraph(graphID int64) { + defer delete(bm.mapGraph, graphID) + graph := bm.mapGraph[graphID] + if graph == nil { + return + } + + graph.Release() +} + +func (bm *Blueprint) CancelTimerId(graphID int64, timerId *uint64) bool{ + tId := *timerId + bm.cancelTimer(timerId) + + graph := bm.mapGraph[graphID] + if graph == nil { + return false + } + + gr,ok := graph.(*Graph) + if !ok { + return false + } + + delete(gr.mapTimerID, tId) + return true +} \ No newline at end of file diff --git a/util/blueprint/exec.go b/util/blueprint/exec.go index bae1447..da3de2b 100644 --- a/util/blueprint/exec.go +++ b/util/blueprint/exec.go @@ -8,6 +8,7 @@ type IBaseExecNode interface { GetPorts() ([]IPort, []IPort) getExecNodeInfo() (*ExecContext, *execNode) setExecNodeInfo(gr *ExecContext, en *execNode) + GetBlueprintModule() IBlueprintModule } type IInnerExecNode interface { @@ -528,3 +529,12 @@ func (en *BaseExecNode) getInnerExecNode() IInnerExecNode { func (en *BaseExecNode) setVariableName(name string) bool { return false } + + +func (en *BaseExecNode) GetBlueprintModule() IBlueprintModule{ + if en.gr == nil { + return nil + } + + return en.gr.IBlueprintModule +} diff --git a/util/blueprint/graph.go b/util/blueprint/graph.go index 7329544..d9b4aa7 100644 --- a/util/blueprint/graph.go +++ b/util/blueprint/graph.go @@ -4,17 +4,21 @@ import ( "fmt" "github.com/goccy/go-json" "time" + "github.com/duanhf2012/origin/v2/service" ) +const ReturnVarial = "g_Return" type IGraph interface { - Do(entranceID int64, args ...any) error + Do(entranceID int64, args ...any) (Port_Array,error) Release() } type IBlueprintModule interface { SafeAfterFunc(timerId *uint64, d time.Duration, AdditionData interface{}, cb func(uint64, interface{})) - CancelTimerId(timerId *uint64) bool TriggerEvent(graphID int64, eventID int64, args ...any) error + CancelTimerId(graphID int64,timerId *uint64) bool + GetGameService() service.IService + GetBattleService() service.IService } type baseGraph struct { @@ -26,13 +30,15 @@ type Graph struct { *baseGraph graphContext IBlueprintModule + mapTimerID map[uint64]struct{} } type graphContext struct { context map[string]*ExecContext // 上下文 variables map[string]IPort // 变量 - globalVariables map[string]IPort // 全局变量 + globalVariables map[string]IPort // 全局变量,g_Return,为执行返回值 } + type nodeConfig struct { Id string `json:"id"` Class string `json:"class"` @@ -128,10 +134,10 @@ func (gc *graphConfig) GetNodeByID(nodeID string) *nodeConfig { return nil } -func (gr *Graph) Do(entranceID int64, args ...any) error { +func (gr *Graph) Do(entranceID int64, args ...any) (Port_Array,error) { entranceNode := gr.entrance[entranceID] if entranceNode == nil { - return fmt.Errorf("entranceID:%d not found", entranceID) + return nil,fmt.Errorf("entranceID:%d not found", entranceID) } gr.variables = map[string]IPort{} @@ -139,7 +145,22 @@ func (gr *Graph) Do(entranceID int64, args ...any) error { gr.globalVariables = map[string]IPort{} } - return entranceNode.Do(gr, args...) + err := entranceNode.Do(gr, args...) + if err != nil { + return nil, err + } + + if gr.globalVariables!= nil { + port := gr.globalVariables[ReturnVarial] + if port != nil { + array,ok := port.GetArray() + if ok{ + return array,nil + } + } + } + + return nil,nil } func (gr *Graph) GetNodeInPortValue(nodeID string, inPortIndex int) IPort { @@ -165,6 +186,10 @@ func (gr *Graph) GetNodeOutPortValue(nodeID string, outPortIndex int) IPort { func (gr *Graph) Release() { // 有定时器关闭定时器 + for timerID := range gr.mapTimerID { + gr.CancelTimerId(gr.graphID, &timerID) + } + gr.mapTimerID = nil // 清理掉所有数据 *gr = Graph{} diff --git a/util/blueprint/sysnodes.go b/util/blueprint/sysnodes.go index 15f7244..764fc50 100644 --- a/util/blueprint/sysnodes.go +++ b/util/blueprint/sysnodes.go @@ -649,8 +649,12 @@ func (em *CreateTimer) Exec() (int, error) { if err != nil { log.Warnf("CreateTimer SafeAfterFunc error timerId:%d err:%v", timerId, err) } + + em.gr.IBlueprintModule.CancelTimerId(graphID,&timerId) }) + em.gr.mapTimerID[timerId] = struct{}{} + outPort := em.GetOutPort(1) if outPort == nil { return -1, fmt.Errorf("CreateTimer outParam 1 not found") @@ -676,7 +680,7 @@ func (em *CloseTimer) Exec() (int, error) { } id := uint64(timerID) - ok = em.gr.IBlueprintModule.CancelTimerId(&id) + ok = em.gr.IBlueprintModule.CancelTimerId(em.gr.graphID, &id) if !ok { log.Warnf("CloseTimer CancelTimerId:%d", id) }