|
1 | | -import { Image } from "./image.class"; |
| 1 | +import { Image, isImage } from "./image.class"; |
| 2 | +import { Region } from "./region.class"; |
| 3 | +import { OptionalSearchParameters } from "./optionalsearchparameters.class"; |
| 4 | +import { isLineQuery, isTextQuery, TextQuery } from "./query.class"; |
| 5 | +import { RegionResultFindInput } from "./screen.class"; |
| 6 | +import { ProviderRegistry } from "./provider/provider-registry.class"; |
2 | 7 |
|
3 | | -export class MatchRequest { |
4 | | - constructor( |
| 8 | +export class MatchRequest<NEEDLE_TYPE, PROVIDER_DATA_TYPE> { |
| 9 | + public constructor( |
5 | 10 | public readonly haystack: Image, |
6 | | - public readonly needle: Image, |
| 11 | + public readonly needle: NEEDLE_TYPE, |
7 | 12 | public readonly confidence: number, |
8 | | - public readonly searchMultipleScales: boolean = true |
| 13 | + public readonly providerData?: PROVIDER_DATA_TYPE |
9 | 14 | ) {} |
10 | 15 | } |
| 16 | + |
| 17 | +export function isImageMatchRequest<PROVIDER_DATA_TYPE>( |
| 18 | + matchRequest: any |
| 19 | +): matchRequest is MatchRequest<Image, PROVIDER_DATA_TYPE> { |
| 20 | + return isImage(matchRequest.needle); |
| 21 | +} |
| 22 | + |
| 23 | +export function isTextMatchRequest<PROVIDER_DATA_TYPE>( |
| 24 | + matchRequest: any |
| 25 | +): matchRequest is MatchRequest<TextQuery, PROVIDER_DATA_TYPE> { |
| 26 | + return isTextQuery(matchRequest.needle); |
| 27 | +} |
| 28 | + |
| 29 | +export function createMatchRequest<PROVIDER_DATA_TYPE>( |
| 30 | + providerRegistry: ProviderRegistry, |
| 31 | + needle: TextQuery | Promise<TextQuery>, |
| 32 | + searchRegion: Region, |
| 33 | + minMatch: number, |
| 34 | + screenImage: Image, |
| 35 | + params?: OptionalSearchParameters<PROVIDER_DATA_TYPE> |
| 36 | +): MatchRequest<TextQuery, PROVIDER_DATA_TYPE>; |
| 37 | +export function createMatchRequest<PROVIDER_DATA_TYPE>( |
| 38 | + providerRegistry: ProviderRegistry, |
| 39 | + needle: Image | Promise<Image>, |
| 40 | + searchRegion: Region, |
| 41 | + minMatch: number, |
| 42 | + screenImage: Image, |
| 43 | + params?: OptionalSearchParameters<PROVIDER_DATA_TYPE> |
| 44 | +): MatchRequest<Image, PROVIDER_DATA_TYPE>; |
| 45 | +export function createMatchRequest<PROVIDER_DATA_TYPE>( |
| 46 | + providerRegistry: ProviderRegistry, |
| 47 | + needle: RegionResultFindInput | Promise<RegionResultFindInput>, |
| 48 | + searchRegion: Region, |
| 49 | + minMatch: number, |
| 50 | + screenImage: Image, |
| 51 | + params?: OptionalSearchParameters<PROVIDER_DATA_TYPE> |
| 52 | +): |
| 53 | + | MatchRequest<TextQuery, PROVIDER_DATA_TYPE> |
| 54 | + | MatchRequest<Image, PROVIDER_DATA_TYPE>; |
| 55 | +export function createMatchRequest<PROVIDER_DATA_TYPE>( |
| 56 | + providerRegistry: ProviderRegistry, |
| 57 | + needle: RegionResultFindInput | Promise<RegionResultFindInput>, |
| 58 | + searchRegion: Region, |
| 59 | + minMatch: number, |
| 60 | + screenImage: Image, |
| 61 | + params?: OptionalSearchParameters<PROVIDER_DATA_TYPE> |
| 62 | +): |
| 63 | + | MatchRequest<Image, PROVIDER_DATA_TYPE> |
| 64 | + | MatchRequest<TextQuery, PROVIDER_DATA_TYPE> { |
| 65 | + if (isImage(needle)) { |
| 66 | + providerRegistry |
| 67 | + .getLogProvider() |
| 68 | + .info( |
| 69 | + `Searching for image ${ |
| 70 | + needle.id |
| 71 | + } in region ${searchRegion.toString()}. Required confidence: ${minMatch}` |
| 72 | + ); |
| 73 | + |
| 74 | + return new MatchRequest( |
| 75 | + screenImage, |
| 76 | + needle, |
| 77 | + minMatch, |
| 78 | + params?.providerData |
| 79 | + ); |
| 80 | + } else if (isTextQuery(needle)) { |
| 81 | + providerRegistry.getLogProvider().info( |
| 82 | + `Searching for ${isLineQuery(needle) ? "line" : "word"} { |
| 83 | + ${isLineQuery(needle) ? needle.by.line : needle.by.word} |
| 84 | + } in region ${searchRegion.toString()}. Required confidence: ${minMatch}` |
| 85 | + ); |
| 86 | + |
| 87 | + return new MatchRequest( |
| 88 | + screenImage, |
| 89 | + needle, |
| 90 | + minMatch, |
| 91 | + params?.providerData |
| 92 | + ); |
| 93 | + } |
| 94 | + throw new Error(`Unknown input type: ${JSON.stringify(needle)}`); |
| 95 | +} |
0 commit comments