Advertisement
Guest User

Untitled

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