diff --git a/src/kaplus.js b/src/kaplus.js index 4eed44d..e42a542 100644 --- a/src/kaplus.js +++ b/src/kaplus.js @@ -437,13 +437,204 @@ function showAutoPlanerMenu(village, module) { navRow.insertBefore(separatorCell, navCells[6]); } +function handleAutoPlanerTargetChange(village) { + let targetX = document.getElementById("target_x"); + let targetY = document.getElementById("target_y"); + let targetId = document.getElementById("target_id"); + let targetName = document.getElementById("target_name"); + + console.log(targetX.value, targetY.value); + +} + +function handleAutoPlanerUnitChange() { + let buildingSelect = document.getElementById("autoPlanerBuilding"); + let unitInputs = document.getElementById("autoPlanerUnitRow").getElementsByTagName("input"); + let totalUnitCount = 0; + let spyCount = 0; + let kataCount = 0; + let attackButton = document.getElementById("autoPlanerSubmitAttack"); + let supportButton = document.getElementById("autoPlanerSubmitSupport"); + let spyingButton = document.getElementById("autoPlanerSubmitSpying"); + + for (let i = 0; i < unitInputs.length; i ++) { + let unitCount = 0; + if (unitInputs[i].value !== "") { + unitCount = parseInt(unitInputs[i].value); + } + if (unitInputs[i].getAttribute("name") === "spy") { + spyCount = unitCount; + } + if (unitInputs[i].getAttribute("name") === "kata") { + kataCount = unitCount; + } + totalUnitCount += unitCount; + } + + if (kataCount > 0) { + buildingSelect.removeAttribute("disabled"); + } else { + buildingSelect.setAttribute("disabled", "disabled"); + } + + if (spyCount > 0 && spyCount === totalUnitCount) { + spyingButton.removeAttribute("disabled"); + } else { + spyingButton.setAttribute("disabled", "disabled"); + } + + if (totalUnitCount > 0) { + attackButton.removeAttribute("disabled"); + supportButton.removeAttribute("disabled"); + } else { + attackButton.setAttribute("disabled", "disabled"); + supportButton.setAttribute("disabled", "disabled"); + } +} + +function handleAutoPlanerSubmit(event, village) { + event.preventDefault(); + + let autoPlanerError = document.getElementById("autoPlanerError"); + autoPlanerError.textContent = " "; + + let nowTime = Date.now(); + let formData = new FormData(this); + formData.set("kind", event.submitter.getAttribute("name")); + + // Check start village + let ownVillages = sessionStorage.getItem("ownVillages"); + if (ownVillages === null) { + autoPlanerError.textContent = "Impossible de récupérer la liste de vos propres villages, " + + "merci de recharger la page ou de vous reconnecter au jeu si le problème persiste!" + return; + } + ownVillages = JSON.parse(ownVillages); + for (let ownVillageId in ownVillages) { + let ownVillage = ownVillages[ownVillageId]; + if (ownVillage.x === parseInt(formData.get("start_x").toString()) && + ownVillage.y === parseInt(formData.get("start_y").toString())) { + formData.set("start_id", ownVillageId); + formData.set("start_name", ownVillage.name); + break; + } + } + if (! formData.has("start_id")) { + autoPlanerError.textContent = "Opération impossible car le village d'origine ne vous appartient pas!"; + return; + } + + // Check target time + let targetTime = Date.parse( + formData.get("year").toString() + "-" + + formData.get("month").toString().padStart(2, "0") + "-" + + formData.get("day").toString().padStart(2, "0") + "T" + + formData.get("hour").toString().padStart(2, "0") + ":" + + formData.get("minute").toString().padStart(2, "0") + ":" + + formData.get("second").toString().padStart(2, "0") + ) + parseInt(formData.get("tenth").toString()) * 100; + if (nowTime > targetTime) { + autoPlanerError.textContent = "Opération impossible car l'heure d'arrivée est dans le passé!"; + return; + } + formData.set("target_time", targetTime.toString()); + formData.delete("year"); + formData.delete("month"); + formData.delete("day"); + formData.delete("hour"); + formData.delete("minute"); + formData.delete("second"); + formData.delete("tenth"); + + // Replace empty unit values by 0 + for (let unit in UNITS) { + if (formData.get(unit) === "") { + formData.set(unit, "0"); + } + } + + // Calculate start time + let durationSeconds = Math.sqrt( + Math.pow(parseInt(formData.get("target_x").toString()) - parseInt(formData.get("start_x").toString()), 2) + + Math.pow(parseInt(formData.get("target_y").toString()) - parseInt(formData.get("start_y").toString()), 2) + ) + if (formData.get("snob") !== "0") { + durationSeconds *= 700; + } else if (formData.get("kata") !== "0" || formData.get("ram") !== "0" || formData.get("kind") === "spying") { + durationSeconds *= 600; + } else if (formData.get("sword") !== "0") { + durationSeconds *= 440; + } else if (formData.get("farmer") !== "0") { + durationSeconds *= 400; + } else if (formData.get("spear") !== "0" || formData.get("axe") !== "0" || formData.get("bow") !== "0") { + durationSeconds *= 360; + } else if (formData.get("heavy") !== "0") { + durationSeconds *= 220; + } else if (formData.get("light") !== "0") { + durationSeconds *= 200; + } else if (formData.get("spy") !== "0") { + durationSeconds *= 180; + } else { + autoPlanerError.textContent = "Opération impossible car aucune unité n'a été sélectionnée!"; + return; + } + let duration = Math.round(durationSeconds) * 1000; + let startTime = targetTime - duration; + if (nowTime > startTime) { + autoPlanerError.textContent = "Opération impossible car l'heure de départ est dans le passé!"; + return; + } + formData.set("start_time", startTime.toString()); + + // Check target village + let xhr = new XMLHttpRequest(); + xhr.addEventListener("readystatechange", function () { + if (xhr.readyState === xhr.DONE) { + let parser = new DOMParser(); + let doc = parser.parseFromString(xhr.responseText, "text/html"); + let mapContainer = doc.getElementById("mapContainer"); + let map = mapContainer.getElementsByTagName("table")[1]; + let mapRows = map.getElementsByTagName("tr"); + let mapRow = mapRows[Math.floor(mapRows.length / 2) - 1]; + let mapCells = mapRow.getElementsByTagName("td"); + let mapCell = mapCells[Math.floor(mapCells.length / 2) + 1]; + let mapLinks = mapCell.getElementsByTagName("a"); + if (mapLinks.length === 0) { + autoPlanerError.textContent = "Opération impossible car le village cible n'existe pas!"; + return; + } + formData.set("target_id", mapLinks[0].getAttribute("href").replace(/^.*id=(\d{4}).*$/, "$1")); + formData.set( + "target_name", mapLinks[0].getAttribute("onmouseover").replace(/^.*'(.*) \(\d{3}\|\d{3}\).*$/, "$1") + ); + + // Save attack in localStorage + console.log("saving", formData); + let autoPlanedAttacks = localStorage.getItem("autoPlanedAttacks"); + if (autoPlanedAttacks === null) { + autoPlanedAttacks = []; + } else { + autoPlanedAttacks = JSON.parse(autoPlanedAttacks); + } + autoPlanedAttacks.push(Object.fromEntries(formData)); + localStorage.setItem("autoPlanedAttacks", JSON.stringify(autoPlanedAttacks)); + } + }); + xhr.open( + "GET", + "/?village=" + village + "&s=map&x=" + formData.get("target_x").toString() + + "&y=" + formData.get("target_y").toString(), + ); + xhr.send(); +} + function showAutoPlanerPage(village) { let mainContentPane = document.getElementsByClassName("contentpane")[1]; for (let i = mainContentPane.childNodes.length - 1; i >= 5; i --) { mainContentPane.childNodes[i].remove(); } - let autoPlanerForm = createCustomElement("form", {"name": "kingsage"}); + let autoPlanerForm = createCustomElement("form", {"name": "kingsage", "id": "autoPlanerForm"}); let borderList = createCustomElement("table", {"class": "borderlist", "width": "820"}); let titleRow = createCustomElement("tr"); let titleCell = createCustomElement("th", {"colspan": "7"}, "Nouvel ordre autonome"); @@ -525,7 +716,7 @@ function showAutoPlanerPage(village) { "number", "target_x", null, - {"id": "target_x", "required": "required", "min": "1", "max": "999"}, + {"id": "target_x", "required": "required", "min": "0", "max": "999"}, {"width": "50px"}, ); targetXCell.appendChild(targetXInput); @@ -536,7 +727,7 @@ function showAutoPlanerPage(village) { "number", "target_y", null, - {"id": "target_y", "required": "required", "min": "1", "max": "999"}, + {"id": "target_y", "required": "required", "min": "0", "max": "999"}, {"width": "50px"}, ); targetYCell.appendChild(targetYInput); @@ -572,7 +763,7 @@ function showAutoPlanerPage(village) { dateCell.appendChild(createCustomInput( "number", "month", - now.getMonth().toString(), + (now.getMonth() + 1).toString(), {"required": "required", "min": "1", "max": "12"}, {"width": "40px"}, )); @@ -631,23 +822,20 @@ function showAutoPlanerPage(village) { } unitBorderList.appendChild(unitTitleRow); - let unitInputRow = createCustomElement("tr"); + let unitInputRow = createCustomElement("tr", {"id": "autoPlanerUnitRow"}); for (let unit in UNITS) { let unitInputCell = createCustomElement("td"); - let unitInput = createCustomInput("number", unit, null, {"min": "0"}, {"width": "61px"}); + let unitInput = createCustomInput( + "number", + unit, + null, + {"id": "autoPlanerUnit" + unit.charAt(0).toUpperCase() + unit.slice(1), "min": "0"}, {"width": "61px"}, + ); + unitInput.addEventListener("change", handleAutoPlanerUnitChange); switch (unit) { case "spy": case "light": case "heavy": case "ram": case "snob": unitInput.style.width = "62px"; break; - case "kata": - unitInput.addEventListener("change", function () { - if (autoPlanerForm.kata.value !== "0" && autoPlanerForm.genre.value === "attack") { - autoPlanerForm.building.removeAttribute("disabled"); - } else { - autoPlanerForm.building.setAttribute("disabled", "disabled"); - } - }); - break; } unitInputCell.appendChild(unitInput); unitInputRow.appendChild(unitInputCell); @@ -663,7 +851,7 @@ function showAutoPlanerPage(village) { labelRow.appendChild(createCustomElement("td", null, "Étiquette:")); let labelCell = createCustomElement("td", {"colspan": "6"}); let labelInput = createCustomInput( - "text", "label", null, {"maxlength": "10"}, {"width": "100px"} + "text", "label", null, {"maxlength": "10", "placeholder": "Optionnel"}, {"width": "100px"} ); labelCell.appendChild(labelInput); labelRow.appendChild(labelCell); @@ -679,31 +867,11 @@ function showAutoPlanerPage(village) { occurrencesRow.appendChild(occurrencesCell); additionalBorderList.appendChild(occurrencesRow); - let genreRow = createCustomElement("tr"); - genreRow.appendChild(createCustomElement("td", null, "Type:")); - let genreCell = createCustomElement("td", {"colspan": "6"}); - let genreSelect = createCustomElement( - "select", {"name": "genre", "value": "attack"}, {"required": "required"}, {"width": "200px"} - ); - genreSelect.appendChild(createCustomElement("option", {"value": "attack"}, "Attaque")); - genreSelect.appendChild(createCustomElement("option", {"value": "support"}, "Renfort")); - genreSelect.appendChild(createCustomElement("option", {"value": "spy"}, "Espionnage")); - genreSelect.addEventListener("change", function () { - if (autoPlanerForm.kata.value !== "0" && autoPlanerForm.genre.value === "attack") { - autoPlanerForm.building.removeAttribute("disabled"); - } else { - autoPlanerForm.building.setAttribute("disabled", "disabled"); - } - }); - genreCell.appendChild(genreSelect); - genreRow.appendChild(genreCell); - additionalBorderList.appendChild(genreRow); - let buildingRow = createCustomElement("tr"); buildingRow.appendChild(createCustomElement("td", null, "Bâtiment visé:")); let buildingCell = createCustomElement("td", {"colspan": "6"}); let buildingSelect = createCustomElement( - "select", {"name": "building", "disabled": "disabled"}, null, {"width": "200px"} + "select", {"name": "building", "disabled": "disabled", "id": "autoPlanerBuilding"}, null, {"width": "200px"} ); buildingSelect.appendChild(createCustomElement("option", null, "Sélectionner bâtiment")); for (let building in BUILDINGS) { @@ -713,19 +881,8 @@ function showAutoPlanerPage(village) { buildingRow.appendChild(buildingCell); additionalBorderList.appendChild(buildingRow); - autoPlanerForm.addEventListener("submit", function (event) { - event.preventDefault(); - let formData = new FormData(this); - let autoPlanedAttacks = localStorage.getItem("autoPlanedAttacks"); - if (autoPlanedAttacks === null) { - autoPlanedAttacks = []; - } else { - autoPlanedAttacks = JSON.parse(autoPlanedAttacks); - } - autoPlanedAttacks.push(Object.fromEntries(formData)); - localStorage.setItem("autoPlanedAttacks", JSON.stringify(autoPlanedAttacks)); - }) - + autoPlanerForm.appendChild(createCustomElement("p", {"class": "error", "id": "autoPlanerError"}, " ")); + autoPlanerForm.appendChild(createCustomElement("br")); autoPlanerForm.appendChild(borderList); autoPlanerForm.appendChild(createCustomElement("br")); autoPlanerForm.appendChild(unitBorderList); @@ -734,11 +891,26 @@ function showAutoPlanerPage(village) { autoPlanerForm.appendChild(createCustomElement("br")); autoPlanerForm.appendChild(createCustomInput( "submit", - null, - "> Ajouter une nouvelle entrée", - null, - {"font-weight": "bold", "padding": "5px 20px", "cursor": "pointer"} + "attack", + "> Programmer une nouvelle attaque", + {"id": "autoPlanerSubmitAttack", "disabled": "disabled"}, + {"font-weight": "bold", "width": "250px", "padding": "5px 0", "cursor": "pointer"} )); + autoPlanerForm.appendChild(createCustomInput( + "submit", + "support", + "> Programmer un nouveau renfort", + {"id": "autoPlanerSubmitSupport", "disabled": "disabled"}, + {"font-weight": "bold", "width": "250px", "padding": "5px 0", "margin": "0 35px", "cursor": "pointer"} + )); + autoPlanerForm.appendChild(createCustomInput( + "submit", + "spying", + "> Programmer un nouvel espionnage", + {"id": "autoPlanerSubmitSpying", "disabled": "disabled"}, + {"font-weight": "bold", "width": "250px", "padding": "5px 0", "cursor": "pointer"} + )); + autoPlanerForm.addEventListener("submit", handleAutoPlanerSubmit); mainContentPane.appendChild(autoPlanerForm); }