jukebox/credits.js
Eric Villnow a796176a17 init
2025-10-03 21:29:17 -05:00

73 lines
1.7 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// credits.js
const Credits = (() => {
const STORAGE_KEY = "jukeboxCredits";
// Get credits from storage or initialize to 0
function getCredits() {
const stored = localStorage.getItem(STORAGE_KEY);
return stored ? parseInt(stored, 10) : 0;
}
// Save credits back to storage
function setCredits(value) {
localStorage.setItem(STORAGE_KEY, value);
updateDisplay();
}
// Update the UI
function updateDisplay() {
const el = document.getElementById("creditDisplay");
if (el) {
el.textContent = getCredits();
}
const nextEl = document.getElementById("trackModalNextButton");
const queueEl = document.getElementById("trackModalQueueButton");
if(Credits.get()>=3){
nextEl.classList.remove('disabled');
}else{
nextEl.classList.add('disabled');
}
if(Credits.get()>=2){
queueEl.classList.remove('disabled');
document.getElementById('msg').innerHTML = "";
}else{
queueEl.classList.add('disabled');
document.getElementById('msg').innerHTML = "<span class='badge rounded-pill text-bg-danger'>No Moneys</span>";
}
}
// Public API
return {
init() {
updateDisplay();
},
add(amount = 1) {
const current = getCredits();
setCredits(current + amount);
},
remove(amount = 1) {
const current = getCredits();
if(amount > current){
return false;
}
setCredits(Math.max(0, current - amount)); // dont go negative
return true;
},
reset() {
setCredits(0);
},
get() {
return getCredits();
}
};
})();
// Initialize once DOM is ready
document.addEventListener("DOMContentLoaded", () => {
Credits.init();
});