diff --git a/src/Sanderling.ABot/Bot/Bot.cs b/src/Sanderling.ABot/Bot/Bot.cs index dede913..0bcd6a1 100644 --- a/src/Sanderling.ABot/Bot/Bot.cs +++ b/src/Sanderling.ABot/Bot/Bot.cs @@ -46,77 +46,86 @@ public class Bot public Int64? ToggleLastAgeStepCountFromModule(Accumulation.IShipUiModule module) => stepIndex - ToggleLastStepIndexFromModule?.TryGetValueNullable(module); + IEnumerable StepOutputListTaskPath() => + ((IBotTask)new BotTask { Component = RootTaskListComponent() }) + ?.EnumeratePathToNodeFromTreeDFirst(node => node?.Component) + ?.Where(taskPath => (taskPath?.LastOrDefault()).ShouldBeIncludedInStepOutput()) + ?.Take(1); + + void MemorizeStepInput(BotStepInput input) + { + ConfigSerialAndStruct = input?.ConfigSerial?.String?.DeserializeIfDifferent(ConfigSerialAndStruct) ?? ConfigSerialAndStruct; + + MemoryMeasurementAtTime = input?.FromProcessMemoryMeasurement?.MapValue(measurement => measurement?.Parse()); + + MemoryMeasurementAccu.Accumulate(MemoryMeasurementAtTime); + + OverviewMemory.Aggregate(MemoryMeasurementAtTime); + } + + void MemorizeStepResult(BotStepResult stepResult) + { + var setMotionMouseWaypointUIElement = + stepResult?.ListMotion + ?.Select(motion => motion?.MotionParam) + ?.Where(motionParam => 0 < motionParam?.MouseButton?.Count()) + ?.Select(motionParam => motionParam?.MouseListWaypoint) + ?.ConcatNullable()?.Select(mouseWaypoint => mouseWaypoint?.UIElement)?.WhereNotDefault(); + + foreach (var mouseWaypointUIElement in setMotionMouseWaypointUIElement.EmptyIfNull()) + MouseClickLastStepIndexFromUIElementId[mouseWaypointUIElement.Id] = stepIndex; + } + public BotStepResult Step(BotStepInput input) { StepLastInput = input; Exception exception = null; - var listTaskPath = new List(); + var listMotion = new List(); try { - ConfigSerialAndStruct = input?.ConfigSerial?.String?.DeserializeIfDifferent(ConfigSerialAndStruct) ?? ConfigSerialAndStruct; - - MemoryMeasurementAtTime = input?.FromProcessMemoryMeasurement?.MapValue(measurement => measurement?.Parse()); + MemorizeStepInput(input); - MemoryMeasurementAccu.Accumulate(MemoryMeasurementAtTime); + var outputListTaskPath = StepOutputListTaskPath()?.ToArray(); - OverviewMemory.Aggregate(MemoryMeasurementAtTime); + foreach (var moduleToggle in outputListTaskPath.ConcatNullable().OfType().Select(moduleToggleTask => moduleToggleTask?.module).WhereNotDefault()) + ToggleLastStepIndexFromModule[moduleToggle] = stepIndex; - var sequenceTaskPath = - ((IBotTask)new BotTask { Component = SequenceRootTask() })?.EnumeratePathToNodeFromTreeDFirst(node => node?.Component)?.Where(path => null != path?.LastOrDefault()); + foreach (var taskPath in outputListTaskPath.EmptyIfNull()) + { + var taskMotionParam = taskPath?.LastOrDefault()?.Motion; - var sequenceTaskPathWithMotion = sequenceTaskPath?.Where(task => null != task?.LastOrDefault()?.Motion); + if (null == taskMotionParam) + continue; - listTaskPath.Add(sequenceTaskPathWithMotion?.FirstOrDefault()); + listMotion.Add(new MotionRecommendation + { + Id = motionId++, + MotionParam = taskMotionParam, + }); + } } catch (Exception e) { exception = e; } - var listMotion = new List(); - - foreach (var moduleToggle in listTaskPath.ConcatNullable().OfType().Select(moduleToggleTask => moduleToggleTask?.module).WhereNotDefault()) - ToggleLastStepIndexFromModule[moduleToggle] = stepIndex; - - foreach (var taskPath in listTaskPath.EmptyIfNull()) - { - var taskMotionParam = taskPath?.LastOrDefault()?.Motion; - - if (null == taskMotionParam) - continue; - - listMotion.Add(new MotionRecommendation - { - Id = motionId++, - MotionParam = taskMotionParam, - }); - } - - var setMotionMOuseWaypointUIElement = - listMotion - ?.Select(motion => motion?.MotionParam) - ?.Where(motionParam => 0 < motionParam?.MouseButton?.Count()) - ?.Select(motionParam => motionParam?.MouseListWaypoint) - ?.ConcatNullable()?.Select(mouseWaypoint => mouseWaypoint?.UIElement)?.WhereNotDefault(); - - foreach (var mouseWaypointUIElement in setMotionMOuseWaypointUIElement.EmptyIfNull()) - MouseClickLastStepIndexFromUIElementId[mouseWaypointUIElement.Id] = stepIndex; - var stepResult = stepLastResult = new BotStepResult { Exception = exception, ListMotion = listMotion?.ToArrayIfNotEmpty(), }; + MemorizeStepResult(stepResult); + ++stepIndex; return stepResult; } - IEnumerable SequenceRootTask() + IEnumerable RootTaskListComponent() { yield return new EnableInfoPanelCurrentSystem { MemoryMeasurement = MemoryMeasurementAtTime?.Value }; diff --git a/src/Sanderling.ABot/Bot/BotExtension.cs b/src/Sanderling.ABot/Bot/BotExtension.cs index 32d1a6d..c0b6457 100644 --- a/src/Sanderling.ABot/Bot/BotExtension.cs +++ b/src/Sanderling.ABot/Bot/BotExtension.cs @@ -27,5 +27,8 @@ static public int AttackPriorityIndex( this Bot bot, IOverviewEntry entry) => AttackPriorityIndexForOverviewEntryEWar(bot?.OverviewMemory?.SetEWarTypeFromOverviewEntry(entry)); + + static public bool ShouldBeIncludedInStepOutput(this IBotTask task) => + null != task?.Motion; } }