Apps Home
|
Create an App
FoxyRaffle2
Author:
holymoly9697
Description
Source Code
Launch App
Current Users
Created by:
Holymoly9697
cb.settings_choices = [ {name: 'totalTickets', type: 'int', defaultValue: 0, label: 'Total Tickets (0 = no limit)'}, {name: 'ticketPrice', type: 'int', required: true, label: 'Ticket Price'}, { name: 'multiWin', type: 'choice', choice1: 'yes', choice2: 'no', defaultValue: 'yes', label: 'Win more than once?' }, {name: 'timelimit', type: 'int', defaultValue: 10, label: 'Timelimit in minutes (0 = no limit)'}, {name: 'maxTicketsPerUser', type: 'int', defaultValue: 0, label: 'Max tickets per user (0 = no limit)'}, {name: 'rulesNotificationTime', type: 'int', defaultValue: 5, label: 'Display rules (minutes) (0 = disable)'}, {name: 'prize1', type: 'str', required: false}, {name: 'prize2', type: 'str', required: false}, {name: 'prize3', type: 'str', required: false}, {name: 'prize4', type: 'str', required: false}, {name: 'prize5', type: 'str', required: false}, {name: 'prize6', type: 'str', required: false}, {name: 'prize7', type: 'str', required: false}, {name: 'prize8', type: 'str', required: false}, {name: 'prize9', type: 'str', required: false}, {name: 'prize10', type: 'str', required: false}, ]; const totalTickets = cb.settings.totalTickets; const ticketPrice = cb.settings.ticketPrice; const multiWin = cb.settings.multiWin; const timelimit = cb.settings.timelimit; const maxTicketsPerUser = cb.settings.maxTicketsPerUser; const rulesNotificationTime = cb.settings.rulesNotificationTime; const members = {}; const tickets = []; const winners = []; const prizes = []; const startTime = new Date(); let ticketsAvailable = totalTickets; let ticketsSold = 0; let timer = null; let hasDrawn = false; let setup = false; showRules(); cb.onEnter(user => { if(!hasDrawn){ if(user.is_mod){ cb.sendNotice(`ooooooooh ${user.user} is here! better give them a winning ticket. just kidding. (or am i?)`, null, '#ffd700', null, 'bold'); } } }); cb.onTip(tip => { if(!hasDrawn){ const user = tip.from_user; const amt = tip.amount; const isTicketPrice = amt % ticketPrice === 0 if(isTicketPrice){ const numTickets = amt / ticketPrice purchaseTicket(user, numTickets); } } }); cb.onMessage(msg => { const user = msg.user; const txt = msg.m; const command = txt.split(' '); if (user === cb.room_slug || msg.is_mod) { switch (command[0]) { case '/drawWinners': drawWinners(); msg['X-Spam'] = true; break; case '/drawNow': drawWinners(true); msg['X-Spam'] = true; break; case '/draw': drawOne(); msg['X-Spam'] = true; break; case '/winners': showWinners(); msg['X-Spam'] = true; break; case '/give': if(command.length === 3){ let toUser = command[1]; let num = parseInt(command[2]); purchaseTicket(toUser, num); cb.sendNotice(`Gave ${toUser} ${num} tickets`, cb.room_slug); if (msg.is_mod) { cb.sendNotice(`Gave ${toUser} ${num} tickets`, user); } } else { cb.sendNotice('Invalid give command usage. Should be /give username ##', user); } break; case '/save': saveResults(); msg['X-Spam'] = true; break; default: break; } } switch (command[0]) { case '/rules': showRules(user); msg['X-Spam'] = true; break; default: break; } return msg; }); cb.onDrawPanel(user => { return { template: '3_rows_of_labels', row1_label: totalTickets > 0 ? 'Tickets (Sold / Total): ' : 'Tickets sold: ', row1_value: totalTickets > 0 ? `${ticketsSold} / ${totalTickets}` : `${ticketsSold}`, row2_label: 'Last ticket: ', row2_value: tickets.length > 0 ? `#${tickets.length} - ${tickets[tickets.length - 1]}` : `N/A`, row3_label: 'Ticket price: ', row3_value: `${ticketPrice} token(s)` } }); function showRules(user){ let sendUser = user || null; if(user === cb.room_slug){ sendUser = null; } if(!setup){ setup = true; cb.sendNotice(`FoxyRaffle has begun! Tip multiples of ${ticketPrice} token(s) to buy a ticket.`, null, '#ffd700', null, 'bold'); if(totalTickets > 0){ cb.sendNotice(`There are ${totalTickets} tickets available. Grab them fast!`, null, '#ffd700', null, 'bold'); } if (maxTicketsPerUser > 0) { cb.sendNotice(`You can only purchase up to ${maxTicketsPerUser} ticket(s) for this raffle`, null, '#ffd700', null, 'bold'); } if (timelimit > 0) { timer = setTimeout(drawWinners, timelimit * 60 * 1000); cb.sendNotice(`Winners will be drawn in ${timelimit} minute(s)!`, null, '#ffd700', null, 'bold'); } else { cb.sendNotice(`Foxy will choose when to draw the winners!`, null, '#ffd700', null, 'bold'); } cb.sendNotice(`Prizes available to win:`, null, '#ffd700', null, 'bold'); for (let i = 1; i <= 10; i++) { let prize = cb.settings['prize' + i]; if (prize !== '') { prizes.push(prize); cb.sendNotice(`${prizes.length}) ${prize}`, null, '#ffd700', null, 'bold'); } } } else if(!hasDrawn) { const timeDiff = Math.ceil((new Date().getTime() - startTime.getTime()) / (1000 * 60)); const drawTime = timelimit - timeDiff; cb.sendNotice(`FoxyRaffle is in progress! Tip multiples of ${ticketPrice} token(s) for a chance to win prizes!`, sendUser, '#ffd700', null, 'bold'); if(totalTickets > 0){ cb.sendNotice(`There are ${totalTickets} tickets available. Grab them fast!`, sendUser, '#ffd700', null, 'bold'); } if (maxTicketsPerUser > 0) { cb.sendNotice(`You can only purchase up to ${maxTicketsPerUser} ticket(s) for this raffle`, sendUser, '#ffd700', null, 'bold'); } if (timelimit > 0) { if(drawTime > 0){ cb.sendNotice(`Winners will be drawn in approx. ${drawTime} minute(s)!`, sendUser, '#ffd700', null, 'bold'); } else { cb.sendNotice(`Winners will be drawn shortly!`, sendUser, '#ffd700', null, 'bold'); } } else { cb.sendNotice(`Foxy will choose when to draw the winners!`, sendUser, '#ffd700', null, 'bold'); } cb.sendNotice(`Prizes available to win:`, sendUser, '#ffd700', null, 'bold'); for (let i = 0; i < prizes.length; i++) { let prize = prizes[i]; cb.sendNotice(`${i+1}) ${prize}`, sendUser, '#ffd700', null, 'bold'); } } if(!user && rulesNotificationTime > 0 && !hasDrawn){ setTimeout(showRules, rulesNotificationTime * 60 * 1000); } } function purchaseTicket(user, numTickets) { /*if (totalTickets !== 0 && ticketsAvailable <= 0) { cb.sendNotice('Sorry, tickets are sold out!', user, '#ffd700', null, 'bold'); return; }*/ if (!members[user]) { members[user] = 0; } /*if (maxTicketsPerUser > 0 && members[user] + numTickets >= maxTicketsPerUser) { cb.sendNotice('You already have the maximum amount of tickets!', user, '#ffd700', null, 'bold'); return; }*/ const ticketNum = tickets.length + 1; for(let i = 0; i < numTickets; i++){ tickets.push(user); } const newTicketNum = tickets.length; members[user] += numTickets; ticketsAvailable -= numTickets; ticketsSold += numTickets; if(numTickets === 1){ cb.sendNotice(`You bought ticket #${ticketNum}! You have a total of ${members[user]} ticket(s)`, user, '#ffd700', null, 'bold'); } else { cb.sendNotice(`You bought tickets #${ticketNum}-${newTicketNum}! You have a total of ${members[user]} ticket(s)`, user, '#ffd700', null, 'bold'); } cb.drawPanel(); /*if (totalTickets > 0 && ticketsAvailable <= 0) { drawWinners(); }*/ } function drawWinners(now) { if(!hasDrawn){ if (totalTickets === 0 || ticketsAvailable > 0) { if(now){ doDrawing(); } else { cb.sendNotice('Drawing raffle winners in 1 minute! Get your last minute tickets quick!', null, '#ffd700', null, 'bold'); setTimeout(doDrawing, 60 * 1000); } } else { doDrawing(); } } } function doDrawing() { if(tickets.length <= 0){ cb.sendNotice(`Can't draw winners until at least 1 ticket has sold. You can redraw with /drawWinners command.`, cb.room_slug, '#ffd700', null, 'bold'); return } if(!hasDrawn){ hasDrawn = true; cb.sendNotice('Raffle drawing commencing!', null, '#ffd700', null, 'bold'); let usedNames = []; let usedNums = []; for (let p = 0; p < prizes.length; p++) { let winner, num; let retries = 0; do { num = randNumber(0, tickets.length - 1); winner = tickets[num]; retries++; } while (retries <= 100 && (usedNames.indexOf(winner) !== -1 || usedNums.indexOf(num) !== -1)); if(retries >= 100){ break; } if(multiWin === 'no'){ usedNames.push(winner); } usedNums.push(num); winners.push({ name: winner, prize: prizes[p], number: num + 1 }); } showWinners(); cb.sendNotice('Congratulations to our winners and thanks for playing!', null, '#ffd700', null, 'bold'); } } function drawOne(){ if(tickets.length > 0){ let num = randNumber(0, tickets.length - 1); let winner = tickets[num]; let obj = { name: winner, prize: 'Foxy\'s choice', number: num + 1 }; winners.push(obj); cb.sendNotice(`Winner: #${obj.number} - ${winner} Prize: ${obj.prize}`, null, '#ffd700', null, 'bold'); } } function showWinners(){ if(winners.length){ for(let i = 0; i < winners.length; i++){ let winner = winners[i]; cb.sendNotice(`${i + 1}) Winner: #${winner.number} - ${winner.name} Prize: ${winner.prize}`, null, '#ffd700', null, 'bold'); } if(winners.length < prizes.length){ cb.sendNotice('Not all prizes are claimed because we couldn\'t find enough unique winners. Foxy can draw additional winners using the /draw command.', null, '#ffd700', null, 'bold'); } } } function saveResults(){ cb.sendNotice(JSON.stringify({tickets, winners}), cb.room_slug); } function randNumber(min, max) { return Math.floor(Math.random() * (max + 1 - min) + min); }
© Copyright Chaturbate 2011- 2024. All Rights Reserved.