// This code is licensed under the same terms as Habitica: // https://raw.githubusercontent.com/HabitRPG/habitrpg/develop/LICENSE /* ========================================== */ /* [Users] Required script data to fill in */ /* ========================================== */ const USER_ID = "PasteYourUserIdHere" const API_TOKEN = "PasteYourApiTokenHere" // Do not share this to anyone const WEB_APP_URL = "PasteGeneratedWebAppUrlHere" /* ========================================== */ /* [Users] Required customizations to fill in */ /* ========================================== */ const CREATE_DRAINING_POTION = 1 // Change this 1 to a 0 if you don't want the Draining Potion (you lose Mana) const CREATE_FATIGUING_POTION = 1 // Change this 1 to a 0 if you don't want the Fatiguing Potion (you lose Health) const CREATE_STUPEFYING_POTION = 1 // Change this 1 to a 0 if you don't want the Stupefying Potion (you lose Experience) /* ========================================== */ /* [Users] Optional customizations to fill in */ /* ========================================== */ // Do you want to get private message notifications? (examples include if you're already at max Mana or exceeded the limit for daily potion usage) // If you don't want them, change the 1 to a 0 in the line below const NOTIFICATIONS_ON = 1 /* ========================================== */ /* [Users] Do not edit code below this line */ /* ========================================== */ const AUTHOR_ID = "0034eb14-b4d8-494e-8386-d3f33cff7922" const SCRIPT_NAME = "Anti-Potions" const HEADERS = { "x-client" : AUTHOR_ID + " - " + SCRIPT_NAME, "x-api-user" : USER_ID, "x-api-key" : API_TOKEN, } const scriptProperties = PropertiesService.getScriptProperties(); // Constants can have properties changed // Parts of messages that get reused const NOTES_START = "Pay " const NOTES_MID1 = ", gain " const NOTES_MID2 = " GP (Instant Use). Maximum " const NOTES_END = " per day." const MSG_LEVEL_LOCK_START = "You must be at least level " const MSG_LEVEL_LOCK_END = " to use this " const MSG_LEVEL_LOCK_END_POTION = MSG_LEVEL_LOCK_END + "potion." const MSG_DAILY_USAGE_EXCEEDED_START = "You can only use this " const MSG_DAILY_USAGE_EXCEEDED_START_POTION = MSG_DAILY_USAGE_EXCEEDED_START + "potion " const MSG_DAILY_USAGE_EXCEEDED_END = " times each day." const MSG_INSUFFICIENT_START = "Insufficient " const MSG_INSUFFICIENT_END = " to use this " const MSG_INSUFFICIENT_END_POTION = MSG_INSUFFICIENT_END + "potion." // Conversion rate between stats const XP_CONVERSION_RATE = 6 // 150 XP = 25 GP const MP_CONVERSION_RATE = 1.2 // 30 MP = 25 GP const HP_CONVERSION_RATE = 0.6 // 15 HP = 25 GP // Stats for each potion const ANTI_MP_POTION_LEVEL_LOCK = 11 const MAX_DAILY_ANTI_MP_POTION_USAGE = 4 const ANTI_MP_POTION_MP_COST = 30 const ANTI_MP_POTION_GP_GAIN = ANTI_MP_POTION_MP_COST / MP_CONVERSION_RATE const MAX_DAILY_ANTI_HP_POTION_USAGE = 4 const ANTI_HP_POTION_HP_COST = 15 const ANTI_HP_POTION_GP_GAIN = ANTI_HP_POTION_HP_COST / HP_CONVERSION_RATE const ANTI_XP_POTION_LEVEL_LOCK = 21 const MAX_DAILY_ANTI_XP_POTION_USAGE = 4 const ANTI_XP_POTION_XP_COST = 150 const ANTI_XP_POTION_GP_GAIN = ANTI_XP_POTION_XP_COST / XP_CONVERSION_RATE // Messages for each potion const MSG_ANTI_MP_POTION_LEVEL_LOCK_FAIL = MSG_LEVEL_LOCK_START + ANTI_MP_POTION_LEVEL_LOCK + MSG_LEVEL_LOCK_END_POTION const MSG_ANTI_MP_POTION_DAILY_USAGE_EXCEEDED = MSG_DAILY_USAGE_EXCEEDED_START_POTION + MAX_DAILY_ANTI_MP_POTION_USAGE + MSG_DAILY_USAGE_EXCEEDED_END const MSG_INSUFFICIENT_MP = MSG_INSUFFICIENT_START + "Mana" + MSG_INSUFFICIENT_END_POTION const MSG_ANTI_HP_POTION_DAILY_USAGE_EXCEEDED = MSG_DAILY_USAGE_EXCEEDED_START_POTION + MAX_DAILY_ANTI_HP_POTION_USAGE + MSG_DAILY_USAGE_EXCEEDED_END const MSG_INSUFFICIENT_HP = MSG_INSUFFICIENT_START + "Health" + MSG_INSUFFICIENT_END_POTION const MSG_ANTI_XP_POTION_LEVEL_LOCK_FAIL = MSG_LEVEL_LOCK_START + MAX_DAILY_ANTI_XP_POTION_USAGE + MSG_LEVEL_LOCK_END_POTION const MSG_ANTI_XP_POTION_DAILY_USAGE_EXCEEDED = MSG_DAILY_USAGE_EXCEEDED_START_POTION + MAX_DAILY_ANTI_XP_POTION_USAGE + MSG_DAILY_USAGE_EXCEEDED_END const MSG_INSUFFICIENT_XP = MSG_INSUFFICIENT_START + "Experience" + MSG_INSUFFICIENT_END_POTION // Button constants for each potion const ANTI_MP_POTION_TEXT = "**Draining Potion** ![Orange-brown potion](https://raw.githubusercontent.com/mike-the-monk/Habitica-customizations/main/Automated%20FCV%20scripts/images/draining%20potion%20transparent%20large%20wide%20432.png)" const ANTI_MP_POTION_ALIAS = "antiMPpotion" const ANTI_MP_POTION_NOTES = NOTES_START + ANTI_MP_POTION_MP_COST + " MP" + NOTES_MID1 + ANTI_MP_POTION_GP_GAIN + NOTES_MID2 + MAX_DAILY_ANTI_MP_POTION_USAGE + NOTES_END const ANTI_MP_POTION_VALUE = "0" const ANTI_HP_POTION_TEXT = "**Fatiguing Potion** ![Teal potion](https://raw.githubusercontent.com/mike-the-monk/Habitica-customizations/main/Automated%20FCV%20scripts/images/fatiguing%20potion%20transparent%20large%20wide%20432.png)" const ANTI_HP_POTION_ALIAS = "antiHPpotion" const ANTI_HP_POTION_NOTES = NOTES_START + ANTI_HP_POTION_HP_COST + " HP" + NOTES_MID1 + ANTI_HP_POTION_GP_GAIN + NOTES_MID2 + MAX_DAILY_ANTI_HP_POTION_USAGE + NOTES_END const ANTI_HP_POTION_VALUE = "0" const ANTI_XP_POTION_TEXT = "**Stupefying Potion** ![Indigo potion](https://raw.githubusercontent.com/mike-the-monk/Habitica-customizations/main/Automated%20FCV%20scripts/images/stupefying%20potion%20transparent%20large%20wide%20432.png)" const ANTI_XP_POTION_ALIAS = "antiXPpotion" const ANTI_XP_POTION_NOTES = NOTES_START + ANTI_XP_POTION_XP_COST + " XP" + NOTES_MID1 + ANTI_XP_POTION_GP_GAIN + NOTES_MID2 + MAX_DAILY_ANTI_XP_POTION_USAGE + NOTES_END const ANTI_XP_POTION_VALUE = "0" const ANTI_MP_POTION_BUTTON = { "text": ANTI_MP_POTION_TEXT, "type": "reward", "alias": ANTI_MP_POTION_ALIAS, "notes": ANTI_MP_POTION_NOTES, "value": ANTI_MP_POTION_VALUE, } const ANTI_HP_POTION_BUTTON = { "text": ANTI_HP_POTION_TEXT, "type": "reward", "alias": ANTI_HP_POTION_ALIAS, "notes": ANTI_HP_POTION_NOTES, "value": ANTI_HP_POTION_VALUE, } const ANTI_XP_POTION_BUTTON = { "text": ANTI_XP_POTION_TEXT, "type": "reward", "alias": ANTI_XP_POTION_ALIAS, "notes": ANTI_XP_POTION_NOTES, "value": ANTI_XP_POTION_VALUE, } const CRON_COUNT_KEY = "CRON_COUNT_KEY" const ANTI_MP_USAGE_KEY = "ANTI_MP_USAGE_KEY" const ANTI_HP_USAGE_KEY = "ANTI_HP_USAGE_KEY" const ANTI_XP_USAGE_KEY = "ANTI_XP_USAGE_KEY" const TIMESTAMP_KEY = "TIMESTAMP_KEY" var cronCountKey = "" var AntiMpUsageKey = "" var AntiHpUsageKey = "" var AntiXpUsageKey = "" var timestampKey = "" function doOneTimeSetup() { if (CREATE_STUPEFYING_POTION == 1) { api_createNewTaskForUser([ANTI_XP_POTION_BUTTON]) } if (CREATE_FATIGUING_POTION == 1) { api_createNewTaskForUser([ANTI_HP_POTION_BUTTON]) } if (CREATE_DRAINING_POTION == 1) { api_createNewTaskForUser([ANTI_MP_POTION_BUTTON]) } // Next, create the webhook const options = { "scored" : true, } const payload = { "url" : WEB_APP_URL, "label" : SCRIPT_NAME + " Webhook", "type" : "taskActivity", "options" : options, } apiMult_createNewWebhookNoDuplicates(payload) // set script properties so they carry over to next session initScriptProperties() } // do things when the webhook runs function doPost(e) { const dataContents = JSON.parse(e.postData.contents) const type = dataContents.type const task = dataContents.task // Sanitize task alias let sanitizedAlias = "sanitized" // This will be the value if undefined, null, or blank if ( (task.alias != undefined) && (task.alias != null) && (task.alias != "") ) { sanitizedAlias = task.alias } if ( (type == "scored") && ( (sanitizedAlias == ANTI_MP_POTION_ALIAS) || (sanitizedAlias == ANTI_HP_POTION_ALIAS) || (sanitizedAlias == ANTI_XP_POTION_ALIAS) ) ) { var timestampKey = TIMESTAMP_KEY var timeStart = Number(scriptProperties.getProperty(timestampKey)) if ( (timeStart == 0) || (timeStart == "") || (timeStart == undefined) || (timeStart == null) ) { timeStart = Date.now() } var timeEnd = Date.now() // Rate limiting: If it's been less than 30 seconds since they last clicked one of these buttons, do nothing if ( (timeEnd - timeStart) >= 30000 ) { // Run all the things that run regardless of the potion const response = api_getAuthenticatedUserProfile("stats") let user = JSON.parse(response).data var cronCountKey = CRON_COUNT_KEY var AntiMpUsageKey = ANTI_MP_USAGE_KEY var AntiHpUsageKey = ANTI_HP_USAGE_KEY var AntiXpUsageKey = ANTI_XP_USAGE_KEY var cronCount = Number(scriptProperties.getProperty(cronCountKey)) var AntiMpUsage = Number(scriptProperties.getProperty(AntiMpUsageKey)) var AntiHpUsage = Number(scriptProperties.getProperty(AntiHpUsageKey)) var AntiXpUsage = Number(scriptProperties.getProperty(AntiXpUsageKey)) let hp = user.stats.hp let exp = user.stats.exp let mp = user.stats.mp let gp = user.stats.gp let lvl = user.stats.lvl // If they've Cronned, reset all counters if (cronCount != user.flags.cronCount) { cronCount = user.flags.cronCount resetCounters() // Save to non-volatile memory scriptProperties.setProperty(cronCountKey, cronCount) } // Switch-case based on which button was pressed. All button-click actions are in a separate function. switch (sanitizedAlias){ case ANTI_MP_POTION_ALIAS: doButtonAntiMpPotion(lvl, mp, gp, AntiMpUsageKey, AntiMpUsage) break case ANTI_HP_POTION_ALIAS: doButtonAntiHpPotion(hp, gp, AntiHpUsageKey, AntiHpUsage) break case ANTI_XP_POTION_ALIAS: doButtonAntiXpPotion(lvl, exp, gp, AntiXpUsageKey, AntiXpUsage) break } // Save values to non-volatile memory, but only from within the rate-limited if-block (we don't want to save the timestamp unless the script ran) timeStart = timeEnd scriptProperties.setProperty(timestampKey, timeStart) } } return HtmlService.createHtmlOutput() } // When the Anti-MP Potion button is clicked function doButtonAntiMpPotion(lvl, mp, gp, AntiMpUsageKey, AntiMpUsage){ // If level lock fails, send failure message let sufficientLevel = checkLevelLock(lvl, ANTI_MP_POTION_LEVEL_LOCK, "potion.") if (sufficientLevel) { // Check if they've exceeded daily usage. If yes, send failure message. let usageLimitOkay = checkUsageLimit(AntiMpUsage, 1, MAX_DAILY_ANTI_MP_POTION_USAGE, "potion ", false, false) if (usageLimitOkay) { // Check if they have sufficient Mana. Send failure message if not. if (mp < ANTI_MP_POTION_MP_COST) { api_sendPrivateMessage({"message" : MSG_INSUFFICIENT_MP, "toUserId" : USER_ID}) } else { // Run potion as normal since all checks passed. api_updateUser({"stats.mp" : mp - ANTI_MP_POTION_MP_COST, "stats.gp" : gp + ANTI_MP_POTION_GP_GAIN}) AntiMpUsage++ // Save new counter value since it's updated scriptProperties.setProperty(AntiMpUsageKey, AntiMpUsage) } } } } // When the Anti-HP Potion button is clicked function doButtonAntiHpPotion(hp, gp, AntiHpUsageKey, AntiHpUsage){ // Check if they've exceeded daily usage. If yes, send failure message. let usageLimitOkay = checkUsageLimit(AntiHpUsage, 1, MAX_DAILY_ANTI_HP_POTION_USAGE, "potion ", false, false) if (usageLimitOkay){ // Check if they have sufficient Health. Send failure message if not. if (hp < ANTI_HP_POTION_HP_COST) { api_sendPrivateMessage({"message" : MSG_INSUFFICIENT_HP, "toUserId" : USER_ID}) } else { // Run potion as normal since all checks passed. api_updateUser({"stats.hp" : hp - ANTI_HP_POTION_HP_COST, "stats.gp" : gp + ANTI_HP_POTION_GP_GAIN}) AntiHpUsage++ // Save new counter value since it's updated scriptProperties.setProperty(AntiHpUsageKey, AntiHpUsage) } } } // When the Anti-XP Potion button is clicked function doButtonAntiXpPotion(lvl, exp, gp, AntiXpUsageKey, AntiXpUsage){ // If level lock fails, send failure message let sufficientLevel = checkLevelLock(lvl, ANTI_XP_POTION_LEVEL_LOCK, "potion.") if (sufficientLevel) { // Check if they've exceeded daily usage. If yes, send failure message. let usageLimitOkay = checkUsageLimit(AntiXpUsage, 1, MAX_DAILY_ANTI_XP_POTION_USAGE, "potion ", false, false) if (usageLimitOkay) { // Check if they have sufficient Experience. Send failure message if not. if (exp < ANTI_XP_POTION_XP_COST) { api_sendPrivateMessage({"message" : MSG_INSUFFICIENT_XP, "toUserId" : USER_ID}) } else { // Run potion as normal since all checks passed. api_updateUser({"stats.exp" : exp - ANTI_XP_POTION_XP_COST, "stats.gp" : gp + ANTI_XP_POTION_GP_GAIN}) AntiXpUsage += ANTI_XP_POTION_XP_COST // Save new counter value since it's updated scriptProperties.setProperty(AntiXpUsageKey, AntiXpUsage) } } } } // Create custom reward buttons function api_createNewTaskForUser(payload) { var params = { "method" : "post", "headers" : HEADERS, "contentType" : "application/json", "payload" : JSON.stringify(payload), // Rightmost button goes on top "muteHttpExceptions" : true, } var url = "https://habitica.com/api/v3/tasks/user" UrlFetchApp.fetch(url, params) } // Create a webhook if no duplicate exists function apiMult_createNewWebhookNoDuplicates(payload) { const response = api_getWebhooks() const webhooks = JSON.parse(response).data var duplicateExists = 0 for (var i in webhooks) { if (webhooks[i].label == payload.label) { duplicateExists = 1 } } // If webhook to be created doesn't exist yet if (!duplicateExists) { api_createNewWebhook(payload) } } // Used to see existing webhooks, and therefore if there's a duplicate function api_getWebhooks() { const params = { "method" : "get", "headers" : HEADERS, "muteHttpExceptions" : true, } const url = "https://habitica.com/api/v3/user/webhook" return UrlFetchApp.fetch(url, params) } // Creates a webhook (as part of the "don't make it if there's a duplicate" function) function api_createNewWebhook(payload) { const params = { "method" : "post", "headers" : HEADERS, "contentType" : "application/json", "payload" : JSON.stringify(payload), "muteHttpExceptions" : true, } const url = "https://habitica.com/api/v3/user/webhook" return UrlFetchApp.fetch(url, params) } // Sets initial properties that will be used/saved later. function initScriptProperties() { const responseUser = api_getAuthenticatedUserProfile("stats") let user = JSON.parse(responseUser).data let cronCount = user.flags.cronCount scriptProperties.setProperty(CRON_COUNT_KEY, cronCount) var timestampInit = Date.now() scriptProperties.setProperty(TIMESTAMP_KEY, timestampInit) scriptProperties.setProperty(ANTI_MP_USAGE_KEY, 0) scriptProperties.setProperty(ANTI_HP_USAGE_KEY, 0) scriptProperties.setProperty(ANTI_XP_USAGE_KEY, 0) } // Gets user info so I can use it, especially stats like mana, experience, and level function api_getAuthenticatedUserProfile(userFields) { const params = { "method" : "get", "headers" : HEADERS, "muteHttpExceptions" : true, } var url = "https://habitica.com/api/v3/user" if (userFields != "") { url += "?userFields=" + userFields } return UrlFetchApp.fetch(url, params) } // Changes stats function api_updateUser(payload) { const params = { "method" : "put", "headers" : HEADERS, "contentType" : "application/json", "payload" : JSON.stringify(payload), "muteHttpExceptions" : true, } const url = "https://habitica.com/api/v3/user" return UrlFetchApp.fetch(url, params) } // Send a notification as a private message, only if they're enabled function api_sendPrivateMessage(payload) { switch (NOTIFICATIONS_ON){ // Check if notifications are on, send message if yes case 0: break case 1: const params = { "method" : "post", "headers" : HEADERS, "contentType" : "application/json", "payload" : JSON.stringify(payload), "muteHttpExceptions" : true, } const url = "https://habitica.com/api/v3/members/send-private-message" return UrlFetchApp.fetch(url, params) break } } // Resets usage counters, usually done at Cron function resetCounters(){ scriptProperties.setProperty(ANTI_MP_USAGE_KEY, 0) scriptProperties.setProperty(ANTI_HP_USAGE_KEY, 0) scriptProperties.setProperty(ANTI_XP_USAGE_KEY, 0) } // Check if sufficient level, send message if not. function checkLevelLock(level, levelLock, messageEndVariable){ if (level < levelLock){ let errorMsg = MSG_LEVEL_LOCK_START + levelLock + MSG_LEVEL_LOCK_END + messageEndVariable api_sendPrivateMessage({"message" : errorMsg, "toUserId" : USER_ID}) return false } else { return true } } // Check if (daily) usage exceeded, send message if yes. function checkUsageLimit(usageCounter, newUsageIncrement, maxUsage, messageMidVariable, isForXp, isForXpPaid){ if ( (usageCounter + newUsageIncrement) > maxUsage ) { let errorMsg = MSG_DAILY_USAGE_EXCEEDED_START + messageMidVariable + maxUsage + MSG_DAILY_USAGE_EXCEEDED_END // Since the max usage for XP potion/skill is different, the message will be different. if (isForXp) { // Message start differs if it's XP gained vs. XP paid if (isForXpPaid) { errorMsg = MSG_DAILY_USAGE_EXCEEDED_XP_START_PAY } else { errorMsg = MSG_DAILY_USAGE_EXCEEDED_XP_START } errorMsg += messageMidVariable + MSG_DAILY_USAGE_EXCEEDED_XP_MID_1 + maxUsage + MSG_DAILY_USAGE_EXCEEDED_XP_MID_2 + MAX_DAILY_XP_POTION_USAGE + MSG_DAILY_USAGE_EXCEEDED_XP_END } api_sendPrivateMessage({"message" : errorMsg, "toUserId" : USER_ID}) return false } else { return true } } // Send a notification as a private message regardless of if they're enabled function api_sendPrivateMessageAlways(payload) { const params = { "method" : "post", "headers" : HEADERS, "contentType" : "application/json", "payload" : JSON.stringify(payload), "muteHttpExceptions" : true, } const url = "https://habitica.com/api/v3/members/send-private-message" return UrlFetchApp.fetch(url, params) } // FUNCTIONS FOR DEBUGGING. SCRIPT DOES NOT USE THEM, THEY MUST BE TRIGGERED MANUALLY // Manually reset usage counters function debugResetCounters() { resetCounters() } // Retrieves saved values and sends them in a private message function debugGetSavedValues(){ let cronCount = Number(scriptProperties.getProperty(CRON_COUNT_KEY)); let AntiMpUsage = Number(scriptProperties.getProperty(ANTI_MP_USAGE_KEY)) let AntiHpUsage = Number(scriptProperties.getProperty(ANTI_HP_USAGE_KEY)) let AntiXpUsage = Number(scriptProperties.getProperty(ANTI_XP_USAGE_KEY)) // saving these will make my life easier let MSG_JOINER_BEFORE = " is `" let MSG_JOINER_AFTER = "`, " api_sendPrivateMessageAlways({"message" : "Saved values are as follows: cronCount is `" + cronCount + MSG_JOINER_AFTER + "AntiMpUsage" + MSG_JOINER_BEFORE + AntiMpUsage + MSG_JOINER_AFTER + "AntiHpUsage" + MSG_JOINER_BEFORE + AntiHpUsage + MSG_JOINER_AFTER + "AntiXpUsage" + MSG_JOINER_BEFORE + AntiXpUsage + "`", "toUserId" : USER_ID}) } // Mannually edits and saves the selected value (in case it saved incorrectly) function debugManuallyEditSavedValue() { // Fill them in below. AntiMpUsage is shown as a sample. let newValue = 0 let key = ANTI_MP_USAGE_KEY // Save scriptProperties.setProperty(key, newValue) }