Guest User

Untitled

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