DigDig.IO Gold Bot

A simple bot that farms gold in digdig.io

目前为 2021-09-08 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name DigDig.IO Gold Bot
  3. // @namespace https://tampermonkey.net/
  4. // @version 0.0.6
  5. // @description A simple bot that farms gold in digdig.io
  6. // @author Zertalious (Zert)
  7. // @match *://digdig.io/*
  8. // @icon https://www.google.com/s2/favicons?domain=digdig.io
  9. // @grant none
  10. // @run-at document-end
  11. // ==/UserScript==
  12.  
  13. const chunkSize = 32;
  14. const goldPositions = [];
  15.  
  16. let isDead = false;
  17. let isRunning = false;
  18.  
  19. let health = 0;
  20.  
  21. let border = null;
  22.  
  23. const chunks = [];
  24.  
  25. let serverIndex = 0;
  26. const servers = [];
  27.  
  28. const modes = [ 'ffa', 'teams', 'maze' ];
  29.  
  30. let angle = Math.random() * Math.PI * 2;
  31.  
  32. init();
  33.  
  34. async function init() {
  35.  
  36. for ( let i = 0; i < modes.length; i ++ ) {
  37.  
  38. const response = await fetch( 'https://api.n.m28.io/endpoint/digdig-' + modes[ i ] + '/findEach' );
  39.  
  40. const json = await response.json();
  41.  
  42. for ( let key in json.servers ) {
  43.  
  44. servers.push( json.servers[ key ].id );
  45.  
  46. }
  47.  
  48. }
  49.  
  50. isRunning = true;
  51.  
  52. window.addEventListener( 'keyup', function ( event ) {
  53.  
  54. if ( String.fromCharCode( event.keyCode ) === 'B' ) {
  55.  
  56. isRunning = ! isRunning;
  57.  
  58. setTimeout( function () {
  59.  
  60. setAttack( isRunning );
  61.  
  62. }, 0 );
  63.  
  64. }
  65.  
  66. } );
  67.  
  68. }
  69.  
  70. window.requestAnimationFrame = new Proxy( window.requestAnimationFrame, {
  71. apply( target, thisArgs, args ) {
  72.  
  73. if ( isRunning === true ) {
  74.  
  75. args[ 0 ] = new Proxy( args[ 0 ], {
  76. apply( target, thisArgs, args ) {
  77.  
  78. isDead = false;
  79.  
  80. healthX.length = 0;
  81. health = 0;
  82.  
  83. border = null;
  84.  
  85. goldPositions.length = 0;
  86.  
  87. Reflect.apply( ...arguments );
  88.  
  89. if ( isDead === true || health <= 0 ) {
  90.  
  91. pressEnter();
  92.  
  93. return;
  94.  
  95. }
  96.  
  97. setAttack( true );
  98.  
  99. if ( goldPositions.length > 0 ) {
  100.  
  101. let target;
  102.  
  103. if ( border !== null ) {
  104.  
  105. const [ bx, by, br ] = border;
  106.  
  107. for ( let i = 0; i < goldPositions.length; i ++ ) {
  108.  
  109. const [ x, y ] = goldPositions[ i ];
  110.  
  111. if ( Math.hypot( x - bx, y - by ) < br ) {
  112.  
  113. mouseMove( x, y );
  114.  
  115. break;
  116.  
  117. }
  118.  
  119. }
  120.  
  121. } else {
  122.  
  123. mouseMove(
  124. goldPositions[ 0 ][ 0 ],
  125. goldPositions[ 0 ][ 1 ]
  126. );
  127.  
  128. }
  129. return;
  130.  
  131. }
  132.  
  133. mouseMove(
  134. ( Math.cos( angle ) * 0.5 + 0.5 ) * window.innerWidth,
  135. ( Math.sin( angle ) * 0.5 + 0.5 ) * window.innerHeight
  136. );
  137.  
  138. if ( health <= 0.05 ) {
  139.  
  140. angle = Math.random() * Math.PI * 2;
  141.  
  142. serverIndex = ( serverIndex + 1 ) % servers.length;
  143.  
  144. cp6.forceServerID( servers[ serverIndex ] );
  145.  
  146. while ( chunks.length > 0 ) {
  147.  
  148. chunks.shift().golds.length = 0;
  149.  
  150. }
  151.  
  152. }
  153.  
  154. }
  155. } );
  156.  
  157. }
  158.  
  159. return Reflect.apply( ...arguments );
  160.  
  161. }
  162. } );
  163.  
  164. const Context = CanvasRenderingContext2D.prototype;
  165.  
  166. Context.arc = new Proxy( Context.arc, {
  167. apply( target, thisArgs, [ x, y, r ] ) {
  168.  
  169. if ( thisArgs.fillStyle === '#222222' && x !== 0 && y !== 0 ) {
  170.  
  171. border = [ x, y, r ];
  172.  
  173. }
  174.  
  175. return Reflect.apply( ...arguments );
  176.  
  177. }
  178. } );
  179.  
  180. Context.drawImage = new Proxy( Context.drawImage, {
  181. apply( target, thisArgs, [ { golds } ] ) {
  182.  
  183. if ( golds !== undefined ) {
  184.  
  185. const { a, d, e, f } = thisArgs.getTransform();
  186.  
  187. for ( let i = 0; i < golds.length; i ++ ) {
  188.  
  189. const [ m, n ] = golds[ i ];
  190.  
  191. goldPositions.push( [
  192. m / 32 * a + e,
  193. n / 32 * d + f
  194. ] );
  195.  
  196. }
  197.  
  198. }
  199.  
  200. return Reflect.apply( ...arguments );
  201.  
  202. }
  203. } );
  204.  
  205. const healthX = [];
  206.  
  207. const params = {
  208. apply( target, thisArgs, [ x ] ) {
  209.  
  210. healthX[ target.name === 'moveTo' ? 0 : 1 ] = x;
  211.  
  212. return Reflect.apply( ...arguments );
  213.  
  214. }
  215. };
  216.  
  217. Context.moveTo = new Proxy( Context.moveTo, params );
  218. Context.lineTo = new Proxy( Context.lineTo, params );
  219.  
  220. Context.stroke = new Proxy( Context.stroke, {
  221. apply( target, thisArgs, args ) {
  222.  
  223. if ( thisArgs.strokeStyle === '#75dd34' ) {
  224.  
  225. health = ( healthX[ 0 ] - healthX[ 1 ] ) / ( 2 * healthX[ 0 ] );
  226.  
  227. }
  228.  
  229. return Reflect.apply( ...arguments );
  230.  
  231. }
  232. } );
  233.  
  234. const OffscreenContext = typeof OffscreenCanvasRenderingContext2D !== 'undefined' ?
  235. OffscreenCanvasRenderingContext2D.prototype : Context;
  236.  
  237. OffscreenContext.fillRect = new Proxy( OffscreenContext.fillRect, {
  238. apply( target, thisArgs, args ) {
  239.  
  240. if ( thisArgs.fillStyle === '#000000' ) {
  241.  
  242. isDead = true;
  243.  
  244. }
  245.  
  246. return Reflect.apply( ...arguments );
  247.  
  248. }
  249. } );
  250.  
  251. OffscreenContext.putImageData = new Proxy( OffscreenContext.putImageData, {
  252. apply( target, thisArgs, [ { data } ] ) {
  253.  
  254. if ( thisArgs.canvas.width === chunkSize && thisArgs.canvas.height === chunkSize ) {
  255.  
  256. thisArgs.canvas.golds = [];
  257.  
  258. for ( let i = 0; i < data.length; i += 4 ) {
  259.  
  260. if ( isGoldColor( data[ i ], data[ i + 1 ], data[ i + 2 ] ) ) {
  261.  
  262. const index = i / 4;
  263.  
  264. thisArgs.canvas.golds.push( [
  265. index % chunkSize,
  266. Math.floor( index / chunkSize )
  267. ] );
  268.  
  269. }
  270.  
  271. }
  272.  
  273. chunks.push( thisArgs.canvas );
  274.  
  275. }
  276.  
  277. return Reflect.apply( ...arguments );
  278.  
  279. }
  280. } );
  281.  
  282. OffscreenContext.fillRect = new Proxy( OffscreenContext.fillRect, {
  283. apply( target, thisArgs, [ x, y, width, height ] ) {
  284.  
  285. if ( thisArgs.canvas.width === chunkSize && thisArgs.canvas.height === chunkSize ) {
  286.  
  287. if ( thisArgs.canvas.golds === undefined ) {
  288.  
  289. thisArgs.canvas.golds = [];
  290.  
  291. }
  292.  
  293. if ( isGoldColor( thisArgs.fillStyle ) ) {
  294.  
  295. thisArgs.canvas.golds.push( [ x, y ] );
  296.  
  297. }
  298.  
  299. }
  300.  
  301. return Reflect.apply( ...arguments );
  302.  
  303. }
  304. } );
  305.  
  306. OffscreenContext.clearRect = new Proxy( OffscreenContext.clearRect, {
  307. apply( target, thisArgs, [ x, y, width, height ] ) {
  308.  
  309. if ( thisArgs.canvas.golds !== undefined ) {
  310.  
  311. if ( width === 32 && height === 32 ) {
  312.  
  313. thisArgs.canvas.golds.length = 0;
  314.  
  315. } else {
  316.  
  317. for ( let i = 0; i < thisArgs.canvas.golds.length; i ++ ) {
  318.  
  319. const [ m, n ] = thisArgs.canvas.golds[ i ];
  320.  
  321. if ( m === x && n === y ) {
  322.  
  323. thisArgs.canvas.golds.splice( i, 1 );
  324. break;
  325.  
  326. }
  327.  
  328. }
  329.  
  330. }
  331. }
  332.  
  333. return Reflect.apply( ...arguments );
  334.  
  335. }
  336. } );
  337.  
  338. function isGoldColor( r, g, b ) {
  339.  
  340. if ( arguments.length === 1 ) {
  341.  
  342. g = parseInt( r.substring( 3, 5 ), 16 );
  343. b = parseInt( r.substring( 5, 7 ), 16 );
  344. r = parseInt( r.substring( 1, 3 ), 16 );
  345.  
  346. }
  347.  
  348. return Math.hypot( 0xa5 - r, 0x9e - g, 0x15 - b ) < 25;
  349.  
  350. }
  351.  
  352. function pressEnter() {
  353.  
  354. keyEvent( 'keydown', 13 );
  355. keyEvent( 'keyup', 13 );
  356.  
  357. }
  358.  
  359. function setAttack( bool ) {
  360.  
  361. keyEvent( bool !== false ? 'keydown' : 'keyup', 32 );
  362.  
  363. }
  364.  
  365. function setHeal( bool ) {
  366.  
  367. keyEvent( bool !== false ? 'keydown' : 'keyup', 16 );
  368.  
  369. }
  370.  
  371. function keyEvent( type, keyCode ) {
  372.  
  373. window.dispatchEvent( new KeyboardEvent( type, { keyCode } ) );
  374.  
  375. }
  376.  
  377. function mouseMove( clientX, clientY ) {
  378.  
  379. window.Module.canvas.dispatchEvent(
  380. new MouseEvent( 'mousemove', {
  381. clientX,
  382. clientY
  383. } )
  384. );
  385.  
  386. }