using System;
using System.Collections.Generic;
using System.Linq;
using IwbZero.Expr;
using IwbZero.ToolCommon.StringModel;
using WeEngine.Enum;
using WeEngine.Packages;
namespace WeEngine.Functions
{
///
/// 随机选择情景流块
///
public class RandomSelectFlowBlock : RunTimeBase,IFunction
{
public string Invoke(ExprObject exprObj)
{
var child = exprObj.GetChild(0);
GetPackage(child.Expr);
Select();
return "TRUE";
}
private void Select()
{
var list = Rp.CurRoundInfo.Children.Select(a => (SceneFlowBlock)a).Where(a => a.BlockType == SceneFlowBlockType.Evolutionary).ToList();
SceneFlowBlock selectBlock = list.FirstOrDefault();
if (list.Count>1)
{
var randomResult = ExprModule.Evaluate($"IFunRandomSelection({Rp.CurRoundInfo.ControlRate})");
if (randomResult.ValB())
{
selectBlock =
list.FirstOrDefault(a => a.NodeState == NodeState.Running);
}
else
{
var noRunBlock = list.Where(a => a.NodeState == NodeState.New).ToList();
if (noRunBlock.Any())
{
var random = new Random().Next(0, noRunBlock.Count - 1);
selectBlock = noRunBlock[random];
}
}
}
if (selectBlock != null)
{
var sceneCount = Rp.CurRoundInfo.SceneCount;
var flows = new List();
flows.AddRange(selectBlock.Children);
sceneCount = sceneCount > flows.Count ? flows.Count : sceneCount;
while (sceneCount > 0)
{
var id = SelectFlows(ref flows);
var flow = selectBlock.Children.FirstOrDefault(a => a.Id == id);
if (flow != null) flow.NodeState = NodeState.Running;
sceneCount--;
}
}
}
private string SelectFlows(ref List flows)
{
var random = new Random().Next(0, flows.Count - 1);
var flow = flows[random];
flows.Remove(flow);
return flow.Id;
}
}
}