Apps Home
|
Create an App
Free Vote upgrade
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 eill 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; cb.settings_choices = [ {name:'advert_time', type:'int', minValue:1, label:'advertise time(min)',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: "Limit of 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 x; var expr; var cmd; var cmd_found = false; expr = /!(stat|timer|end|help|[1-5])(?:\s+)?(set|start|stop|end|help)?(?:\s+)?(\d+)?/i; cmd_found = expr.test(msg.m); if (!cmd_found) { // cmd or vote not found return message // cb.sendNotice("cmd not found") return (msg); } else { // cmd or vote found msg['X-Spam'] = true; cmd = expr.exec(msg.m); // returns an array of the command parts into var cmd switch (cmd[1]) { //cmd[1] contains the command case "stat": // stat vote_tracker.send_stats(msg.user); break; case "timer": // timer if (msg.user == cb.room_slug) { vote_tracker.timer(cmd); } else { cb.send_Notice("Only the Broadcaster can use thus command", msg.user,"#FF0000", "#FFFFFF", "bold"); } // else break; case "end": // end voting if (msg.user == cb.room_slug) { vote_tracker.end_voting(); cb.drawPanel(); update_subject(); } else { cb.send_Notice("Only the Broadcaster can use thus command", msg.user,"#FF0000", "#FFFFFF", "bold"); } //else break; case "help" : cb.sendNotice ("command help"); 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 ++; } // if all_choices } // for cb.drawPanel(); update_subject(); } // testvoter } // switch } // if !cmd found else return msg; }); // onMessage else & on Message 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!!' }; }); 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; }, 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); }, timer:function(cmd) { cb.sendNotice("timer function entered"); if (IsNullorEmpty(cmd[2])) { cb.sendNotice("Invalid Timer Function Pleasee try again ",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("Timer set requires minutes to set timer for.\n Format is !timer set 999 ",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(" time must be set before starting the timer",cb.room-slug, "#FF0000", "#FFFFFF", "bold"); } else { start_time = new Date().getTime(); // timer_id = cb.setTimeout(vote_tracker.end_voting(), time_set * 60000); minute_timer_id = cb.setTimeout(remaining_time, 60000); } break; case "stop" : cb.sendNotice("timer sub function " + cmd[2]); break; case "end" : cb.sendNotice("timer sub function " + cmd[2]); break; case "help" : cb.sendNotice("timer sub function " + cmd[2]); break; } } }, end_voting:function() { cb.sendNotice("end voting function"); finished = true; } }; vote_tracker.init(); 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.