DollarAkshay

INDIA FLAG for r/place

Apr 2nd, 2017
3,562
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // +-----------------------------------------------+
  2. // |                                               |
  3. // | Script by DollarAkshay v0.4.5                 |
  4. // | r/place INDIA FLAG SCRIPT                     |
  5. // | Github  : https://github.com/DollarAkshay     |
  6. // | Website : http://dollarakshay.com             |
  7. // | Reddit : u/DollarAkshay                       |
  8. // |                                               |
  9. // +-----------------------------------------------+
  10.  
  11.  
  12. var modhash = window.reddit.modhash;
  13. var sec = 0;
  14. setInterval(function(){
  15.     console.log("Drawing in " + (sec--) + " seconds");
  16.     if(sec<=0){
  17.         sec = 30;
  18.     }
  19. }, 1000);
  20.  
  21. const draw = function(seconds){
  22.    
  23.     sec = seconds = Math.ceil(seconds)
  24.    
  25.     setTimeout(function(){
  26.        
  27.         var colourNames = ["white", "light grey", "dark grey", "black", "pink", "red", "orange", "brown", "yellow", "light green", "dark green", "light blue", "medium blue", "dark blue", "light purple", "dark purple"]
  28.    
  29.         var url = "https://rawgit.com/DollarAkshay/Miscelaneous/master/Reddit%20Place%20India%20Flag%20%2B%20Pakistan%20Bitmap.js";
  30.  
  31.         $.getScript(url)
  32.         .done(function( script, textStatus ) {
  33.            
  34.             console.log("Bitmap Loaded...");
  35.             var xhttp = new XMLHttpRequest();
  36.             xhttp.responseType = "arraybuffer";
  37.             xhttp.open("GET", "https://www.reddit.com/api/place/board-bitmap", true);
  38.             xhttp.onload = function(t) {
  39.                 var n = xhttp.response;
  40.                 var s = new Uint8Array(n, 4);
  41.                
  42.                 var canvas = [];
  43.                 for(var i=0; i<1000; i++){
  44.                     canvas.push([]);
  45.                     for(var j=0; j<1000; j++){
  46.                         canvas[i].push(0);
  47.                     }
  48.                 }
  49.                
  50.                 for(var i = 0; i<500000; i++){
  51.                     var x1 = (i*2+0)%1000, y1 = Math.floor((i*2+0)/1000);
  52.                     var x2 = (i*2+1)%1000, y2 = Math.floor((i*2+1)/1000);
  53.                     canvas[y1][x1] = s[i]>>>4;
  54.                     canvas[y2][x2] = s[i] & 15;
  55.                 }
  56.                
  57.                 var repairCells = [];
  58.                 for(var i=0; i<height; i++){
  59.                     for(var j=0; j<bitMap[i].length; j++){
  60.                         if( canvas[startY+i][startX+j] != bitMap[i][j]){
  61.                             repairCells.push([startX+j, startY+i]);
  62.                         }
  63.                     }
  64.                 }
  65.                
  66.                 if(repairCells.length){
  67.                     var cellIndex = Math.floor(Math.random() * repairCells.length);
  68.                     var cx = repairCells[cellIndex][0], cy = repairCells[cellIndex][1];
  69.                     var cellColour = bitMap[cy-startY][cx-startX];
  70.                     console.log("################################");
  71.                     console.log("Drawing at "+cx+", "+cy);
  72.                     console.log("Replacing '"+colourNames[canvas[cy][cx]]+"' with '"+colourNames[bitMap[cy-startY][cx-startX]]+"' colour");
  73.                     console.log("################################");
  74.                     console.log("");
  75.                    
  76.                     $.ajax({ url: "https://www.reddit.com/api/place/draw.json",type: "POST",
  77.                         headers: { "x-modhash": modhash },  data: { x: cx,y: cy, color: cellColour }
  78.                     })
  79.                     .done(function(data) {
  80.                         draw(data.wait_seconds);
  81.                     })
  82.                     .error(function(data){
  83.                         if(data == null || data.responseJSON == null || typeof data.responseJSON.wait_seconds === 'undefined' || data.responseJSON.wait_seconds == null){
  84.                             draw(60);
  85.                         }
  86.                         else{
  87.                             draw(data.responseJSON.wait_seconds);
  88.                         }
  89.                     }
  90.                     );
  91.                 }
  92.                 else{
  93.                     console.log("################################");
  94.                     console.log("Flag is Perfect. Not going to draw.");
  95.                     console.log("################################");
  96.                     draw(30);
  97.                 }
  98.                
  99.             }
  100.             xhttp.send();
  101.            
  102.         })
  103.         .fail(function( jqxhr, settings, exception ) {
  104.             console.log( "Triggered ajaxError handler." );
  105.             draw(30);
  106.         });
  107.     }, Math.abs(seconds) * 1000);
  108. }
  109. draw(0);
Add Comment
Please, Sign In to add comment