Advertisement
Guest User

Untitled

a guest
Apr 11th, 2017
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function AutoPXLS(images){
  2. //
  3.  
  4.   function shuffle(array) {
  5.     var currentIndex = array.length, temporaryValue, randomIndex;
  6.  
  7.     // While there remain elements to shuffle...
  8.     while (0 !== currentIndex) {
  9.  
  10.       // Pick a remaining element...
  11.       randomIndex = Math.floor(Math.random() * currentIndex);
  12.       currentIndex -= 1;
  13.  
  14.       // And swap it with the current element.
  15.       temporaryValue = array[currentIndex];
  16.       array[currentIndex] = array[randomIndex];
  17.       array[randomIndex] = temporaryValue;
  18.     }
  19.  
  20.     return array;
  21.   }
  22.  
  23.   images = shuffle(images);
  24.  
  25. // ===
  26.  
  27.   if (Notification.permission !== "granted")
  28.     Notification.requestPermission();
  29.  
  30.   var om = App.socket.onmessage;
  31.  
  32.   App.socket.onmessage = function(message){
  33.     var m = JSON.parse(message.data);
  34.  
  35.     if(m.type == "captcha_required"){
  36.       if (Notification.permission !== "granted")
  37.         Notification.requestPermission();
  38.       else {
  39.         var notification = new Notification('Notification title', {
  40.           body: "Hey there! Enter the captcha!",
  41.         });
  42.       }
  43.     }
  44.  
  45.     om(message);
  46.   }
  47. //
  48.  
  49.  
  50.  
  51.   var Painter = function(config){
  52.     var board = document.getElementById("board").getContext('2d');
  53.     var title = config.title || "unnamed";
  54.  
  55.     var img = new Image();
  56.     img.crossOrigin = "anonymous";
  57.     img.src = config.image;
  58.     var x = config.x;
  59.     var y = config.y;
  60.  
  61.     var canvas = document.createElement('canvas');
  62.     var image;
  63.  
  64.     var image_loaded_flag = false;
  65.  
  66.  
  67.     function isSamePixelColor(coords){
  68.       var board_pixel = board.getImageData((parseInt(x) + parseInt(coords["x"])), (parseInt(y) + parseInt(coords["y"])), 1, 1).data;
  69.       var image_pixel = image.getImageData(coords["x"], coords["y"], 1, 1).data;
  70.  
  71.       if(image_pixel[3] <= 127) return true;
  72.  
  73.       for(var i = 0; i < 3; i++){
  74.         if(board_pixel[i] != image_pixel[i]) return false;
  75.       }
  76.       return true;
  77.     }
  78.  
  79.     function getColorId(coords){
  80.       var pixel = image.getImageData(coords["x"], coords["y"], 1, 1).data;
  81.       var colors = [
  82.         [255,255,255],
  83.         [228,228,228],
  84.         [136,136,136],
  85.         [34,34,34],
  86.         [255,167,209],
  87.         [229,0,0],
  88.         [229,149,0],
  89.         [160,106,66],
  90.         [229,217,0],
  91.         [148,224,68],
  92.         [2,190,1],
  93.         [0,211,221],
  94.         [0,131,199],
  95.         [0,0,234],
  96.         [207,110,228],
  97.         [130,0,128]
  98.       ];
  99.  
  100.       var color_id = -1;
  101.       var flag = false;
  102.       for(var i = 0; i < colors.length; i++){
  103.         flag = true;
  104.         for(var j = 0; j < 3; j++){
  105.           if(pixel[j] != colors[i][j]){
  106.             flag = false;
  107.             break;
  108.           }
  109.         }
  110.         if(flag){
  111.           color_id = i;
  112.           break;
  113.         }
  114.       }
  115.       if(color_id < 0)
  116.         console.log("pixel at x:" + coords.x + " y: " + coords.y + " has incorrect color.");
  117.      
  118.       return color_id;
  119.     }
  120.  
  121.     function tryToDraw(){
  122.       function randomInteger(min, max) {
  123.         var rand = min - 0.5 + Math.random() * (max - min + 1)
  124.         rand = Math.round(rand);
  125.         return rand;
  126.       }
  127.       var pixelCountX2 = canvas.width * canvas.height * 2;
  128.       var counter = 0;
  129.       while(true) {
  130.         _x = randomInteger(0, canvas.width);
  131.         _y = randomInteger(0, canvas.height);
  132.         var coords = {x: _x, y: _y};
  133.  
  134.         if(counter > pixelCountX2) {
  135.           for(var _y = 0; _y < canvas.height; _y++){
  136.             for(var _x = 0; _x < canvas.width; _x++){
  137.               var coords = {x: _x, y: _y};
  138.  
  139.               if(isSamePixelColor(coords)){
  140.                 //console.log("same color, skip");
  141.               }
  142.               else{
  143.                 var color_id = getColorId(coords);
  144.                 if(color_id < 0) continue;
  145.         console.clear();      
  146.                 console.log("drawing not random: " + title + " coords " + " x:" + (parseInt(x) + parseInt(coords["x"])) + " y:" + (parseInt(y) + parseInt(coords["y"])) + " color: " + color_id);
  147.        
  148.                 App.switchColor(color_id);
  149.                 App.attemptPlace ( (parseInt(x) + parseInt(coords["x"])), (parseInt(y) + parseInt(coords["y"])) );
  150.                 return 20;
  151.               }
  152.             }
  153.           }
  154.           console.log(title + " is correct");
  155.           return -1;
  156.         }
  157.           if(isSamePixelColor(coords)){
  158.               //console.log("same color, skip");
  159.               counter++;
  160.           }
  161.           else{
  162.             var color_id = getColorId(coords);
  163.             if(color_id < 0) continue;
  164.         console.clear();
  165.             console.log("drawing random: " + title + " coords " + " x:" + (parseInt(x) + parseInt(coords["x"])) + " y:" + (parseInt(y) + parseInt(coords["y"])) + " color: " + color_id);
  166.  
  167.             App.switchColor(color_id);
  168.             App.attemptPlace ( (parseInt(x) + parseInt(coords["x"])), (parseInt(y) + parseInt(coords["y"])) );
  169.             return 20;
  170.           }
  171.       }
  172.     }
  173.  
  174.     function drawImage(){
  175.       if(image_loaded_flag){
  176.         return tryToDraw();
  177.       }
  178.       return -1;
  179.     }
  180.  
  181.     function isReady(){
  182.       return image_loaded_flag;
  183.     }
  184.  
  185.     img.onload = function(){
  186.       canvas.width = img.width;
  187.       canvas.height = img.height;
  188.       image = canvas.getContext('2d');
  189.       image.drawImage(img, 0, 0, img.width, img.height);
  190.  
  191.       image_loaded_flag = true;
  192.     };
  193.  
  194.  
  195.  
  196.     return {
  197.       drawImage: drawImage,
  198.       isReady: isReady
  199.     }
  200.   };
  201.  
  202.  
  203.   var painters = [];
  204.   for(var i = 0; i < images.length; i++){
  205.     painters[i] = Painter(images[i]);
  206.   }
  207.  
  208.   function draw(){
  209.     var timer = (App.cooldown-(new Date).getTime())/1E3;
  210.     if(0<timer){
  211.       console.log("timer: " + timer);
  212.       setTimeout(draw, 1000);
  213.     }
  214.     else{
  215.       for(var i = 0; i < painters.length; i++){
  216.         if(painters[i].isReady()){
  217.           var result = painters[i].drawImage();
  218.  
  219.           if(result > 0){
  220.             setTimeout(draw, result*1000);
  221.             return;
  222.           }
  223.           else{
  224.             continue;
  225.           }
  226.         }
  227.         else{
  228.           continue;
  229.         }
  230.       }
  231.       setTimeout(draw, 3000);
  232.     }
  233.  
  234.     return;
  235.   }
  236.  
  237.   draw();
  238. }
  239.  
  240.   var images = [
  241.     {
  242.     title: "ragnarok",
  243.     x: 1000,    
  244.     y: 580,    
  245.     image: "http://i.imgur.com/eZ7po5a.png"
  246.     }
  247.     ]
  248.  
  249.     AutoPXLS(images);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement