diff --git a/app/index.html b/app/index.html index 1215af7..a0ef281 100644 --- a/app/index.html +++ b/app/index.html @@ -54,7 +54,7 @@
Roll initiative
diff --git a/app/js/initiative.js b/app/js/initiative.js index c30f419..fc85ed9 100644 --- a/app/js/initiative.js +++ b/app/js/initiative.js @@ -1,10 +1,12 @@ const Modals = require("./modals"); -var initiative = function () { +var initiative = (function () { var roundTimer; var order; var currentNode; - var monsterColor = "rgb(197, 0, 0)", playerColor = "rgb(101, 117, 197)", defaultPlayerColor = "#000000"; + var monsterColor = "rgb(197, 0, 0)", + playerColor = "rgb(101, 117, 197)", + defaultPlayerColor = "#000000"; var roundCounter; var isMainWindow = false; function setAsMain() { @@ -12,33 +14,38 @@ var initiative = function () { } function loadEventHandlers() { - [...document.querySelectorAll(".initiative_explanation_text_node")].forEach(ele => ele.onfocus = function (e) { - e.target.select(); - }); - - [...document.querySelectorAll(".initiativeNode")].forEach(ele => ele.onmousedown = function (e) { - var tmp = e.target; - while (tmp.parentNode) { - if (tmp.parentNode.classList.contains("initiativeNode")) { - currentNode = tmp.parentNode; - break; - } - tmp = tmp.parentNode; - } + [...document.querySelectorAll(".initiative_explanation_text_node")].forEach( + (ele) => + (ele.onfocus = function (e) { + e.target.select(); + }) + ); - if (e.button == 2) { - var popupWindow = document.getElementById("initiative_control_bar"); - popupWindow.style.left = e.clientX + "px"; - popupWindow.style.top = e.clientY + "px"; - popupWindow.classList.remove("hidden"); - document.addEventListener("click", hidePopupOnClick); - function hidePopupOnClick(e) { - popupWindow.classList.add("hidden"); - document.removeEventListener("click", hidePopupOnClick); - } - } - }); + [...document.querySelectorAll(".initiativeNode")].forEach( + (ele) => + (ele.onmousedown = function (e) { + var tmp = e.target; + while (tmp.parentNode) { + if (tmp.parentNode.classList.contains("initiativeNode")) { + currentNode = tmp.parentNode; + break; + } + tmp = tmp.parentNode; + } + if (e.button == 2) { + var popupWindow = document.getElementById("initiative_control_bar"); + popupWindow.style.left = e.clientX + "px"; + popupWindow.style.top = e.clientY + "px"; + popupWindow.classList.remove("hidden"); + document.addEventListener("click", hidePopupOnClick); + function hidePopupOnClick(e) { + popupWindow.classList.add("hidden"); + document.removeEventListener("click", hidePopupOnClick); + } + } + }) + ); } function editCurrentNode() { @@ -48,27 +55,20 @@ var initiative = function () { for (i = 0; i < order.length; i++) { if (order[i].name == nodeName) break; } - Modals.prompt( - `${nodeName} - new score`, - "Score: ", - (result) => { - if (result == null) return false; - order[i].roll = parseInt(result); - sortAndDisplay(); - } - ); - - + Modals.prompt(`${nodeName} - new score`, "Score: ", (result) => { + if (!result) return false; + order[i].roll = parseInt(result); + sortAndDisplay(); + }); } function removeCurrentNode() { var nodes = [...document.querySelectorAll("#initBar .initiativeNode")]; if (nodes.length > 1) { var nameToRemove = currentNode.getElementsByClassName("initiative_name_node")[0].innerHTML; - console.log(`Remove node ${nameToRemove}`) - order = order.filter(x => x.name != nameToRemove); + + order = order.filter((x) => x.name != nameToRemove); sortAndDisplay(); nextRound(-1); - } else { firstNodeAsStart(nodes[0]); } @@ -84,101 +84,85 @@ var initiative = function () { publishEvent({ empty: true }); } - (firstNode.querySelector(".init_value_node") || {}).innerHTML = ""; (firstNode.querySelector(".initiative_explanation_text_node") || {}).innerHTML = ""; - - ["initiative_node_active", "player_node", "monster_node"].forEach(className => firstNode.classList.remove(className)); - + ["initiative_node_active", "player_node", "monster_node"].forEach((className) => firstNode.classList.remove(className)); } - function roll() { + function roll(evt) { order = []; - if (settings.countRounds) - roundCounter = [1, 0]; + roundCounter = [1, 0]; if (settings.autoInitiative) { autoRollPlayers(); rollForMonsters(() => sortAndDisplay()); return; } - rollForMonsters(function (noMonsters) { - var inputs = partyArray.map(p => { + var inputs = partyArray.map((p) => { return { required: true, label: p.character_name, - id: p.id - } + id: p.id, + }; }); if (noMonsters) inputs.push({ required: false, label: "Monsters", - id: "monster_init" - }) - Modals.multiInputPrompt("Initiative scores", - inputs, (resultAr) => { - if (!resultAr) - return; + id: "monster_init", + }); + Modals.multiInputPrompt("Initiative scores", inputs, (resultAr) => { + if (!resultAr) return; - resultAr.filter(x => x.value != "" && x.value != null).map(res => { - var pc = partyArray.find(x => x.id == res.id); + resultAr + .filter((x) => x.value != "" && x.value != null) + .map((res) => { + var pc = partyArray.find((x) => x.id == res.id); return { name: pc?.character_name || "Monsters", roll: parseInt(res.value || 0), dex: pc?.dexterity || 0, isPlayer: pc != null, - color: pc?.color || monsterColor - } - }).forEach(x => order.push(x)); - sortAndDisplay(); - - } - ); - - - }) - - - + color: pc?.color || monsterColor, + }; + }) + .forEach((x) => order.push(x)); + sortAndDisplay(); + }); + }); function autoRollPlayers() { - for (var i = 0; i < partyArray.length; i++) { - order.push( - { - name: partyArray[i].character_name, - roll: (d(20) + parseInt(partyArray[i].dexterity)), - dex: parseInt(partyArray[i].dexterity), - isPlayer: true, - color: partyArray[i].color == defaultPlayerColor ? playerColor : partyArray[i].color - }); + order.push({ + name: partyArray[i].character_name, + roll: d(20) + parseInt(partyArray[i].dexterity), + dex: parseInt(partyArray[i].dexterity), + isPlayer: true, + color: partyArray[i].color == defaultPlayerColor ? playerColor : partyArray[i].color, + }); } } function rollForMonsters(callback) { - combatLoader.getLoadedMonsters(monsters => { + combatLoader.getLoadedMonsters((monsters) => { if (monsters.length == 0) { order.push({ name: "Monsters", roll: d(20), dex: 0, isPlayer: false, - }); } else { for (var i = 0; i < monsters.length; i++) { var init = monsters[i].initiative || Util.getAbilityScoreModifier(monsters[i].dexterity); init = parseInt(init); - console.log(init) - order.push( - { - name: monsters[i].name, - roll: d(20) + init, - dex: init, - isPlayer: false - }); + order.push({ + name: monsters[i].name, + roll: d(20) + init, + dex: init, + isPlayer: false, + }); } } @@ -198,8 +182,7 @@ var initiative = function () { var node = nodes[0]; firstNodeAsStart(node); - if (bar) - bar.classList.remove("initiative_cover_image"); + if (bar) bar.classList.remove("initiative_cover_image"); if (roundTimer) { roundTimer.destroy(); @@ -209,8 +192,7 @@ var initiative = function () { emptyInitiative(); document.getElementById("round_counter_container").classList.add("hidden"); document.getElementById("initiative_control_bar").classList.add("hidden"); - document.querySelector(".roundcounter__value").innerHTML = ("1"); - + document.querySelector(".roundcounter__value").innerHTML = "1"; } function sort() { order.sort(function (a, b) { @@ -220,7 +202,6 @@ var initiative = function () { return b.roll - a.roll; } }); - } function sortAndDisplay() { document.querySelector("#initiative_popup_window")?.classList.add("hidden"); @@ -229,22 +210,19 @@ var initiative = function () { sort(); emptyInitiative(); var initNodes = [...document.querySelectorAll(".initiativeNode")]; - initNodes.forEach(x => { + initNodes.forEach((x) => { x.classList.remove("init_not_started"); x.onclick = null; - }); - console.log(initNodes) //Create buttons in initiative elements. var bar = document.getElementById("initBar"); for (var j = 0; j < order.length; j++) { var newNode = j > 0 ? initNodes[0].cloneNode(true) : initNodes[0]; - (newNode.querySelector(".init_value_node") || {}).innerHTML = (order[j].roll); - newNode.querySelector(".initiative_name_node").innerHTML = (order[j].name); + (newNode.querySelector(".init_value_node") || {}).innerHTML = order[j].roll; + newNode.querySelector(".initiative_name_node").innerHTML = order[j].name; (newNode.querySelector(".initiative_explanation_text_node") || {}).value = "Write note"; bar.appendChild(newNode); - if (order[j].isPlayer) { newNode.classList.add("player_node"); newNode.classList.remove("monster_node"); @@ -255,11 +233,9 @@ var initiative = function () { newNode.style.backgroundColor = getColor(order[j]); } initiative.loadEventHandlers(); - if (bar && bar.classList.contains("initative_has_cover_image")) - bar.classList.add("initiative_cover_image"); - if (isMainWindow) - publishEvent({ order: order }); - //Loads the roundcounter. + if (bar && bar.classList.contains("initative_has_cover_image")) bar.classList.add("initiative_cover_image"); + if (isMainWindow) publishEvent({ order: order }); + //Loads the roundcounter. if (settings.countRounds) { var roundCounterCont = document.getElementById("round_counter_container"); roundCounterCont.classList.remove("hidden"); @@ -270,17 +246,13 @@ var initiative = function () { nextRound(1); } else { initNodes = [...document.querySelectorAll(".initiativeNode")]; - initNodes.forEach(x => { + initNodes.forEach((x) => { x.classList.add("initiative_node_inactive"); - }); - } - } function getColor(entry) { - if (entry.color) return Util.hexToHSL(entry.color, 40); if (entry.isPlayer) return playerColor; return monsterColor; @@ -295,59 +267,53 @@ var initiative = function () { } else if (roundCounter[1] <= 1 && roundCounter[0] != 1) { return max; } - } return roundCounter[1] + 1; } /** - * Moves the current active node in Initiative nodes, sign - * for whos turn it is. - * @param {*Direction; back in a round or forward} sign + * Moves the current active node in Initiative nodes, sign + * for whos turn it is. + * @param {*Direction; back in a round or forward} sign */ function nextRound(sign) { - if (!roundCounter) - return; + if (!roundCounter) return; - if (roundCounter[0] == 1 && roundCounter[1] == 1 && sign < 0) - return false; + if (roundCounter[0] == 1 && roundCounter[1] == 1 && sign < 0) return false; var initNodes = [...document.querySelectorAll(".initiativeNode")]; var max = initNodes.length; - console.log(roundCounter) - if (roundCounter[1] >= max && sign > 0 || roundCounter[1] <= 1 && sign < 0) { + if ((roundCounter[1] >= max && sign > 0) || (roundCounter[1] <= 1 && sign < 0)) { if (roundCounter[1] >= max) { roundCounter[1] = 1; } else if (roundCounter[1] <= 1 && roundCounter[0] != 1) { roundCounter[1] = max; } roundCounter[0] += 1 * sign; - document.querySelector(".roundcounter__value").innerHTML = (roundCounter[0]); + document.querySelector(".roundcounter__value").innerHTML = roundCounter[0]; } else { roundCounter[1] += 1 * sign; - } - initNodes.forEach(node => { + initNodes.forEach((node) => { node.classList.remove("initiative_node_active"); node.classList.add("initiative_node_inactive"); - }) + }); var currentNode = document.querySelector(".initiativeNode:nth-child(" + roundCounter[1] + ")"); if (currentNode) { - currentNode.classList.add("initiative_node_active") - currentNode.classList.remove("initiative_node_inactive") + currentNode.classList.add("initiative_node_active"); + currentNode.classList.remove("initiative_node_inactive"); if (currentNode.classList.contains("initiative_node_action_readied")) { currentNode.classList.remove("initiative_node_action_readied"); } var current = order[roundCounter[1] - 1]; - if (current && !current.isPlayer && frameHistoryButtons) //is player + if (current && !current.isPlayer && frameHistoryButtons) + //is player frameHistoryButtons.clickButtonNamed(current.name); - - } if (isMainWindow) { - publishEvent({ round_increment: roundCounter, order:order }); + publishEvent({ round_increment: roundCounter, order: order }); notifyMapToolNextPlayer(); } return current; @@ -361,14 +327,12 @@ var initiative = function () { function notifyMapToolNextPlayer() { if (settings.enable.mapTool) { var name = document.querySelector(".initiative_node_active>.initiative_name_node").innerHTML; - var pc = partyArray.find(x => x.name == name); + var pc = partyArray.find((x) => x.name == name); if (!pc) { window.api.messageWindow("maptoolWindow", "next-player-round", { player: null, darkvision: false }); return; } window.api.messageWindow("maptoolWindow", "next-player-round", { player: name, darkvision: parseInt(pc.darkvision) > 0 }); - - } } @@ -381,27 +345,26 @@ var initiative = function () { { label: "Combatant name", required: true, - id: "name" - }, { + id: "name", + }, + { label: "Initiative score (incl dex)", required: true, - id: "init" - } - ] + id: "init", + }, + ]; Modals.multiInputPrompt("Add to initiative", inputs, (resultArr) => { //Break if user cancels if (resultArr == null) return false; - console.log(resultArr) - order.push( - { - name: resultArr.find(x => x.id == "name").value, - roll: parseInt(resultArr.find(x => x.id == "init").value), - dex: 0, - isPlayer: false - }); + console.log(resultArr); + order.push({ + name: resultArr.find((x) => x.id == "name").value, + roll: parseInt(resultArr.find((x) => x.id == "init").value), + dex: 0, + isPlayer: false, + }); sortAndDisplay(); nextRound(0); - }); } @@ -413,9 +376,8 @@ var initiative = function () { function getState() { return { - order: order - - } + order: order, + }; } function setReadyAction() { @@ -431,14 +393,13 @@ var initiative = function () { callBackArr.push(callback); } function publishEvent(arg) { - callBackArr.forEach(callback => { - callback(arg) + callBackArr.forEach((callback) => { + callback(arg); }); } function currentActor() { - - var current = document.querySelector((".initiativeNode:nth-child(" + roundCounter[1] + ") .initiative_name_node"))?.innerHTML; + var current = document.querySelector(".initiativeNode:nth-child(" + roundCounter[1] + ") .initiative_name_node")?.innerHTML; if (current == null) { return null; } @@ -465,10 +426,8 @@ var initiative = function () { getState: getState, empty: emptyInitiative, setRoundCounter: setRoundCounter, - currentActor: currentActor - } - -}(); - + currentActor: currentActor, + }; +})(); -module.exports = initiative; \ No newline at end of file +module.exports = initiative; diff --git a/app/js/script.js b/app/js/script.js index d212b10..f0658c0 100644 --- a/app/js/script.js +++ b/app/js/script.js @@ -161,7 +161,6 @@ document.addEventListener("DOMContentLoaded", function () { window.api.messageWindow("maptoolWindow", "notify-main-reloaded"); }, 1000); - $(".initiativeNode").on("click", initiative.roll); initiative.setAsMain(); initiative.addEventListener((evt) => { window.api.messageWindow("maptoolWindow", "intiative-updated", evt); diff --git a/docs/client/initiative.client.js b/docs/client/initiative.client.js index 46b08c4..a7f4ccd 100644 --- a/docs/client/initiative.client.js +++ b/docs/client/initiative.client.js @@ -45,9 +45,9 @@ var initiative = function () { } } - + function createNode(entry) { - var newNode = Util.ele("div", "initiativeNode init--PC"); + var newNode = Util.ele("div", "initiativeNode initiative_node_inactive init--PC"); var p = Util.ele("p", "initiative_name_node", entry.name) newNode.appendChild(p); if (entry.isPlayer) { diff --git a/docs/client/map.js b/docs/client/map.js index de89032..75bc0d8 100644 --- a/docs/client/map.js +++ b/docs/client/map.js @@ -1296,7 +1296,7 @@ function dragPawn(elmnt) { tooltip.classList.add("hidden"); var movedThings = [elmnt, ...selectedPawns]; - console.log(movedThings); + serverNotifier.notifyServer( "object-moved", movedThings @@ -2152,8 +2152,8 @@ const map = (function () { function setPawnRotation(pawn, degrees) { var isMob = pawn.getAttribute("data-mob_size") != null; var element = isMob ? pawn.querySelector(".mob_token_container") : pawn.querySelector(".token_photo"); - - element.style.setProperty("--pawn-rotate", degrees + "deg"); + if(isNaN(degrees))degrees = 0; + element.style.setProperty("--pawn-rotate", (degrees || 0) + "deg"); if (serverNotifier.isServer()) { serverNotifier.notifyServer("token-rotate-set", { id: pawn.id,