Slaves Leaderboard
Author: vikingbeastboy

You are viewing version 295379. There is a newer version of this app. See the latest version of this app.

Description Source Code Launch Bot Current Users

Short Description:

Custom bot from Tippers Leaderboard

Full Description

/*********************************************

Slaves Leaderboard

Keeps track of all tips and shows a leaderboard of all slaves for the current session. The top 3 can be printed on a fixed interval, after each tip, or when the top 3 has changed.

Slaves can view the full slave leaderboard using the "!sb" command.


These are the settings:

Print top 3 after tips

Whether to print the leaderboard after tips. Choices are:

Print top 3 at least once every (minutes, 0 to disable)

Time (in minutes) to automatically print the top 3 if no tips were received that caused the leaderboard to be printed in that time (based on the previous setting).

Setting this value to 0 disables this feature.

Highlight tip leader in chat

Set to 'yes' to highlight the current leader.


Remind tippers who tipped 25 tokens to rate

When set to 'yes' any tipper who has tipped 25 tokens or more during this session is asked to rate.

Version history:

3 Aug 2013: v1.0; initial release
3 Aug 2013: v1.1; sorting fixed, added option to run as both an App and Bot
23 Aug 2013: v1.2; more compact notice output, only sending !lb output to requesting user
28 Aug 2013: v1.2.1; only match !lb at the beginning of the input, don't process !lb if it was already handled
28 Dec 2013; v1.3; delay printing of leaderboard until tipping has stopped, only print top 3 if it has changed
30 Dec 2013; v1.4; added settings
31 Dec 2013; v1.4.1; code cleanup
13 Jul 2014; v1.5; added option to ask tippers who tipped over 25 tokens to rate

*********************************************/

var VERSION = '1.5',
COMMAND_SHOW_LEADERBOARD = '!sb',
CONFIG_COLOR_LEADER = '#9f9',
INTERVAL_MULTIPLIER = 60000,
UPDATE_TIME = 5000,
RATE_FROM = 25,
RATE_MESSAGE = ":rate-me",
RATE_MESSAGE_DELAY = 1500,
NL = '\n',
user_total_tips = {},
user_last_tip_time = {},
last_top3 = '',
leader_username,
update_counter = 0,
interval_counter = 0,
silent_room = true;

// settings
cb.settings_choices = [{
name: 'print_on_tip',
type: 'choice',
choice1: 'always',
choice2: 'only if the top 3 changed',
choice3: 'never',
defaultValue: 'only if the top 3 changed',
label: "Print top 3 after tips"
}, {
name: 'print_interval',
type: 'int',
minValue: 0,
defaultValue: 10,
label: "Print top 3 at least once every (minutes, 0 to disable)",
required: true
}, {
name: 'highlight',
type: 'choice',
choice1: 'yes',
choice2: 'no',
defaultValue: 'no',
label: "Highlight tip leader in chat"
}, {
name: 'rate',
type: 'choice',
choice1: 'yes',
choice2: 'no',
defaultValue: 'yes',
label: "Remind tippers who tipped 25 tokens to rate"
}];

// handlers
cb.onTip(function (tip) {
handleTip(tip.amount, tip.from_user);
scheduleUpdate();

silent_room = false;
});

cb.onMessage(function (msg) {
// handle user commands
if ((msg.m.indexOf(COMMAND_SHOW_LEADERBOARD) == 0) && !msg['X-Spam']) {
sendLeaderboard(true, msg.user);
msg['X-Spam'] = true;
}

// highlight leader
if ((cb.settings.highlight == 'yes') && (msg.user == leader_username)) {
msg.background = CONFIG_COLOR_LEADER;
}

// schedule interval
if (update_counter == 0){
scheduleInterval();
}

if (!msg['X-Spam']){
silent_room = false;
}

return msg;
});

// functions
function handleTip(amount, user) {
if (amount <= 0) return;

var before = user_total_tips[user] || 0,
after = user_total_tips[user] = before + amount;

user_last_tip_time[user] = new Date().valueOf();

if ((before < RATE_FROM) && (after >= RATE_FROM)){
cb.setTimeout(function(){
cb.chatNotice(RATE_MESSAGE, user);
}, RATE_MESSAGE_DELAY);
}
}

function sortLeaderboard(){
var lb = [];

for (var user in user_total_tips) {
if (user_total_tips.hasOwnProperty(user)){
lb.push([user_total_tips[user], -user_last_tip_time[user], user]);
}
}

if (lb.length > 0){
lb.sort(function(a, b){
for (var i=0; i < a.length; i++){
if (a[i] < b[i]){
return 1;
}
if (a[i] > b[i]){
return -1;
}
}
return 0;
});
leader_username = lb[0][2];
}

return lb;
}

function formatRanking(leaderboard, rank){
if (rank < leaderboard.length) {
var p = leaderboard[rank];
return p[2] + ' (' + p[0] + ' token' + (p[0]!=1 ? 's' : '') + ')';
}
else {
return '--';
}
}

function getTop3(leaderboard){
var rank, result='';
for (rank=0; rank<3; rank++){
if (rank>0){
result += '|';
}
if (rank < leaderboard.length){
result += leaderboard[rank][2];
}
}
return result;
}

function sendLeaderboard(force, to_user) {
var lb = sortLeaderboard(),
l = to_user ? lb.length : 3,
out = 'Tippers leaderboard';

if (!force) {
var top3 = getTop3(lb);
if (top3 == last_top3){
return;
}
last_top3 = top3;
}

if (l<3) { l=3; }

if (l < lb.length) {
out += ' top 3 (Type !sb to see the full slave leaderboard)';
}
if (to_user){
out += ' [v' + VERSION+']';
}

for (var rank=0; rank 0) {
var counter = ++interval_counter;

cb.setTimeout(function(){
if (interval_counter == counter){
sendLeaderboard(!silent_room);
}
}, interval*INTERVAL_MULTIPLIER);

silent_room = true;
}
}

function init(){

}

© Copyright Chaturbate 2011- 2026. All Rights Reserved.