autoatk

mail@pastecode.io avatar
unknown
javascript
2 years ago
4.7 kB
6
Indexable
Never
var data
var cv = document.getElementById('screen')
var e1 = new Event('mousedown')
var e2 = new Event('mouseup')
var press1 = 0
var press2 = 0
var x
var y
var enemyFound
var name = app.game.playerName
var lastDistance = 0
var clickedTimes = 0

name = name.substring(0, 1).toLocaleUpperCase() + name.substring(1)

setInterval(function() {
  data = app.game.Bc.DA
  var eY, eX
  var eL = 0
  var pL = 0
  enemyFound = false

  for (var key in data) {
    if (data[key]['color'] == '#ffff00' && !enemyFound) {
      enemyFound = true
      eX = data[key]['Cq']['x'] + data[key]['Cs']['width'] / 2
      eY = data[key]['Cq']['y']
    }

    if (data[key]['color'] == '#ffffff' && data[key]['H1'] == name) {
      x = data[key]['Cq']['x'] + data[key]['Cs']['width'] / 2
      y = data[key]['Cq']['y']
    }

    for (var i = 1; i < 5; i += 1) {
      if (y + 19 + i * 19 == data[key]['Cq']['y']) {
        // console.log('line under player by ' + i + ' space')

        if (pL < i) {
          pL = i
        }
      }
    }

    if (enemyFound) {
      for (var i = 1; i < 5; i += 1) {
        if (eY + 19 + i * 19 == data[key]['Cq']['y']) {
          // console.log('line enemy under by ' + i + ' space')

          if (eL < i) {
            eL = i
          }
        }
      }
    }
  }

  if (eL != 0) {
    eY += eL * 19

    // console.log('enemy line pushed by ' + eL + ' space')
  }
  if (pL != 0) {
    y += pL * 19

    // console.log('player line pushed by ' + pL + ' space')
    // var canvas = cv.getContext('2d');
    // canvas.fillStyle="#FFFFFF";
    // canvas.fillRect(x,y,4,4);
  }

  if (press1 != 0) {
    KeyEvent.simulateUp(press1, press1)
    press1 = 0
  }

  if (press2 != 0) {
    KeyEvent.simulateUp(press2, press2)
    press2 = 0
  }

  if (enemyFound) {
    var distance = lineDistance(eX, x, eY, y)

    if (distance <= 250) {
      if (distance == lastDistance && clickedTimes >= 8) {
        clickedTimes = 0
        press2 = 0

        // console.log('too long fighting')
      }

      KeyEvent.simulateDown(press1, press1)
      KeyEvent.simulateDown(press2, press2)

      lastDistance = distance
    }

    if (distance <= 60) {
      if (clickedTimes == 0) {
        app.game.app.canvas.dispatchEvent(e1)
        clickedTimes = 1

        // console.log('Click pressed')
      } else {
        clickedTimes += 1

        // console.log('Click hold ' + clickedTimes)
      }
    } else {
      if (clickedTimes >= 1) {
        app.game.app.canvas.dispatchEvent(e2)
        clickedTimes = 0

        // console.log('Click release')
      }
    }
  } else {
    if (clickedTimes >= 1) {
      app.game.app.canvas.dispatchEvent(e2)
      clickedTimes = 0

      // console.log('Click release')
    }
  }
}, 500)

function lineDistance(x1, x2, y1, y2) {
  var xs, ys
  xs = x2 - x1
  xs = xs * xs

  ys = y2 - y1
  ys = ys * ys
  return Math.sqrt(xs + ys)
}

var KeyEvent = function(data, type) {
  this.keyCode = 'keyCode' in data ? data.keyCode : 0
  this.charCode = 'charCode' in data ? data.charCode : 0
  this.type = type || 'keypress'
}

KeyEvent.prototype.toNative = function() {
  var event = document.createEventObject
    ? document.createEventObject()
    : document.createEvent('Events')

  if (event.initEvent) event.initEvent(this.type, true, true)

  event.keyCode = this.keyCode
  event.which = this.charCode || this.keyCode

  return event
}

KeyEvent.prototype.fire = function(element) {
  var event = this.toNative()

  if (element.dispatchEvent) {
    element.dispatchEvent(event)

    return
  }

  element.fireEvent('on' + this.type, event)
}

KeyEvent.simulateDown = function(charCode, keyCode, element) {
  if (element === undefined) {
    element = document
  }

  var keyEvents = []

  keyEvents.push(
    new KeyEvent(
      {
        charCode: 0,
        keyCode: keyCode
      },
      'keydown'
    )
  )

  keyEvents.push(
    new KeyEvent(
      {
        charCode: charCode,
        keyCode: charCode
      },
      'keypress'
    )
  )

  for (i = 0; i < keyEvents.length; i++) {
    keyEvents[i].fire(element)
  }
}

KeyEvent.simulateUp = function(charCode, keyCode, element) {
  if (element === undefined) {
    element = document
  }

  var keyEvents = []

  keyEvents.push(
    new KeyEvent(
      {
        charCode: 0,
        keyCode: keyCode
      },
      'keyup'
    )
  )

  for (i = 0; i < keyEvents.length; i++) {
    keyEvents[i].fire(element)
  }
}

window.KeyEvent = KeyEvent