rideride

/r/livecounting on /r/place

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