Zafinar

Untitled

Apr 2nd, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. OFFSET_X = 921;
  2. OFFSET_Y = 626;
  3.  
  4. waitDuration = 0;
  5. modHash = null;
  6.  
  7. //white: 0, lightGrey: 1, grey: 2, black: 3,
  8. //pink: 4, red: 5, orange: 6, brown: 7,
  9. //yellow: 8, lightGreen: 9, green: 10, cyan: 11,
  10. //teal: 12, blue: 13, fuchsia: 14, purple: 15
  11. f = -1;
  12.  
  13.  
  14.  
  15. image = [[5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5],
  16. [5,0,0,0,5,0,0,0,0,0,5,0,0,0,5,0,0,5,5,0,0,0,5],
  17. [5,0,5,5,5,0,5,0,5,0,5,0,5,0,5,0,5,0,5,0,5,5,5],
  18. [5,0,0,0,5,0,5,0,5,0,5,0,0,0,5,0,5,0,5,5,0,5,5],
  19. [5,5,5,0,5,0,5,0,5,0,5,0,5,0,5,0,5,0,5,5,5,0,5],
  20. [5,0,0,0,5,0,5,0,5,0,5,0,5,0,5,0,5,0,5,0,0,0,5],
  21. [5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5]];
  22.  
  23.  
  24. function start() {
  25. modHash = getModHash();
  26. main(0, 0);
  27. }
  28.  
  29. function main(x, y) {
  30. var timer = drawIfNecessary(x, y);
  31. console.log('waiting ' + timer + ' milliseconds');
  32. x = (x + 1) % image[y].length;
  33. if (x === 0) {
  34. y = (y + 1) % image.length;
  35. }
  36. setTimeout(main, timer, x, y);
  37. }
  38.  
  39. function drawIfNecessary(x, y) {
  40. if (image[y][x] < 0) return 0;
  41. canvasX = x + OFFSET_X;
  42. canvasY = y + OFFSET_Y;
  43. var pixelColour = pixelRequest(canvasX, canvasY);
  44. console.log('found pixel colour ' + pixelColour);
  45. var desiredColour = image[y][x];
  46. return (pixelColour == desiredColour) ? 0 : draw(canvasX, canvasY, desiredColour);
  47. }
  48.  
  49.  
  50. function sendSynchronousRequest(method, url, headers, params) {
  51. var xhr = new XMLHttpRequest();
  52. xhr.open(method, url, false);
  53. for (var h in headers) {
  54. xhr.setRequestHeader(h, headers[h]);
  55. }
  56. var firstParam = true;
  57. var paramString = '';
  58. for (var p in params) {
  59. if (firstParam) {
  60. firstParam = false;
  61. } else {
  62. paramString += '&';
  63. }
  64. paramString += p + '=' + params[p];
  65. }
  66. xhr.send(paramString);
  67. return xhr;
  68. }
  69.  
  70.  
  71. function getModHash() {
  72. var xhr = sendSynchronousRequest('GET', 'api/me.json', {}, {});
  73.  
  74. if (xhr.readyState !== 4) throw 'oh shit';
  75. if (xhr.status !== 200) {
  76. alert("Couldn't obtain modhash. Are you logged into Reddit?");
  77. throw 'bollocks';
  78. }
  79. var response = JSON.parse(xhr.responseText);
  80. return response.data.modhash;
  81. }
  82.  
  83.  
  84. function pixelRequest(x, y) {
  85. var url = '/api/place/pixel.json?x=' + x + '&y=' + y;
  86.  
  87. var xhr = sendSynchronousRequest('GET', url, {
  88. 'x-requested-with': 'XMLHttpRequest',
  89. 'x-modhash': modHash
  90. }, {});
  91.  
  92. if (xhr.readyState !== 4) throw 'oh shit';
  93. if (xhr.status !== 200) {
  94. console.log(xhr);
  95. return "jews";
  96. }
  97. var response = JSON.parse(xhr.responseText);
  98. return response.color;
  99. }
  100.  
  101.  
  102. function draw(x, y, colour) {
  103. console.log('attempting to draw a pixel at (' + x + ',' + y + ')');
  104. var xhr = sendSynchronousRequest('POST', '/api/place/draw.json', {
  105. 'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
  106. 'x-requested-with': 'XMLHttpRequest',
  107. 'x-modhash': modHash
  108. }, {
  109. x: x,
  110. y: y,
  111. color: colour
  112. });
  113.  
  114. if (xhr.readyState !== 4) throw 'oh shit';
  115. if (xhr.status !== 200 && xhr.status !== 429) {
  116. console.log(xhr);
  117. throw 'bollocks';
  118. }
  119. var response = JSON.parse(xhr.responseText);
  120. return response.wait_seconds * 1000;
  121. }
  122.  
  123.  
  124. start();
Advertisement
Add Comment
Please, Sign In to add comment