using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Threading;
using IwbZero.Expr;
using IwbZero.ToolCommon.LogHelpers;
using IwbZero.ToolCommon.StringModel;
using WeEngine.CommonDto;
using WeEngine.CommonDto.WeInfo;
using WeEngine.Components;
using WeEngine.Enum;
using WeEngine.Message;
using WeEngine.Packages;
using WeEngine.Strategy.Runner;
namespace WeEngine.Module
{
public class EvolutionRunner : IRunnerStrategy
{
public EvolutionRunner(ComponentRunner componentRunner)
{
ComponentRunner = componentRunner;
}
private ComponentRunner ComponentRunner { get; }
public string Message { get; set; }
public virtual PackageNode PackageInfo { get; set; }
public NodeBase CurrentNode { get; set; }
private NodeBase GetNodeByPath(string path)
{
var node = PackageInfo.GetNodeByPath(path);
return node;
}
#region 运行
///
/// 运行方案包
///
///
///
///
public virtual bool Run(NodeBase node, OperationType operationType)
{
CurrentNode = node;
return Run(operationType);
}
///
/// 运行
///
/// 匹配关键字时('path|word|role|scenePath')
///
///
public virtual bool Run(string path, OperationType operationType)
{
if (operationType == OperationType.MatchKey)
{
string[] arr = path.Split('|');
if (arr.Length == 4)
{
CurrentNode = GetNodeByPath(arr[0]);
dynamic obj = new ExpandoObject();
obj.BehaviorWord = arr[1];
obj.BehaviorRole = arr[2];
obj.ScenePath = arr[3];
CurrentNode.ExtendData = obj;
return Run(OperationType.MatchKey);
}
return false;
}
CurrentNode = GetNodeByPath(path);
return Run(operationType);
}
///
/// 运行
///
///
///
private bool Run(OperationType operationType)
{
if (CurrentNode != null)
{
return RunNode(CurrentNode, operationType);
}
return false;
}
///
/// 运行节点
///
///
///
///
///
public bool RunNode(PackageNode curPackageInfo, string id, OperationType operationType)
{
if (operationType == OperationType.MatchKey)
{
string[] arr = id.Split('|');
if (arr.Length == 3)
{
var curNode = curPackageInfo.GetNodeByPath(arr[0]);
dynamic obj = new ExpandoObject();
obj.BehaviorWord = arr[1];
obj.BehaviorRole = arr[2];
curNode.ExtendData = obj;
return RunNode(curNode, OperationType.MatchKey);
}
return false;
}
{
var curNode = curPackageInfo.GetNodeByPath(id);
return RunNode(curNode, operationType);
}
}
///
/// 运行节点
///
///
///
///
private bool RunNode(NodeBase node, OperationType operationType)
{
var flag = true;
switch (operationType)
{
//case OperationType.PreEvent:
// flag = RunnerPre(node);
// break;
//case OperationType.RunningEvent:
// flag = RunnerRunning(node);
// break;
//case OperationType.RunnedEvent:
// flag = RunnerRunned(node);
// break;
//case OperationType.PostEvent:
// flag = RunnerPost(node);
// break;
//case OperationType.SelectNextNode:
// SelectNext2Run(node);
// break;
case OperationType.MatchKey:
flag = RunnerMatchKey(node);
break;
case OperationType.MatchSuccess:
flag = RunnerMatchSuccess(node);
SendMsg(node, MessageType.RunningInfo, $"培训营[{node.CurPackageInfo.RunningId}]评分已变化");
break;
//case OperationType.Start:
//case OperationType.Stop:
//case OperationType.NextRound:
//case OperationType.NextFlowNodes:
//case OperationType.NextFlowNode:
default:
switch (operationType)
{
case OperationType.Start:
flag = Start(node);
SendNodeChange(node);
break;
case OperationType.Stop:
flag = Stop(node);
SendNodeChange(node);
break;
case OperationType.NextRound:
flag = NextRound(node);
SendNodeChange(node);
break;
case OperationType.NextFlowNodes:
flag = NextFlowNodes(node);
SendNodeChange(node);
break;
case OperationType.NextFlowNode:
flag = NextFlowNode(node);
SendNodeChange(node);
break;
}
//SendMsg(node, MessageType.RunningInfo, $"培训营[{node.CurPackageInfo.RunningId}]评分已变化");
//SendMsg(node, MessageType.RunningInfo, $"培训营[{node.CurPackageInfo.RunningId}]情景数据结构已变化@{node.NodePath}");
break;
}
return flag;
}
private void SendNodeChange(NodeBase node)
{
SendMsg(node, MessageType.RunningInfo, $"培训营[{node.CurPackageInfo.RunningId}]评分已变化");
SendMsg(node, MessageType.RunningInfo, $"培训营[{node.CurPackageInfo.RunningId}]情景数据结构已变化@{node.NodePath}");
}
#endregion 运行
///
/// 节点状态变更事件
///
///
///
public void NodeChangeEventHandle(NodeBase node, NodeState oldState)
{
if (node.NodeState == NodeState.New)
{
return;
}
if (node.NodeState == NodeState.ReStart)
{
//重启后自动变更为运行状态
node.NodeState = NodeState.Running;
return;
}
switch (oldState)
{
case NodeState.New:
node.PreConditionValidated = true;
RunnerPre(node);
if (!node.NotNeedValidated && !node.PreConditionValidated)
{
node.NodeState = NodeState.New;
if (node.NodeType == NodeType.FlowNode || node.NodeType == NodeType.SceneFlowBlock)
{
SelectNext2Run(node);
}
return;
}
if (node.IsWait)
{
node.NodeState = NodeState.Wait;
return;
}
node.NotNeedValidated = false;
if (node.NodeState == NodeState.Running)
{
if (node.NodeType != NodeType.KeyWord)
{
typeof(NodeBase).LogDebug($"节点 - [{node.NodeType}] - [{node.CurPackageInfo.RunningId}] - [{node.NodePath}] -【已启动】");
}
RunnerRunning(node);
}
break;
case NodeState.Wait:
{
if (node.NodeState == NodeState.Running)
{
typeof(NodeBase).LogDebug($"节点 - [{node.NodeType}] - [{node.CurPackageInfo.RunningId}] - [{node.NodePath}] -【已启动】");
RunnerRunning(node);
}
break;
}
case NodeState.Running:
if (node.NodeState == NodeState.RunEnd)
{
node.RunnedConditionValidated = true;
RunnerRunned(node);
if (!node.RunnedConditionValidated)
{
node.NodeState = oldState;
return;
}
if (node.NodeType != NodeType.KeyWord)
{
typeof(NodeBase).LogDebug($"节点 - [{node.NodeType}] - [{node.CurPackageInfo.RunningId}] - [{node.NodePath}] -【已结束】");
}
}
else if (node.NodeState == NodeState.Complete)
{
if (node.NodeType != NodeType.KeyWord)
{
typeof(NodeBase).LogDebug($"节点 - [{node.NodeType}] - [{node.CurPackageInfo.RunningId}] - [{node.NodePath}] -【【已完成】】");
}
RunnerPost(node);
}
break;
case NodeState.RunEnd:
if (node.NodeState == NodeState.Complete)
{
RunnerPost(node);
typeof(NodeBase).LogDebug($"节点- [{node.NodeType}] - [{node.CurPackageInfo.RunningId}] - [{node.NodePath}] - 【【 已完成 】】");
}
break;
case NodeState.ReStart:
break;
}
}
///
/// 开始
///
///
///
private bool Start(NodeBase node)
{
if (node.NodeState == NodeState.Running)
{
return false;
}
node.NodeState = NodeState.Running;
return true;
}
///
/// 结束
///
///
///
private bool Stop(NodeBase node)
{
if (node.NodeState == NodeState.Running)
{
node.NodeState = NodeState.RunEnd;
}
return true;
}
///
/// 前置验证
///
///
///
private void RunnerPre(NodeBase node)
{
node.SetPreVariables();
if (node.EventComponentNode != null && node.EventComponentNode.PreComponent.IsNotEmpty() &&
!node.EventComponentNode.PreComponent.EndsWith("|"))
{
var vars = node.GetVariables();
var arr = node.EventComponentNode.PreComponent.StrToArray("|");
string componentId = arr[0], mapping = "";
if (arr.Length >= 2)
{
mapping = arr[1];
}
ComponentRunner.Variables.Clear();
ComponentRunner.Variables = ComponentRunner.Variables.MergeHashtable(vars);
ComponentRunner.CurrentNode = node;
ComponentRunner.Run(componentId, mapping);
node.PreConditionValidated = ComponentRunner.GetValue(DefaultVariable.RunValidate.UAndT()).ValB();
typeof(EvolutionRunner).LogDebug($"---[运行前事务:{node.PreConditionValidated}]--->[GroupNo:{node.CurPackageInfo.RunningId},Path:{node.NodePath},Component:{node.EventComponentNode.PreComponent},\r\n" +
$" ============> RoundScore:{ ComponentRunner.GetValue(DefaultVariable.RoundScore)}--{ComponentRunner.GetValue(DefaultVariable.FullRoundScore)}," +
$" FlowScore:{ ComponentRunner.GetValue(DefaultVariable.FlowScore)}--{ComponentRunner.GetValue(DefaultVariable.FullFlowScore)}]");
}
//if (node.PreConditionValidated)
//{
// SelectChildRun(node);
//}
//else if (node.NodeType == NodeType.FlowNode|| node.NodeType == NodeType.SceneFlowBlock)
//{
// SelectNext2Run(node);
//}
}
///
/// 通知下一节点运行
///
///
private void SelectNext2Run(NodeBase node)
{
if (node.HasParent && (node.NodeType == NodeType.SceneFlowBlock || node.NodeType == NodeType.FlowNode))
{
var next = node.PreConditionValidated;
if (node.NodeType == NodeType.SceneFlowBlock)
{
var b = (SceneFlowBlockNode)node;
if (b.BlockType == SceneFlowBlockType.Objective)
{
next = false;
}
}
if (next)
{
return;
}
var nodes = node.Parent.Children;
var index = nodes.FindIndex(a => a.Id == node.Id && a.InternalNo == node.InternalNo);
index++;
if (index > 0 && index < nodes.Count)
{
var nextNode = nodes[index];
if (nextNode != null)
{
typeof(EvolutionRunner).LogDebug($"---[准备启动下一节点]--->[GroupNo:{node.CurPackageInfo.RunningId},本节点Path:{node.NodePath}]");
nextNode.NodeState = NodeState.Running;
}
}
else if (node.NodeType == NodeType.FlowNode)
{
var flowNode = (FlowNode)node;
if (flowNode.SelfFlow.NodeState == NodeState.Running)
{
if (node.CurPackageInfo?.WaitForRunNode?.Values.Any(a => a.Any(s => s.StartsWith(flowNode.SelfFlow.NodePath))) ?? false)
{
typeof(EvolutionRunner).LogDebug($"---[没有节点运行,情景流中等待运行的节点]--->[GroupNo:{node.CurPackageInfo?.RunningId},Path:{node.NodePath}]");
return;
}
typeof(EvolutionRunner).LogDebug($"---[没有节点运行,结束情景流]--->[GroupNo:{node.CurPackageInfo?.RunningId},Path:{node.NodePath}]");
flowNode.SelfFlow.NodeState = NodeState.RunEnd;
}
}
else if (node.NodeType == NodeType.SceneFlowBlock)
{
var b = (SceneFlowBlockNode)node;
if (b.BlockType != SceneFlowBlockType.Objective)
{
node.NodeState = NodeState.New;
typeof(EvolutionRunner).LogDebug($"---[随机选取演化情景块]--->[GroupNo:{node.CurPackageInfo.RunningId},Path:{node.NodePath}]");
SelectRandomFlowBlock(node);
}
}
}
//if ( node.NodeType == NodeType.SceneFlowBlock)
//{
// var blockNode = ((SceneFlowBlockNode)node);
// if (((SceneRoundNode)blockNode.Parent).EvolutionBlockHasSelected && blockNode.BlockType != SceneFlowBlockType.Objective)
// {
// return;
// }
// if (node.PreConditionValidated && blockNode.BlockType != SceneFlowBlockType.Objective)
// {
// SelectRandomFlowBlock(node);
// return;
// }
//} else if (node.PreConditionValidated)
//{
// return;
//}
}
///
/// 运行时事件
///
///
///
private void RunnerRunning(NodeBase node)
{
SendRunInfo(node);
SelectChildRun(node);
if (node.EventComponentNode != null && node.EventComponentNode.RunningComponent.IsNotEmpty() && !node.EventComponentNode.RunningComponent.EndsWith("|"))
{
typeof(EvolutionRunner).LogDebug($"---[运行时事务]---> [GroupNo:{node.CurPackageInfo.RunningId},Path:{node.NodePath},Component:{node.EventComponentNode.RunningComponent}]");
var vars = node.GetVariables();
var arr = node.EventComponentNode.RunningComponent.StrToArray("|");
string componentId = arr[0], mapping = "";
if (arr.Length == 2)
{
mapping = arr[1];
}
ComponentRunner.Variables.Clear();
ComponentRunner.Variables = ComponentRunner.Variables.MergeHashtable(vars);
ComponentRunner.CurrentNode = node;
ComponentRunner.Run(componentId, mapping);
}
}
private void SendRunInfo(NodeBase node)
{
if (node.NodeType == NodeType.ScenePackage)
{
SendMsg(node, MessageType.Start, $"培训营{node.CurPackageInfo.RunningId}开始演练");
}
if (node.NodeType == NodeType.SceneInfo)
{
var scene = (SceneNode)node;
//var pushSceneInfo = new PushSceneInfo(scene);
SendMsg(node, MessageType.SceneInfo, new { No = scene.Id, Path = scene.NodePath }.Obj2String());
if (scene.EnvironResources != null && scene.EnvironResources.Any())
{
Thread.Sleep(2000);
foreach (var render in scene.EnvironResources)
{
render.NodePath = scene.NodePath;
SendMsg(node, MessageType.Environment, render.Obj2StringCamelCase());
}
}
}
else if (node.NodeType == NodeType.Behavior)
{
var behaviorNode = (BehaviorNode)node;
if (behaviorNode.EnvironResources != null && behaviorNode.EnvironResources.Any())
{
foreach (var render in behaviorNode.EnvironResources)
{
render.NodePath = behaviorNode.NodePath;
SendMsg(node, MessageType.Environment, render.Obj2StringCamelCase());
}
}
}
var guideInfos = node.GuideInfos?.Where(a => a.IsPush).ToList();
if (guideInfos != null && guideInfos.Any())
{
foreach (var guideInfo in guideInfos)
{
SendMsg(node, MessageType.GuideInfo, new GuidePushDto() { RunningId = node.CurPackageInfo.RunningId, GuideNo = guideInfo.Id, GuideName = guideInfo.Name, GuideDesc = guideInfo.Description, No = node.Id, Path = node.NodePath }.Obj2StringCamelCase());
}
}
}
///
/// 通知子节点运行
///
///
private void SelectChildRun(NodeBase node)
{
switch (node.NodeType)
{
case NodeType.ScenePackage:
{
var n = ((PackageNode)node);
var roundIndex = n.CurrentRoundIndex + 1;
var roundInfo = n.Children.Select(a => (SceneRoundNode)a).FirstOrDefault(a => a.RoundIndex == roundIndex);
if (roundInfo != null)
{
roundInfo.NodeState = NodeState.Running;
}
else
{
Stop(n);
}
break;
}
case NodeType.SceneRound:
{
//SelectChild2Run((SceneRoundNode)node);
var roundNode = (SceneRoundNode)node;
if (!roundNode.HasChild)
{
return;
}
var child = roundNode.Children.FirstOrDefault();
if (child != null)
{
child.NodeState = NodeState.Running;
}
break;
}
case NodeType.SceneFlowBlock:
{
var n = ((SceneFlowBlockNode)node);
if (n.BlockType == SceneFlowBlockType.Objective)
{
//SelectAllChildren2Run(node);
if (!n.HasChild)
{
return;
}
foreach (var child in n.Children)
{
child.NodeState = NodeState.Running;
}
SelectNext2Run(node);
}
else if (n.BlockType == SceneFlowBlockType.Evolutionary && !((SceneRoundNode)n.Parent).EvolutionBlockHasSelected)
{
SelectRandomFlowBlock(node);
}
//else if (n.BlockType == SceneFlowBlockType.Evolutionary &&
// !((SceneRoundNode)n.Parent).EvolutionBlockHasSelected)
//{
// ((SceneRoundNode)n.Parent).EvolutionBlockHasSelected = true;
//}
//else
//{
// n.PreConditionValidated = false;
// SelectRandomFlowBlock(node);
//}
break;
}
case NodeType.SceneFlow:
{
var n = ((SceneFlowNode)node);
var runChild = n.RunningNode;
//SelectChild2Run(runChild);
if (runChild == null)
{
runChild = (FlowNode)n.Children.FirstOrDefault();
if (runChild != null) runChild.NodeState = NodeState.Running;
}
else if (runChild.HasChild)
{
var child = runChild.Children.FirstOrDefault();
if (child != null)
{
child.NodeState = NodeState.Running;
}
}
break;
}
case NodeType.FlowNode:
{
var n = ((FlowNode)node);
n.SelfFlow.RunningNode = n;
foreach (var sceneInfo in n.SceneInfos)
{
sceneInfo.NodeState = NodeState.Running;
}
break;
}
case NodeType.SceneInfo:
{
var n = ((SceneNode)node);
if (!n.HasChild)
{
return;
}
foreach (var child in n.Children)
{
child.NodeState = NodeState.Running;
}
break;
}
case NodeType.Behavior:
{
var n = ((BehaviorNode)node);
if (!n.HasChild)
{
return;
}
foreach (var child in n.Children)
{
child.NodeState = NodeState.Running;
}
break;
}
case NodeType.KeyWord:
{
var n = ((KeywordNode)node);
if (!n.HasChild)
{
return;
}
foreach (var child in n.Children)
{
child.NodeState = NodeState.Running;
}
break;
}
}
}
///
/// 选取演化情景流块(带权重)
///
///
private void SelectRandomFlowBlock(NodeBase node)
{
var curRoundInfo = node.CurPackageInfo.CurRoundInfo;
var list = curRoundInfo.Children.Select(a => (SceneFlowBlockNode)a).Where(a => a.BlockType == SceneFlowBlockType.Evolutionary).ToList();
if (curRoundInfo.EvolutionBlockHasSelected)
{
return;
}
curRoundInfo.EvolutionBlockHasSelected = true;
SceneFlowBlockNode selectBlock = null;
if (list.Count > 0)
{
if (node.NodeState == NodeState.Running)
{
//生成随机值,判断是否在权重范围内
//var randomResult = "1";
var randomResult = EvalExpr.Evaluate($"IFunRandomSelection({curRoundInfo.ControlRate})");
if (randomResult.ValB())
{
selectBlock = (SceneFlowBlockNode)node;
}
}
if (selectBlock == null)
{
typeof(EvolutionRunner).LogDebug($"---[选取其他演化情景块]---> [GroupNo:{node.CurPackageInfo.RunningId},Path:{node.NodePath}]");
var noRunBlock = list.Where(a => a.NodeState == NodeState.New).ToList();
if (noRunBlock.Any())
{
node.NodeState = NodeState.New;
var random = new Random().Next(0, noRunBlock.Count - 1);
selectBlock = noRunBlock[random];
}
else
{
selectBlock = (SceneFlowBlockNode)node;
}
}
}
//选取情景流
if (selectBlock != null)
{
typeof(EvolutionRunner).LogDebug($"================> [NewPath:{selectBlock.NodePath}]");
selectBlock.NotNeedValidated = true;
selectBlock.NodeState = NodeState.Running;
var sceneCount = curRoundInfo.SceneCount;
var flows = new List();
flows.AddRange(selectBlock.Children);
sceneCount = sceneCount > flows.Count ? flows.Count : sceneCount;
while (sceneCount > 0)
{
var id = SelectRandomFlows(ref flows);
var flow = selectBlock.Children.FirstOrDefault(a => a.Id == id);
if (flow != null)
{
typeof(EvolutionRunner).LogDebug($"---[选取演化情景流]---> [Path:{flow.NodePath}]");
flow.NodeState = NodeState.Running;
}
sceneCount--;
}
}
}
///
/// 演化情景流块选取情景流
///
///
///
private string SelectRandomFlows(ref List flows)
{
var random = new Random().Next(0, flows.Count - 1);
var flow = flows[random];
flows.Remove(flow);
return flow.Id;
}
///
/// 运行结束时事件
///
///
///
private void RunnerRunned(NodeBase node)
{
node.SetRunnedVariables();
if (node.EventComponentNode != null && node.EventComponentNode.RunnedComponent.IsNotEmpty() && !node.EventComponentNode.RunnedComponent.EndsWith("|"))
{
var vars = node.GetVariables();
var arr = node.EventComponentNode.RunnedComponent.StrToArray("|");
string componentId = arr[0], mapping = "";
if (arr.Length == 2)
{
mapping = arr[1];
}
ComponentRunner.Variables.Clear();
ComponentRunner.Variables = ComponentRunner.Variables.MergeHashtable(vars);
ComponentRunner.CurrentNode = node;
ComponentRunner.Run(componentId, mapping);
node.RunnedConditionValidated = ComponentRunner.GetValue(DefaultVariable.RunValidate.UAndT()).ValB();
typeof(EvolutionRunner).LogDebug($"---[运行结束事务{node.RunnedConditionValidated}]--->[GroupNo:{node.CurPackageInfo.RunningId},Path:{node.NodePath},Component:{node.EventComponentNode.RunnedComponent},\r\n" +
$" ============> RoundScore:{ ComponentRunner.GetValue(DefaultVariable.RoundScore)}--{ComponentRunner.GetValue(DefaultVariable.FullRoundScore)}," +
$" FlowScore:{ ComponentRunner.GetValue(DefaultVariable.FlowScore)}--{ComponentRunner.GetValue(DefaultVariable.FullFlowScore)}]");
}
if (node.RunnedConditionValidated)
{
SelectPrevEnd(node);
node.NodeState = NodeState.Complete;
}
}
///
/// 通知父节点结束
///
///
private void SelectPrevEnd(NodeBase node)
{
if (node.CurPackageInfo.NodeState != NodeState.Running)
{
return;
}
switch (node.NodeType)
{
case NodeType.ScenePackage:
{
var n = ((PackageNode)node);
if (n.AutoNextRound)
{
}
break;
}
case NodeType.SceneRound:
{
var n = ((SceneRoundNode)node);
if (n.HasParent && !n.Parent.ChildIsRunning)
{
var nextRound = n.Parent.Children.Select(a => (SceneRoundNode)a)
.FirstOrDefault(a => a.RoundIndex == n.RoundIndex + 1);
if (nextRound == null)
{
n.Parent.NodeState = NodeState.RunEnd;
}
}
break;
}
case NodeType.SceneFlowBlock:
{
SelectParent2Stop(node);
break;
}
case NodeType.SceneFlow:
SelectParent2Stop(node);
break;
case NodeType.FlowNode:
var flowNode = (FlowNode)node;
if (flowNode.SceneInfos.Any())
{
foreach (var s in flowNode.SceneInfos)
{
s.NodeState = NodeState.RunEnd;
}
}
if (!flowNode.HasChild && flowNode.SelfFlow != null)
{
flowNode.SelfFlow.NodeState = NodeState.RunEnd;
}
break;
case NodeType.SceneInfo:
var sceneNode = (SceneNode)node;
if (sceneNode.HasChild)
{
foreach (var child in sceneNode.Children)
{
child.NodeState = NodeState.RunEnd;
}
}
break;
case NodeType.Behavior:
break;
default:
SelectParent2Stop(node);
break;
}
}
///
/// 通知父节点结束
///
///
private void SelectParent2Stop(NodeBase node)
{
if (node.HasParent && !node.Parent.ChildIsRunning)
{
node.Parent.NodeState = NodeState.RunEnd;
}
}
///
/// 后置事件
///
///
///
private void RunnerPost(NodeBase node)
{
if (node.EventComponentNode != null && node.EventComponentNode.PostComponent.IsNotEmpty() && !node.EventComponentNode.PostComponent.EndsWith("|"))
{
typeof(EvolutionRunner).LogDebug($"---[运行完成后事务]--->[GroupNo:{node.CurPackageInfo.RunningId},Path:{node.NodePath},Component:{node.EventComponentNode.PostComponent}]");
var vars = node.GetVariables();
var arr = node.EventComponentNode.PostComponent.StrToArray("|");
string componentId = arr[0], mapping = "";
if (arr.Length == 2)
{
mapping = arr[1];
}
ComponentRunner.Variables.Clear();
ComponentRunner.Variables = ComponentRunner.Variables.MergeHashtable(vars);
ComponentRunner.CurrentNode = node;
ComponentRunner.Run(componentId, mapping);
node.PostConditionValidated = ComponentRunner.GetValue(DefaultVariable.RunValidate.UAndT()).ValB();
}
CallNodesToRun(node);
NextNode(node);
}
///
/// 通知待运行节点
///
///
private void CallNodesToRun(NodeBase node)
{
var waitNodes = node.CurPackageInfo.WaitForRunNode;
if (waitNodes.Count > 0 && waitNodes.ContainsKey(node.NodePath))
{
typeof(EvolutionRunner).LogDebug($"---[等待通知运行的节点]--->\r\n" +
$"======>[{string.Join(",", waitNodes.Keys.ToArray())}]");
var toRunIds = waitNodes[node.NodePath];
if (toRunIds != null && toRunIds.Any())
{
typeof(EvolutionRunner).LogDebug($"---[准备通知节点运行]--->[GroupNo:{node.CurPackageInfo.RunningId},Path:{node.NodePath}\r\n=========>ToRunPaths:{string.Join(",", toRunIds.ToArray())}]");
var temp = new List();
temp.AddRange(toRunIds);
foreach (var nodeId in temp)
{
var runNode = node.CurPackageInfo.GetNodeByPath(nodeId);
if (runNode.NodeState == NodeState.Wait && runNode.WaitPath.Any(a => a == node.NodePath))
{
typeof(EvolutionRunner).LogDebug($"---[移除待等待节点]--->[Path:{runNode.NodePath}] [Remove Path:{node.NodePath}]");
runNode.WaitPath.Remove(node.NodePath);
}
if (!runNode.IsWait && (runNode.NodeType != NodeType.FlowNode ||
runNode.Parent.Children.All(a =>
a.NodeState == NodeState.New || a.NodeState == NodeState.Wait)))
{
typeof(EvolutionRunner).LogDebug($"---[通知节点运行]--->[Path:{runNode.NodePath}]");
runNode.NodeState = NodeState.Running;
}
}
}
}
}
///
/// 关键字匹配
///
///
///
private bool RunnerMatchKey(NodeBase node)
{
typeof(EvolutionRunner).LogDebug($"---[关键字开始匹配]---> [ groupNo:{node.CurPackageInfo.RunningId}]");
node.CurPackageInfo.EvalManager?.Instance?.MatchKeyWord(node);
return true;
}
///
/// 关键字匹配成功
///
///
///
private bool RunnerMatchSuccess(NodeBase node)
{
if (node.NodeType != NodeType.KeyWord)
{
return false;
}
var n = (KeywordNode)node;
string word = node.ExtendData?.MatchWord ?? "";
n.HasMatched = true;
n.MatchWord = word;
var roundScore = new RoundScoreInfo
{
No = n.CurPackageInfo.RunningId,
RoundIndex = n.CurPackageInfo.CurrentRoundIndex,
RoundScore = n.CurPackageInfo.CurRoundInfo.ActualScore,
TotalScore = n.CurPackageInfo.CurRoundInfo.RoundFullScore,
KeyWords = word,
TagScores = new List()
};
var behavior = (BehaviorNode)node.Parent;
if (behavior.BehaviorScoreType == BehaviorScoreType.Normal && behavior.BehaviorTagArray.Length > 0)
{
foreach (var tag in behavior.BehaviorTagArray)
{
decimal totalScore = 0;
if (n.CurPackageInfo.BehaviorTagFullScoreDic.ContainsKey(tag))
{
totalScore = n.CurPackageInfo.BehaviorTagFullScoreDic[tag];
}
roundScore.TagScores.Add(new TagScoreInfo()
{
TagNo = tag,
SystemScore = n.ActualScore,
CorrectionScore = 0,
TotalScore = totalScore
});
}
}
var msg = roundScore.Obj2String();
typeof(EvolutionRunner).LogDebug($"---[关键字匹配成功]--->[ groupNo:{node.CurPackageInfo.RunningId} ]\r\n" +
$" ===============> MatchWord:{word}\r\n" +
$" ===============> Word: {n.Word}\r\n" +
$" ===============> Tag:[{msg}]");
SendMsg(node, MessageType.RoundScore, msg);
SendMsg(node, MessageType.CommandMatchSuccess, new WeCmdMatchSuccessDto(behavior.Id, behavior.NodePath, $"{word}|{n.Word}", behavior.BehaviorScoreType).Obj2StringCamelCase());
return true;
}
///
/// 下一节点
///
///
private void NextNode(NodeBase node)
{
switch (node.NodeType)
{
case NodeType.ScenePackage:
{
SendMsg(node, MessageType.End, $"训练营[{node.CurPackageInfo.RunningId}]结束");
break;
}
case NodeType.SceneRound:
{
typeof(EvolutionRunner).LogDebug($"---[下一轮次]--->[GroupNo:{node.CurPackageInfo.RunningId},本节点Path:{node.NodePath}]");
if (node.CurPackageInfo.AutoNextRound)
{
SelectChildRun(node.CurPackageInfo);
}
break;
}
case NodeType.FlowNode:
{
var n = ((FlowNode)node);
if (n.HasChild && n.Children.Count > 0)
{
typeof(EvolutionRunner).LogDebug($"---[下一流节点]--->[GroupNo:{n.CurPackageInfo.RunningId},本节点Path:{n.NodePath}]");
SelectChildRun(n.SelfFlow);
}
}
break;
}
}
///
/// 下一轮次
///
///
///
private bool NextRound(NodeBase node)
{
if (node.CurPackageInfo.CurRoundInfo.NodeState == NodeState.Complete)
{
SelectChildRun(node.CurPackageInfo);
}
else
{
NextFlowNodes(node);
NextRound(node);
}
//var child = node.CurPackageInfo.CurRoundInfo;
//if (child.NodeState == NodeState.Running)
//{
// child.NodeState = NodeState.RunEnd;
// //child.NodeState = NodeState.Complete;
// return true;
//}
//if (child.NodeState == NodeState.RunEnd)
//{
// child.NodeState = NodeState.Complete;
// return true;
//}
return true;
}
///
/// 下一情景流
///
///
///
public bool NextFlowNode(NodeBase node)
{
FlowNode flowNode = null;
if (node.NodeType == NodeType.SceneFlow)
{
flowNode = ((SceneFlowNode)node).RunningNode;
//flowNode.NodeState = NodeState.Running;
}
else if (node.NodeType == NodeType.FlowNode)
{
flowNode = (FlowNode)node;
}
if (flowNode != null && flowNode.NodeState != NodeState.New)
{
if (flowNode.NodeState == NodeState.Running)
{
typeof(EvolutionRunner).LogDebug($"---[节点准备结束]--->[GroupNo:{flowNode.CurPackageInfo.RunningId},Path:{flowNode.NodePath}]");
flowNode.NodeState = NodeState.RunEnd;
//flowNode.NodeState = NodeState.Complete;
}
else if (flowNode.NodeState == NodeState.RunEnd)
{
typeof(EvolutionRunner).LogDebug($"---[节点准备完成]--->[GroupNo:{flowNode.CurPackageInfo.RunningId},Path:{flowNode.NodePath}]");
flowNode.NodeState = NodeState.Complete;
}
else
{
return false;
}
return true;
}
return false;
}
///
/// 下一情景流(当前运行的所有情景流)
///
///
///
private bool NextFlowNodes(NodeBase node)
{
if (node.CurPackageInfo != null)
{
if (node.CurPackageInfo.CurFlowNodes.Any())
{
EndNode(node.CurPackageInfo.CurFlowNodes.Where(a => a != null).Select(a => (NodeBase)a).ToList(), "流节点");
//foreach (var child in node.CurPackageInfo.CurFlowNodes)
//{
// if (child.NodeState == NodeState.New)
// {
// continue;
// }
// if (child.NodeState == NodeState.Running)
// {
// typeof(EvolutionRunner).LogDebug($"---[流节点准备结束]--->[GroupNo:{child.CurPackageInfo.RunningId},Path:{child.NodePath}]");
// child.NodeState = NodeState.RunEnd;
// //child.NodeState = NodeState.Complete;
// }
// else if (child.NodeState == NodeState.RunEnd)
// {
// typeof(EvolutionRunner).LogDebug($"---[流节点准备完成]--->[GroupNo:{child.CurPackageInfo.RunningId},Path:{child.NodePath}]");
// child.NodeState = NodeState.Complete;
// }
//}
return true;
}
if (node.CurPackageInfo.CurFlowInfos.Any())
{
EndNode(node.CurPackageInfo.CurFlowInfos.Select(a => (NodeBase)a).ToList(), "情景流准备结束");
return true;
}
if (node.CurPackageInfo.CurBlockInfos.Any())
{
EndNode(node.CurPackageInfo.CurBlockInfos.Select(a => (NodeBase)a).ToList(), "情景快准备结束");
return true;
}
var round = node.CurPackageInfo.CurRoundInfo;
if (round.NodeState == NodeState.Running)
{
typeof(EvolutionRunner).LogDebug($"---[轮次准备结束]--->[GroupNo:{round.CurPackageInfo.RunningId},Path:{round.NodePath}]");
round.NodeState = NodeState.RunEnd;
//child.NodeState = NodeState.Complete;
}
else if (round.NodeState == NodeState.RunEnd)
{
typeof(EvolutionRunner).LogDebug($"---[轮次准备完成]--->[GroupNo:{round.CurPackageInfo.RunningId},Path:{round.NodePath}]");
round.NodeState = NodeState.Complete;
}
if (node.CurPackageInfo.AutoNextRound)
{
NextRound(node.CurPackageInfo.CurRoundInfo);
}
return true;
}
return false;
}
///
/// 结束节点
///
///
///
private void EndNode(List nodes, string str)
{
foreach (var child in nodes)
{
if (child == null)
{
continue;
}
if (child.NodeState == NodeState.New)
{
continue;
}
if (child.NodeState == NodeState.Running)
{
typeof(EvolutionRunner).LogDebug($"---[{str}准备结束]--->[GroupNo:{child.CurPackageInfo.RunningId},Path:{child.NodePath}]");
child.NodeState = NodeState.RunEnd;
//child.NodeState = NodeState.Complete;
}
else if (child.NodeState == NodeState.RunEnd)
{
typeof(EvolutionRunner).LogDebug($"---[{str}准备完成]--->[GroupNo:{child.CurPackageInfo.RunningId},Path:{child.NodePath}]");
child.NodeState = NodeState.Complete;
}
}
}
///
/// 发送消息
///
///
///
///
///
private void SendMsg(NodeBase node, MessageType type, string msg)
{
var weMsg = new WeMessage(type, node.CurPackageInfo.RunningId, node.CurPackageInfo.TargetClientId, msg);
node.CurPackageInfo.MessageSender?.SendMsg(weMsg);
}
}
}