IdlePixel Bait Thrower

Opens x amount of bait at once and collates the loot

  1. // ==UserScript==
  2. // @name IdlePixel Bait Thrower
  3. // @namespace lbtechnology.info
  4. // @version 1.0.5
  5. // @description Opens x amount of bait at once and collates the loot
  6. // @author Lux-Ferre
  7. // @license MIT
  8. // @match *://idle-pixel.com/login/play*
  9. // @grant none
  10. // @require https://greatest.deepsurf.us/scripts/441206-idlepixel/code/IdlePixel+.js?anticache=20220905
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. class BaitPlugin extends IdlePixelPlusPlugin {
  17. constructor() {
  18. super("baitplugin", {
  19. about: {
  20. name: GM_info.script.name,
  21. version: GM_info.script.version,
  22. author: GM_info.script.author,
  23. description: GM_info.script.description
  24. },
  25. });
  26. this.tracking_bait = false
  27. this.total_bait_loots = 0
  28. this.bait_counter = 0
  29. this.bait_loot = {}
  30. }
  31.  
  32. onLogin(){
  33. $(`itembox[data-item="bait"]`).attr("onClick", "IdlePixelPlus.plugins.baitplugin.open_input_dialogue('BAIT')")
  34. $(`itembox[data-item="super_bait"]`).attr("onClick", "IdlePixelPlus.plugins.baitplugin.open_input_dialogue('SUPER_BAIT')")
  35. $(`itembox[data-item="mega_bait"]`).attr("onClick", "IdlePixelPlus.plugins.baitplugin.open_input_dialogue('MEGA_BAIT')")
  36. }
  37. onMessageReceived(data){
  38. if(this.tracking_bait && data.startsWith("OPEN_LOOT_DIALOGUE")){
  39. const values = data.split("=")[1]
  40. const values_array = values.split("~")
  41. const items = this.parseItemData(values_array)
  42. this.bait_loot = this.addToLoot(this.bait_loot, items)
  43. this.bait_counter++;
  44. if (this.bait_counter>=this.total_bait_loots){
  45. this.tracking_bait = false
  46. this.createLootPopup()
  47. }
  48. }
  49. }
  50. open_input_dialogue(bait_type){
  51. const prettyBaitName = bait_type.toLowerCase().split('_').map(word => word.charAt(0).toUpperCase() + word.slice(1)).join(' ');
  52. document.getElementById("modal-input-image").src = get_image(`images/${bait_type.toLowerCase()}.png`);
  53. document.getElementById("modal-input-description").innerHTML = `How many ${prettyBaitName} do you want to throw?`;
  54. document.getElementById("modal-input-text").value = 0;
  55. document.getElementById("modal-input-button-text").innerHTML = "Throw!";
  56. document.getElementById("modal-input-button").onclick = function()
  57. {
  58. IdlePixelPlus.plugins.baitplugin.throwBait(bait_type, parseInt(document.getElementById("modal-input-text").value));
  59. }
  60. Modals.toggle("modal-item-input");
  61. }
  62. throwBait(bait_type, num){
  63. this.bait_counter = 0;
  64. this.bait_loot = {}
  65. this.tracking_bait = true
  66.  
  67. const currentBait = window[`var_${bait_type.toLowerCase()}`]
  68.  
  69. if(num > currentBait){
  70. this.total_bait_loots = currentBait
  71. } else {
  72. this.total_bait_loots = num
  73. }
  74. for (let i = 0; i < this.total_bait_loots; i++) {
  75. websocket.send(`THROW_${bait_type}`);
  76. }
  77. }
  78. addToLoot(totalLoot, newLoot){
  79. for (let [itemName, value] of Object.entries(newLoot)) {
  80. if (totalLoot.hasOwnProperty(itemName)){
  81. totalLoot[itemName].number = totalLoot[itemName].number + value.number
  82. } else {
  83. totalLoot[itemName] = value
  84. }
  85. }
  86. return totalLoot
  87. }
  88. parseItemData(values_array){
  89. const items = {}
  90. let background = ""
  91. for(let i = 2; i < values_array.length; i+=0){
  92. const image = values_array[i];
  93. i++;
  94. let [number, ...label] = values_array[i].split(" ");
  95. number = parseInt(number)
  96. label = label.join(" ")
  97. i++;
  98. background = values_array[i];
  99. i++;
  100. items[image] = {
  101. number: number,
  102. label: label,
  103. background: background
  104. }
  105. }
  106. return items
  107. }
  108. createLootPopup(){
  109. const images = [];
  110. const labels = [];
  111. const background = [];
  112. for (let [itemName, value] of Object.entries(this.bait_loot)){
  113. images.push(itemName);
  114. const newLabel = `${value.number} ${value.label}`
  115. labels.push(newLabel);
  116. background.push(value.background);
  117. }
  118.  
  119. this.open_loot_dialogue(images, labels, background);
  120. }
  121. open_loot_dialogue(loot_images_array, loot_labels_array, loot_background_color_array){
  122. const loot_body = document.getElementById("modal-loot-body");
  123. let html = "";
  124. for(let i = 0; i < loot_images_array.length; i++)
  125. {
  126. let image = loot_images_array[i];
  127. let label = loot_labels_array[i];
  128. let background_color = loot_background_color_array[i];
  129. if(!isNaN(label))
  130. label = "+" + format_number(label);
  131. if(label.endsWith("(NEW)"))
  132. {
  133. label = label.substring(0, label.length-5);
  134. label += " <img class='blink' src='https://idlepixel.s3.us-east-2.amazonaws.com/images/new.png' />"
  135. }
  136. if(label.endsWith("(UNIQUE)"))
  137. {
  138. label = label.substring(0, label.length-8);
  139. label += " <img class='blink' src='https://idlepixel.s3.us-east-2.amazonaws.com/images/unique.png' />"
  140. }
  141. html += "<div class='loot' style='background-color:"+background_color+"'>";
  142. html += "<img src='https://idlepixel.s3.us-east-2.amazonaws.com/"+image+"' class='w50 me-3' />";
  143. html += label;
  144. html += "</div>";
  145. }
  146. loot_body.innerHTML = html;
  147. if($('#modal-loot:visible').length == 0){
  148. Modals.toggle("modal-loot");
  149. }
  150. }
  151. }
  152.  
  153. const plugin = new BaitPlugin();
  154. IdlePixelPlus.registerPlugin(plugin);
  155.  
  156. })();