SerlitePD

Primedice 4 Mass Tip Helper

Jul 17th, 2016
2,002
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. //  PD4 Tipping Utility
  3. //  Created by Serlite
  4. //
  5. //  Instructions:
  6. //  - Paste code below into console
  7. //  - Tipping controls will appear beneath chat interface
  8. //  - Each line is considered a separate tip; either specify only a username and the base
  9. //    amount will be used, or use the syntax "username value" to specify the tip amount:
  10. //      User1
  11. //      User2
  12. //      User3 0.005
  13. //      User4
  14. */
  15.  
  16. loadjQuery();
  17. initializeTipTools();
  18.  
  19. // Adds jQuery to document if needed
  20. function loadjQuery(){
  21.     if (!window.jQuery){
  22.         var script = document.createElement("SCRIPT");
  23.         script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js';
  24.         script.type = 'text/javascript';
  25.         document.getElementsByTagName("head")[0].appendChild(script);
  26.     }
  27. }
  28.  
  29. // Creates GUI for tipping utility
  30. function initializeTipTools(){
  31.     // Give time for jQuery to load
  32.     if (!window.jQuery){
  33.         setTimeout(initializeTipTools, 500);
  34.     }
  35.     else{
  36.         jQuery(".chat").append(
  37.             "<div style='padding:20px;flex-shrink:0;font-size:12px;'>"+
  38.                 "<p style='padding: 10px 0; font-size: 15px; border-top: 1px solid #EEE;'>Mass Tip Helper</p>"+
  39.                 "<textarea id='tip-name-input' style='border:1px solid #cfd8dc; border-radius:3px; width:100%; padding:11px 15px; font-size:12px;' placeholder='example_user_overridden 0.002'></textarea>"+
  40.                 "<label for='tip-amount-input' style='margin-top:10px;display:block;'>Base tip amount</label>"+
  41.                 "<input id='tip-amount-input' style='border:1px solid #cfd8dc; border-radius:3px; width:100%; padding:11px 15px; font-size:12px;' placeholder='0.00001000 min' type='text' />"+
  42.                 "<label for='tip-interval-input' style='margin-top:10px;display:block;'>Tip interval (seconds)</label>"+
  43.                 "<input id='tip-interval-input' style='border:1px solid #cfd8dc; border-radius:3px; width:100%; padding:11px 15px; font-size:12px;' placeholder='0' type='text' />"+
  44.                 "<label style='float:left;margin-top:21px;'>Private <input id='tip-private' style='vertical-align:middle;' type='checkbox' /></label>"+
  45.                 "<button id='tip-button' style='padding:10px 20px 12px; background:#4d5e80; box-shadow:inset 0 -2px 0 rgba(0,0,0,.2); color:#fff; border-radius:3px; font-weight:600; float:right; font-size:13px; margin-top:10px;'>Send Tips</button>"+
  46.             "</div>"
  47.         );
  48.  
  49.         jQuery("#tip-button").click(queueSendTips);
  50.     }
  51. }
  52.  
  53. function queueSendTips(){
  54.     var tips = getNameValuePairs();
  55.     var isPrivate = jQuery("#tip-private")[0].checked;
  56.     var tipInterval = parseFloat(jQuery("#tip-interval-input").val());
  57.     tipInterval = isNaN(tipInterval) ? 0 : tipInterval;
  58.  
  59.     // Early exit if no tips possible
  60.     if (tips.length == 0){
  61.         return;
  62.     }
  63.  
  64.     if (confirm("You are about to send a total of " + calculateTotalAmount(tips) + " BTC to " + tips.length + " users. Is this correct?")){
  65.         for (var i = 0; i < tips.length; i++){
  66.             // Self-invoking function so tips[i] doesn't fall out of scope for the setTimeout()
  67.             (function(i){
  68.                 setTimeout(function(){
  69.                     sendTip(tips[i], isPrivate);
  70.                 }, i * tipInterval * 1000);
  71.             })(i);
  72.         }
  73.     }
  74. }
  75.  
  76. function sendTip(tipObject, isPrivate){
  77.     if (isPrivate){
  78.         tipObject.hide = true;
  79.     }
  80.     try{
  81.         tipObject.amount = Math.ceil(tipObject.amount * 100000000); // I wish we could just express things in actual BTC for the request!
  82.        
  83.         jQuery.post("https://api.primedice.com/api/tip?access_token=" + localStorage.getItem('token'), tipObject, function(response) {
  84.             if (!response){
  85.                 console.log("Failed to tip " + tipObject.username + ": No response from server.");
  86.             }
  87.             else if (!response.user){
  88.                 console.log("Failed to tip " + tipObject.username + ": " + response);
  89.             }
  90.         });
  91.     }
  92.     catch(e){
  93.         console.log("Error: " + e);
  94.     }
  95. }
  96.  
  97. function getNameValuePairs(){
  98.     var baseAmount = jQuery("#tip-amount-input").val();
  99.     var nameValues = jQuery("#tip-name-input").val().split("\n");
  100.     var nameValuePairs = [];
  101.  
  102.     for (var i = 0; i < nameValues.length; i++){
  103.         // Check for empty input
  104.         if (nameValues[i] == ""){
  105.             continue;
  106.         }
  107.  
  108.         var valuePair = nameValues[i].split(" ");
  109.         var amountSupplied = valuePair.length > 1;
  110.         var tipAmount = amountSupplied ? valuePair[1] : baseAmount;
  111.  
  112.         // Some value checking so we don't bother tipping with invalid information
  113.         if (!isNumber(tipAmount)){
  114.             if (!amountSupplied){
  115.                 console.log("Invalid amount for " + valuePair[0] + ": No amount specified, and base amount (" + tipAmount + ") is missing/non-numeric.");
  116.             }
  117.             else{
  118.                 console.log("Invalid amount for " + valuePair[0] + ": Amount specified (" + tipAmount + ") is non-numeric.");
  119.             }
  120.             continue;
  121.         }
  122.  
  123.         nameValuePairs.push(
  124.             {
  125.                 username: valuePair[0],
  126.                 amount: tipAmount
  127.             }
  128.         );
  129.     }
  130.  
  131.     return nameValuePairs;
  132. }
  133.  
  134. function calculateTotalAmount(tips){
  135.     var total = 0;
  136.     for (var i = 0; i < tips.length; i++){
  137.         total += Number(tips[i].amount);
  138.     }
  139.     return total.toFixed(8);
  140. }
  141.  
  142. function isNumber(n) {
  143.   return !isNaN(parseFloat(n)) && isFinite(n);
  144. }
Advertisement
Add Comment
Please, Sign In to add comment