Skip to content

Commit

Permalink
Merge pull request #195 from Xian55/refactor/linq
Browse files Browse the repository at this point in the history
Refactor: Get rid of linq from Core
  • Loading branch information
Xian55 authored Dec 21, 2021
2 parents 68e2e04 + 0d8cbfd commit 805615d
Show file tree
Hide file tree
Showing 13 changed files with 28 additions and 49 deletions.
3 changes: 1 addition & 2 deletions Core/Actionbar/ActionBarPopulator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Core
Expand Down Expand Up @@ -57,7 +56,7 @@ private void CollectKeyActions()
private void AddUnique(KeyAction a)
{
if (!KeyReader.KeyMapping.ContainsKey(a.Key)) return;
if (sources.Any(i => i.KeyAction.ConsoleKeyFormHash == a.ConsoleKeyFormHash)) return;
if (sources.FindIndex(i => i.KeyAction.ConsoleKeyFormHash == a.ConsoleKeyFormHash) > -1) return;

var source = new ActionBarSource
{
Expand Down
4 changes: 1 addition & 3 deletions Core/Addon/AddonDataProvider/AddonDataProvider.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using SharedLib;
using Game;

Expand Down
6 changes: 2 additions & 4 deletions Core/Addon/EquipmentReader.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Linq;

namespace Core
namespace Core
{
public enum InventorySlotId
{
Expand Down Expand Up @@ -61,7 +59,7 @@ public void Read()

public string ToStringList()
{
return string.Join(", ", equipment.Where(i => i > 0));
return string.Join(", ", equipment);
}

public bool HasRanged()
Expand Down
1 change: 0 additions & 1 deletion Core/AddonComponent/CreatureHistory.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Linq;
using System.Collections.Generic;

namespace Core
Expand Down
8 changes: 2 additions & 6 deletions Core/Blacklist/NoBlacklist.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Linq;

namespace Core
namespace Core
{
public class NoBlacklist: IBlacklist
public class NoBlacklist : IBlacklist
{
public void Add(string name)
{
Expand Down
3 changes: 1 addition & 2 deletions Core/Database/WorldMapAreaDB.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
Expand Down Expand Up @@ -52,7 +51,7 @@ public Vector3 GetWorldLocation(int uiMapId, Vector3 p, bool flipXY)

public WorldMapAreaSpot ToMapAreaSpot(float x, float y, float z, string continent, int mapHint)
{
var area = WorldMapAreaFactory.GetWorldMapArea(areas.Values.ToList(), x, y, continent, mapHint);
var area = WorldMapAreaFactory.GetWorldMapArea(new List<WorldMapArea>(areas.Values), x, y, continent, mapHint);
return new WorldMapAreaSpot
{
Y = area.ToMapX(x),
Expand Down
12 changes: 5 additions & 7 deletions Core/Goals/CastingHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Microsoft.Extensions.Logging;
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Numerics;

Expand Down Expand Up @@ -369,7 +368,7 @@ public async ValueTask<bool> CastIfReady(KeyAction item, int sleepBeforeCast = 0
}
else
{
if (item.RequirementObjects.Any())
if (item.RequirementObjects.Count > 0)
{
(bool firstReq, double firstReqElapsedMs) = await wait.InterruptTask(SpellQueueTimeMs,
() => !item.CanRun()
Expand Down Expand Up @@ -434,16 +433,15 @@ public async ValueTask<bool> SwitchToCorrectStanceForm(Form beforeForm, KeyActio
return true;
}

var formKeyAction = classConfig.Form
.Where(s => s.FormEnum == item.FormEnum)
.FirstOrDefault();

if (formKeyAction == null)
int index = classConfig.Form.FindIndex(x => x.FormEnum == item.FormEnum);
if (index == -1)
{
logger.LogWarning($"Unable to find key in Form to transform into {item.FormEnum}");
return false;
}

KeyAction formKeyAction = classConfig.Form[index];

await input.KeyPress(formKeyAction.ConsoleKey, formKeyAction.PressDuration);
(bool notChanged, double elapsedMs) = await wait.InterruptTask(SpellQueueTimeMs, () => beforeForm != playerReader.Form);
item.LogInformation($" ... form changed: {!notChanged} | Delay: {elapsedMs}ms");
Expand Down
1 change: 0 additions & 1 deletion Core/Goals/CombatGoal.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Core.GOAP;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Threading.Tasks;
Expand Down
5 changes: 2 additions & 3 deletions Core/Goals/CorpseRunGoal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System;
using System.Numerics;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SharedLib.Extensions;

Expand Down Expand Up @@ -31,7 +30,7 @@ public CorpseRunGoal(PlayerReader playerReader, ConfigurableInput input, IPlayer
this.input = input;
this.playerDirection = playerDirection;
this.stopMoving = stopMoving;
this.spiritWalkerPath = spiritWalker.ToList();
this.spiritWalkerPath = spiritWalker;
this.logger = logger;
this.stuckDetector = stuckDetector;

Expand Down Expand Up @@ -101,7 +100,7 @@ public override async ValueTask PerformAction()

lastDistance = distance;

if (distance < 30 && points.Any())
if (distance < 30 && points.Count > 0)
{
logger.LogInformation($"Move to next point");
points.Pop();
Expand Down
2 changes: 1 addition & 1 deletion Core/Goals/GoalThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void OnActionEvent(object sender, ActionEventArgs e)
}
else if (e.Key == GoapKey.consumecorpse && (bool)e.Value == false)
{
if (routeInfo != null && routeInfo.PoiList.Any())
if (routeInfo != null && routeInfo.PoiList.Count > 0)
{
var closest = routeInfo.PoiList.Where(p => p.Name == "Corpse").
Select(i => new { i, d = addonReader.PlayerReader.PlayerLocation.DistanceXYTo(i.Location) }).
Expand Down
11 changes: 3 additions & 8 deletions Core/Goals/GoapGoal.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Core.GOAP;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

Expand Down Expand Up @@ -107,9 +106,7 @@ public void AddPrecondition(GoapKey key, object value)

public void RemovePrecondition(GoapKey key)
{
Preconditions.Where(o => o.Key.Equals(key))
.ToList()
.ForEach(o => Preconditions.Remove(o));
Preconditions.RemoveWhere(o => o.Key.Equals(key));
}

public void AddEffect(GoapKey key, object value)
Expand All @@ -119,9 +116,7 @@ public void AddEffect(GoapKey key, object value)

public void RemoveEffect(GoapKey key)
{
Effects.Where(o => o.Key.Equals(key))
.ToList()
.ForEach(o => Effects.Remove(o));
Effects.RemoveWhere(o => o.Key.Equals(key));
}

public virtual void OnActionEvent(object sender, ActionEventArgs e)
Expand All @@ -130,7 +125,7 @@ public virtual void OnActionEvent(object sender, ActionEventArgs e)

public virtual string Description()
{
return $"{Name} " + (Keys.Count == 1 ? $"[{Keys.First().ConsoleKey}]" : "");
return $"{Name} " + (Keys.Count == 1 ? $"[{Keys[0].ConsoleKey}]" : "");
}
}
}
20 changes: 10 additions & 10 deletions Core/Goals/MountHandler.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using Core.Goals;
using Microsoft.Extensions.Logging;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace Core
Expand Down Expand Up @@ -39,10 +37,11 @@ public async ValueTask MountUp()

if (playerReader.Class == PlayerClassEnum.Druid)
{
classConfig.Form
.Where(s => s.FormEnum == Form.Druid_Travel)
.ToList()
.ForEach(async key => await castingHandler.SwitchToCorrectStanceForm(playerReader.Form, key));
int index = classConfig.Form.FindIndex(s => s.FormEnum == Form.Druid_Travel);
if (index > -1)
{
await castingHandler.SwitchToCorrectStanceForm(playerReader.Form, classConfig.Form[index]);
}
}
else
{
Expand Down Expand Up @@ -72,10 +71,11 @@ public async ValueTask Dismount()
{
if (playerReader.Class == PlayerClassEnum.Druid && playerReader.Form == Form.Druid_Travel)
{
classConfig.Form
.Where(s => s.FormEnum == Form.Druid_Travel)
.ToList()
.ForEach(async k => await input.KeyPress(k.ConsoleKey, 50));
int index = classConfig.Form.FindIndex(s => s.FormEnum == Form.Druid_Travel);
if (index > -1)
{
await input.KeyPress(classConfig.Form[index].ConsoleKey, 50);
}
}
else
{
Expand Down
1 change: 0 additions & 1 deletion Core/MiniMapNodeAlert/MinimapNodeFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using SharedLib;
using Game;

#nullable enable
Expand Down

0 comments on commit 805615d

Please sign in to comment.