$(document).ready(function() { // Top Checkbox Controls $('input[type="checkbox"]').change(function() { var name = $(this).attr('name'); if ( 'trackAC' == name ) { var eles = document.getElementsByClassName('ac-tracker'); } if ( 'trackHP' == name ) { var eles = document.getElementsByClassName('hp-tracker'); } if ( 'trackRounds' == name ) { var eles = document.getElementsByClassName('rounds-tracker'); } if ($(this).is(':checked')) { Array.prototype.forEach.call(eles, function(ele) { ele.style.display = ''; }); } else { Array.prototype.forEach.call(eles, function(ele) { ele.style.display = 'none'; }); } }); $(document).on('click', '.character-remove', function() { var characterId = $(this).closest('tr').data('character-id'); var allTrsWithCharacterId = document.querySelectorAll(`tr[data-round-character-id="${characterId}"]`); allTrsWithCharacterId.forEach(function(tr) { tr.remove(); }); var allTrsWithCharacterId = document.querySelectorAll(`tr[data-character-id="${characterId}"]`); allTrsWithCharacterId.forEach(function(tr) { tr.remove(); }); findByIdAndRemove( characterId, characterInitiativeList ); }); $(document).on('click', '.hp-minus', function() { var characterId = $(this).closest('tr').data('character-id'); var dataValue = parseInt($(this).data('value'), 10); var allTrsWithCharacterId = document.querySelectorAll(`tr[data-character-id="${characterId}"]`); allTrsWithCharacterId.forEach(function(tr) { var $input = $(tr).find('input[name="hp"]'); var newValue = parseInt($input.val(), 10) - dataValue; $input.val(newValue); }); var hpIndex = findById( characterId ); hpIndex.hp = parseInt(hpIndex.hp) - dataValue; }); $(document).on('click', '.hp-plus', function() { var characterId = $(this).closest('tr').data('character-id'); var dataValue = parseInt($(this).data('value'), 10); var allTrsWithCharacterId = document.querySelectorAll(`tr[data-character-id="${characterId}"]`); allTrsWithCharacterId.forEach(function(tr) { var $input = $(tr).find('input[name="hp"]'); var newValue = parseInt($input.val(), 10) + dataValue; $input.val(newValue); }); var hpIndex = findById( characterId ); hpIndex.hp = parseInt(hpIndex.hp) + dataValue; }); }); let currentCharacter; let roundCount = 0; let roundHistory = []; let characterInitiativeList = []; function findById(id) { return characterInitiativeList.find(item => item.id === id); } function findByIdAndRemove(id, array) { const index = array.findIndex(item => item.id === id); if (index !== -1) { array.splice(index, 1); } } const resetCharBtn = document.getElementById('character-reset'); const resetCharacter = async function (event) { formReset(); } const resetListBtn = document.getElementById('list-reset'); const resetListEvent = async function (event) { resetList(); } const nextCharBtn = document.getElementById('list-next'); const nextCharacter = async function (event) { cycleCharacter(); } function cycleCharacter() { if ( !currentCharacter ) { currentCharacter = 0; } if ( currentCharacter == characterInitiativeList.length ) { currentCharacter = 0; roundCount++; addRoundHistory(); updateRoundCount(); } if ( !characterInitiativeList[currentCharacter] ) { currentCharacter = 0; } var allTrs = document.querySelectorAll(`tr`); allTrs.forEach(function(tr) { tr.classList.remove("success"); }); var allTrsWithCharacterId = document.querySelectorAll(`tr[data-character-id="${characterInitiativeList[currentCharacter].id}"]`); allTrsWithCharacterId.forEach(function(tr) { tr.classList.add("success"); }); currentCharacter++; } const clearListBtn = document.getElementById('list-clear'); const clearListEvent = async function (event) { clearList(); hideList(); } const sortCharBtn = document.getElementById('character-sort'); const sortCharacters = async function (event) { event.preventDefault(); sortthem(); } function sortthem() { var fieldName = document.getElementById('sortBy').value; clearListHTML(); resetList(); var newList = sortCharacterListByField( fieldName, characterInitiativeList ); for (let i = 0; i < newList.length; i++) { addCharacterRow( newList[i] ); } } function sortCharacterListByField( fieldName, list ) { if ( 'id' == fieldName ) { return list; } let sortedList = [...list]; sortedList.sort( ( a, b ) => { let fieldA = a[fieldName]; let fieldB = b[fieldName]; if ( 'type' == fieldName || 'name' == fieldName ) { return fieldA.localeCompare(fieldB); } if (fieldA > fieldB) { return -1; } if (fieldA < fieldB) { return 1; } return 0; }); return sortedList; } const addCharBtn = document.getElementById('character-add'); const addCharacter = function (event) { createCharacterFromForm(); } function createCharacterFromForm() { showList(); var id = Date.now().toString(36) + Math.random().toString(36).substring(2);; var name = document.getElementById('name').value; if ( !name ) { name = '4816'; } var initiative = document.getElementById('initiative').value; if ( !initiative ) { initiative = getRandomInt( 1, 21 ); } initiative = parseInt( initiative ); var ac = document.getElementById('ac').value; if ( !ac ) { ac = 0; } ac = parseInt( ac ); var hp = document.getElementById('hp').value; if ( !hp ) { hp = 0; } hp = parseInt( hp ); var characterType = document.querySelector('input[name="characterType"]:checked').value; var character = { id: id, name: name, initiative: initiative, ac: ac, hp: hp, type: characterType, }; characterInitiativeList.push( character ); // re-order the list by initiative to keep the next character working properly characterInitiativeList = sortCharacterListByField( 'initiative', characterInitiativeList ); addCharacterRow( character ); addRoundRow( character ); } const d20Btn = document.getElementById('d20-roll'); const rollD20 = async function (event) { event.preventDefault(); var resultDiv = document.getElementById('initiative'); resultDiv.value = getRandomInt( 1, 21 ); } const resetRoundsBtn = document.getElementById('rounds-reset'); const resetRoundsEvent = async function (event) { resetRoundCount(); } const clearRoundsBtn = document.getElementById('rounds-clear'); const clearRoundsEvent = async function (event) { clearRounds(); } window.addEventListener('DOMContentLoaded', (event) => { d20Btn.addEventListener('click', rollD20); addCharBtn.addEventListener('click', addCharacter); resetCharBtn.addEventListener('click', resetCharacter); resetListBtn.addEventListener('click', resetListEvent); clearListBtn.addEventListener('click', clearListEvent); nextCharBtn.addEventListener('click', nextCharacter); resetRoundsBtn.addEventListener('click', resetRoundsEvent); clearRoundsBtn.addEventListener('click', clearRoundsEvent); sortCharBtn.addEventListener('click', sortCharacters); updateRoundCount(); }); function formReset() { document.getElementById('name').value = null; document.getElementById('initiative').value = null; document.getElementById('ac').value = null; document.getElementById('hp').value = null; } function showList() { var tbody = document.getElementById('character-container'); tbody.style.display = ''; } function addCharacterRow( character ) { var characterList = document.getElementById('character-list'); var pcList = document.getElementById('character-list-pc'); var npcList = document.getElementById('character-list-npc'); var row = document.createElement('tr'); row.setAttribute('data-character-id', character.id); var hpStyle = ''; var acStyle = ''; var checkbox = document.getElementById('trackHP'); if ( !checkbox.checked ) { var hpStyle = 'style="display: none;"'; } var checkbox = document.getElementById('trackAC'); if ( !checkbox.checked ) { var acStyle = 'style="display: none;"'; } row.innerHTML = `