Tank-Randomizer

Bring an element of surprise to your tank customization experience with the Tank Randomizer,

  1. // ==UserScript==
  2. // @name Tank-Randomizer
  3. // @author kamarov
  4. // @description Bring an element of surprise to your tank customization experience with the Tank Randomizer,
  5. // @version 4.0.0
  6. // @namespace https://github.com/kamarov-therussiantank
  7. // @license GPL-3.0
  8. // @match https://*.tanktrouble.com/*
  9. // @run-at document-end
  10. // @grant GM_addStyle
  11. // @require https://update.greasyfork.org/scripts/482092/1297984/TankTrouble%20Development%20Library.js
  12. // @noframes
  13. // ==/UserScript==
  14.  
  15. GM_addStyle(`
  16. .randomize-button {
  17. margin-bottom: 10px;
  18. height: 20px;
  19. width: 100px;
  20. }
  21. .partSelectAccessory, .partSelectPaint {
  22. cursor: pointer;
  23. outline: none;
  24. width: 78%;
  25. border: 1px solid var(--jq-borderColorDefault);
  26. border-radius: 5px;
  27. background: var(--jq-bgColorDefault) var(--jq-widget-button-disabled-hovered-or-active-bg) 50% 50% repeat-x;
  28. font-weight: 600;
  29. font-size: 11px;
  30. color: #555;
  31. margin-bottom: 5px;
  32. padding: 3px;
  33. }
  34. .partSelectAccessory:hover {
  35. color: #1a1a1a;
  36. }
  37. .partSelectPaint:hover {
  38. color: #1a1a1a;
  39. }
  40. :root.dark .partSelectAccessory:hover {
  41. color: #fff;
  42. }
  43. :root.dark .partSelectPaint:hover {
  44. color: #fff;
  45. }
  46. :root.dark .partSelectAccessory {
  47. color: #e7e7e7;
  48. }
  49. :root.dark .partSelectPaint {
  50. color: #e7e7e7;
  51. }
  52. .partSelectSection {
  53. font-size: 12px;
  54. }
  55. .partTexts {
  56. font-family: TankTrouble;
  57. font-size: 12px;
  58. color: #e7c811;
  59. text-shadow: -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000, 1px 1px 0 #000;
  60. margin-bottom: 5px;
  61. margin-top: 3px;
  62. }
  63. `);
  64.  
  65. whenContentInitialized().then(() => {
  66. var id = Users.getAllPlayerIds()[0];
  67. var turret = [];
  68. var back = [];
  69. var barrel = [];
  70. var front = [];
  71. var treads = [];
  72. var colours = [];
  73. var baseColor = '';
  74.  
  75. function randomizeTurretPaint() {
  76. randomizeTurretColor();
  77. }
  78.  
  79. function randomizeTurretColor() {
  80. Backend.getInstance().setColour(
  81. function(result) {
  82. Users.updateUser(id, true, false);
  83. },
  84. null,
  85. null,
  86. id,
  87. 'turret',
  88. colours[Math.floor(Math.random() * colours.length)],
  89. Caches.getPlayerDetailsCache()
  90. );
  91. }
  92.  
  93. function randomizeBasePaint() {
  94. randomizeBaseColor();
  95. }
  96.  
  97. function randomizeBaseColor() {
  98. Backend.getInstance().setColour(
  99. function(result) {
  100. Users.updateUser(id, true, false);
  101. },
  102. null,
  103. null,
  104. id,
  105. 'base',
  106. colours[Math.floor(Math.random() * colours.length)],
  107. Caches.getPlayerDetailsCache()
  108. );
  109. }
  110.  
  111. function randomizeTreadsPaint() {
  112. randomizeTreadsColor();
  113. }
  114.  
  115. function randomizeTreadsColor() {
  116. Backend.getInstance().setColour(
  117. function(result) {
  118. Users.updateUser(id, true, false);
  119. },
  120. null,
  121. null,
  122. id,
  123. 'tread',
  124. colours[Math.floor(Math.random() * colours.length)],
  125. Caches.getPlayerDetailsCache()
  126. );
  127. }
  128.  
  129. function randomizeTurretAccessory() {
  130. randomizeTurretA();
  131. }
  132.  
  133. function randomizeTurretA() {
  134. Backend.getInstance().setAccessory(
  135. function(result) {
  136. Users.updateUser(id, true, false);
  137. },
  138. null,
  139. null,
  140. id,
  141. 'turret',
  142. turret[Math.floor(Math.random() * turret.length)],
  143. Caches.getPlayerDetailsCache()
  144. );
  145. }
  146.  
  147. function randomizeBarrelAccessory() {
  148. randomizeBarrelA();
  149. }
  150.  
  151. function randomizeBarrelA() {
  152. Backend.getInstance().setAccessory(
  153. function(result) {
  154. Users.updateUser(id, true, false);
  155. },
  156. null,
  157. null,
  158. id,
  159. 'barrel',
  160. barrel[Math.floor(Math.random() * barrel.length)],
  161. Caches.getPlayerDetailsCache()
  162. );
  163. }
  164.  
  165. function randomizeBackAccessory() {
  166. randomizeBackA();
  167. }
  168.  
  169. function randomizeBackA() {
  170. Backend.getInstance().setAccessory(
  171. function(result) {
  172. Users.updateUser(id, true, false);
  173. },
  174. null,
  175. null,
  176. id,
  177. 'back',
  178. back[Math.floor(Math.random() * back.length)],
  179. Caches.getPlayerDetailsCache()
  180. );
  181. }
  182.  
  183. function randomizeFrontAccessory() {
  184. randomizeFrontA();
  185. }
  186.  
  187. function randomizeFrontA() {
  188. Backend.getInstance().setAccessory(
  189. function(result) {
  190. Users.updateUser(id, true, false);
  191. },
  192. null,
  193. null,
  194. id,
  195. 'front',
  196. front[Math.floor(Math.random() * front.length)],
  197. Caches.getPlayerDetailsCache()
  198. );
  199. }
  200.  
  201. function randomizeAllPartsAccessories() {
  202. randomizeFrontAccessory();
  203. randomizeBackAccessory();
  204. randomizeTurretAccessory();
  205. randomizeBarrelAccessory();
  206. }
  207.  
  208. function randomizeAllPartsPaints() {
  209. randomizeTurretPaint();
  210. randomizeBasePaint();
  211. randomizeTreadsPaint();
  212. }
  213.  
  214. Backend.getInstance().getGarageContent(
  215. function(result) {
  216. var boxes = result['boxes'];
  217. for (var box in boxes) {
  218. var accessories = boxes[box]['accessories'];
  219. var sprays = boxes[box]['sprayCans'];
  220. for (var accessory in accessories) {
  221. var thing = accessories[accessory];
  222. if (thing['type'] == 'front') {
  223. front.push(thing['value']);
  224. }
  225. if (thing['type'] == 'back') {
  226. back.push(thing['value']);
  227. }
  228. if (thing['type'] == 'tread') {
  229. treads.push(thing['value']);
  230. }
  231. if (thing['type'] == 'barrel') {
  232. barrel.push(thing['value']);
  233. }
  234. if (thing['type'] == 'turret') {
  235. turret.push(thing['value']);
  236. }
  237. }
  238. for (var spray in sprays) {
  239. var thing = sprays[spray]['colour'];
  240. if (thing['type']) {
  241. colours.push(thing['rawValue']);
  242. }
  243. }
  244. }
  245. baseColor = colours[Math.floor(Math.random() * colours.length)];
  246. },
  247. function(res) {},
  248. function(res) {},
  249. id,
  250. Caches.getGarageContentCache()
  251. );
  252.  
  253. var snippet = $(`
  254. <div id="randomizerSnippet" class="snippet">
  255. <div class="header">Tank Randomizer</div>
  256. <hr>
  257. </div>
  258. `);
  259.  
  260. var content = $('<div></div>');
  261.  
  262. var accessoriesButton = $('<button class="randomize-button button" type="button" tabindex="-1">Randomize</button>');
  263. var barrelAccessoryButton = $('<button class="randomize-button button" type="button" tabindex="-1">Randomize</button>');
  264. var turretAccessoryButton = $('<button class="randomize-button button" type="button" tabindex="-1">Randomize</button>');
  265. var frontAccessoryButton = $('<button class="randomize-button button" type="button" tabindex="-1">Randomize</button>');
  266. var backAccessoryButton = $('<button class="randomize-button button" type="button" tabindex="-1">Randomize</button>');
  267. var paintButton = $('<button class="randomize-button button" type="button" tabindex="-1">Randomize</button>');
  268. var turretPaintButton = $('<button class="randomize-button button" type="button" tabindex="-1">Randomize</button>');
  269. var basePaintButton = $('<button class="randomize-button button" type="button" tabindex="-1">Randomize</button>');
  270. var treadsPaintButton = $('<button class="randomize-button button" type="button" tabindex="-1">Randomize</button>');
  271. var accessoryText = $('<div class="partTexts">Accessories</div>');
  272. var paintText = $('<div class="partTexts">Paints</div>');
  273.  
  274. const createNewWrapper = $('<div class="createNewWrapper"></div>');
  275. const accessoryPartSelect = $("<select class='partSelectAccessory'></select>");
  276. const paintPartSelect = $("<select class='partSelectPaint'></select>");
  277.  
  278. const allAccessoriesOption = $('<option value="allAccessory">All</option>');
  279. accessoryPartSelect.append(allAccessoriesOption);
  280. const barrelAccessoriesOption = $('<option value="barrelAccessory">Barrel</option>');
  281. accessoryPartSelect.append(barrelAccessoriesOption);
  282. const turretAccessoriesOption = $('<option value="turretAccessory">Turret</option>');
  283. accessoryPartSelect.append(turretAccessoriesOption);
  284. const frontAccessoriesOption = $('<option value="frontAccessory">Front</option>');
  285. accessoryPartSelect.append(frontAccessoriesOption);
  286. const backAccessoriesOption = $('<option value="backAccessory">Back</option>');
  287. accessoryPartSelect.append(backAccessoriesOption);
  288. const allPaintsOption = $('<option value="allPaints">All</option>');
  289. paintPartSelect.append(allPaintsOption);
  290. const turretPaintsOption = $('<option value="turretPaints">Turret</option>');
  291. paintPartSelect.append(turretPaintsOption);
  292. const basePaintsOption = $('<option value="basePaints">Base</option>');
  293. paintPartSelect.append(basePaintsOption);
  294. const treadsPaintsOption = $('<option value="treadsPaints">Treads</option>');
  295. paintPartSelect.append(treadsPaintsOption);
  296.  
  297. accessoriesButton.on('mouseup', () => randomizeAllPartsAccessories());
  298. barrelAccessoryButton.on('mouseup', () => randomizeBarrelAccessory());
  299. turretAccessoryButton.on('mouseup', () => randomizeTurretAccessory());
  300. frontAccessoryButton.on('mouseup', () => randomizeFrontAccessory());
  301. backAccessoryButton.on('mouseup', () => randomizeBackAccessory());
  302. paintButton.on('mouseup', () => randomizeAllPartsPaints());
  303. turretPaintButton.on('mouseup', () => randomizeTurretPaint());
  304. basePaintButton.on('mouseup', () => randomizeBasePaint());
  305. treadsPaintButton.on('mouseup', () => randomizeTreadsPaint());
  306.  
  307. // Function to toggle buttons based on selection
  308. function toggleButtonsBasedOnSelection() {
  309. const selectedAccessoryValue = accessoryPartSelect.val();
  310. const selectedPaintValue = paintPartSelect.val();
  311.  
  312. // Hide all buttons
  313. accessoriesButton.show();
  314. barrelAccessoryButton.hide();
  315. turretAccessoryButton.hide();
  316. frontAccessoryButton.hide();
  317. backAccessoryButton.hide();
  318. paintButton.show();
  319. turretPaintButton.hide();
  320. basePaintButton.hide();
  321. treadsPaintButton.hide();
  322.  
  323. // Show accessory buttons based on selection
  324. if (selectedAccessoryValue === "allAccessories") {
  325. accessoriesButton.show();
  326. } else if (selectedAccessoryValue === "barrelAccessory") {
  327. barrelAccessoryButton.show();
  328. accessoriesButton.hide();
  329. } else if (selectedAccessoryValue === "turretAccessory") {
  330. turretAccessoryButton.show();
  331. accessoriesButton.hide();
  332. } else if (selectedAccessoryValue === "frontAccessory") {
  333. frontAccessoryButton.show();
  334. accessoriesButton.hide();
  335. } else if (selectedAccessoryValue === "backAccessory") {
  336. backAccessoryButton.show();
  337. accessoriesButton.hide();
  338. }
  339.  
  340. // Show paint buttons based on selection
  341. if (selectedPaintValue === "allPaints") {
  342. paintButton.show();
  343. } else if (selectedPaintValue === "turretPaints") {
  344. turretPaintButton.show();
  345. paintButton.hide();
  346. } else if (selectedPaintValue === "basePaints") {
  347. basePaintButton.show();
  348. paintButton.hide();
  349. } else if (selectedPaintValue === "treadsPaints") {
  350. treadsPaintButton.show();
  351. paintButton.hide();
  352. }
  353. }
  354.  
  355. // Event listeners for selection changes
  356. accessoryPartSelect.on('change', toggleButtonsBasedOnSelection);
  357. paintPartSelect.on('change', toggleButtonsBasedOnSelection);
  358.  
  359. // Append elements to the DOM
  360. createNewWrapper.append(accessoryText);
  361. createNewWrapper.append(accessoryPartSelect);
  362. createNewWrapper.append(accessoriesButton);
  363. createNewWrapper.append(barrelAccessoryButton);
  364. createNewWrapper.append(turretAccessoryButton);
  365. createNewWrapper.append(frontAccessoryButton);
  366. createNewWrapper.append(backAccessoryButton);
  367. createNewWrapper.append(paintText);
  368. createNewWrapper.append(paintPartSelect);
  369. createNewWrapper.append(paintButton);
  370. createNewWrapper.append(turretPaintButton);
  371. createNewWrapper.append(basePaintButton);
  372. createNewWrapper.append(treadsPaintButton);
  373.  
  374. content.append(createNewWrapper);
  375.  
  376. snippet.append(content);
  377. $('#secondaryContent').append(snippet);
  378.  
  379. // Initial toggle based on current selection values
  380. $(document).ready(function() {
  381. toggleButtonsBasedOnSelection();
  382. });
  383.  
  384. function getRandomColorFromGarage() {
  385. return colours[Math.floor(Math.random() * colours.length)];
  386. }
  387. });