Zafinar

Untitled

Apr 3rd, 2017
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 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,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,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.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,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,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,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,5],
  22. [3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3],
  23. [5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5],
  24. [5,5,5,5,5,5,5,3,3,5,5,5,5,5,5,3,3,5,5,5,5,5,5,5],
  25. [5,5,5,5,5,5,3,3,3,3,5,5,5,5,3,3,3,3,5,5,5,5,5,5],
  26. [5,5,5,5,5,5,5,5,3,3,3,5,5,3,3,3,5,5,5,5,5,5,5,5],
  27. [5,3,5,5,5,3,3,5,5,3,3,3,3,3,3,5,5,3,3,5,5,5,3,5],
  28. [5,5,3,3,3,5,3,3,5,5,3,3,3,3,5,5,3,3,5,3,3,3,5,5],
  29. [5,3,5,5,5,3,3,3,3,3,3,3,3,3,3,3,3,3,3,5,5,5,3,5],
  30. [5,5,3,3,3,5,3,3,3,3,3,3,3,3,3,3,3,3,5,3,3,3,5,5],
  31. [5,3,5,5,5,3,3,3,3,3,3,3,3,3,3,3,3,3,3,5,5,5,3,5],
  32. [5,5,3,3,3,5,3,3,3,3,3,3,3,3,3,3,3,3,5,3,3,3,5,5],
  33. [5,3,5,5,5,3,3,3,3,3,3,3,3,3,3,3,3,3,3,5,5,5,3,5],
  34. [5,5,3,3,3,5,3,3,3,3,3,3,3,3,3,3,3,3,5,3,3,3,5,5],
  35. [5,5,5,5,5,3,3,3,3,3,3,3,3,3,3,3,3,3,3,5,5,5,5,5],
  36. [5,5,3,3,3,5,5,3,5,3,3,3,3,3,3,5,3,5,5,3,3,3,5,5],
  37. [5,5,5,5,5,5,3,5,5,5,3,3,3,3,5,5,5,3,5,5,5,5,5,5],
  38. [5,5,5,3,3,3,5,5,5,3,3,3,3,3,3,5,5,5,3,3,3,5,5,5],
  39. [5,5,5,5,5,5,5,5,3,3,5,3,3,5,3,3,5,5,5,5,5,5,5,5],
  40. [5,5,5,5,5,5,3,3,3,5,5,3,3,5,5,3,3,3,5,5,5,5,5,5],
  41. [5,5,5,5,5,5,5,3,5,5,3,3,3,3,5,5,3,5,5,5,5,5,5,5],
  42. [5,5,5,5,5,5,5,5,5,3,3,3,3,3,3,5,5,5,5,5,5,5,5,5],
  43. [5,5,5,5,5,5,5,5,5,5,3,3,3,3,5,5,5,5,5,5,5,5,5,5],
  44. [5,5,5,5,5,5,5,5,5,5,5,3,3,5,5,5,5,5,5,5,5,5,5,5],
  45. [5,5,5,5,5,5,5,5,5,5,5,3,3,5,5,5,5,5,5,5,5,5,5,5],
  46. [5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5]];
  47.  
  48.  
  49. function start() {
  50. modHash = getModHash();
  51. main(0, 0);
  52. }
  53.  
  54. function main(x, y) {
  55. var timer = drawIfNecessary(x, y);
  56. console.log('waiting ' + timer + ' milliseconds');
  57. x = (x + 1) % image[y].length;
  58. if (x === 0) {
  59. y = (y + 1) % image.length;
  60. }
  61. setTimeout(main, timer, x, y);
  62. }
  63.  
  64. function drawIfNecessary(x, y) {
  65. if (image[y][x] < 0) return 0;
  66. canvasX = x + OFFSET_X;
  67. canvasY = y + OFFSET_Y;
  68. var pixelColour = pixelRequest(canvasX, canvasY);
  69. console.log('found pixel colour ' + pixelColour);
  70. var desiredColour = image[y][x];
  71. return (pixelColour == desiredColour) ? 0 : draw(canvasX, canvasY, desiredColour);
  72. }
  73.  
  74.  
  75. function sendSynchronousRequest(method, url, headers, params) {
  76. var xhr = new XMLHttpRequest();
  77. xhr.open(method, url, false);
  78. for (var h in headers) {
  79. xhr.setRequestHeader(h, headers[h]);
  80. }
  81. var firstParam = true;
  82. var paramString = '';
  83. for (var p in params) {
  84. if (firstParam) {
  85. firstParam = false;
  86. } else {
  87. paramString += '&';
  88. }
  89. paramString += p + '=' + params[p];
  90. }
  91. xhr.send(paramString);
  92. return xhr;
  93. }
  94.  
  95.  
  96. function getModHash() {
  97. var xhr = sendSynchronousRequest('GET', 'api/me.json', {}, {});
  98.  
  99. if (xhr.readyState !== 4) throw 'oh shit';
  100. if (xhr.status !== 200) {
  101. alert("Couldn't obtain modhash. Are you logged into Reddit?");
  102. throw 'bollocks';
  103. }
  104. var response = JSON.parse(xhr.responseText);
  105. return response.data.modhash;
  106. }
  107.  
  108.  
  109. function pixelRequest(x, y) {
  110. var url = '/api/place/pixel.json?x=' + x + '&y=' + y;
  111.  
  112. var xhr = sendSynchronousRequest('GET', url, {
  113. 'x-requested-with': 'XMLHttpRequest',
  114. 'x-modhash': modHash
  115. }, {});
  116.  
  117. if (xhr.readyState !== 4) throw 'oh shit';
  118. if (xhr.status !== 200) {
  119. console.log(xhr);
  120. return "jews";
  121. }
  122. var response = JSON.parse(xhr.responseText);
  123. return response.color;
  124. }
  125.  
  126.  
  127. function draw(x, y, colour) {
  128. console.log('attempting to draw a pixel at (' + x + ',' + y + ')');
  129. var xhr = sendSynchronousRequest('POST', '/api/place/draw.json', {
  130. 'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
  131. 'x-requested-with': 'XMLHttpRequest',
  132. 'x-modhash': modHash
  133. }, {
  134. x: x,
  135. y: y,
  136. color: colour
  137. });
  138.  
  139. if (xhr.readyState !== 4) throw 'oh shit';
  140. if (xhr.status !== 200 && xhr.status !== 429) {
  141. console.log(xhr);
  142. throw 'bollocks';
  143. }
  144. var response = JSON.parse(xhr.responseText);
  145. return response.wait_seconds * 1000;
  146. }
  147.  
  148.  
  149. start();
Add Comment
Please, Sign In to add comment