Advertisement
Guest User

Untitled

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