73 lines
1.7 KiB
JavaScript
73 lines
1.7 KiB
JavaScript
// 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)); // don’t go negative
|
||
return true;
|
||
},
|
||
reset() {
|
||
setCredits(0);
|
||
},
|
||
get() {
|
||
return getCredits();
|
||
}
|
||
};
|
||
})();
|
||
|
||
// Initialize once DOM is ready
|
||
document.addEventListener("DOMContentLoaded", () => {
|
||
Credits.init();
|
||
});
|