Skip to content

Feature/51/find hooks #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ It's work in progress and will undergo constant modification.

- [x] findOnScreen
- [x] waitFor
- [ ] Hooks to trigger actions based on images
- [x] Hooks to trigger actions based on images

## Integration

Expand Down
3 changes: 0 additions & 3 deletions lib/provider/opencv/template-matching-finder.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export class TemplateMatchingFinder implements FinderInterface {
const matchResult = await TemplateMatchingFinder.match(haystack, scaledNeedle);
if (debug) {
this.debugImage(scaledNeedle, "scaled_needle.png");
console.log(`Scaled needle: ${matchResult.confidence}`);
}
return new MatchResult(
matchResult.confidence,
Expand Down Expand Up @@ -79,7 +78,6 @@ export class TemplateMatchingFinder implements FinderInterface {
const matchResult = await TemplateMatchingFinder.match(scaledHaystack, needle);
if (debug) {
this.debugImage(scaledHaystack, "scaled_haystack.png");
console.log(`Scaled haystack: ${matchResult.confidence}`);
}
return new MatchResult(
matchResult.confidence,
Expand Down Expand Up @@ -142,7 +140,6 @@ export class TemplateMatchingFinder implements FinderInterface {
const matchResults = [];
const unscaledResult = await TemplateMatchingFinder.match(haystack, needle);
if (debug) {
console.log(`Unscaled result: ${unscaledResult.confidence}`);
TemplateMatchingFinder.debugResult(
haystack,
unscaledResult,
Expand Down
16 changes: 16 additions & 0 deletions lib/screen.class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ describe("Screen.", () => {
expect(visionAdapterMock.findOnScreenRegion).toHaveBeenCalledWith(matchRequest);
});

it("should call registered hook before resolve", async () => {
const matchResult = new MatchResult(0.99, searchRegion);
VisionAdapter.prototype.findOnScreenRegion = jest.fn(() => {
return Promise.resolve(matchResult);
});
const visionAdapterMock = new VisionAdapter();

const SUT = new Screen(visionAdapterMock);
const testCallback = jest.fn(() => Promise.resolve());
const imagePath = "test/path/to/image.png";
SUT.on(imagePath, testCallback);
await SUT.find(imagePath);
expect(testCallback).toBeCalledTimes(1);
expect(testCallback).toBeCalledWith(matchResult);
});

it("should reject with insufficient confidence.", async () => {
const matchResult = new MatchResult(0.8, searchRegion);

Expand Down
16 changes: 14 additions & 2 deletions lib/screen.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@ import { FileType } from "./file-type.enum";
import { generateOutputPath } from "./generate-output-path.function";
import { LocationParameters } from "./locationparameters.class";
import { MatchRequest } from "./match-request.class";
import { MatchResult } from "./match-result.class";
import { Region } from "./region.class";
import { timeout } from "./util/poll-action.function";

export type FindHookCallback = (target: MatchResult) => Promise<void>;

export class Screen {
public config = {
confidence: 0.99,
resourceDirectory: cwd(),
};

constructor(private vision: VisionAdapter) {
constructor(
private vision: VisionAdapter,
private findHooks: Map<string, FindHookCallback> = new Map<string, FindHookCallback>()) {
}

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

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

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

Expand All @@ -49,6 +53,10 @@ export class Screen {
try {
const matchResult = await this.vision.findOnScreenRegion(matchRequest);
if (matchResult.confidence >= minMatch) {
const possibleHook = this.findHooks.get(pathToNeedle);
if (possibleHook) {
await possibleHook(matchResult);
}
resolve(matchResult.location);
} else {
reject(
Expand All @@ -73,6 +81,10 @@ export class Screen {
return timeout(500, timeoutMs, () => this.find(pathToNeedle, params));
}

public on(pathToNeedle: string, callback: FindHookCallback) {
this.findHooks.set(pathToNeedle, callback);
}

public async capture(
fileName: string,
fileFormat: FileType = FileType.PNG,
Expand Down
5 changes: 0 additions & 5 deletions lib/util/poll-action.function.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ describe("poll-action", () => {
const updateInterval = 200;
const maxDuration = 1000;
const action = jest.fn(() => {
console.log(`Polling...`);
return Promise.reject(false);
});

Expand All @@ -29,7 +28,6 @@ describe("poll-action", () => {
const updateInterval = 200;
const maxDuration = 1000;
const action = jest.fn(async () => {
console.log(`Polling...`);
return false;
});

Expand All @@ -52,7 +50,6 @@ describe("poll-action", () => {
const updateInterval = 200;
const maxDuration = 1000;
const action = jest.fn(() => {
console.log(`Polling...`);
return Promise.resolve(true);
});

Expand All @@ -71,7 +68,6 @@ describe("poll-action", () => {
const updateInterval = 200;
const maxDuration = 1000;
const action = jest.fn(async () => {
console.log(`Polling...`);
return true;
});

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