Apps Home
|
Create an App
FREE VOTE V2
Author:
everlast_69
Description
Source Code
Launch App
Current Users
Created by:
Everlast_69
/* * Title: FREE POLL V2 * Author: Everlast_69 (original code by Naturist_be) * Version: 2.0 (Original Version 1.0 (23/07/13)) * Thanks to Naturist_be for your effort in creating this app. * Summary: FREE POLL Everyone can play! * * Description: everlast_69: below is the origional description followed by my modifications. 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 * * Modified by everlast_69 (17/01/20) This app was oriionally created by Naturist_be on 23/07/13, more then 6 years ago and has not been modified since. This was a good app but needed to be maintained over the years. 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 using Rergular Expressions. 3. stripped off the ! and passed the number to the original code. 4. Added a setting to set the frequency the app will display the voting options 5. added a cb.onEnter function to tell users when they enter the room that the poll is running */ var total_voted = 0; var finished = false; var voters = []; var ADVERT_TIME = 5; var voting_for = ' '; var subject_is_set_with_0 = false; cb.settings_choices = [ {name:'voting_for', type:'str', label:' What are we voting for?', maxLength:200}, {name:'display_time',type:'int', label:'status dusplay time in minutes', minValue:1, maxValue:10, default:3}, {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) { // 1 var exp = /!(stat|[0-5])/; // regular expression to find !stat or !n with n = 1 to 5 var found = exp.test(msg.m); if (found) { //2 var cmd = exp.exec(msg.m); msg['X-Spam'] = true; if (cmd[1] == 'stat') { // 3 vote_tracker.send_stats(msg.user); } else { // close 2 open 3 var vote = cmd[1].toString(); if ( testvoter(msg.user) == 1){ // 4 for (var i=0; i < all_choices().length; i++) { // 5 if (all_choices()[i].charAt(0) == vote) { // 6 vote_tracker.track_vote(0, all_choices()[i]); voters.push({user:msg.user}); total_voted ++; } // end if (all.... } // end for cb.drawPanel(); update_subject(); } // end if (testvoter } // end else } // end if (found 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!!' }; }); cb.onEnter(function(user){ var msg = "Welcome " + user.user + ", " + cb.room_slug + " is taking a poll.\n"; msg += instructions(); cb.sendNotice(msg); }); 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.display_time; voting_for = cb.settings.voting_for; update_subject(); }, 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("We are voting for \n" + voting_for + "\n Poll choices and stats: ", user, "#666699", "#FFFFFF", "bold"); cb.chatNotice(msg, user); } }; function IsNullorEmpty(value) { if (value == null || value == undefined || value.length == 0) { return true; } else { return false; } } 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("We are voting for \n" + voting_for + "\n Poll choices and stats: ", '', "#666699", "#FFFFFF", "bold"); cb.chatNotice(msga); cb.setTimeout(advert, (ADVERT_TIME * 60000)); } 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); } vote_tracker.init();
© Copyright Chaturbate 2011- 2024. All Rights Reserved.