diff --git a/js/sr2ini.js b/js/sr2ini.js index 77ac8dd..0b8d73c 100644 --- a/js/sr2ini.js +++ b/js/sr2ini.js @@ -1,7 +1,24 @@ -const penalty = [0, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4]; -const dmg = ["", "L", "M", "S", "D"]; +const DAMAGE_PENALTY = [0, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4]; +const DAMAGE_NIVEAU = ["", "L", "M", "S", "D"]; -const damageMonitorHTML = ['\n', +const COMBATANT_TABLE_ROW = [ +'\n', //TODO: add data-damage-* attributes with initial damage levels + '\n', + '\n', + '\n', + '\n', +''].join(""); + +const DAMAGE_MONITOR_HTML = [ +'
D+\n', + '
\n', + '\n', + '\n', + '
\n', + '\n', + '
\n', + '
\n', + '
\n', '\n', '\n', '\n', @@ -19,9 +36,9 @@ const damageMonitorHTML = ['
\n', '
'].join(""); -const maxIniClass = "table-primary"; -const zeroIniClass = "table-secondary"; -const regularClass = "table-success"; +const MAX_INI_CLASS = "table-primary"; +const ZERO_INI_CLASS = "table-secondary"; +const REGULAR_INI_CLASS = "table-success"; /* * helper functions @@ -29,22 +46,19 @@ const regularClass = "table-success"; // roll for initiative with the given reaction and number of ini dice function rollForInitiative(dice, rea) { - let ini = 0; - for ( let i = 0; i < parseInt(dice); i++ ) { - roll = Math.floor(Math.random() * 6) + 1; - ini += roll; - } - return ini + parseInt(rea); + let diceRolls = Array.from({length: parseInt(dice)}, () => Math.ceil(Math.random() * 6)); +console.log(diceRolls); + return diceRolls.reduce((a, b) => a + b, 0) + parseInt(rea); } // figure out whose action comes first out of two combatants a and b function whoGoesFirst(a, b) { + // check for K.O./death let tmpA = $(a).hasClass("out-of-commission") ? -1 : parseInt($(a).find(".combatantIni").text()) || 0; let tmpB = $(b).hasClass("out-of-commission") ? -1 : parseInt($(b).find(".combatantIni").text()) || 0; -console.log("iniA, iniB are", tmpA, tmpB); // compare ini let comparer = tmpB - tmpA; if (comparer != 0) { @@ -54,7 +68,6 @@ console.log("iniA, iniB are", tmpA, tmpB); { tmpA = parseInt($(a).find(".combatantRea").text()) || 0; tmpB = parseInt($(b).find(".combatantRea").text()) || 0 -console.log("Reaction values are: ", tmpA, tmpB); return tmpB - tmpA; } } @@ -64,21 +77,20 @@ console.log("Reaction values are: ", tmpA, tmpB); function sortTable() { // remove previous classes from rows, disable act buttons - $(".combatantRow").removeClass(maxIniClass + " " + zeroIniClass + " " + regularClass).find(".act-button").prop("disabled", true).attr("aria-disabled", "true"); + $(".combatantRow").removeClass(MAX_INI_CLASS + " " + ZERO_INI_CLASS + " " + REGULAR_INI_CLASS).find(".act-button").prop("disabled", true).attr("aria-disabled", "true"); // get ini value for every combatant; set to 0 if K.O./dead let iniValues = $.map( $(".combatantRow"), function(tr, i) { -console.log($(tr).find(".combatantIni")); +//TODO: don't rely on class out-of-commission being set, check and set it here instead return $(tr).hasClass("out-of-commission") ? 0 : parseInt($(tr).find(".combatantIni").text()); }); -console.log(iniValues); //TODO: maxIni wid nicht vergeben wenn damage > 0 // compute highest ini let iniMax = Math.max.apply(null, iniValues); -console.log(iniValues); - // add contextual classes + // add contextual classes +//TODO: I should probably add badges here as well, maybe even effectiveIni $(".combatantRow").each( function() { // K.O./dead -> don't add anything if ( $(this).hasClass("out-of-commission") ) { @@ -86,16 +98,16 @@ console.log(iniValues); } // ini = zero if ( parseInt($(this).find(".combatantIni").text()) == 0 ) { - $(this).addClass(zeroIniClass); + $(this).addClass(ZERO_INI_CLASS); return true; } // ini = max and non-zero if ( parseInt($(this).find(".combatantIni").text()) == iniMax && iniMax > 0 ) { - $(this).addClass(maxIniClass).find(".act-button").prop("disabled", false).removeAttr("aria-disabled"); + $(this).addClass(MAX_INI_CLASS).find(".act-button").prop("disabled", false).removeAttr("aria-disabled"); return true; } // everything else - $(this).addClass(regularClass); + $(this).addClass(REGULAR_INI_CLASS); }) // sort rows and append them in new order @@ -109,24 +121,25 @@ console.log(iniValues); // returns a combatant's effective ini value (modified by wound penalties) -function getEffectiveIni(value, dmgLvl1, dmgLvl2) { +function getEffectiveIni(value, damageLevel1, damageLevel2) { let effectiveIni; // was function called with 1 argument (tr jQuery object)? if ( arguments.length == 1 && $(value).is("tr.combatantRow") ) { let trueIni = parseInt($(value).attr("data-true-ini")); - let dmgStun = parseInt($(value).attr("data-damage-stun")) || 0; - let dmgPhysical = parseInt($(value).attr("data-damage-physical")) || 0; - effectiveIni = trueIni - penalty[dmgStun] - penalty[dmgPhysical]; + let damageStun = parseInt($(value).attr("data-damage-stun")) || 0; + let damagePhysical = parseInt($(value).attr("data-damage-physical")) || 0; + effectiveIni = trueIni - DAMAGE_PENALTY[damageStun] - DAMAGE_PENALTY[damagePhysical]; } - // or with 3 arguments (ini and dmg levels)? + // or with 3 arguments (ini and damage levels)? else if ( arguments.length == 3 ) { - effectiveIni = parseInt(value) - penalty[parseInt(dmgLvl1)] - penalty[parseInt(dmgLvl2)]; + effectiveIni = parseInt(value) - DAMAGE_PENALTY[parseInt(damageLevel1)] - DAMAGE_PENALTY[parseInt(damageLevel2)]; } // else { return NaN; } +//TODO: maybe check here for out-of-commission and return 'dead' or something instead of int return effectiveIni < 0 ? 0 : effectiveIni; } @@ -212,6 +225,7 @@ console.log("damage level is", damageLevel); // add damage level to table row as as data attribute $tr.attr("data-damage-" + damageType, damageLevel); +//TODO: class out-of-commission should be set in sortTable() with the other contextual classes if ( damageLevel == 10 || otherDamageLevel == 10 ) { $tr.addClass("out-of-commission"); } else { @@ -228,10 +242,10 @@ console.log("damage level is", damageLevel); // add damage level badges if ( $tr.attr("data-damage-stun") && $tr.attr("data-damage-stun") != "0" ) { - $tr.find(".combatantIni").append('' + dmg[penalty[$tr.attr("data-damage-stun")]] + ''); + $tr.find(".combatantIni").append('' + DAMAGE_NIVEAU[DAMAGE_PENALTY[$tr.attr("data-damage-stun")]] + ''); } if ( $tr.attr("data-damage-physical") && $tr.attr("data-damage-physical") != "0" ) { - $tr.find(".combatantIni").append('' + dmg[penalty[$tr.attr("data-damage-physical")]] + 'Physical damage level'); + $tr.find(".combatantIni").append('' + DAMAGE_NIVEAU[DAMAGE_PENALTY[$tr.attr("data-damage-physical")]] + ''); } // resort @@ -369,21 +383,25 @@ function addCombatant (e) { // hide modal $("#combatantModal").modal("hide"); - // get values - let name = $("#combatantModalName").val().trim(); - let ini = $("#combatantModalIni").val().trim(); - let dice = $("#combatantModalDice").val().trim(); - let rea = $("#combatantModalRea").val().trim(); - //TODO: retrieve initial damage levels - // roll for initiative if necessary - ini = (ini != "") ? ini : rollForInitiative(dice, rea); - - // get effective initiative value (modified by wound penalties) - let effectiveIni = getEffectiveIni(ini, 0, 0); + let ini = $("#combatantModalIni").val().trim(); + ini = (ini != "") ? ini : rollForInitiative($("#combatantModalDice").val(), $("#combatantModalRea").val()); // construct jQuery object for table row - let $tr = $($.parseHTML( [ + let $tr = $($.parseHTML(COMBATANT_TABLE_ROW)); + $tr.find(".damage-dropdown").append($.parseHTML(DAMAGE_MONITOR_HTML)); + + // populate table row with values from modal + $tr.attr("data-true-ini", ini); + $tr.find(".combatantName").text($("#combatantModalName").val().trim()); + $tr.find(".combatantIni").text(getEffectiveIni(ini, 0, 0)); // don't need to use contents()[0].data here b/c wound badges are added later + $tr.find(".combatantDice").text($("#combatantModalDice").val().trim()); + $tr.find(".combatantRea").text($("#combatantModalRea").val().trim()); + + //TODO: retrieve initial damage levels + + // construct jQuery object for table row +/* let $tr = $($.parseHTML( [ '\n', //TODO: add data-damage-* attributes with initial damage levels '', name, '\n', '', effectiveIni, '\n', @@ -394,24 +412,24 @@ function addCombatant (e) { '\n', '
\n', '\n', - damageMonitorHTML + "\n", + DAMAGE_MONITOR_HTML + "\n", '
\n', '\n', '\n', ''].join("") - )); + ));*/ + +//TODO: mark initial damage levels with active class -> what? don't know what this means -//TODO: mark initial damage levels with active class + // add handler to table cells (click to edit) + $tr.find(".combatantName, .combatantIni, .combatantDiceAndRea").on("click", handleEditButtonClick); - // add handlers to table row buttons + // add handlers to action buttons $tr.find("button.edit-button").on("click", handleEditButtonClick); $tr.find("button.act-button").on("click", handleActButtonClick); $tr.find("button.damage-button").on("click", handleDamageButtonClick); $tr.find("button.remove-button").on("click", handleRemoveButtonClick); - // add handler to table cells (click to edit) - $tr.find(".combatantName, .combatantIni, .combatantDiceAndRea").on("click", handleEditButtonClick); - // add handler to damage monitor $tr.find(".damage-stun, .damage-physical").on("click", handleDamageMonitorClick);