Advertisement
Guest User

Untitled

a guest
Apr 9th, 2017
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  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. while(true) {
  128. _x = randomInteger(0, canvas.width);
  129. _y = randomInteger(0, canvas.height);
  130. var coords = {x: _x, y: _y};
  131.  
  132. if(isSamePixelColor(coords)){
  133. //console.log("same color, skip");
  134. }
  135. else{
  136.  
  137. var color_id = getColorId(coords);
  138. if(color_id < 0) continue;
  139.  
  140. console.log("drawing " + title + " coords " + " x:" + (parseInt(x) + parseInt(coords["x"])) + " y:" + (parseInt(y) + parseInt(coords["y"])));
  141.  
  142. App.switchColor(color_id);
  143. App.attemptPlace ( (parseInt(x) + parseInt(coords["x"])), (parseInt(y) + parseInt(coords["y"])) );
  144. return 20;
  145. }
  146. }
  147.  
  148. console.log(title + " is correct");
  149. return -1;
  150. }
  151.  
  152. function drawImage(){
  153. if(image_loaded_flag){
  154. return tryToDraw();
  155. }
  156. return -1;
  157. }
  158.  
  159. function isReady(){
  160. return image_loaded_flag;
  161. }
  162.  
  163. img.onload = function(){
  164. canvas.width = img.width;
  165. canvas.height = img.height;
  166. image = canvas.getContext('2d');
  167. image.drawImage(img, 0, 0, img.width, img.height);
  168.  
  169. image_loaded_flag = true;
  170. };
  171.  
  172.  
  173.  
  174. return {
  175. drawImage: drawImage,
  176. isReady: isReady
  177. }
  178. };
  179.  
  180.  
  181. var painters = [];
  182. for(var i = 0; i < images.length; i++){
  183. painters[i] = Painter(images[i]);
  184. }
  185.  
  186. function draw(){
  187. var timer = (App.cooldown-(new Date).getTime())/1E3;
  188. if(0<timer){
  189. console.log("timer: " + timer);
  190. setTimeout(draw, 1000);
  191. }
  192. else{
  193. for(var i = 0; i < painters.length; i++){
  194. if(painters[i].isReady()){
  195. var result = painters[i].drawImage();
  196.  
  197. if(result > 0){
  198. setTimeout(draw, result*1000);
  199. return;
  200. }
  201. else{
  202. continue;
  203. }
  204. }
  205. else{
  206. continue;
  207. }
  208. }
  209. setTimeout(draw, 3000);
  210. }
  211.  
  212. return;
  213. }
  214.  
  215. draw();
  216. }
  217.  
  218. var images = [
  219. {title: "YOBA",
  220. x: 148,
  221. y: 527,
  222. image: "http://i.imgur.com/cRw6sal.png"
  223. }
  224. ]
  225.  
  226. AutoPXLS(images);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement