Apps Home
|
Create an App
Incremental Goals and Menu
Author:
tvaeera
Description
Source Code
Launch App
Current Users
Created by:
Tvaeera
/** START #################### VARS ################### **/ var chaturbateAppSettings = { /** {Object} tippers - the tippers **/ tippers: {}, /** {String} admin - the app admin username **/ admin: "", /** {String} repeatModeOptions - options for repeat **/ repeatModeOptions: { lastGoal: 'Last Goal', fromStart: 'From Start', random: 'Random', hiddenShow: 'Hidden Show' }, /** {String} panelModeOptions - options for panel **/ panelModeOptions: { goal: 'goal', menu: 'menu' }, /**{String} repeatMode - what to do when you reached last goal **/ repeatMode: "", /** {Number} topLimit - how many tippers should show **/ topLimit: 5, /** {Number} notificationsInterval - how often should show notificationsInterval in chat **/ notificationsInterval: 3, /** {Object} settings - the settings loaded from JSON **/ settings: { room: { goals:[ {tokens: "loading..."} ] } }, /** {Object} current - the current app state **/ current: { goal: 0, tokens: 0, totalTokens: 0, moreGoals : true, panel: 'goal' } }; /** END #################### VARS ################### **/ /** START #################### COMMANDS ################### **/ /** {Object} commands - the commands available for the app **/ var chaturbateAppCommands = { /** * {Function} save - export the current settings - ONLY FOR THE ADMIN and MODERATORS * @param {Object} msg - CB message **/ saveSettings: function(msg) { if (msg.user = chaturbateAppSettings.admin || msg.is_mod === true) { chaturbateApp.notice(JSON.stringify(chaturbateAppSettings.settings), chaturbateAppSettings.admin); } else { chaturbateApp.notice("You do not have permission to access the command!"); } }, /** * {Function} tippers - export the current tippers - ONLY FOR THE ADMIN and MODERATORS * @param {Object} msg - CB message **/ saveTippers: function(msg) { if (msg.user = chaturbateAppSettings.admin || msg.is_mod === true) { chaturbateApp.notice(JSON.stringify(chaturbateAppSettings.tippers), chaturbateAppSettings.admin); } else { chaturbateApp.notice("You do not have permission to access the command!"); } }, topTippers: function(msg) { if (msg.user = chaturbateAppSettings.admin || msg.is_mod === true) { chaturbateAppProxy.getTippers(); } else { chaturbateApp.notice("You do not have permission to access the command!"); } }, hashtags: function(msg) { var hashtags = msg.m.replace("/hashtags ", ""); if (msg.user = chaturbateAppSettings.admin || msg.is_mod === true) { chaturbateAppSettings.settings.room.hashtags = hashtags chaturbateAppProxy.updateRoomSubjectForGoal(chaturbateAppSettings.admin, 0); } else { chaturbateApp.notice("You do not have permission to access the command!"); } } }; /** END #################### COMMANDS ################### **/ /** START #################### METHODS ################### **/ var chaturbateAppProxy = { /** * get the topLimit tippers. */ getTippers: function (top, forPanel) { var topLimit = top || chaturbateAppSettings.topLimit; var sorted = Object.keys(chaturbateAppSettings.tippers).sort( function(a, b) { return -(chaturbateAppSettings.tippers[a] - chaturbateAppSettings.tippers[b]) } ); for (var i = 0; i < Math.min(topLimit, sorted.length); i++) { var username = sorted[i]; if(forPanel) { return username + ": " + chaturbateAppSettings.tippers[username]; } if (i === 0) { chaturbateApp.notice("Top Tippers: "); } chaturbateApp.notice(username + ": " + chaturbateAppSettings.tippers[username]); } }, /** * add a new tipper or tokens to an existing one * @param {string} username - The tipper username. * @param {int} tokens - The tokens. */ addTipper: function (username, tokens) { if (chaturbateAppSettings.tippers.hasOwnProperty(username) === true) { chaturbateAppSettings.tippers[username] += tokens; } else { chaturbateAppSettings.tippers[username] = tokens; } }, updateRoomSubjectForGoal: function more (tipper, tokens) { var currentGoal = chaturbateAppSettings.settings.room.goals[chaturbateAppSettings.current.goal]; var goalReached = false; var goalsMaxIndex = chaturbateAppSettings.settings.room.goals.length - 1; if (currentGoal.tokens <= chaturbateAppSettings.current.tokens + tokens) { var usedTokens = currentGoal.tokens - chaturbateAppSettings.current.tokens; chaturbateAppSettings.current.totalTokens += usedTokens; tokens -= usedTokens; goalReached = true; chaturbateAppSettings.current.tokens = 0; } else { chaturbateAppSettings.current.totalTokens += tokens; chaturbateAppSettings.current.tokens += tokens; } if (goalReached) { chaturbateAppSettings.current.moreGoals = chaturbateAppSettings.current.moreGoals && (chaturbateAppSettings.current.goal < goalsMaxIndex); if (chaturbateAppSettings.current.moreGoals) { chaturbateAppSettings.current.goal++; } switch (chaturbateAppSettings.repeatMode) { case chaturbateAppSettings.repeatModeOptions.fromStart: if (!chaturbateAppSettings.current.moreGoals) { chaturbateAppSettings.current.goal = 0; chaturbateAppSettings.current.moreGoals = true; } break; case chaturbateAppSettings.repeatModeOptions.random: if (!chaturbateAppSettings.current.moreGoals) { chaturbateAppSettings.current.goal = helper.randomNumber(0, goalsMaxIndex); } break; case chaturbateAppSettings.repeatModeOptions.hiddenShow: break; case chaturbateAppSettings.repeatModeOptions.lastGoal: default: break; } chaturbateApp.notice(tipper + " reached \""+currentGoal.title+"\" goal!"); chaturbateApp.notice("Thank you " + tipper + "!", tipper); } var title = currentGoal.title+ " ["+ chaturbateAppSettings.current.tokens+ "/"+ currentGoal.tokens+ "] "+ chaturbateAppSettings.settings.room.hashtags; chaturbateApp.changeRoomSubject(title); if (goalReached) { more(tipper, tokens); } } }; /** END #################### METHODS ################### **/ /** START #################### HELPER ################### **/ var helper = { isCommand: function(msg) { var isCommand = false; if (msg.charAt(0) === "/") { isCommand = true; } return isCommand; }, randomNumber: function (min, max) { return Math.floor(Math.random() * (max - min + 1) + min); }, getFirstWord: function (str) { var spacePosition = str.indexOf(' '); if (spacePosition === -1) return str; else return str.substr(0, spacePosition); }, getCommand: function(msg) { var command = null; msg = msg.substr(1); msg = this.getFirstWord(msg); var BreakException = {}; try { Object.getOwnPropertyNames(chaturbateAppCommands).forEach( function (val, idx, array) { if(val === msg) { command = msg; throw BreakException; } } ); } catch (e) { if (e === BreakException) return command; } return command; } }; /** END #################### HELPER ################### **/ /** START #################### CB INTEGRATION ################### **/ var chaturbateApp = { init: function () { this.setupSettings(); this.logCommands(); this.onTip(); this.drawPanel(); cb.setTimeout(this.readSettings, 1000); }, changeRoomSubject: function(subject) { cb.changeRoomSubject(subject); }, setupSettings: function () { var settings = [ {name: 'admin', type: 'str', minLength: 0, maxLength: 255, defaultValue: "tvaeera", label: "Admin" }, {name:'topLimit', type:'int', minValue:0, maxValue:10, defaultValue: 5, label: "Top tippers" }, {name:'notificationsInterval', type:'int', minValue:0, maxValue:10, defaultValue: 5, label: "Notifications interval" }, {name:'repeatMode', type:'choice', choice1: chaturbateAppSettings.repeatModeOptions.lastGoal, choice2: chaturbateAppSettings.repeatModeOptions.fromStart, choice3: chaturbateAppSettings.repeatModeOptions.random, choice4: chaturbateAppSettings.repeatModeOptions.hiddenShow, defaultValue: chaturbateAppSettings.repeatModeOptions.lastGoal, label: "After last goal strategy" }, {name:'settings', type: 'str', required: true, minLength: 1, maxLength: 10000, defaultValue: "", label: "JSON Settings" } ]; cb.settings_choices = settings; }, readSettings: function () { try { chaturbateAppSettings.settings = JSON.parse(cb.settings.settings); } catch (e) { cb.setTimeout(this.readSettings, 500); return null; } chaturbateAppSettings.admin = cb.settings.admin; chaturbateAppSettings.topLimit = cb.settings.topLimit; chaturbateAppSettings.notificationsInterval = cb.settings.notificationsInterval; chaturbateAppSettings.repeatMode = cb.settings.repeatMode; cb.setTimeout(this.topTippers, chaturbateAppSettings.notificationsInterval); chaturbateAppProxy.updateRoomSubjectForGoal(chaturbateAppSettings.admin, 0); cb.setTimeout(cb.drawPanel, 1000) return null; }, logCommands: function() { cb.onMessage(function(msg) { var message = msg.m; var isCommand = helper.isCommand(message); if (isCommand) { msg['X-Spam'] = true; var command = helper.getCommand(message); if (command !== null) { chaturbateAppCommands[command](msg); } } return msg; }); }, notice: function(message, to_user, background, foreground, weight, to_group) { var background = background || "#000000"; var foreground = foreground || "#FF0000"; //cb.sendNotice(message, [to_user], [background], [foreground], [weight], [to_group]) cb.sendNotice(message, to_user, background, foreground, weight, to_group); }, onTip: function() { cb.onTip(function (tip) { var tokens = parseInt(tip['amount']); var tipper = tip['from_user']; chaturbateAppProxy.addTipper(tipper, tokens); chaturbateAppProxy.updateRoomSubjectForGoal(tipper, tokens); cb.drawPanel(); //chaturbateAppProxy.roomSubject(); }); }, topTippers: function() { chaturbateAppProxy.getTippers(); cb.setTimeout(chaturbateApp.getTippers, chaturbateAppSettings.notificationsInterval); }, drawPanel: function() { cb.onDrawPanel(function() { switch (chaturbateAppSettings.current.panel) { case chaturbateAppSettings.panelModeOptions.goal: return { 'template': '3_rows_of_labels', 'row1_label': 'Tip Received / Goal :', 'row1_value': chaturbateAppSettings.current.tokens + " / "+ chaturbateAppSettings.settings.room.goals[chaturbateAppSettings.current.goal].tokens, 'row2_label': 'Highest Tipper:', 'row2_value': chaturbateAppProxy.getTippers(1, true), 'row3_label': 'Total tips so far:', 'row3_value': chaturbateAppSettings.current.totalTokens }; break; } }); } }; /** END #################### CB INTEGRATION ################### **/ /** START #################### CB INIT ################### **/ chaturbateApp.init(); /** END #################### CB INIT ################### **/
© Copyright Chaturbate 2011- 2025. All Rights Reserved.