export class SimulationAPI { dispatchEvent ( event ) { document . body . dispatchEvent ( event ) ; } param key keyDown ( key ) { this . dispatchEvent ( new KeyboardEvent ( "keydown" , { key } ) ) ; } param key keyUp ( key ) { this . dispatchEvent ( new KeyboardEvent ( "keyup" , { key } ) ) ; } param key param delay pressKey ( key , delay = 0 ) { if ( delay > 0 ) { setTimeout ( ( ) => { this . keyDown ( key ) ; this . keyUp ( key ) ; } , delay ) ; } else { this . keyDown ( key ) ; this . keyUp ( key ) ; } } param target param delay clickTarget ( target , delay = 0 ) { if ( delay > 0 ) { setTimeout ( ( ) => { target . dispatchEvent ( new MouseEvent ( "mousedown" , { bubbles : true } ) ) ; target . dispatchEvent ( new MouseEvent ( "mouseup" , { bubbles : true } ) ) ; target . dispatchEvent ( new MouseEvent ( "click" , { bubbles : true } ) ) ; } , delay ) ; } else { target . dispatchEvent ( new MouseEvent ( "mousedown" , { bubbles : true } ) ) ; target . dispatchEvent ( new MouseEvent ( "mouseup" , { bubbles : true } ) ) ; target . dispatchEvent ( new MouseEvent ( "click" , { bubbles : true } ) ) ; } } param target param text param delay fillTextInput ( target , text , delay = 0 ) { if ( delay > 0 ) { setTimeout ( ( ) => { target . value = text ; } , delay ) ; } else { target . value = text ; } } param choices returns getValidKey ( choices ) { const possible_keys = [ "a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i" , "j" , "k" , "l" , "m" , "n" , "o" , "p" , "q" , "r" , "s" , "t" , "u" , "v" , "w" , "x" , "y" , "z" , "0" , "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , " " , ] ; let key ; if ( choices == "NO_KEYS" ) { key = null ; } else if ( choices == "ALL_KEYS" ) { key = possible_keys [ Math . floor ( Math . random ( ) * possible_keys . length ) ] ; } else { const flat_choices = choices . flat ( ) ; key = flat_choices [ Math . floor ( Math . random ( ) * flat_choices . length ) ] ; } return key ; } mergeSimulationData ( default_data , simulation_options ) { return { ... default_data , ... simulation_options ?. data , } ; } ensureSimulationDataConsistency ( trial , data ) { if ( data . rt ) { data . rt = Math . round ( data . rt ) ; } if ( trial . trial_duration && data . rt && data . rt > trial . trial_duration ) { data . rt = null ; if ( data . response ) { data . response = null ; } if ( data . correct ) { data . correct = false ; } } if ( trial . choices && trial . choices == "NO_KEYS" ) { if ( data . rt ) { data . rt = null ; } if ( data . response ) { data . response = null ; } } if ( trial . allow_response_before_complete ) { if ( trial . sequence_reps && trial . frame_time ) { const min_time = trial . sequence_reps * trial . frame_time * trial . stimuli . length ; if ( data . rt < min_time ) { data . rt = null ; data . response = null ; } } } } }