Apps Home
|
Create an App
Free Vote Mod
Author:
everlast_69
Description
Source Code
Launch App
Current Users
Created by:
Everlast_69
/* * Title: FREE POLL * Author: Naturist_be * Version: 1.0 (23/07/13) * Summary: FREE Hangman! Everyone can play! * * Description: Options: - Set the number of votes limit - Set at least 2 choices (maximum of 5) - Users can vote only once List of commands: Users only type the number of the choice they want !stat = display choices and stats Have fun! Naturist_be * (17/01/20) Modified by everlast_69 since this app was written many changes have happened on CB. This app no longer functions corrctly. There are many bots that modify messages by adding things like tokens tipped and other information to the beginning of the message. This causes this app to fail and not count votes. I have modified the way votes are made so this does not happen by doing the following modifications: 1. changed the vote syntax from a single number to !n. users now type !1 to vote for option 1 2. modified the onMessage function to find the !n anywhere in the message 3. stripped off the ! and passed the number to the original code. A regular Extression is used to find the Free Vite commands in the message /!(stat|timer|end|help|[1-5])(?:\s+)?(set|start|stop|end|help)?(?:\s+)?(\d+)?/i here's how it works: ! searches for an ! in the message followed by one of these strings: (stat|timer|end|help|[1-5]) the () indicate to return the string it found as group 1 (?:\s+)? the \s+ is continue searching for 1 or more whitespaces the () indicate this is a group. the ?: is do not return this group and the following ? says this group is optional (set|start|stop|end|help)? next find one if these strings and return as group 2 but the ending ? makes this search optional. again search for whitespace following group 2 but finding it is optional. (\d+)? group 3 is 1 or more numbers and is optional. if the message contains !stat then group 1 will = stat, groups 2 & 3 will be null if the message contains !timer set 10 then group1 = timer, group 2 = set and group 3 = 10 the results are returned as an array with group 1 = element 1, 2 = 2 & 3 = 3. element 0 = the entire command found. */ var total_voted = 0; var finished = false; var voters = []; var ADVERT_TIME = 3; var timer_id = ""; var time_set = 0; var start_time = 0; var elapsed_time = 0; var time_remaining = 0; var minute_timer_id = 0; var timing = false; var voting_for = ''; var text = { cmd_expression: /!(stat|timer|end|help|[1-5])(?:\s+)?(set|start|stop|cancel|help)?(?:\s+)?(\d+)?/i, only_broadcaster:"Only the Broadcaster can use this command", help: "We are voting for " + cb.settings.voting_for + ".\nanyone, even greys. can vote. you don't need to have tokens\nto enter a vote type an exclamination point(!) followed by the number of the option you want to vote for.\nlike this !2 to vote for option 2. You can only vote one time.", timer_help: "The timer is a countdown timer. it will count down the set number of minutes then end the voting.\nthe timer has the following commands:\n!timer set ??? where ??? = number of minutes to set the timer to end the poll\n!timer start starts the timer after setting the number of minutes\n!timer stop stops the timer. Starting the timer will resume the countdown.\n!timer cancel will cancel the countdown.", no_timer_cmd: "The timer Commmand Requires a Function.\nUse !timer help for timer instructions.", time_must_be_set: "time must be set before starting the timer", set_req_min: "Timer set requires minutes to set timer.\n Format is !timer set 999 where 999 = minutes to set timer" }; cb.settings_choices = [ {name:'voting_for', type: 'str',label:'What are we voting for?',maxLength: 200}, {name:'advert_time', type:'int', minValue:1, label:'show stats every ??? minutes',maxValue:999, default:3}, {name: 'end_minutes', type:'int', minValue:1, label:'How many minutes to run vote 0 = no end time', maxValue:999, default:0}, {name:'max_votes', type:'int', minValue:1, label: "End poll after this many votes", maxValue:99999, default:50}, {name:'choice1', type:'str', label: "Choice 1", maxLength: 200}, {name:'choice2', type:'str', label: "Choice 2", maxLength: 200}, {name:'choice3', type:'str', label: "Choice 3", maxLength: 200, required: false}, {name:'choice4', type:'str', label: "Choice 4", maxLength: 200, required: false}, {name:'choice5', type:'str', label: "Choice 5", maxLength: 200, required: false} ]; cb.onMessage(function (msg) { var expr; var cmd; var cmd_found = false; expr = text.cmd_expression; cmd_found = expr.test(msg.m); if (cmd_found) { msg['X-Spam'] = true; cmd = expr.exec(msg.m); switch (cmd[1]) { //cmd[1] contains the command case "stat": vote_tracker.send_stats(msg.user); break; case "timer": if (msg.user == cb.room_slug) { timer(cmd); } else { cb.send_Notice(text.only_broadcaster, msg.user,"#FF0000", "#FFFFFF", "bold"); } break; case "end": if (msg.user == cb.room_slug) { finished = true; cb.drawPanel(); update_subject(); } else { cb.send_Notice(text.only_broadcaster, msg.user,"#FF0000", "#FFFFFF", "bold"); } //else break; case "help" : cb.sendNotice (text.help,msg.user); break; case "1": case "2": case "3": case "4": case "5": var vote = Number(cmd[1]); cb.sendNotice("onMessage vote = " + vote); if ( testvoter(msg.user)==1){ for (var i=0; i < all_choices().length; i++) { if (all_choices()[i].charAt(0) == vote) { vote_tracker.track_vote(0, all_choices()[i]); voters.push({user:msg.user}); total_voted ++; } } cb.drawPanel(); update_subject(); } } } return msg; } ); cb.onDrawPanel(function (user) { var bestchoice_display = '\u2605 start voting! \u2605'; var nbchoice = 0; if (vote_tracker.bestchoice) { nbchoice = vote_tracker.bestchoice.nb_voted; bestchoice_display = vote_tracker.bestchoice.choice+ ' (' + nbchoice + ')'; } if (!finished) return { 'template':'3_rows_11_21_31', 'row1_value':'WELCOME TO FREE POLL!', 'row2_value': 'Top choice: ' + bestchoice_display, 'row3_value': 'Votes remaining: ' + total_voted + "/" + cb.settings.max_votes }; else return { 'template':'3_rows_11_21_31', 'row1_value':'FREE POLL - by Naturist_be', 'row2_value': 'Elected: ' + bestchoice_display, 'row3_value':'Thanks all for your vote!!' }; }); /* function instructions() { var msg = "We are voting for " + voting_for + ".\nanyone, even greys. can vote. you don't need to have tokens"; msg += "to enter a vote type an exclamination point(!) followed by the number of the option you want to vote for.\n"; msg += "like this !2 to vote for option 2. You can only vote one time."; return msg; } */ var _all_choices_cache = null; function all_choices() { if (_all_choices_cache) { return _all_choices_cache; } var choices = []; for (var i=0; i < 5; i++) { var name = 'choice' + i; if (cb.settings[name]) { choices.push(i +' - ' + cb.settings[name]); } } _all_choices_cache = choices; return choices; } var vote_tracker = { _data:[], bestchoice: null, init:function() { for (var i=0; i < all_choices().length; i++) { vote_tracker.track_vote(0, all_choices()[i]); } ADVERT_TIME = cb.settings.advert_time; time_set = cb.settings.end_minutes; voting_for = cb.settings.voting_for; }, track_vote:function(voteamount, choice) { var found = false; for (var i=0; i < vote_tracker._data.length; i++) { if (vote_tracker._data[i].choice == choice) { vote_tracker._data[i].nb_voted += 1; found = true; } } if (!found) { vote_tracker._data.push({ choice:choice, nb_voted:0, }); } vote_tracker._update_bestchoice(); }, _update_bestchoice:function() { if (vote_tracker._data.length == 0) { return; } vote_tracker._data.sort(function(a,b) { return (b.nb_voted - a.nb_voted); }); if (vote_tracker._data[0].nb_voted > 0) { vote_tracker.bestchoice = vote_tracker._data[0]; } }, send_stats:function(user) { var msg = ""; for (var i=0; i < vote_tracker._data.length; i++) { if (i > 0) { msg += "\n"; } msg += vote_tracker._data[i].choice + " (" + vote_tracker._data[i].nb_voted + " votes)"; } cb.chatNotice(" Poll choices and stats: ", user, "#666699", "#FFFFFF", "bold"); cb.chatNotice(msg, user); }, help:function(user) { var msguser = ''; if (user != cb.room_slug) { msguser = user; } cb.sendNotice(text.help, msguser); } }; vote_tracker.init(); function timer(cmd) { cb.sendNotice("timer function entered"); if (IsNullorEmpty(cmd[2])) { cb.sendNotice(text.no_timer_cmd,cb.room_slug, "#FF0000", "#FFFFFF", "bold"); } else { cb.sendNotice("timer sub function " + cmd[2]); switch (cmd[2]) { case "set": if (IsNullorEmpty(cmd[3])) { cb.sendNotice(text.set_req_min,cb.room_slug, "#FF0000", "#FFFFFF", "bold"); } else { time_set = cmd[3]; cb.sendNotice("minutes to set = " + time_set); } break; case "start" : cb.sendNotice("timer sub function " + cmd[2]); if (time_set == 0) { cb.sendNotice(text.time_must_be_set,cb.room_slug, "#FF0000", "#FFFFFF", "bold"); } else { start_time = new Date().getTime(); minute_timer_id = cb.setTimeout(remaining_time, 60000); } break; case "stop" : cb.sendNotice("timer sub function " + cmd[2]); break; case "cancel" : cb.sendNotice("timer sub function " + cmd[2]); break; case "help" : cb.sendNotice("timer sub function " + cmd[2]); break; default: cb.sendNotice("Invalid Timer Commmand Function\n" + text.timer_help,cb.room_slug, "#FF0000", "#FFFFFF", "bold"); } } } function IsNullorEmpty(value) { if (value == null || value == undefined || value.length == 0) { return true; } else { return false; } } function remaining_time() { var time_now = new Date().getTime(); var elapsed = time_now - start_time; elapsed_time = elapsed.getMinutes(); time_remaining = start_time.getMinutes() - elapsed_time; if (time_remaining <= 0) { vote_tracker.end_voting(); } else { minute_timer_id = cb.setTimeout(remaining_time, 60000); } } function votes_remaining() { var r = cb.settings. max_votes - total_voted; if (r < 1) { finished = true; return 0; } else { finished = false; return r; } } function testvoter(user) { for(var i=0; i < voters.length; i++){ if(voters[i].user==user){ cb.chatNotice('You can only vote once!', user, "#FF0000", "#FFFFFF", "bold"); return 0; } } return 1; } function advert() { var msga = "Poll choices & stats: \n"; for (var i=0; i < vote_tracker._data.length; i++) { if (i > 0) { msga += "\n"; } msga += vote_tracker._data[i].choice + " (" + vote_tracker._data[i].nb_voted + " votes)"; } msga += "\nTo vote type !n where n = the number of your vote."; cb.chatNotice(msga); cb.setTimeout(advert, (ADVERT_TIME * 60000)); } var subject_is_set_with_0 = false; function update_subject() { var ft = ""; if (votes_remaining() == 0) { if (subject_is_set_with_0) { return; } subject_is_set_with_0 = true; } else { subject_is_set_with_0 = false; } if (vote_tracker.bestchoice) { if (finished == true) { ft = "Elected: " + vote_tracker.bestchoice.choice;} else {ft = "Top choice: "+ vote_tracker.bestchoice.choice + " (" + votes_remaining() + " votes remaining) Type !stat to see choices and stats.";} //ft = vote_tracker.bestchoice.choice; } else { ft = "\u2605 start voting! \u2605 Type !stat to see choices and stats."; vote_tracker.send_stats(); cb.setTimeout(advert, (ADVERT_TIME * 60000)); } var new_subject = ft; cb.changeRoomSubject(ft); } update_subject();
© Copyright Chaturbate 2011- 2024. All Rights Reserved.