"use strict"; function showBattleScreen(attacker, defender) { if (customization) return; closeDialogs(".stable"); const battle = {name:"Battle", attackers:[attacker], defenders:[defender]}; const battleAttackers = document.getElementById("battleAttackers"); const battleDefenders = document.getElementById("battleDefenders"); addHeaders(); addRegiment(battleAttackers, attacker); addRegiment(battleDefenders, defender); $("#battleScreen").dialog({ title: battle.name, resizable: false, width: fitContent(), close: closeBattleScreen, position: {my: "center", at: "center", of: "#map"} }); if (modules.showBattleScreen) return; modules.showBattleScreen = true; // add listeners document.getElementById("battleAddRegiment").addEventListener("click", addSide); function addHeaders() { document.getElementById("battleScreen").querySelectorAll("th").forEach(el => el.remove()); const attackers = battleAttackers.querySelector("tr"); const defenders = battleDefenders.querySelector("tr"); let headers = ""; for (const u of options.military) { const label = capitalize(u.name.replace(/_/g, ' ')); headers += `${u.icon}`; } headers += "Total"; attackers.insertAdjacentHTML("beforebegin", headers); defenders.insertAdjacentHTML("beforebegin", headers); } function addRegiment(div, regiment) { const state = ra(pack.states), supply = rand(1000) + " " + distanceUnitInput.value; const color = state.color[0] === "#" ? state.color : "#999"; const icon = ` ${regiment.icon}`; const body = ``; let initial = `${icon}${regiment.name.slice(0,25)}`; let casualties = `${state.fullName}`; let survivors = `Supply line length: ${supply}`; for (const u of options.military) { initial += `${regiment.u[u.name]||0}`; casualties += `0`; survivors += `${regiment.u[u.name]||0}`; } initial += `${regiment.a||0}`; casualties += `0`; survivors += `${regiment.a||0}`; div.innerHTML += body + initial + casualties + survivors + ""; } function addSide() { const states = pack.states.filter(s => s.i && !s.removed); const stateOptions = states.map(s => ``).join(""); const regiments = states[0].military.map(r => ``).join(""); alertMessage.innerHTML = `
`; $("#alert").dialog({resizable: false, title: "Add regiment to the battle", buttons: { Add: function() { $(this).dialog("close"); const div = document.getElementById("addSideSide").selectedIndex ? battleDefenders : battleAttackers; const state = pack.states.find(s => s.i == document.getElementById("addSideState").value); const regiment = state.military.find(r => r.i == document.getElementById("addSideRegiment").value); addRegiment(div, regiment); }, Cancel: function() {$(this).dialog("close");} } }); document.getElementById("addSideState").onchange = function () { const state = pack.states.find(s => s.i == this.value); const regiments = state.military.map(r => ``).join(""); document.getElementById("addSideRegiment").innerHTML = regiments; } } function closeBattleScreen() { } }