Advertisement
qaisjp

compsoc image

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