Apps Home
|
Create an App
Leader Countdown
Author:
dckfghtr1
Description
Source Code
Launch App
Current Users
Created by:
Dckfghtr1
var ONE_SECOND = 1000; var ONE_MINUTE = 60 * ONE_SECOND; var scoreboard = {}; var minimumBid = cb.settings.minimum_bid; var reminingTimeAnnounceInterval = cb.settings.remaining_time_announce_interval * ONE_MINUTE; var totalTime = cb.settings.duration_minutes * ONE_MINUTE; var elapsedTime = 0; var lastRemainingTimeAnnouncement = null; var lastTime = Date.now(); var timer = null; var highScore = null; var timeRemaining = totalTime / ONE_MINUTE; var panel = null; var done = false; var showPreamble = function(who) { cb.sendNotice(["Tip to gain points and increase your score!", "Whoever is in the lead when the time runs out wins."].join(" "), who, null, null, "bold"); cb.sendNotice(cb.settings.join_notification, who, null, null, "bold"); } var updateHighScore = function(score, user) { highScore.score = score; highScore.user = user; drawPanel(); announceHighScore(); } var updateTimeRemaining = function() { var time = Math.ceil((totalTime - elapsedTime) / ONE_MINUTE); if(time !== timeRemaining) { timeRemaining = time; drawPanel(); announceTimeRemaining(); } } var announceTimeRemaining = function() { var now = Date.now(); if(!lastRemainingTimeAnnouncement || now - lastRemainingTimeAnnouncement > reminingTimeAnnounceInterval) { cb.sendNotice([timeRemaining, (timeRemaining == 1) ? "minute" : "minutes", "remaining."].join(" ")); } lastRemainingTimeAnnouncement = now; } var announceHighScore = function() { if(highScore.user) { cb.sendNotice([highScore.user, "is now in the lead with a score of", highScore.score].join(" ")); } else { cb.sendNotice(["No one is in the lead! Tip", minimumBid, "to become the leader."].join(" ")); } } var announceWinner = function() { if(highScore.user) { cb.sendNotice(["Time's up! The leader is", highScore.user, "with a final score of", highScore.score, "! Thanks to everyone who tipped!"].join(" ")); } else { cb.sendNotice(["Time's up! No winner."].join(" ")); } } var findHighScore = function() { var users = Object.keys(scoreboard); var currentHighScoreUser = null; users.forEach(function(user) { if(scoreboard[user].present && scoreboard[user].score > highScore.score && scoreboard[user].score > minimumBid) { currentHighScoreUser = user; } }); if(currentHighScoreUser) { return { score: scoreboard[currentHighScoreUser].score, user: currentHighScoreUser }; } else { return { score: 0, user: undefined }; } } var handleTip = function(tip) { if(done) return; var user = tip["from_user"]; var amount = tip["amount"]; if(!scoreboard.hasOwnProperty(user)) { scoreboard[user] = { score: 0, present: true }; } // TODO: apply random bonuses var points = amount; scoreboard[user].score += points; cb.sendNotice([user, "gained", points, "points and has a new score of", scoreboard[user].score, "points."].join(" ")); if(scoreboard[user].score >= minimumBid && scoreboard[user].score > highScore.score) { updateHighScore(scoreboard[user].score, user); } } var handleEnter = function(user) { if(done) return; var who = user["user"]; if(scoreboard.hasOwnProperty(who)) { scoreboard[who].present = true; if(scoreboard[who].score > highScore.score) { updateHighScore(scoreboard[who].score, who); } } showPreamble(who); } var handleLeave = function(user) { if(done) return; var who = user["user"]; if(scoreboard.hasOwnProperty(who)) { scoreboard[who].present = false; if(highScore.user == who) { var newHighScore = findHighScore(); updateHighScore(newHighScore.score, newHighScore.user); } } } var updatePanel = function() { panel = { "template": "3_rows_of_labels", "row1_label": "Leader", "row1_value": highScore.user || "-", "row2_label": "Score", "row2_value": highScore.score, "row3_label": "Time remaining", "row3_value": [timeRemaining, (timeRemaining == 1) ? "minute" : "minutes"].join(" ") }; } var drawPanel = function() { updatePanel(); cb.drawPanel(); } var handleDrawPanel = function() { return panel; } var handleTimer = function() { var now = Date.now(); var elapsed = now - lastTime; lastTime = now; elapsedTime += elapsed; if(elapsedTime < totalTime) { updateTimeRemaining(); timer = cb.setTimeout(handleTimer, 10 * ONE_SECOND); } else { timer = null; done = true; updateTimeRemaining(0); announceWinner(); } } var handleMessage = function(message) { var who = message["user"]; var text = message["m"]; if("/" == text[0]) { message["X-Spam"] = true; if(done) return message; if(who !== cb.room_slug) { if("/score" == text) { if(scoreboard.hasOwnProperty(who)) { if(who == highScore.user) { cb.sendNotice(["You're the leader with", scoreboard[who].score, "points!"], who); } else { cb.sendNotice(["You have", scoreboard[who].score, "points."], who); } } else { cb.sendNotice(["You haven't tipped yet!"], who); } } } else if(who == cb.room_slug) { if(text.match(/^\/extend/)) { var extendTime = parseInt(text.split(" ")[1]); if(!Number.isNaN(extendTime)) { extendTime = extendTime * ONE_MINUTE; totalTime += extendTime; if(totalTime > 360 * ONE_MINUTE) { totalTime = 360 * ONE_MINUTE; } updateTimeRemaining(); } } } } return message; } highScore = findHighScore(); cb.onEnter(handleEnter); cb.onLeave(handleLeave); cb.onTip(handleTip); cb.onDrawPanel(handleDrawPanel); cb.onMessage(handleMessage); cb.settings_choices = [ { name: "duration_minutes", type: "int", minValue: "1", maxValue: "360", defaultValue: "60", label: "Duration (minutes)" }, { name: "minimum_bid", type: "int", minValue: "1", maxValue: "1000", defaultValue: "25", label: "Minimum bid" }, { name: "remaining_time_announce_interval", type: "int", minValue: "1", maxValue: "10", defaultValue: 1, label: "Remaining time announce interval (minutes)" }, { name: "join_notification", type: "str", minLength: 1, maxLength: 255, label: "Join notification" } ]; timer = cb.setTimeout(handleTimer, ONE_MINUTE); showPreamble(); updatePanel();
© Copyright Chaturbate 2011- 2025. All Rights Reserved.