import { Mod } from "../mod" ; /**
 * Allow the user to hold "control" key and double click
 * items slots in the bank to quickly move items
 */ export class RapidExchange extends Mod { private exchangeInventory : any ; private exchangeStorage : any ; private tradeWithPlayerAndNPCInventory : any ; private keyPressed : boolean = false ; private supportedWindows : Array < string > = [ "exchangeInventory" , "exchangeStorage" , "tradeWithPlayerAndNPCInventory" ] ; startMod ( ) { this . supportedWindows = [ "exchangeInventory" , "exchangeStorage" , "tradeWithPlayerAndNPCInventory" ] ; const windows = this . wGame . gui . windowsContainer . getChildren ( ) ; windows . forEach ( ( window ) => { if ( this . supportedWindows . includes ( window . id ) ) this [ window . id ] = window ; } ) ; this . setKeyListenner ( ) ; this . setInventoryEventListener ( ) ; } private exchangeInventoryEvent ( slot ) { this . moveItem ( slot . itemInstance , "exchangeInventory" , false ) ; } private exchangeStorageEvent ( slot ) { this . moveItem ( slot . itemInstance , "exchangeStorage" , true ) ; } private tradeWithPlayerAndNPCInventoryEvent ( slot ) { this . moveItem ( slot . itemInstance , "tradeWithPlayerAndNPCInventory" , false ) ; } private setKeyListenner ( ) { const keydown = ( event ) => { if ( event . keyCode == 17 ) { this . keyPressed = true ; } } ; const keyup = ( event ) => { if ( event . keyCode == 17 ) { this . keyPressed = false ; } } ; this . wGame . addEventListener ( "keydown" , keydown , true ) ; this . wGame . addEventListener ( "keyup" , keyup , true ) ; this . addOnResetListener ( ( ) => { this . wGame . removeEventListener ( "keydown" , keydown ) ; this . wGame . removeEventListener ( "keyup" , keyup ) ; } ) ; } private setInventoryEventListener ( ) { if ( this . exchangeInventory ) { this . on ( this . exchangeInventory , 'slot-doubletap' , this . exchangeInventoryEvent . bind ( this ) ) ; } if ( this . exchangeStorage ) { this . on ( this . exchangeStorage , 'slot-doubletap' , this . exchangeStorageEvent . bind ( this ) ) ; } if ( this . tradeWithPlayerAndNPCInventory ) { this . on ( this . tradeWithPlayerAndNPCInventory , 'slot-doubletap' , this . tradeWithPlayerAndNPCInventoryEvent . bind ( this ) ) ; } this . on ( this . wGame . gui , 'ExchangeObjectAddedMessage' , ( msg ) => { if ( msg . remote ) return ; const UID = msg . object . objectUID ; const quantity = msg . object . quantity ; const remove = ( ) => { this . moveItem ( { objectUID : UID , quantity : quantity } , "tradeWithPlayer" , true ) ; } ; const myTradeSpace = this . getWindow ( "tradeWithPlayer" ) ; setTimeout ( ( ) => { myTradeSpace . _allSlots . getChild ( 'slot' + UID ) . on ( 'doubletap' , remove ) ; } , 500 ) ; } ) ; } private moveItem ( itemInstance , inventoryId , toStorage ) { if ( itemInstance . quantity > 1 && this . keyPressed ) { this . wGame . dofus . sendMessage ( 'ExchangeObjectMoveMessage' , { objectUID : itemInstance . objectUID , quantity : toStorage ? - itemInstance . quantity : itemInstance . quantity } ) ; this . hideMinMaxSelector ( inventoryId ) ; } } private getWindow ( id ) { const windows = this . wGame . gui . windowsContainer . getChildren ( ) ; let window = null ; for ( const i in windows ) { if ( windows [ i ] . id === id ) { window = windows [ i ] ; break ; } } if ( ! window ) return null ; if ( window . _myTradeSpace ) return window . _myTradeSpace ; return window ; } private hideMinMaxSelector ( id ) { const window = this . getWindow ( id ) ; if ( ! window ) { return ; } switch ( id ) { case "exchangeInventory" : case "exchangeStorage" : window . closeMinMaxSelector ( ) ; break ; case "tradeWithPlayerAndNPCInventory" : case "tradeWithPlayer" : for ( const i in window . _childrenList ) { if ( window . _childrenList [ i ] . rootElement && window . _childrenList [ i ] . rootElement . className == "minMaxSelector" ) { window . _childrenList [ i ] . hide ( ) ; } } } } }