Diep.io build selector

At the beginning of the script are the builds, you can edit them or add more at any time. Stats should be added in sequence. Also, you can look into the browser console to see the build and stats arrangement. That's all, enjoy the game.

当前为 2023-06-06 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Diep.io build selector
  3. // @match *://diep.io/*
  4. // @version 1.3.1-logical
  5. // @description At the beginning of the script are the builds, you can edit them or add more at any time. Stats should be added in sequence. Also, you can look into the browser console to see the build and stats arrangement. That's all, enjoy the game.
  6. // @icon https://i.imgur.com/pvqsu5e.png
  7. // @license GNU GPLv3
  8. // @author LimeXator
  9. // @namespace https://greasyfork.org/users/1003196
  10. // ==/UserScript==
  11.  
  12. const builds = [
  13. {
  14. name: "Anni",
  15. values: [0, 2, 0, 7, 7, 7, 3, 7]
  16. },
  17. {
  18. name: "Sprayer",
  19. values: [0, 0, 0, 7, 7, 7, 7, 5]
  20. },
  21. {
  22. name: "Ram",
  23. values: [5, 7, 7, 0, 0, 0, 7, 7]
  24. },
  25. {
  26. name: "OverLord",
  27. values: [0, 5, 0, 7, 7, 7, 0, 7]
  28. },
  29. {
  30. name: "Trapper",
  31. values: [5, 7, 0, 0, 7, 7, 7, 0]
  32. },
  33. {
  34. name: "Necro",
  35. values: [0, 0, 0, 7, 6, 7, 6, 7]
  36. },
  37. {
  38. name: "Glass",
  39. values: [2,3,0,7,7,7,7,0,]
  40. },
  41. {
  42. name: "Tri-Angle",
  43. values: [4,4,4,0,7,7,7,0]
  44. },
  45. {
  46. name: "Assassin",
  47. values: [2, 3, 0, 7, 7, 7, 3, 4]
  48. },
  49. {
  50. name: "Factory",
  51. values: [0,0,0,5,7,7,7,7]
  52. }
  53. // Add more builds
  54. ];
  55.  
  56. function sendCommand(command) {
  57. input.execute(command);
  58. }
  59.  
  60. function convertBuildToString(build) {
  61. let result = "";
  62. for (let i = 0; i < build.length; i++) {
  63. for (let j = 0; j < build[i]; j++) {
  64. result += (i + 1).toString();
  65. }
  66. }
  67. return result;
  68. }
  69.  
  70. function optimizeBuildString(buildString) {
  71. // Create an array that will contain the number of occurrences of each digit in the buildString
  72. const count = new Array(8).fill(0);
  73. for (let i = 0; i < buildString.length; i++) {
  74. count[buildString[i] - 1]++;
  75. }
  76.  
  77. // Create a new result string that will contain all the digits in the buildString, shuffled randomly
  78. let result = "";
  79. while (result.length < buildString.length) {
  80. // Find the next most frequently occurring digit in the buildString
  81. let max = 0;
  82. let maxValue = null;
  83. for (let i = 0; i < count.length; i++) {
  84. if (count[i] > max) {
  85. max = count[i];
  86. maxValue = i + 1;
  87. }
  88. }
  89. // Add the next most frequently occurring digit to the result string
  90. result += maxValue.toString();
  91. count[maxValue - 1]--;
  92. }
  93.  
  94. // Return the result string
  95. return result;
  96. }
  97.  
  98. // If no such digit exists, add the next most frequently occurring digit to the result string
  99.  
  100. function selectBuild(build) {
  101. console.log(`Selecting build: ${build.name}`);
  102. console.log(build.values);
  103.  
  104. // Use optimizeBuildString to reorder stats
  105. const buildString = optimizeBuildString(convertBuildToString(build.values));
  106. sendCommand(`game_stats_build ${buildString}`);
  107. console.log(buildString);
  108. showBuilds();
  109. let values = document.querySelector(".build-values");
  110. if (values) {
  111. values.parentNode.removeChild(values);
  112. }
  113. }
  114.  
  115.  
  116. function pressKey(key) {
  117. const inputElement = document.querySelector("input");
  118. const event = new KeyboardEvent("keydown", {
  119. key,
  120. bubbles: true,
  121. cancelable: true
  122. });
  123. inputElement.dispatchEvent(event);
  124. }
  125.  
  126. function showBuilds() {
  127. // Select the menu element
  128. let menu = document.querySelector("#buildMenu");
  129.  
  130. // If the menu already exists, hide it
  131. if (menu) {
  132. menu.parentNode.removeChild(menu);
  133. return;
  134. }
  135.  
  136. // Create a new menu
  137. menu = document.createElement("div");
  138. menu.id = "buildMenu";
  139. menu.style.position = "fixed";
  140. menu.style.top = "50%";
  141. menu.style.right = "0%";
  142. menu.style.transform = "translate(-50%, -50%)";
  143. menu.style.backgroundColor = "rgba(0, 0, 0, 0.5)";
  144. menu.style.border = "1px solid grey";
  145. menu.style.padding = "5px";
  146. menu.style.textAlign = "center";
  147.  
  148. // Add a link to GreasyFork in the first item of the menu
  149. let item = document.createElement("div");
  150. item.classList.add("menu-item");
  151. let link = document.createElement("a");
  152. link.href = "https://greasyfork.org/cs/scripts/457172-diep-io-smart-build-selector";
  153. link.target = "_blank";
  154. link.innerHTML = "GreasyFork";
  155. link.style.textDecoration = "none";
  156. link.style.color = "white";
  157. item.appendChild(link);
  158. menu.appendChild(item);
  159.  
  160. // Add items to the menu
  161. builds.forEach(build => {
  162. let buildElement = document.createElement("div");
  163. buildElement.id = `build-${build.id}`;
  164. buildElement.classList.add("menu-item");
  165. buildElement.innerHTML = build.name;
  166. buildElement.style.cursor = "pointer";
  167. buildElement.style.fontFamily = "sans-serif";
  168. buildElement.addEventListener("mouseenter", () => showBuildValues(build));
  169. buildElement.addEventListener("mouseleave", () => {
  170. let values = document.querySelector(".build-values");
  171. if (values) {
  172. values.parentNode.removeChild(values);
  173. }
  174. });
  175. buildElement.addEventListener("click", () => selectBuild(build));
  176. menu.appendChild(buildElement);
  177. });
  178.  
  179. // Add the menu to the document
  180. document.body.appendChild(menu);
  181. }
  182. function showBuildValues(build) {
  183. // Select the values element
  184. let values = document.querySelector(".build-values");
  185.  
  186. // If the values element already exists, update its text
  187. if (values) {
  188. values.innerHTML = build.values;
  189. return;
  190. }
  191.  
  192. // Create a new values element
  193. values = document.createElement("div");
  194. values.classList.add("build-values");
  195. values.innerHTML = build.values;
  196. values.style.position = "fixed";
  197. values.style.top = "0";
  198. values.style.right = "0";
  199. values.style.backgroundColor = "rgba(0, 0, 0, 0.5)";
  200. values.style.color = "white";
  201. values.style.padding = "5px";
  202.  
  203. // Add the values element to the document
  204. document.body.appendChild(values);
  205. }
  206.  
  207. document.addEventListener("keydown", function(event) {
  208. // show the build menu when the "r" key is pressed
  209. if (event.key === "r") {
  210. showBuilds();
  211. let values = document.querySelector(".build-values");
  212. if (values) {
  213. values.parentNode.removeChild(values);
  214. }
  215. }
  216. });