diff --git a/js/sr2ini.js b/js/sr2ini.js
index 98ccedb..2e01ec9 100644
--- a/js/sr2ini.js
+++ b/js/sr2ini.js
@@ -12,19 +12,33 @@ function rollForInitiative(dice, rea) {
return ini + parseInt(rea);
}
+
+// figure out whose action comes first out of two combatants a and b
+function whoGoesFirst(a, b) {
+ let comparer = parseInt($(b).find(".combatantIni").text()) - parseInt($(a).find(".combatantIni").text());
+ if (comparer != 0) {
+ return comparer;
+ } else {
+ let reaA = parseInt($(a).find(".combatantRea").text());
+ let reaB = parseInt($(b).find(".combatantRea").text());
+ reaA = isNaN(reaA) ? 0 : reaA;
+ if (isNaN(reaB)) { reaB = 0; }
+console.log(reaA, reaB);
+ return reaB - reaA;
+ }
+}
+
+
// sorts the combatants by ini value
function sortTable() {
- // sort rows
- let $rows = $(".combatantRow").toArray().sort(function(a, b) {
- return parseInt($(b).find(".combatantIni").text()) - parseInt($(a).find(".combatantIni").text());
- });
-
- // append rows in sorted order
+ // sort rows and append them in new order
+ let $rows = $(".combatantRow").toArray().sort(whoGoesFirst);
for ( var i = 0; i < $rows.length; i++ ) {
$("#combatantsTable").append($rows[i]);
}
- // compute highest ini
+ // add contectual classes to rows – currently for highest ini and ini = 0
+ // compute highest ini
let iniValues = $.map( $(".combatantIni"), function(td, i) {
return parseInt($(td).text());
});
@@ -32,13 +46,13 @@ function sortTable() {
// add contextual classes to rows
$(".combatantRow").each( function() {
- // default: remove previous classes, disable act button
+ // always remove previous classes, disable act button
$(this).removeClass("table-primary table-secondary").find(".act-button").prop("disabled", true).attr("aria-disabled", "true");
- // ini is zero: add class
+ // add class if ini is zero
if ( parseInt($(this).find(".combatantIni").text()) == 0 ) {
$(this).addClass("table-secondary");
}
- // ini is max and non-zero: add class, enable act button
+ // add class, enable act button if ini is max and non-zero
else if ( parseInt($(this).find(".combatantIni").text()) == iniMax && iniMax > 0 ) {
$(this).addClass("table-primary").find(".act-button").prop("disabled", false).removeAttr("aria-disabled");
}
@@ -215,7 +229,7 @@ function addCombatant (e) {
'
', ini, '
\n',
'
\n',
'
\n',
- '
\n',
+ '
\n',
'
\n',
'
', dice, '
\n',
'
+
\n',
@@ -224,11 +238,11 @@ function addCombatant (e) {
'