Apps Home
|
Create an App
Handy Lottery
Author:
juirrl
Description
Source Code
Launch App
Current Users
Created by:
Juirrl
cb.settings_choices = function () { var output = []; var ins_index = 0; makeInstructions('Lottery Settings'); output.push({name: 'lotteryTime', label: 'Lottery time (seconds).', type: 'int', defaultValue: 300}); output.push({name: 'ticketPrice', label: 'Ticket Price.', type: 'int', defaultValue: 25}); output.push({name: 'rotate', label: 'Automatically start new lottery.', type: 'choice', choice1: 'Yes', choice2: 'No', defaultValue: 'Yes'}); output.push({name: 'rColor', label: 'Text foreground color', type: 'str', defaultValue: '#000000', required: false}); output.push({name: 'rBg', label: 'Text background color', type: 'str', defaultValue: '75FAFA', required: false}); function makeInstructions(text) { output.push({name: 'instructions_' + ins_index, label: text, type: 'choice', choice1: '', required: false}); ins_index++; } return output; }(); const commandSuffix = "Handy Set Sliders:"; endTime = -1; var lastInstruction = ""; var lotteryWinner = ''; var startTime; var strokeString = ''; var speedString = ''; var lotteryRunning = false; var tickets = []; var rotationTracker; var delayTracker; var stopControlTracker; var panelTracker; var panelUpdateTime = 2; var lotteryDelay = 10; var lotteryTime = $('lotteryTime'); var ticketPrice = $('ticketPrice'); var rotate = $('rotate'); var rBg = $('rBg'); var rColor = $('rColor'); var noticeResponse = $('response'); function $ (str, uc) { var setting = cb.settings[str]; if (typeof setting == 'string') { if (uc) setting = setting.toUpperCase(); setting = setting.trim(); if (setting.length > 0) return setting; } else { if (setting) return setting; } } function toMinutesAndSeconds(totalSeconds) { const totalMinutes = Math.floor(totalSeconds / 60); const seconds = totalSeconds % 60; const minutes = totalMinutes % 60; if ( minutes <= 0 ) { return seconds + 's'; } return minutes + 'm ' + seconds + 's'; } function showHelpMenu(msg) { var str = "--------------------- Handy Lottery Help Menu ---------------------"; if (msg['user'] == cb.room_slug ) { str += "\n/handy start lottery - Start the handy control lottery."; str += "\n/handy stop lottery - Stop the handy control lottery."; str += "\n/handy resend - Resend the last message send to the handy."; } if (msg['user'] == cb.room_slug || msg['user'] == lotteryWinner ) { str += "\n/handy set X Y Z - Set the handy to start its stroke at X, end at Y, and speed Z. X, Y, and Z are numbers between 0 and 100."; } say(str, msg['user'], rColor, rBg ); } function say(str, user, color, bg) { cb.sendNotice(str, user, bg, color, 'bold', ''); } function setSlider(msg) { var words = msg['m'].split(" "); if ( words.length != 5 ) { say('Wrong format. \'/handy set X Y X\' Where X Y and Z are numbers between 0 and 100', msg['user']); return; } var lowerLimit = parseInt(words[2]); var upperLimit = parseInt(words[3]); var sliderSpeed = parseInt(words[4]); if ( lowerLimit < 0 ) lowerLimit = 0; if ( lowerLimit > 90 ) lowerLimit = 90; if ( upperLimit < 10 ) upperLimit = 10; if ( upperLimit > 100 ) upperLimit = 100; if ( upperLimit < lowerLimit ) upperLimit = lowerLimit + 10; if ( sliderSpeed < 0 ) sliderSpeed = 0; if ( sliderSpeed > 100 ) sliderSpeed = 100; var output = msg['user']+ ' is setting the handy to:\n'; strokeString = ''; for (let i = 0; i <20; i += 1 ) { if ( i * 5 <= lowerLimit ) { strokeString += "_"; } else if ( i*5 > upperLimit ) { strokeString += "_"; } else { strokeString += "#"; } } output += 'Stroke: ' + strokeString; speedString = ''; for (let i = 0 ; i <20 ; i += 1 ) { if ( i * 5 < sliderSpeed ) { speedString += "#"; } else { speedString += "_"; } } output += "\nSpeed: " + speedString; say(output, '', rColor, rBg ); lastInstruction = commandSuffix + lowerLimit + ' ' + upperLimit + ' ' + sliderSpeed; cb.log(lastInstruction); } function stopLottery(msg) { if (!lotteryRunning) { say('The lottery is already stopped.', msg['user'], rColor, rBg ); return; } endTime = -1; var output = '----------------------------------------\n'; output += 'The broadcaster has ended the Handy lottery!\n'; output += 'Sorry, the fun is over.\n'; output += '----------------------------------------'; tickets = []; lotteryRunning = true; cb.cancelTimeout(rotationTracker); cb.cancelTimeout(delayTracker); say(output, '', rColor, rBg ); } function startLottery(msg) { if (lotteryRunning) { say('The lottery is already running.', msg['user'], rColor, rBg ); return; } doStartLottery(); } function doStartLottery() { var output = '----------------------------------------\n'; output += 'The Handy lottery has started!\n'; output += 'Ticket price is ' + ticketPrice + ' tokens!\n'; output += 'Tip ' + ticketPrice + ' tokens to enter, enter as many times as you want!\n'; output += 'In ' + toMinutesAndSeconds(lotteryTime) + ' a winner will be selected.\n'; output += 'Winner gains full control of The Handy for ' + toMinutesAndSeconds(lotteryTime) + '!\n' output += 'Good luck!\n'; output += '----------------------------------------'; tickets = []; lotteryRunning = true; cb.cancelTimeout(rotationTracker); endTime = new Date().getTime() + lotteryTime * 1000; rotationTracker = cb.setTimeout(pickWinner, lotteryTime * 1000); updatePanel(); say(output, '', rColor, rBg ); } function stopControl() { if ( lotteryWinner == '' ) return; var output = '----------------------------------------\n'; output += 'Sorry, times up, you can no longer control the handy.\n'; output += '----------------------------------------'; say(output, lotteryWinner, rColor, rBg ); lotteryWinner = ''; } cb.onDrawPanel(function(user) { if ( ! lotteryRunning && lotteryWinner == '') { return { 'template': '3_rows_of_labels', 'row1_label': 'The Handy Lottery!', 'row1_value': '', 'row2_label': '', 'row2_value': '', 'row3_label': '', 'row3_value': '' }; } var timeString = ''; var ticketString = ''; if ( lotteryRunning ) { var timeString = getRemainingTime() + ' remaining'; var ticketString = 'Tickets ' + ticketPrice + 'tks'; } var strStr = ''; var spdStr = ''; var winStr = ''; if ( lotteryWinner != '' ) { strStr = strokeString; spdStr = speedString; winStr = 'Winner: ' + lotteryWinner; } return { 'template': '3_rows_of_labels', 'row1_label': 'The Handy Lottery!', 'row1_value': timeString, 'row2_label': ticketString, 'row2_value': winStr, 'row3_label': strStr, 'row3_value': spdStr }; }); function updatePanel() { cb.drawPanel(); cb.cancelTimeout(panelTracker); panelTracker = cb.setTimeout(updatePanel, panelTracker * 1000); } function getRemainingTime() { if ( endTime <= 0 ) return 0; return toMinutesAndSeconds( Math.floor( ( endTime - ( new Date().getTime() ) ) / 1000 ) ); } function pickWinner() { if ( tickets.length <= 0 ) { cb.cancelTimeout(stopControlTracker); stopControl(); lotteryWinner = ''; if ( rotate == 'Yes' ) { doStartLottery(); } else { say('The lottery has ended!', '', rColor, rBg ); endTime = -1; lotteryRunning = false; } return; } var newWinner = tickets[Math.floor(Math.random() * (tickets.length))]; tickets = []; var output = '----------------------------------------\n'; output += 'The lottery has ended, time to pick a winner!\n'; output += 'And the winner is: ' + newWinner + ' !!!\n'; output += 'Congratulations, ' + newWinner + ', you have full control of the handy for '+ toMinutesAndSeconds(lotteryTime) +'!\n'; output += '----------------------------------------'; say(output, '', rColor, rBg ); if ( newWinner == lotteryWinner ) { var output = '----------------------------------------\n'; output += 'Congratulations! You have won again and retained control!\n'; output += '----------------------------------------'; say(output, newWinner, rColor, rBg ); } else { cb.cancelTimeout(stopControlTracker); stopControl(); var output = '----------------------------------------\n'; output += 'Congratulations! You have won control of the handy\n'; output += 'To control the handy type "/handy set X Y Z"\n'; output += 'X Y and Z must be numbers between 0 and 100\n'; output += 'X - The bottom of the handys stroke\n'; output += 'Y - The top of the handys stroke\n'; output += 'Z - The handys speed\n'; output += 'For example, for very long medium speed strokes type:\n'; output += '/handy set 0 100 50\n'; output += 'For a fast tease the tip type:\n'; output += '/handy set 65 100 80\n'; output += 'Have fun!\n'; output += '----------------------------------------'; say(output, newWinner, rColor, rBg ); } lotteryWinner = newWinner; cb.cancelTimeout(stopControlTracker); stopControlTracker = cb.setTimeout(stopControl, ( lotteryTime +lotteryDelay )* 1000 + 100); if ( rotate == 'No' ) { say('Rototation off, ending lottery', '', rColor, rBg ); lotteryRunning = false; } else { cb.cancelTimeout(delayTracker); delayTracker = cb.setTimeout(doStartLottery, lotteryDelay * 1000); } } function resend(msg) { cb.log(lastInstruction); } cb.onTip(function (tip) { if ( ! lotteryRunning || tip.is_anon_tip ) return; if ( tip.amount == ticketPrice ) { tickets.push(tip.from_user); const counts = {}; for (const num of tickets) { counts[num] = counts[num] ? counts[num] + 1 : 1; } output = 'Thank you ' + tip.from_user + '! You have been entered into the lottery. You have ' + counts[tip.from_user] + ' tickets out of ' + tickets.length + '.'; say(output, '', rColor, rBg ); } }); cb.onMessage(function (msg) { const prefix = "/handy"; if ( msg == undefined || msg['m'] == undefined ) return msg; var words = msg['m'].toLowerCase().split(" "); if ( words[0] != prefix ) return msg; msg['m'] = msg['m'].toLowerCase(); //-------- Commands only accessible to broadcaster ------------ if (msg['user'] == cb.room_slug ) { if ( words[1] == "start" && words[2] == "lottery" ) { startLottery(msg); } if ( words[1] == "stop" && words[2]== "lottery" ) { stopLottery(msg); } if ( words[1] == "resend" ) { resend(msg); } } if (msg['user'] == cb.room_slug || msg['user'] == lotteryWinner ) { if ( words[1] == "set" ) { setSlider(msg); } } //-------- Commands accessible to everyone ------------ if (words[1] == "help" ) { showHelpMenu(msg); } msg['X-Spam'] = true; return msg; });
© Copyright Freesexcam 2011- 2024. All Rights Reserved.