RandomSelectFlowBlock.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using IwbZero.Expr;
  5. using IwbZero.ToolCommon.StringModel;
  6. using WeEngine.Enum;
  7. using WeEngine.Packages;
  8. namespace WeEngine.Functions
  9. {
  10. /// <summary>
  11. /// 随机选择情景流块
  12. /// </summary>
  13. public class RandomSelectFlowBlock : RunTimeBase,IFunction
  14. {
  15. public string Invoke(ExprObject exprObj)
  16. {
  17. var child = exprObj.GetChild(0);
  18. GetPackage(child.Expr);
  19. Select();
  20. return "TRUE";
  21. }
  22. private void Select()
  23. {
  24. var list = Rp.CurRoundInfo.Children.Select(a => (SceneFlowBlock)a).Where(a => a.BlockType == SceneFlowBlockType.Evolutionary).ToList();
  25. SceneFlowBlock selectBlock = list.FirstOrDefault();
  26. if (list.Count>1)
  27. {
  28. var randomResult = ExprModule.Evaluate($"IFunRandomSelection({Rp.CurRoundInfo.ControlRate})");
  29. if (randomResult.ValB())
  30. {
  31. selectBlock =
  32. list.FirstOrDefault(a => a.NodeState == NodeState.Running);
  33. }
  34. else
  35. {
  36. var noRunBlock = list.Where(a => a.NodeState == NodeState.New).ToList();
  37. if (noRunBlock.Any())
  38. {
  39. var random = new Random().Next(0, noRunBlock.Count - 1);
  40. selectBlock = noRunBlock[random];
  41. }
  42. }
  43. }
  44. if (selectBlock != null)
  45. {
  46. var sceneCount = Rp.CurRoundInfo.SceneCount;
  47. var flows = new List<NodeBase>();
  48. flows.AddRange(selectBlock.Children);
  49. sceneCount = sceneCount > flows.Count ? flows.Count : sceneCount;
  50. while (sceneCount > 0)
  51. {
  52. var id = SelectFlows(ref flows);
  53. var flow = selectBlock.Children.FirstOrDefault(a => a.Id == id);
  54. if (flow != null) flow.NodeState = NodeState.Running;
  55. sceneCount--;
  56. }
  57. }
  58. }
  59. private string SelectFlows(ref List<NodeBase> flows)
  60. {
  61. var random = new Random().Next(0, flows.Count - 1);
  62. var flow = flows[random];
  63. flows.Remove(flow);
  64. return flow.Id;
  65. }
  66. }
  67. }