Advertisement
Ayasee

Habitica Buff Script

Dec 23rd, 2020 (edited)
876
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function schedulePartyBuff() {
  2.   var habId = "id";
  3.   var habToken = "token";
  4.   var threshold = 200; // when your mana is more than this number, it will buff your party until this number is reached again
  5.   var sleepTime = 30000; // pause in the loop for 30000 milliseconds (30 seconds); this is to avoid the servers being overloaded
  6.  
  7.   var paramsTemplateGet = {
  8.     "method" : "get",
  9.     "headers" : {
  10.       "x-api-user" : habId,
  11.       "x-api-key" : habToken    
  12.     }
  13.   }
  14.  
  15.   // get class and type of buff
  16.   var response = UrlFetchApp.fetch("https://habitica.com/api/v3/user?userFields=flags.classSelected,stats", paramsTemplateGet);
  17.   response = JSON.parse(response);
  18.  
  19.   if (response.data.flags.classSelected && response.data.stats.mp > threshold) {
  20.       var skillId;
  21.     var cost;
  22.  
  23.     switch(response.data.stats.class) {
  24.       case "warrior":
  25.         skillId = "valorousPresence"; // STR option
  26.         cost = 20;
  27.         // skillId = "intimidate"; // CON option
  28.         // cost = 15;
  29.        
  30.         break;
  31.       case "rogue":
  32.         skillId = "toolsOfTrade" // PER
  33.         cost = 25;
  34.        
  35.         break;
  36.       case "wizard":
  37.         skillId = "earth";
  38.         cost = 35;
  39.        
  40.         break;
  41.       case "healer":
  42.         if (response.data.stats.hp < 20) {
  43.           skillId = "healAll";
  44.         } else {
  45.           skillId = "protectAura"; // CON
  46.         }
  47.          
  48.         cost = 30;
  49.     }
  50.      
  51.     // a Google script may not take more time than 6 minutes (360000 milliseconds), so we need to limit the maximum amount of buffs, or else you'll get errors
  52.     var maxBuffs = (360000 / sleepTime) - 1;
  53.        
  54.     var buffs = Math.trunc((response.data.stats.mp - threshold) / cost);
  55.    
  56.     if (buffs > maxBuffs) {
  57.       buffs = maxBuffs;
  58.     }
  59.    
  60.     var url = "https://habitica.com/api/v3/user/class/cast/" + skillId
  61.      
  62.     var paramsTemplate = {
  63.       "method" : "post",
  64.       "headers" : {
  65.          "x-api-user" : habId,
  66.          "x-api-key" : habToken
  67.       }
  68.     }
  69.     var params = paramsTemplate;
  70.    
  71.     for (var i = 0; i < buffs; i++) {
  72.       UrlFetchApp.fetch(url, params)
  73.       Utilities.sleep(sleepTime);
  74.      
  75.       // for healers: switch from healing to buffing after a few heals (default: set to 3 for 4 heals)
  76.       if (skillId == "healAll" && i == 3) { skillId = "protectAura"; }
  77.     }
  78.   }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement