Bots Home
|
Create an App
Auction 1.0
Author:
jmmprogrammer
Description
Source Code
Launch Bot
Current Users
Created by:
Jmmprogrammer
/** * * auction.js * * Once the aution is configurered, type "/sa" to start it. * * Other commands are "/status", "/rules", "/bids", "/intro" * */ var __stillRunning = false; var _biddersArray = new Array; var _bidsArray = new Array; var _number = 0; var _countDownDateTime = 0; var _days = 0; var _hours = 0; var _minutes = 0; var _seconds = 0; var _timerTimeout; var _statusInterval = 5; cb.settings_choices = [ {name:'__backgroundColor', type:'str', minLength: 7, maxLength: 7, label:"Background Color", defaultValue:'#FF00FF'}, {name:'__foregroundColor', type:'str', minLength: 7, maxLength: 7, label:"Foreground Color", defaultValue:'#FFFFFF'}, {name:'__auctionItem', type:'str', maxLength: 100, label:"Item to Auction Off"}, {name:'__originalTokenValue', type:'int', minValue: 1, maxValue: 10000, defaultValue: 1000,label: 'How much is this item worth normally?'}, {name:'__duration', type:'int', minValue: 1, maxValue: 999, defaultValue: 1,label: 'Number of minutes (minimum 1)'}, {name:'__interval', type:'int', minValue: 1, maxValue: 999, defaultValue: 5,label: 'Print an update every X minutes'} ]; cb.onEnter(function (user) { var outMsg = "Welcome to my room " + user['user'] + "\n"; outMsg += "I realize that of all of the rooms to visit, I should be thankful that you visited mine.... so thank you.\n"; if ( __stillRunning ){ outMsg += "We're having an aution by the way. At the moment, you have a chance to win \n"; outMsg += "\"" + cb.settings['__auctionItem'] + "\" valued at " + cb.settings['__originalTokenValue'] + " tokens."; } cb.sendNotice( outMsg, user['user'], cb.settings['__backgroundColor'], cb.settings['__foregroundColor']); printIntro(user['user']); }); cb.onTip(function ( tip ) { var _amountTipped = 0; _amountTipped = parseInt(tip['amount']); var str = tip['message']; str = str.toLowerCase(); if ( str.includes("bid") ){ handleBid(tip['from_user'], _amountTipped); } }); // // whenever anyone adds a message to the chat room... // cb.onMessage(function (msg) { // // "/bids" -> prints out the leader board // if ( msg['m'] == '/bids' ){ msg['X-Spam'] = true; printLeaderBoard( msg['user'] ); } // // "/rules" -> prints out the rules // if ( msg['m'] == '/rules' ){ msg['X-Spam'] = true; printRules( msg['user'] ); } // // only the broadcaster or the mod... // if(( msg['user'] == cb.room_slug )||(msg['is_mod'] == true)){ // // // if ( msg['m'] == '/testbid'){ msg['X-Spam'] = true; handleBid( cb.room_slug, 1 ); } // // prints out the status for everyone to see // if ( msg['m'] == '/status' ){ msg['X-Spam'] = true; printStatus(); } // // prints out the intro message only for the him/her to see // if ( msg['m'] == '/intro' ){ msg['X-Spam'] = true; printIntro( msg['user'] ); } // // YOU MUST TYPE "/sa" TO START THE AUCTION!!! // if ( msg['m'] == '/sa' ){ msg['X-Spam'] = true; if( cb.settings['__auctionItem']){ // // sets the correct flag to let all of the // other functions know that the auction is running. // __stillRunning = true; // // Depending on the configured duration, will set the end-time // setEndDateTime(); // // Starts the time thread // _timerTimeout = cb.setTimeout(countdownTimer, 1000); // // Two seconds after the aution is started, print the // intro message and the rules to the chat // cb.setTimeout(function(){ printIntro(); printRules(); }, 2000); } } } }); // // // function setEndDateTime(){ // Get current date and time var now = new Date().getTime(); var minutes = 0; minutes = parseInt(cb.settings.__duration); _countDownDateTime = now + ( minutes * 60000 ); } // // If a username is provided, this function will print out the rules // only to the user. // // If no username is provided, everyone sees the rules. // function printRules(user){ var userStr = ""; var outMsg = ""; if(user){ userStr = user; } outMsg = " --- AUCTION RULES --- \n"; outMsg += " - To bid, you must type \"bid\" in the message field when you tip. \n"; outMsg += " - When there is a tie, the bidder who was the first to have the highest amount wins the tie.\n"; outMsg += " - Your bids accumulate. Whatever you bid will be added on to what you have already bid.\n"; outMsg += " - Type \"/bids\" to see the leader board.\n"; outMsg += " - Type \"/rules\" to see these rules again."; cb.sendNotice( outMsg, userStr, cb.settings['__backgroundColor'], cb.settings['__foregroundColor']); } // // If a username is provided, this function will print out the leader board // only to the user. // // If no username is provided, everyone sees the leader board. // function printLeaderBoard(user){ var userStr = ""; var outMsg = ""; if(user){ userStr = user; } if(( _hours > 0 )||( _minutes > 0 ) || ( _seconds > 0 )){ outMsg = " --- LEADER BOARD with " + _hours + " hrs. " + _minutes + " min. " + _seconds + " sec. left --- \n"; } else{ outMsg = " --- LEADER BOARD --- \n"; } for( var x = 0; x < _bidsArray.length; x++ ){ outMsg += _bidsArray[x][1] + " tokens bid by " + _bidsArray[x][0] + "\n"; } if( _bidsArray.length < 1 ){ outMsg += "No one has bid.\n"; } outMsg = outMsg.slice(0, -1); cb.sendNotice( outMsg, userStr, cb.settings['__backgroundColor'], cb.settings['__foregroundColor']); } // // If a username is provided, this function will print out the intro message // only to the user. // // If no username is provided, everyone sees the message. // function printIntro(user){ if ( __stillRunning ){ var outStr = ""; var userStr = ""; if(user){ userStr = user; } // // if NO ONE has bid // if( _bidsArray.length < 1 ){ outStr = " --- AUCTION TIME --- \n"; outStr += "Bid on \"" + cb.settings['__auctionItem'] + "\" valued at " + cb.settings['__originalTokenValue'] + " tokens.\n"; outStr += "Bidding ends in " + _hours + " hrs. " + _minutes + " min. " + _seconds + " sec.\n"; outStr += " - Type \"/bids\" to see the leader board.\n - Type \"/rules\" to see the rules."; } // // someone has bid // else{ outStr = " --- AUCTION TIME --- \n"; outStr += "Bid on \"" + cb.settings['__auctionItem'] + "\" valued at " + cb.settings['__originalTokenValue'] + " tokens.\n"; outStr += _bidsArray[0][0] + " is the current leader with " + _bidsArray[0][1] + " tokens.\n"; outStr += "Bidding ends in " + _hours + " hrs. " + _minutes + " min. " + _seconds + " sec.\n"; outStr += " - Type \"/bids\" to see the leader board.\n - Type \"/rules\" to see the rules."; } cb.sendNotice( outStr, userStr, cb.settings['__backgroundColor'], cb.settings['__foregroundColor']); } } // // // function handleBid(user, amt) { var retVal = 0; if ( __stillRunning ) { var tempArr = [user, amt]; // // if the user is already in the array, look him up in the second array, and update his bid // if(cbjs.arrayContains(_biddersArray, user)) { for( var x = 0; x < _bidsArray.length; x++ ){ if( _bidsArray[x][0] == user) { _bidsArray[x][1] += amt; cb.sendNotice( "You have bid a total of " + _bidsArray[x][1] + " tokens.", user, cb.settings['__backgroundColor'], cb.settings['__foregroundColor']); } } retVal = 1; } // // if the user is not in the array, add him to both arrays // else{ cb.sendNotice( "You have bid a total of " + amt + " tokens.", user, cb.settings['__backgroundColor'], cb.settings['__foregroundColor']); _biddersArray[_number] = user; _bidsArray[_number] = tempArr; _number++; retVal = 0; } // // Sort the bids // bubbleSort(_bidsArray); var bidStr = ""; // // Announce the leader // //cb.sendNotice( _bidsArray[0][0] + " is the current leader with " + _bidsArray[0][1] + " tokens.", '', cb.settings['__backgroundColor'], cb.settings['__foregroundColor']); bidStr = _bidsArray[0][0] + " is the current leader with " + _bidsArray[0][1] + " tokens.\n"; // // Announce the remaining time // //cb.sendNotice( "Bidding ends in " + _hours + " hrs. " + _minutes + " min. " + _seconds + " sec.", '', cb.settings['__backgroundColor'], cb.settings['__foregroundColor']); bidStr += "Bidding ends in " + _hours + " hrs. " + _minutes + " min. " + _seconds + " sec."; cb.sendNotice( bidStr, '', cb.settings['__backgroundColor'], cb.settings['__foregroundColor']); } else{ // // Announce the time is up and that no more bids can be accepted. // cb.sendNotice( "Time has expired. Cannot accept any bids.", user, cb.settings['__backgroundColor'], cb.settings['__foregroundColor']); } return retVal; } // // prints the status // function printStatus(){ if ( __stillRunning ){ var outStr = ""; // // if NO ONE has bid // if( _bidsArray.length < 1 ){ outStr = " --- AUCTION TIME --- \n"; outStr += "Bid on \"" + cb.settings['__auctionItem'] + "\" valued at " + cb.settings['__originalTokenValue'] + " tokens.\n"; outStr += "Bidding ends in " + _hours + " hrs. " + _minutes + " min. " + _seconds + " sec.\n"; outStr += " - Type \"/bids\" to see the leader board.\n - Type \"/rules\" to see the rules."; } // // someone has bid // else{ outStr = " --- AUCTION UPDATE --- \n"; outStr += "Bid on \"" + cb.settings['__auctionItem'] + "\" valued at " + cb.settings['__originalTokenValue'] + " tokens.\n"; outStr += _bidsArray[0][0] + " is the current leader with " + _bidsArray[0][1] + " tokens.\n"; outStr += "Bidding ends in " + _hours + " hrs. " + _minutes + " min. " + _seconds + " sec.\n"; outStr += " - Type \"/bids\" to see the leader board.\n - Type \"/rules\" to see the rules."; } cb.sendNotice( outStr, '', cb.settings['__backgroundColor'], cb.settings['__foregroundColor']); } } // // // function countdownTimer(){ // Get current date and time var now = new Date().getTime(); // Find the distance between now and the count down date var distance = _countDownDateTime - now; // // Time remaining in days, hours, minutes and seconds // _days = Math.floor(distance / (1000 * 60 * 60 * 24)); _hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); _minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); _seconds = Math.floor((distance % (1000 * 60)) / 1000); // // If the count down is over, write some text // if (distance < 0) { var winnerStr = ""; // // If anyone has bid, announce the winner and the winning bid. // if( _bidsArray.length > 0 ){ winnerStr = " --- WE HAVE A WINNER --- \n"; winnerStr += _bidsArray[0][0] + " wins \"" + cb.settings['__auctionItem'] + "\" for " + _bidsArray[0][1] + " tokens."; cb.sendNotice( winnerStr, '', cb.settings['__backgroundColor'], cb.settings['__foregroundColor']); } else{ cb.sendNotice( "Time for the aution has expired.", '', cb.settings['__backgroundColor'], cb.settings['__foregroundColor']); } // // Print out the final results // printLeaderBoard(); // // Turn off the flag that keeps everything running // __stillRunning = false; // // Stop the timer thread // cb.cancelTimeout(countdownTimer); } // // Still running... // else{ var outMsg = ""; // // at least one person has bid // if( _bidsArray.length > 0 ){ outMsg = _bidsArray[0][0] + " is the current leader with " + _bidsArray[0][1] + " tokens.\n"; outMsg += "Bidding for \"" + cb.settings['__auctionItem'] + "\" ends in " + _hours + " hrs. " + _minutes + " min. " + _seconds + " sec.\n"; outMsg += " - To bid, you must type \"bid\" in the message field when you tip.\n"; outMsg += " - Type \"/bids\" to see the leader board.\n - Type \"/rules\" to see the rules."; } // // no one has bid yet // else{ outMsg += "Bidding for \"" + cb.settings['__auctionItem'] + "\" ends in " + _hours + " hrs. " + _minutes + " min. " + _seconds + " sec.\n"; outMsg += " - To bid, you must type \"bid\" in the message field when you tip.\n"; outMsg += " - Type \"/bids\" to see the leader board.\n - Type \"/rules\" to see the rules."; } // // prints out the status at an interval // if (( _minutes > 1 ) && ( _minutes % _statusInterval === 0 ) && ( _seconds === 0 )){ printStatus(); } // // one minute or less to go // if(( _minutes === 1 ) && ( _seconds === 0 )){ cb.sendNotice( "60 seconds until the auction closes.", '', cb.settings['__backgroundColor'], cb.settings['__foregroundColor']); } if(( _minutes === 0 ) && ( _seconds === 50 )){ cb.sendNotice( "50 seconds until the auction closes.", '', cb.settings['__backgroundColor'], cb.settings['__foregroundColor']); } if(( _minutes === 0 ) && ( _seconds === 40 )){ cb.sendNotice( "40 seconds until the auction closes.", '', cb.settings['__backgroundColor'], cb.settings['__foregroundColor']); } if(( _minutes === 0 ) && ( _seconds === 30 )){ cb.sendNotice( "30 seconds until the auction closes.", '', cb.settings['__backgroundColor'], cb.settings['__foregroundColor']); } if(( _minutes === 0 ) && ( _seconds === 20 )){ cb.sendNotice( "20 seconds until the auction closes.", '', cb.settings['__backgroundColor'], cb.settings['__foregroundColor']); } if(( _minutes === 0 ) && ( _seconds === 10 )){ cb.sendNotice( "LAST CALL - 10 seconds until the auction closes.", '', cb.settings['__backgroundColor'], cb.settings['__foregroundColor']); } // keep going cb.setTimeout(countdownTimer, 1000); } } function init(){ _statusInterval = parseInt(cb.settings['__interval']); } init(); function bubbleSort(array) { var swapped; do { swapped = false; for(var i = 0; i < array.length; i++) { if(array[i] && array[i + 1] && array[i][1] < array[i + 1][1]) { swap(array, i, i + 1); swapped = true; } } } while(swapped); return array; } // // swap function helper // function swap(array, i, j) { var temp = array[i]; array[i] = array[j]; array[j] = temp; }
© Copyright Chaturbate 2011- 2024. All Rights Reserved.