Simple Dice Roller

Simple Dice Roller

Roll a die to start ...

Roll History

No rolls yet.

Add New Die Type

Dice Library

No dice configured. Go to Data Configuration to add some.

'; sdr_diceLibraryListEl.innerHTML = '

No dice in library.

'; return; } sdr_data.diceTypes.forEach(sides => { // Add button to Dashboard const btn = document.createElement('button'); btn.type = 'button'; btn.className = 'sdr-btn sdr-btn-primary sdr-dice-btn'; btn.textContent = `Roll d${sides}`; btn.setAttribute('onclick', `sdr_rollDie(${sides})`); sdr_diceButtonsEl.appendChild(btn); // Add item to Config List const item = document.createElement('div'); item.className = 'sdr-list-item'; item.innerHTML = ` d${sides} `; sdr_diceLibraryListEl.appendChild(item); }); } /** * Adds a new die type */ window.sdr_addDieType = function(event) { event.preventDefault(); const sides = parseInt(sdr_newDieSidesInput.value); if (isNaN(sides) || sides < 2) { alert('Please enter a valid number of sides (2 or more).'); return; } if (sdr_data.diceTypes.includes(sides)) { alert(`A d${sides} already exists in the library.`); return; } sdr_data.diceTypes.push(sides); sdr_renderDice(); sdr_newDieSidesInput.value = ''; } /** * Deletes a die type */ window.sdr_deleteDieType = function(sides) { if (confirm(`Are you sure you want to delete the d${sides}?`)) { sdr_data.diceTypes = sdr_data.diceTypes.filter(d => d !== sides); sdr_renderDice(); } } // --- TAB & NAVIGATION FUNCTIONS --- window.sdr_switchTab = function(tabIndex) { sdr_currentTabIndex = tabIndex; sdr_tabContents.forEach((content, index) => { content.classList.toggle('sdr-active', index === tabIndex); }); sdr_tabs.forEach((tab, index) => { tab.classList.toggle('sdr-active', index === tabIndex); }); sdr_updateNavButtons(); } window.sdr_navigateTabs = function(direction) { if (direction === 'next' && sdr_currentTabIndex < sdr_numTabs - 1) { sdr_switchTab(sdr_currentTabIndex + 1); } else if (direction === 'prev' && sdr_currentTabIndex > 0) { sdr_switchTab(sdr_currentTabIndex - 1); } } function sdr_updateNavButtons() { sdr_prevBtn.style.visibility = (sdr_currentTabIndex === 0) ? 'hidden' : 'visible'; sdr_nextBtn.style.visibility = (sdr_currentTabIndex === sdr_numTabs - 1) ? 'hidden' : 'visible'; } // --- PDF GENERATION --- window.sdr_generatePdf = function() { if (typeof jspdf === 'undefined' || typeof jspdf.jsPDF === 'undefined') { alert('Error: PDF generation library (jsPDF) not loaded.'); return; } if (typeof window.jspdf.plugin.autotable === 'undefined') { alert('Error: PDF generation library (jsPDF-AutoTable) not loaded.'); return; } if (sdr_data.rollHistory.length === 0) { alert("There is no roll history to download. Please roll some dice first."); return; } try { const { jsPDF } = jspdf; const doc = new jsPDF(); doc.setFont('helvetica', 'bold'); doc.setFontSize(20); doc.setTextColor(varGet('--sdr-primary-color', '#0073e6')); doc.text("Dice Roll History", 105, 20, { align: 'center' }); doc.setFontSize(11); doc.setTextColor(varGet('--sdr-text-color', '#333')); doc.text(`Total Rolls: ${sdr_data.rollHistory.length}`, 14, 30); const tableColumn = ["Roll #", "Die Type", "Result"]; const tableRows = []; // History is unshifted, so we reverse for chronological order in PDF const historyToExport = [...sdr_data.rollHistory].reverse(); historyToExport.forEach((roll, index) => { tableRows.push([ index + 1, roll.die, roll.result ]); }); doc.autoTable({ head: [tableColumn], body: tableRows, startY: 40, theme: 'striped', headStyles: { fillColor: varGet('--sdr-primary-color', '#0073e6'), textColor: varGet('--sdr-text-light', '#fff') }, styles: { font: 'helvetica', cellPadding: 3 }, alternateRowStyles: { fillColor: varGet('--sdr-light-primary', '#e6f1fc') } }); doc.save('dice_roll_history.pdf'); } catch (e) { console.error("PDF Generation Error: ", e); alert("An error occurred while generating the PDF."); } } // --- UTILITY FUNCTIONS --- function varGet(varName, fallback = '#000') { try { return getComputedStyle(document.documentElement).getPropertyValue(varName).trim() || fallback; } catch (e) { return fallback; } } // --- INITIALIZATION --- function sdr_initTool() { sdr_switchTab(0); sdr_renderDice(); sdr_renderRollHistory(); } sdr_initTool(); // Run the tool }); // End of DOMContentLoaded