Bots Home
|
Create an App
afadfad
Author:
fakeclam2
Description
Source Code
Launch Bot
Current Users
Created by:
Fakeclam2
/********************************************* Tip Leaderboard --------------- Keeps track of all tips and shows a leaderboard for all tippers. No goals, nothing to configure. When run as App, the top 3 tippers are shown in the panel and the leader is highlighted in chat. When run as Bot, the top 3 tippers are shown after a tip is received. The full leaderboard can be printed using the "!lb" command in both the App and Bot mode. 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 *********************************************/ var IAMBOT = false, COMMAND_SHOW_LEADERBOARD = '!lb', CONFIG_COLOR_LEADER = '#9f9', NL = '\n', user_total_tips = {}, user_last_tip_time = {}, last_top3 = '', leader_username, draw_leaderboard_counter = 0; // No settings at all :) cb.settings_choices = []; //handlers cb.onTip(function (tip) { handleTip(tip.amount, tip.from_user); IAMBOT ? scheduleDrawLeaderboard(5000) : cb.drawPanel(); }); if (!IAMBOT) { cb.onDrawPanel(function (user) { var lb = sortLeaderboard(); return { 'template': '3_rows_of_labels', 'row1_label': 'Tip leader:', 'row1_value': formatRanking(lb, 0), 'row2_label': 'Runner up:', 'row2_value': formatRanking(lb, 1), 'row3_label': 'Third place:', 'row3_value': formatRanking(lb, 2) }; }); } cb.onMessage(function (msg) { // user commands if ((msg.m.indexOf(COMMAND_SHOW_LEADERBOARD) == 0) && !msg['X-Spam']) { drawLeaderboard(true, msg.user); msg['X-Spam'] = true; } if (!IAMBOT && (msg.user == leader_username)) { msg.background = CONFIG_COLOR_LEADER; } return msg; }); function handleTip(amount, user) { user_total_tips[user] = (user_total_tips[user] || 0) + amount; user_last_tip_time[user] = new Date().valueOf(); } 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){ var r,i; for (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){ var p = leaderboard[rank]; result += leaderboard[rank][2]+':'+leaderboard[rank][0] } } return result; } function drawLeaderboard(full, to_user) { var rank, lb = sortLeaderboard(), l = full ? lb.length : 3, out = 'Tippers leaderboard'; if (!full && !to_user){ 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 !lb to see the full leaderboard)'; } for (rank=0; rank<l; rank++) { out += NL + '' + (rank+1) + '. ' + formatRanking(lb, rank); } cb.chatNotice(out, to_user); } function scheduleDrawLeaderboard(timeout){ var counter = ++draw_leaderboard_counter; cb.setTimeout(function(){ if (draw_leaderboard_counter == counter){ draw_leaderboard_counter = 0; drawLeaderboard(); } }, timeout); } function init(){ if (!IAMBOT) { cb.drawPanel(); } } init();
© Copyright Chaturbate 2011- 2024. All Rights Reserved.