Apps Home
|
Create an App
Votes
Author:
111
Description
Source Code
Launch App
Current Users
Created by:
111
var total_voted = 0; finished = false; voters = []; ADVERT_TIME = 5; cb.settings_choices = [ {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) { if (msg['m'] == '!stat') { msg['X-Spam'] = true; vote_tracker.send_stats(msg['user']); } if (msg['m'].length == 1 && testvoter(msg['user'])==2){ for (var i=0; i < all_choices().length; i++) { if (all_choices()[i].charAt(0) == msg['m']) { 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 the Popular Position Bot! Which position would you like to see the model in? Type the number of your choice into the chat to cast your vote!', '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':'Choose the goal - by Lee', '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]); } }, 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 > 1) { 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); } }; vote_tracker.init(); 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)"; } cb.chatNotice(msga); cb.setTimeout(advert, (ADVERT_TIME * 60000)); } var _subject_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; } update_subject();
© Copyright Chaturbate 2011- 2024. All Rights Reserved.