Tank-Randomizer

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

当前为 2024-11-27 提交的版本,查看 最新版本

  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 3.0.0
  6. // @namespace https://github.com/kamarov-therussiantank
  7. // @license GPL-3.0
  8. // @match https://*.tanktrouble.com/*
  9. // @desc Randomizes your tank in just one click of a button.
  10. // @run-at document-end
  11. // @grant GM_addStyle
  12. // @require https://update.greasyfork.org/scripts/482092/1297984/TankTrouble%20Development%20Library.js
  13. // @noframes
  14. // ==/UserScript==
  15.  
  16. GM_addStyle(`
  17. .randomize-button {
  18. margin-bottom: 10px;
  19. height: 20px;
  20. width: 100px;
  21. }
  22. `);
  23.  
  24. whenContentInitialized().then(() => {
  25. var id = Users.getAllPlayerIds()[0];
  26. var turret = [];
  27. var back = [];
  28. var barrel = [];
  29. var front = [];
  30. var colours = [];
  31. var baseColor = '';
  32.  
  33.  
  34. // List of LockedAccessories
  35. const backAccessories = ['27', '28', '29', '30', '31', '32'];
  36. const frontAccessories = ['27', '28', '29', '30'];
  37. const turretAccessories = ['27'];
  38. const barrelAccessories = ['27', '28', '29', '30', '31', '32'];
  39.  
  40. // Store objects
  41. var lockedAccessories = {
  42. back: backAccessories,
  43. front: frontAccessories,
  44. turret: turretAccessories,
  45. barrel: barrelAccessories
  46. };
  47.  
  48. function randomizeTankcessoriesA() {
  49. // Function to apply a random accessory while considering locked accessories
  50. function applyRandomAccessory(part, availableAccessories) {
  51. let currentAccessory = getCurrentAccessory(part);
  52. const lockedForPart = lockedAccessories[part] || [];
  53. if (lockedForPart.includes(currentAccessory)) {
  54. console.log(`The ${part} accessory (ID: ${currentAccessory}) is locked, no randomization will occur.`);
  55. return;
  56. }
  57.  
  58.  
  59. // Filter out the locked accessories from the available ones for randomization
  60. const available = availableAccessories.filter(accessory => !lockedForPart.includes(accessory));
  61. if (available.length > 0) {
  62. const randomAccessory = available[Math.floor(Math.random() * available.length)];
  63. Backend.getInstance().setAccessory(
  64. function (result) {
  65. Users.updateUser(id, true, false);
  66. },
  67. null,
  68. null,
  69. id,
  70. part,
  71. randomAccessory,
  72. Caches.getPlayerDetailsCache()
  73. );
  74. }
  75. }
  76.  
  77. // Get the currently equipped accessory for a given part
  78. function getCurrentAccessory(part) {
  79. switch (part) {
  80. case 'back':
  81. return ['27', '28', '29', '30', '31', '32'];
  82. case 'front':
  83. return ['27', '28', '29', '30'];
  84. case 'turret':
  85. return ['27'];
  86. case 'barrel':
  87. return ['27', '28', '29', '30', '31', '32'];
  88. default:
  89. return null;
  90. }
  91. }
  92.  
  93. // Apply accessories, avoiding locked ones for each part
  94. applyRandomAccessory('back', back);
  95. applyRandomAccessory('turret', turret);
  96. applyRandomAccessory('front', front);
  97. applyRandomAccessory('barrel', barrel);
  98. }
  99.  
  100. function randomizePaint() {
  101. randomizePaintC();
  102. }
  103.  
  104. function randomizePaintC() {
  105. var selectedBaseColor = getRandomColorFromGarage();
  106. var selectedTurretColor = getRandomColorFromGarage();
  107. var selectedTreadColor = getRandomColorFromGarage();
  108.  
  109. function setColorForPart(part, color) {
  110. Backend.getInstance().setColour(
  111. function (result) {
  112. Users.updateUser(id, true, false);
  113. },
  114. function (result) { },
  115. function (result) { },
  116. id,
  117. part,
  118. color,
  119. Caches.getPlayerDetailsCache()
  120. );
  121. }
  122.  
  123. setColorForPart('base', selectedBaseColor);
  124. setColorForPart('turret', selectedTurretColor);
  125. setColorForPart('tread', selectedTreadColor);
  126. }
  127.  
  128. Backend.getInstance().getGarageContent(
  129. function (result) {
  130. boxes = result['boxes'];
  131. for (box in boxes) {
  132. accessories = boxes[box]['accessories'];
  133. sprays = boxes[box]['sprayCans'];
  134. for (accessory in accessories) {
  135. thing = accessories[accessory];
  136. if (thing['type'] == 'front') {
  137. front.push(thing['value']);
  138. }
  139. if (thing['type'] == 'back') {
  140. back.push(thing['value']);
  141. }
  142. if (thing['type'] == 'barrel') {
  143. barrel.push(thing['value']);
  144. }
  145. if (thing['type'] == 'turret') {
  146. turret.push(thing['value']);
  147. }
  148. }
  149. for (spray in sprays) {
  150. thing = sprays[spray]['colour'];
  151. if (thing['type']) {
  152. colours.push(thing['rawValue']);
  153. }
  154. }
  155. }
  156.  
  157. // Randomly select the base color
  158. baseColor = colours[Math.floor(Math.random() * colours.length)];
  159. },
  160. function (res) { },
  161. function (res) { },
  162. id,
  163. Caches.getGarageContentCache()
  164. );
  165.  
  166. var snippet = $(`
  167. <div id="randomizerSnippet" class="snippet">
  168. <div class="header">Tank Randomizer</div>
  169. <hr>
  170. <div class="header" style="color: #e7c811; text-shadow: -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000, 1px 1px 0 #000;">Randomize</div>
  171. </div>
  172. `);
  173. var content = $('<div></div>');
  174. var accessoriesButton = $('<button class="randomize-button button" type="button" tabindex="-1">Accessories</button>');
  175. var paintsButton = $('<button class="randomize-button button" type="button" tabindex="-1">Paints</button>');
  176.  
  177. accessoriesButton.on('mouseup', () => randomizeTankcessoriesA());
  178. paintsButton.on('mouseup', () => randomizePaintC());
  179.  
  180. content.append([accessoriesButton, paintsButton]);
  181. snippet.append(content);
  182. $('#secondaryContent').append(snippet);
  183.  
  184. function getRandomColorFromGarage() {
  185. return colours[Math.floor(Math.random() * colours.length)];
  186. }
  187. });