Skip to content

Commit 44ef44a

Browse files
authored
Merge pull request #52 from nut-tree/feature/51/find_hooks
Feature/51/find hooks
2 parents 2f1b734 + f43867d commit 44ef44a

File tree

5 files changed

+31
-11
lines changed

5 files changed

+31
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ It's work in progress and will undergo constant modification.
8989

9090
- [x] findOnScreen
9191
- [x] waitFor
92-
- [ ] Hooks to trigger actions based on images
92+
- [x] Hooks to trigger actions based on images
9393

9494
## Integration
9595

lib/provider/opencv/template-matching-finder.class.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ export class TemplateMatchingFinder implements FinderInterface {
4747
const matchResult = await TemplateMatchingFinder.match(haystack, scaledNeedle);
4848
if (debug) {
4949
this.debugImage(scaledNeedle, "scaled_needle.png");
50-
console.log(`Scaled needle: ${matchResult.confidence}`);
5150
}
5251
return new MatchResult(
5352
matchResult.confidence,
@@ -79,7 +78,6 @@ export class TemplateMatchingFinder implements FinderInterface {
7978
const matchResult = await TemplateMatchingFinder.match(scaledHaystack, needle);
8079
if (debug) {
8180
this.debugImage(scaledHaystack, "scaled_haystack.png");
82-
console.log(`Scaled haystack: ${matchResult.confidence}`);
8381
}
8482
return new MatchResult(
8583
matchResult.confidence,
@@ -142,7 +140,6 @@ export class TemplateMatchingFinder implements FinderInterface {
142140
const matchResults = [];
143141
const unscaledResult = await TemplateMatchingFinder.match(haystack, needle);
144142
if (debug) {
145-
console.log(`Unscaled result: ${unscaledResult.confidence}`);
146143
TemplateMatchingFinder.debugResult(
147144
haystack,
148145
unscaledResult,

lib/screen.class.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,22 @@ describe("Screen.", () => {
4545
expect(visionAdapterMock.findOnScreenRegion).toHaveBeenCalledWith(matchRequest);
4646
});
4747

48+
it("should call registered hook before resolve", async () => {
49+
const matchResult = new MatchResult(0.99, searchRegion);
50+
VisionAdapter.prototype.findOnScreenRegion = jest.fn(() => {
51+
return Promise.resolve(matchResult);
52+
});
53+
const visionAdapterMock = new VisionAdapter();
54+
55+
const SUT = new Screen(visionAdapterMock);
56+
const testCallback = jest.fn(() => Promise.resolve());
57+
const imagePath = "test/path/to/image.png";
58+
SUT.on(imagePath, testCallback);
59+
await SUT.find(imagePath);
60+
expect(testCallback).toBeCalledTimes(1);
61+
expect(testCallback).toBeCalledWith(matchResult);
62+
});
63+
4864
it("should reject with insufficient confidence.", async () => {
4965
const matchResult = new MatchResult(0.8, searchRegion);
5066

lib/screen.class.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,21 @@ import { FileType } from "./file-type.enum";
55
import { generateOutputPath } from "./generate-output-path.function";
66
import { LocationParameters } from "./locationparameters.class";
77
import { MatchRequest } from "./match-request.class";
8+
import { MatchResult } from "./match-result.class";
89
import { Region } from "./region.class";
910
import { timeout } from "./util/poll-action.function";
1011

12+
export type FindHookCallback = (target: MatchResult) => Promise<void>;
13+
1114
export class Screen {
1215
public config = {
1316
confidence: 0.99,
1417
resourceDirectory: cwd(),
1518
};
1619

17-
constructor(private vision: VisionAdapter) {
20+
constructor(
21+
private vision: VisionAdapter,
22+
private findHooks: Map<string, FindHookCallback> = new Map<string, FindHookCallback>()) {
1823
}
1924

2025
public width() {
@@ -34,7 +39,6 @@ export class Screen {
3439
(params && params.searchRegion) || await this.vision.screenSize();
3540

3641
const fullPathToNeedle = normalize(join(this.config.resourceDirectory, pathToNeedle));
37-
// console.log(`Full path to needle: ${fullPathToNeedle}`);
3842

3943
const screenImage = await this.vision.grabScreen();
4044

@@ -49,6 +53,10 @@ export class Screen {
4953
try {
5054
const matchResult = await this.vision.findOnScreenRegion(matchRequest);
5155
if (matchResult.confidence >= minMatch) {
56+
const possibleHook = this.findHooks.get(pathToNeedle);
57+
if (possibleHook) {
58+
await possibleHook(matchResult);
59+
}
5260
resolve(matchResult.location);
5361
} else {
5462
reject(
@@ -73,6 +81,10 @@ export class Screen {
7381
return timeout(500, timeoutMs, () => this.find(pathToNeedle, params));
7482
}
7583

84+
public on(pathToNeedle: string, callback: FindHookCallback) {
85+
this.findHooks.set(pathToNeedle, callback);
86+
}
87+
7688
public async capture(
7789
fileName: string,
7890
fileFormat: FileType = FileType.PNG,

lib/util/poll-action.function.spec.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ describe("poll-action", () => {
66
const updateInterval = 200;
77
const maxDuration = 1000;
88
const action = jest.fn(() => {
9-
console.log(`Polling...`);
109
return Promise.reject(false);
1110
});
1211

@@ -29,7 +28,6 @@ describe("poll-action", () => {
2928
const updateInterval = 200;
3029
const maxDuration = 1000;
3130
const action = jest.fn(async () => {
32-
console.log(`Polling...`);
3331
return false;
3432
});
3533

@@ -52,7 +50,6 @@ describe("poll-action", () => {
5250
const updateInterval = 200;
5351
const maxDuration = 1000;
5452
const action = jest.fn(() => {
55-
console.log(`Polling...`);
5653
return Promise.resolve(true);
5754
});
5855

@@ -71,7 +68,6 @@ describe("poll-action", () => {
7168
const updateInterval = 200;
7269
const maxDuration = 1000;
7370
const action = jest.fn(async () => {
74-
console.log(`Polling...`);
7571
return true;
7672
});
7773

@@ -91,7 +87,6 @@ describe("poll-action", () => {
9187
const maxDuration = 1000;
9288
const delay = 2.2 * updateInterval;
9389
const action = jest.fn(() => {
94-
console.log(`Polling...`);
9590
const interval = (Date.now() - start);
9691
return new Promise<boolean>((resolve, reject) => (interval > delay) ? resolve(true) : reject());
9792
});

0 commit comments

Comments
 (0)