Apps Home
|
Create an App
Auction
Author:
ninjapervs
Description
Source Code
Launch App
Current Users
Created by:
Ninjapervs
// Title: Auction // Author: NinjaPervs // Version: 1.0 (11/22/12) // Description: Users compete with each other to win the auction. var total_tipped = 0; var startup_time = new Date(); var subject_is_set_with_0 = false; var bids = { data:[], last_high_bidder:null, increment:function (username, amount) { var score = bids.pop_user(username); score += amount; bids.insert_score_keeping_sort(username, score); bids.check_for_bidder_change(); }, is_user_high_bidder:function (username) { return bids.last_high_bidder == username; }, check_for_bidder_change:function () { if (bids.data.length == 0) { if (bids.last_high_bidder != null) { cb.chatNotice(bids.last_high_bidder + " is no longer the high bidder."); bids.last_high_bidder = null; } return; } var high_bidder = bids.data[0].username; if (high_bidder != bids.last_high_bidder) { cb.chatNotice(high_bidder + " is the new high bidder."); bids.last_high_bidder = high_bidder; return; } }, score_for:function (username) { for (var i = 0; i < bids.data.length; i++) { if (bids.data[i].username == username) { return bids.data[i].score; } } return 0; }, get_current_high_bidder:function () { return bids.last_high_bidder; }, insert_score_keeping_sort:function (username, score) { var row = {score:score, username:username}; for (var i = 0; i < bids.data.length; i++) { if (score > bids.data[i].score) { bids.data.splice(i, 0, row); return; } } bids.data.push(row); }, pop_user:function (username) { for (var i = 0; i < bids.data.length; i++) { if (bids.data[i].username == username) { var s = bids.data[i]; bids.data.splice(i, 1); return s.score; } } return 0; }, update_subject:function () { if (bids.get_time_left() == 0) { if (subject_is_set_with_0) { return; } subject_is_set_with_0 = true; } else { subject_is_set_with_0 = false; } var high_bidder = bids.data[0].username; var new_subject = cb.settings.auction_description + " [" + bids.format_username(high_bidder) + " is Winning with " + bids.get_time_left() + " remaining in the Auction.]"; cb.log("Changing subject to: " + new_subject); cb.changeRoomSubject(new_subject); }, format_username:function (val) { if (val === null) { return "--"; } else { return val.substring(0, 12); } }, format_time:function (val) { var hours = 0; if ((val / 3600) < 1) { hours = 0; } else { hours = Math.floor(val / 3600); } var minutes = Math.floor( (val % 3600) / 60); var seconds = Math.floor(val % 60); return hours + " h, " + minutes + " min, " + seconds + " sec"; }, send_stats:function (to_user) { cb.chatNotice("== Auction Bids (Top 10) ==", to_user); var len = bids.data.length; if (len > 10) { len = 10; } for (var i = 0; i < len; i++) { cb.chatNotice(bids.data[i].username + ": (" + Math.ceil(bids.data[i].score) + " tokens)", to_user); } if (len == 0) { cb.chatNotice("Nobody has made a bid. Send tips to make a bid.", to_user); } var seconds_running = ((new Date()).getTime() - startup_time.getTime()) / 1000; cb.chatNotice("Auction has been running for " + bids.format_time(seconds_running) + ".", to_user); if (bids.get_time_left() === "Auction has ended") { if (bids.last_high_bidder === null) { cb.chatNotice("The Auction has ended, no bids were made.", to_user); } else { cb.chatNotice("The Auction has ended, congratulations to " + bids.get_current_high_bidder(), to_user); } } else { cb.chatNotice("Auction will end in " + bids.get_time_left() + " .", to_user); } }, get_time_left:function () { var seconds_running = ((new Date()).getTime() - startup_time.getTime()) / 1000; var time_left = (cb.settings.auction_time * 60) - seconds_running; var result = ""; if (time_left <= 0) { result = "Auction has ended" } else { result = bids.format_time(time_left); } return result; }, decay:function () { bids.check_for_bidder_change(); bids.update_subject(); cb.drawPanel(); }, schedule_decay:function () { cb.setTimeout(bids.decay, 10000); } }; bids.schedule_decay(); // Chaturbate hooks cb.settings_choices = [ {name:'starting_bid', type:'int', minValue:0, maxValue:99999,default:10,label:'Starting Bid'}, {name:'auction_description', type: 'str', minLength: 1, maxLength: 255,label:'Auction Description'}, {name:'auction_time', type:'int', minValue:1, maxValue:9999, default:5,label:'Auction Time'} ]; cb.onDrawPanel(function (user) { var high_bidder = bids.get_current_high_bidder(); var time_left = bids.get_time_left(); if (user == cb.room_slug) { if (high_bidder == null) { var high_bidder_str = "--"; } else { var high_bidder_str = high_bidder + " (" + Math.ceil(bids.score_for(high_bidder)) + " tokens)" } return { 'template':'3_rows_12_22_31', 'row1_label':'Auction Time Left: ' + time_left, 'row1_value':high_bidder_str, 'row2_label':'Total Tokens Earned:', 'row2_value':"" + total_tipped + ' Tokens', 'row3_value':'Type /stats for more information.' }; } else if (user == high_bidder) { var lead = bids.score_for(user); if (bids.data.length >= 2) { lead -= bids.data[1].score; } return { 'template':'3_rows_11_21_31', 'row1_value':'Auction Time Left: ' + time_left, 'row2_value':'You Lead The Auction: ' + Math.ceil(lead) + ' Tokens', 'row3_value':'Type /stats for more information.' }; } else { if (high_bidder == null) { var high_bidder_str = "--"; } else { var high_bidder_str = high_bidder + Math.ceil(bids.score_for(high_bidder)+1) + " Tokens)" } return { 'template':'3_rows_11_21_31', 'row1_value':'Auction Time Left: ' + time_left, 'row2_value':'Last Bid: ' + high_bidder_str, 'row3_value':'Type /stats for more information.' }; } }); cb.onTip(function (tip) { bids.increment(tip['from_user'], parseInt(tip['amount'])); total_tipped += parseInt(tip['amount']) bids.update_subject(); cb.drawPanel(); }); cb.onMessage(function (msg) { if (bids.is_user_high_bidder(msg['user'])) { msg['background'] = '#9F9'; } if (msg['m'] == '/stats') { msg['X-Spam'] = true; bids.send_stats(msg['user']); } return msg; });
© Copyright Camscaster.Com 2011- 2024. All Rights Reserved.