Skip to content

Commit aa6eb68

Browse files
authored
Merge pull request #255 from Xian55/feature/npc-name-finder-fuzzy-search
Feature: NpcNameFinder fuzzy search option - Processing with `Parallel.For`
2 parents 4a83342 + 2667e64 commit aa6eb68

File tree

8 files changed

+200
-25
lines changed

8 files changed

+200
-25
lines changed

BlazorServer/Pages/GoalsComponent.razor

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
<div class="card-header">
77
Goals -> @addonReader.PlayerReader.MinRange - @addonReader.PlayerReader.MaxRange
88
| Expand <input type="checkbox" @bind="@Expand" />
9-
<span class="float-right">Update: @addonReader.AvgUpdateLatency.ToString("0.00") ms</span>
9+
<span class="float-right">
10+
Cap: @botController.AvgScreenLatency.ToString("0.0")ms |
11+
Npc: @botController.AvgNPCLatency.ToString("0.0")ms |
12+
Bot: @addonReader.AvgUpdateLatency.ToString("0.0")ms</span>
1013
</div>
1114
@if (ShowGoals)
1215
{

Core/BotController.cs

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using WinAPI;
1616
using Microsoft.Extensions.Configuration;
1717
using SharedLib.NpcFinder;
18+
using Cyotek.Collections.Generic;
1819

1920
namespace Core
2021
{
@@ -34,7 +35,7 @@ public sealed class BotController : IBotController, IDisposable
3435

3536
public Thread? screenshotThread { get; set; }
3637

37-
private const int screenshotTickMs = 150;
38+
private const int screenshotTickMs = 200;
3839
private DateTime lastScreenshot;
3940

4041
public Thread addonThread { get; set; }
@@ -74,6 +75,34 @@ public sealed class BotController : IBotController, IDisposable
7475
private readonly AutoResetEvent addonAutoResetEvent = new(false);
7576
private readonly AutoResetEvent npcNameFinderAutoResetEvent = new(false);
7677

78+
public double AvgScreenLatency
79+
{
80+
get
81+
{
82+
double avg = 0;
83+
for (int i = 0; i < ScreenLatencys.Size; i++)
84+
{
85+
avg += ScreenLatencys.PeekAt(i);
86+
}
87+
return avg /= ScreenLatencys.Size;
88+
}
89+
}
90+
private readonly CircularBuffer<double> ScreenLatencys;
91+
92+
public double AvgNPCLatency
93+
{
94+
get
95+
{
96+
double avg = 0;
97+
for (int i = 0; i < NPCLatencys.Size; i++)
98+
{
99+
avg += NPCLatencys.PeekAt(i);
100+
}
101+
return avg /= NPCLatencys.Size;
102+
}
103+
}
104+
private readonly CircularBuffer<double> NPCLatencys;
105+
77106
public BotController(ILogger logger, IPPather pather, DataConfig dataConfig, IConfiguration configuration)
78107
{
79108
this.logger = logger;
@@ -112,6 +141,9 @@ public BotController(ILogger logger, IPPather pather, DataConfig dataConfig, ICo
112141
minimapNodeFinder = new MinimapNodeFinder(WowScreen, new PixelClassifier());
113142
MinimapImageFinder = minimapNodeFinder as IImageProvider;
114143

144+
ScreenLatencys = new CircularBuffer<double>(5);
145+
NPCLatencys = new CircularBuffer<double>(5);
146+
115147
addonThread = new Thread(AddonRefreshThread);
116148
addonThread.Start();
117149

@@ -157,14 +189,21 @@ public void AddonRefreshThread()
157189
public void ScreenshotRefreshThread()
158190
{
159191
var nodeFound = false;
192+
var stopWatch = new Stopwatch();
160193
while (this.Enabled)
161194
{
162195
if ((DateTime.UtcNow - lastScreenshot).TotalMilliseconds > screenshotTickMs)
163196
{
164197
if (this.WowScreen.Enabled)
165198
{
199+
stopWatch.Restart();
166200
this.WowScreen.UpdateScreenshot();
201+
ScreenLatencys.Put(stopWatch.ElapsedMilliseconds);
202+
203+
stopWatch.Restart();
167204
this.npcNameFinder.Update();
205+
NPCLatencys.Put(stopWatch.ElapsedMilliseconds);
206+
168207
this.WowScreen.PostProcess();
169208
}
170209
else
@@ -189,11 +228,10 @@ public void ScreenshotRefreshThread()
189228
MapId = this.AddonReader.UIMapId.Value,
190229
Spot = this.AddonReader.PlayerReader.PlayerLocation
191230
});
192-
updatePlayerPostion.Reset();
193231
updatePlayerPostion.Restart();
194232
}
195233

196-
Thread.Sleep(10);
234+
Thread.Sleep(5);
197235
}
198236
this.logger.LogInformation("Screenshot thread stoppped!");
199237
}

Core/ConfigBotController.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public class ConfigBotController : IBotController
3737
public event EventHandler? ProfileLoaded;
3838
public event EventHandler<bool>? StatusChanged;
3939

40+
public double AvgScreenLatency { get => throw new NotImplementedException(); }
41+
public double AvgNPCLatency { get => throw new NotImplementedException(); }
42+
4043
public void Shutdown()
4144
{
4245

Core/IBotController.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public interface IBotController
3333
event System.EventHandler? ProfileLoaded;
3434
event System.EventHandler<bool> StatusChanged;
3535

36+
double AvgScreenLatency { get; }
37+
double AvgNPCLatency { get; }
38+
3639
void ToggleBotStatus();
3740
void StopBot();
3841

CoreTests/NpcNameFinder/MockWoWProcess.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ namespace CoreTests
66
{
77
public class MockWoWProcess : IMouseInput
88
{
9-
public ValueTask RightClickMouse(Point position)
9+
public void RightClickMouse(Point position)
1010
{
1111
throw new System.NotImplementedException();
1212
}
1313

14-
public ValueTask LeftClickMouse(Point position)
14+
public void LeftClickMouse(Point position)
1515
{
1616
throw new System.NotImplementedException();
1717
}

CoreTests/NpcNameFinder/Test_NpcNameFinderTarget.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,13 @@ public void Execute()
3232
{
3333
npcNameFinder.ChangeNpcType(NpcNames.Enemy | NpcNames.Neutral);
3434

35-
capturer.Capture();
36-
3735
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
3836
stopwatch.Start();
37+
capturer.Capture();
38+
stopwatch.Stop();
39+
logger.LogInformation($"Capture: {stopwatch.ElapsedMilliseconds}ms");
40+
41+
stopwatch.Restart();
3942
this.npcNameFinder.Update();
4043
stopwatch.Stop();
4144
logger.LogInformation($"Update: {stopwatch.ElapsedMilliseconds}ms");

CoreTests/Program.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Serilog;
22
using Serilog.Extensions.Logging;
3+
using System.Threading;
34
using System.Threading.Tasks;
45

56
namespace CoreTests
@@ -25,7 +26,14 @@ public static void Main()
2526

2627
//var test = new Test_NpcNameFinderTarget(logger);
2728
var test = new Test_NpcNameFinderLoot(logger);
28-
test.Execute();
29+
int count = 1;
30+
int i = 0;
31+
while (i < count)
32+
{
33+
test.Execute();
34+
i++;
35+
Thread.Sleep(150);
36+
}
2937

3038
//MainAsync().GetAwaiter().GetResult();
3139
}

0 commit comments

Comments
 (0)