Skip to content

Commit

Permalink
improve readability by extracting methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
Viir committed Aug 17, 2016
1 parent 753a260 commit 3ccc8d5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 40 deletions.
89 changes: 49 additions & 40 deletions src/Sanderling.ABot/Bot/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,77 +46,86 @@ public class Bot
public Int64? ToggleLastAgeStepCountFromModule(Accumulation.IShipUiModule module) =>
stepIndex - ToggleLastStepIndexFromModule?.TryGetValueNullable(module);

IEnumerable<IBotTask[]> 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<IBotTask[]>();
var listMotion = new List<MotionRecommendation>();

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<ModuleToggleTask>().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<MotionRecommendation>();

foreach (var moduleToggle in listTaskPath.ConcatNullable().OfType<ModuleToggleTask>().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<IBotTask> SequenceRootTask()
IEnumerable<IBotTask> RootTaskListComponent()
{
yield return new EnableInfoPanelCurrentSystem { MemoryMeasurement = MemoryMeasurementAtTime?.Value };

Expand Down
3 changes: 3 additions & 0 deletions src/Sanderling.ABot/Bot/BotExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 3ccc8d5

Please sign in to comment.