import { floatyDebug } from './floaty-debug' ; import { getCaptureImage } from './image' ; import { isImage } from './type-check' ; function getPosition ( screenImage , template , options = { threshold : 0.5 , } ) { const m1 = images . matchTemplate ( screenImage , template , options ) ; return m1 . matches . filter ( ( { point } ) => { return point . y > 100 ; } ) . map ( ( { point , similarity } ) => { return { x : point . x + 5 , y : point . y + 5 , similarity , } ; } ) ; } type PositionFn = ( screenImage : Image ) => { x : number ; y : number ; } [ ] ; type CollectParams = { image : Image ; options ? : MatchTemplateOptions ; } | PositionFn | Image ; function findAndClick ( image , lastPositionsLen ? , afterRun ? ) ; function findAndClick ( config , lastPositionsLen ? , afterRun ? ) ; function findAndClick ( positionFn , lastPositionsLen ? , afterRun ? ) ; function findAndClick ( fnOrParams , lastPositionsLen = 0 , afterRun ? ) { const screenImage = getCaptureImage ( ) ; let positions ; if ( typeof fnOrParams === 'function' ) { positions = fnOrParams ( screenImage ) ; } else if ( isImage ( fnOrParams ) ) { positions = getPosition ( screenImage , fnOrParams ) ; } else { positions = getPosition ( screenImage , fnOrParams . image , fnOrParams . options ) ; } console . log ( 'floatyDebug positions' , { positions } ) ; floatyDebug ( 1000 , ... positions ) ; positions . forEach ( ( { x , y } ) => { press ( x , y , 100 ) ; sleep ( 1000 ) ; } ) ; screenImage . recycle ( ) ; if ( positions . length !== lastPositionsLen ) { if ( afterRun ) { afterRun ( ) ; } findAndClick ( fnOrParams as PositionFn , positions . length , afterRun ) ; } } export { getPosition , findAndClick } ;