MediaWiki:Script/Calculator.js : Différence entre versions

Ligne 185 : Ligne 185 :
 
   var originalPseudo = characterPseudo;
 
   var originalPseudo = characterPseudo;
 
   var count = 0;
 
   var count = 0;
 +
 
 +
  var regex = /(.*)(\d)$/;
 +
  var match = characterPseudo.match(regex);
 +
 
 +
  if (match) {
 +
    originalPseudo = match[1];
 +
    count = match[2];
 +
  }
 
    
 
    
 
   while (isValueInArray(characterPseudo, savedCharactersPseudo)) {
 
   while (isValueInArray(characterPseudo, savedCharactersPseudo)) {
Ligne 300 : Ligne 308 :
 
               characterPseudo = validPseudo(characterPseudo);
 
               characterPseudo = validPseudo(characterPseudo);
 
               [characterDataObject, characterPseudo] = addUniquePseudo(characterDataObject, Object.keys(characters.savedCharacters));
 
               [characterDataObject, characterPseudo] = addUniquePseudo(characterDataObject, Object.keys(characters.savedCharacters));
               handleNewCharacter(characters, characterTemplate, charactersContainer, battle, characterPseudo);
+
               var selectedCharacter = handleNewCharacter(characters, characterTemplate, charactersContainer, battle, characterPseudo)[0];
 
                
 
                
 
               if (selectFilesLength === 1) {
 
               if (selectFilesLength === 1) {
                saveButtonGreen(characters);
+
                 updateForm(characterDataObject, characters.characterCreation, characters, selectedCharacter);
                showElement(characters.characterCreation);
 
                 updateForm(characterDataObject, characters.characterCreation, characters);
 
 
               }
 
               }
 
    
 
    
Ligne 340 : Ligne 346 :
  
  
function updateForm(formData, characterCreation, characters) {
+
function handleStyle(currentCharacter, selectedElement) {
 +
 
 +
  var currentCharacterElement = currentCharacter.element;
 +
 
 +
  if (currentCharacterElement) {
 +
    currentCharacterElement.setAttribute("style", currentCharacter.normal);
 +
  }
 +
 
 +
  selectedElement.setAttribute("style", currentCharacter.onClick);
 +
  currentCharacter.element = selectedElement;
 +
}
 +
 
 +
 
 +
function updateForm(formData, characterCreation, characters, selectedElement) {
 +
 
 +
  saveButtonGreen(characters);
 +
  showElement(characterCreation);
 +
  handleStyle(characters.currentCharacter, selectedElement);
 
    
 
    
 
   characterCreation.reset();
 
   characterCreation.reset();
Ligne 363 : Ligne 386 :
 
      
 
      
 
     var pseudo = target.querySelector("span.input").textContent;
 
     var pseudo = target.querySelector("span.input").textContent;
   
 
    saveButtonGreen(characters);
 
    showElement(characters.characterCreation);
 
  
 
     if (!characters.unsavedChanges) {
 
     if (!characters.unsavedChanges) {
       updateForm(characters.savedCharacters[pseudo], characters.characterCreation, characters);
+
       updateForm(characters.savedCharacters[pseudo], characters.characterCreation, characters, target);
  
 
     } else if (displayedPseudo === pseudo) {
 
     } else if (displayedPseudo === pseudo) {
Ligne 377 : Ligne 397 :
  
 
       if (result) {
 
       if (result) {
         updateForm(characters.savedCharacters[pseudo], characters.characterCreation, characters);
+
         updateForm(characters.savedCharacters[pseudo], characters.characterCreation, characters, target);
        saveButtonGreen(characters);
 
 
         characters.unsavedChanges = false;
 
         characters.unsavedChanges = false;
 
       }
 
       }
Ligne 447 : Ligne 466 :
 
   });
 
   });
 
    
 
    
   return [spanInput, svgContainer];
+
  newCharacterTemplate.addEventListener("keydown", function(event) {
 +
    if (event.keyCode === 13) {
 +
      handleClickOnCharacter(event.currentTarget, characters, battle, true);
 +
    }
 +
  });
 +
 
 +
   return [newCharacterTemplate, spanInput, svgContainer];
 
}
 
}
  
Ligne 465 : Ligne 490 :
 
function addNewCharacter(characters, characterTemplate, charactersContainer, battle, pseudoToDuplicate) {
 
function addNewCharacter(characters, characterTemplate, charactersContainer, battle, pseudoToDuplicate) {
  
   function editAndSetCharacterPseudoInput(spanInput, svgContainer) {
+
   function editAndSetCharacterPseudoInput(selectedCharacter, spanInput, svgContainer) {
 
+
   
 +
    var maxPseudoLength = 15;
 +
   
 
     var selection = window.getSelection();
 
     var selection = window.getSelection();
 
     var range = document.createRange();
 
     var range = document.createRange();
Ligne 497 : Ligne 524 :
 
       spanInput.textContent = characterPseudo;
 
       spanInput.textContent = characterPseudo;
 
        
 
        
      saveButtonGreen(characters);
+
       updateForm(characterDataObject, characters.characterCreation, characters, selectedCharacter);
      showElement(characters.characterCreation);
 
       updateForm(characterDataObject, characters.characterCreation, characters);
 
 
       saveCharacter(characters.savedCharacters, characters.characterCreation, battle, true);
 
       saveCharacter(characters.savedCharacters, characters.characterCreation, battle, true);
 +
    }
 +
   
 +
    function handleMaxLength(event) {
 +
      if (spanInput.textContent.length > maxPseudoLength) {
 +
        spanInput.textContent = spanInput.textContent.slice(0, maxPseudoLength);
 +
        range.setStart(spanInput.childNodes[0], maxPseudoLength);
 +
        selection.removeAllRanges();
 +
        selection.addRange(range);
 +
      }
 
     }
 
     }
  
 
     function handleBlur() {
 
     function handleBlur() {
 
       spanInput.removeEventListener("blur", handleBlur);
 
       spanInput.removeEventListener("blur", handleBlur);
 +
      spanInput.removeEventListener("input", handleMaxLength);
 
       pseudoValidation();
 
       pseudoValidation();
 
     }
 
     }
Ligne 516 : Ligne 551 :
 
         spanInput.removeEventListener("keydown", handleKeyDown);
 
         spanInput.removeEventListener("keydown", handleKeyDown);
 
         spanInput.removeEventListener("blur", handleBlur);
 
         spanInput.removeEventListener("blur", handleBlur);
 +
        spanInput.removeEventListener("input", handleMaxLength);
 
          
 
          
 
         pseudoValidation();
 
         pseudoValidation();
 
       }
 
       }
 
     }
 
     }
 
+
   
 +
    spanInput.addEventListener("input", handleMaxLength);
 
     spanInput.addEventListener("keydown", handleKeyDown);
 
     spanInput.addEventListener("keydown", handleKeyDown);
 
     spanInput.addEventListener("blur", handleBlur);
 
     spanInput.addEventListener("blur", handleBlur);
Ligne 526 : Ligne 563 :
 
    
 
    
 
   hideElement(characters.characterCreation);
 
   hideElement(characters.characterCreation);
   var [spanInput, svgContainer] = handleNewCharacter(characters, characterTemplate, charactersContainer, battle);
+
   var [selectedCharacter, spanInput, svgContainer] = handleNewCharacter(characters, characterTemplate, charactersContainer, battle);
  
   editAndSetCharacterPseudoInput(spanInput, svgContainer);
+
   editAndSetCharacterPseudoInput(selectedCharacter, spanInput, svgContainer);
 
}
 
}
  
Ligne 953 : Ligne 990 :
  
 
     battle.damageResult.textContent = attacker.name + " inflige " + damageFormat(meanDamages, 1) + " dégâts en moyenne à " + victim.name + " (minimum : " + minDamages + ", maximum : " + maxDamages + ").";
 
     battle.damageResult.textContent = attacker.name + " inflige " + damageFormat(meanDamages, 1) + " dégâts en moyenne à " + victim.name + " (minimum : " + minDamages + ", maximum : " + maxDamages + ").";
   })
+
   });
 
    
 
    
 
}
 
}
Ligne 963 : Ligne 1 000 :
 
     unsavedChanges: false,
 
     unsavedChanges: false,
 
     savedCharacters: {},
 
     savedCharacters: {},
 +
    currentCharacter: {element: null},
 
     characterCreation: document.getElementById("character-creation"),
 
     characterCreation: document.getElementById("character-creation"),
 
     addNewCharacterButton: document.getElementById("add-new-character"),
 
     addNewCharacterButton: document.getElementById("add-new-character"),
Ligne 976 : Ligne 1 014 :
 
     randomAttackValue: document.getElementById("random-attack-value")
 
     randomAttackValue: document.getElementById("random-attack-value")
 
   }
 
   }
 +
 +
  characters.currentCharacter.normal = characters.newCharacterTemplate.getAttribute("style");
 +
  characters.currentCharacter.onClick = characters.currentCharacter.normal + ' ' + characters.newCharacterTemplate.dataset.click;
 +
 
 +
  delete characters.newCharacterTemplate.dataset.click;
 +
 
   var savedCharacters = getSavedCharacters();
 
   var savedCharacters = getSavedCharacters();
  

Version du 15 octobre 2023 à 08:06

function showElement(element) {
  element.classList.remove("tabber-noactive");
}


function hideElement(element) {
  element.classList.add("tabber-noactive");
}


function isValueInArray(value, array) {
  return array.indexOf(value) !== -1;
}


function copyObject(object) {
  var copy = {};
  for (var key in object) {
    copy[key] = object[key];
  }
  return copy;
}


function averageArray(arr, weights, cardinal) {
  
  var average = 0;
  
  for (var indexArray = 0; indexArray < arr.length; indexArray++) {
    average += arr[indexArray] * weights[indexArray];
  }
  
  return average / cardinal;
}


function addWeapon(weaponChoice) {
  
  for (var weapon in weaponData) {
    var option = document.createElement("option");
    option.textContent = weaponData[weapon][0];
    option.value = weapon;
    
    var weaponType = weaponData[weapon][1];
    
    if (weaponType !== 0 && weaponType !== 3) {
      hideElement(option);
    }
    weaponChoice.appendChild(option);
  }
}


function filterClass(raceChoice, classChoice, selectValueIsChanged = false) {
  
  var selectedRace = raceChoice.value;
  
  if (selectedRace == "lycan") {
      
    hideElement(classChoice.parentElement);

  } else {

    showElement(classChoice.parentElement);

    for (var option of classChoice.options) {
      if (option.getAttribute("data-race") === selectedRace) {
        if (!selectValueIsChanged) {
          classChoice.value = option.value;
          selectValueIsChanged = true;
        }
        showElement(option);
      } else {
        hideElement(option);
      }
    }
  }
}


function filterWeapon(raceChoice, weaponChoice, selectValueIsChanged = false) {
  
  var allowedWeaponsPerRace = {
    'warrior': [0, 3, 8],
    'ninja': [0, 1, 2, 8],
    'sura': [0, 7, 8],
    'shaman': [4, 6, 8],
    'lycan': [5, 8]
  }
  var allowedWeapons = allowedWeaponsPerRace[raceChoice.value]
  
  if (!selectValueIsChanged) {
    var weaponType = weaponData[weaponChoice.value][1];
    
    if (!isValueInArray(weaponType, allowedWeapons)) {
      weaponChoice.value = "Fist";
    }
  }

  for (var option of weaponChoice.options) {
    var weaponType = weaponData[option.value][1];
    
    if (isValueInArray(weaponType, allowedWeapons)) {
      showElement(option);
      
    } else {
      hideElement(option);
    }
  }
}


function filterUpgrade(weaponUpgrade, weaponChoice, randomAttackValue, currentUpgrade) {
  
  var weaponName = weaponChoice.value;

  if (isValueInArray("serpent", weaponName.toLowerCase())) {
    showElement(randomAttackValue);
    
  } else {
    hideElement(randomAttackValue);
  }
    
  var upgradeNumber = weaponData[weaponName][3].length;
  
  if (!upgradeNumber) {
    
    hideElement(weaponUpgrade.parentElement);
    
  } else {
    
    showElement(weaponUpgrade.parentElement);
    
    weaponUpgrade.innerHTML = "";

    for (var upgrade = 0; upgrade < upgradeNumber; upgrade++) {
   
      var option = document.createElement("option");
      option.value = upgrade;
      option.textContent = "+" + upgrade;
      weaponUpgrade.appendChild(option);
    }
    if (currentUpgrade === undefined) {
      option.selected = true;
    } else {
      weaponUpgrade.value = currentUpgrade;
      currentUpgrade = undefined;
    }
  }
}


function filterForm(characters) {
  
  addWeapon(characters.weaponChoice);
  
  characters.raceChoice.addEventListener("change", function(event) {
    
    filterClass(characters.raceChoice, characters.classChoice);
    filterWeapon(characters.raceChoice, characters.weaponChoice);
    
  });
  
  characters.weaponChoice.addEventListener("change", function(event) {
    
    filterUpgrade(characters.weaponUpgrade, characters.weaponChoice, characters.randomAttackValue);
    
  });
}


function getSavedCharacters() {
  var savedCharacters = localStorage.getItem("savedCharactersCalculator");

  if (savedCharacters) {
    return JSON.parse(savedCharacters);
  }
  return {};
}


function addUniquePseudo(characterDataObject, savedCharactersPseudo) {

  var characterPseudo = characterDataObject.name;
  var originalPseudo = characterPseudo;
  var count = 0;
  
  var regex = /(.*)(\d)$/;
  var match = characterPseudo.match(regex);
  
  if (match) {
    originalPseudo = match[1];
    count = match[2];
  }
  
  while (isValueInArray(characterPseudo, savedCharactersPseudo)) {
    characterPseudo = originalPseudo + count;
    count++;
  }
  
  characterDataObject.name = characterPseudo;
  return [characterDataObject, characterPseudo]
}


function convertToNumber(value) {
  var valueNumber = Number(value);
  return isNaN(valueNumber) ? value : valueNumber;
}


function updateSavedCharacters(savedCharacters) {
  localStorage.setItem("savedCharactersCalculator", JSON.stringify(savedCharacters));
}


function saveCharacter(savedCharacters, characterCreation, battle, newCharacter, characterDataObject) {
  
  if (!characterDataObject) {
    var characterData = new FormData(characterCreation);
    var characterDataObject = {};

    characterData.forEach(function(value, key) {
      characterDataObject[key] = convertToNumber(value);
    });
  }

  savedCharacters[characterDataObject.name] = characterDataObject;
  updateSavedCharacters(savedCharacters);
  
  if (newCharacter) {
    addBattleChoice(battle, characterDataObject.name);
  }
}


function saveButtonGreen(characters, animation) {
  if (animation) {
    characters.saveBar.style.animation = characters.saveButton.dataset.animation;
  } else {
    characters.saveBar.style.animation = "";
    characters.saveBar.style.backgroundColor = "lightgreen";
  }
}


function saveButtonOrange(characters) {
  characters.saveBar.style.animation = "";
  characters.saveBar.style.backgroundColor = "#ffdd40";
}


function characterCreationListener(characters, battle) {

  characters.characterCreation.addEventListener("submit", function(event) {
    event.preventDefault();

    if (characters.unsavedChanges) {
      saveCharacter(characters.savedCharacters, characters.characterCreation, battle);
      saveButtonGreen(characters, true);
      characters.unsavedChanges = false;
    }
  });
}


function downloadCharacter(character) {
  
  var content = JSON.stringify(character);
  var link = document.createElement("a");
  var blob = new Blob([content], {type: "text/plain"});
  var blobURL = URL.createObjectURL(blob);

  link.href = blobURL;
  link.download = character.name + ".txt";
  link.click();
  URL.revokeObjectURL(blobURL);
}


function uploadCharacter(characters, characterTemplate, charactersContainer, battle) {
  
  var fileInput = document.createElement("input");
  
  fileInput.type = "file";
  fileInput.accept = ".txt";
  fileInput.multiple = true;
  fileInput.click();
  
  fileInput.addEventListener("change", function(event) {
    var selectedFiles = event.target.files;
    var selectFilesLength = selectedFiles.length;
    
    hideElement(characters.characterCreation);

    for (var fileIndex = 0; fileIndex < selectFilesLength; fileIndex++) {
      var selectedFile = selectedFiles[fileIndex];
      
      if (selectedFile.type === "text/plain") {
        var reader = new FileReader();
        reader.onload = function(e) {
          var fileContent = e.target.result;
          try {
            var characterDataObject = JSON.parse(fileContent);
            var characterPseudo = characterDataObject.name;
            
            if (characterPseudo) {
              characterPseudo = validPseudo(characterPseudo);
              [characterDataObject, characterPseudo] = addUniquePseudo(characterDataObject, Object.keys(characters.savedCharacters));
              var selectedCharacter = handleNewCharacter(characters, characterTemplate, charactersContainer, battle, characterPseudo)[0];
              
              if (selectFilesLength === 1) {
                updateForm(characterDataObject, characters.characterCreation, characters, selectedCharacter);
              }
  
              saveCharacter(characters.savedCharacters, characters.characterCreation, battle, true, characterDataObject);
            }
          } catch (error) {
            if (error.name === "TypeError") {
              // delete the character
            }
          }
        };
        reader.readAsText(selectedFile);
      }
    }
});
}


function deleteCharacter(characters, pseudo, displayedPseudo, element, battle) {
  
  delete characters.savedCharacters[pseudo];
  
  element.remove();
  
  updateSavedCharacters(characters.savedCharacters);
  removeBattleChoice(battle, pseudo);
  
  if (!Object.keys(characters.savedCharacters).length || characters.characterCreation.name.value === pseudo) {
    saveButtonGreen(characters);
    characters.unsavedChanges = false;
    hideElement(characters.characterCreation);
  }
}


function handleStyle(currentCharacter, selectedElement) {

  var currentCharacterElement = currentCharacter.element;

  if (currentCharacterElement) {
    currentCharacterElement.setAttribute("style", currentCharacter.normal);
  }
  
  selectedElement.setAttribute("style", currentCharacter.onClick);
  currentCharacter.element = selectedElement;
}


function updateForm(formData, characterCreation, characters, selectedElement) {
  
  saveButtonGreen(characters);
  showElement(characterCreation);
  handleStyle(characters.currentCharacter, selectedElement);
  
  characterCreation.reset();
  
  for (var [name, value] of Object.entries(formData)) {
    var formElement = characterCreation[name];
    if (formElement) {
      formElement.value = value;
    }
  }
  filterClass(characters.raceChoice, characters.classChoice, true);
  filterWeapon(characters.raceChoice, characters.weaponChoice, true);
  filterUpgrade(characters.weaponUpgrade, characters.weaponChoice, characters.randomAttackValue, formData.upgrade);
}


function handleClickOnCharacter(target, characters, battle, edition) {
  
  var displayedPseudo = characters.characterCreation.name.value;
  
  if (edition) {
    
    var pseudo = target.querySelector("span.input").textContent;

    if (!characters.unsavedChanges) {
      updateForm(characters.savedCharacters[pseudo], characters.characterCreation, characters, target);

    } else if (displayedPseudo === pseudo) {
      // pass

    } else {
      var result = confirm("Voulez-vous continuer ? Les dernières modifications ne seront pas sauvegardées.");

      if (result) {
        updateForm(characters.savedCharacters[pseudo], characters.characterCreation, characters, target);
        characters.unsavedChanges = false;
      }
    }
  } else {
    
    if (target.tagName === "path") {
      target = target.parentElement;
    }
    
    var pseudo = target.parentElement.dataset.pseudo;
    
    switch (target.dataset.icon) {

      case 'duplicate':

        if (!characters.unsavedChanges) {
          addNewCharacter(characters, characters.newCharacterTemplate, characters.charactersContainer, battle, pseudo);

        } else {
          var result = confirm("Voulez-vous continuer ? Les dernières modifications ne seront pas sauvegardées.");

          if (result) {
            addNewCharacter(characters, characters.newCharacterTemplate, characters.charactersContainer, battle, pseudo);
            saveButtonGreen(characters);
            characters.unsavedChanges = false;
          }
        }
        break;

      case 'download':
        downloadCharacter(characters.savedCharacters[pseudo]);
        break;

      case 'delete':
        var result = confirm("Voulez-vous vraiment supprimer définitivement le personnage " + pseudo + " ?");
        if (result) {
          deleteCharacter(characters, pseudo, displayedPseudo, target.parentElement.parentElement, battle);
        }
        break;
    }
  }
}


function handleNewCharacter(characters, characterTemplate, charactersContainer, battle, pseudo) {
  var newCharacterTemplate = characterTemplate.cloneNode(true);
  var spanInput = newCharacterTemplate.querySelector("span.input");
  var svgContainer = newCharacterTemplate.querySelector("div.svg-container");
  
  newCharacterTemplate.setAttribute("tabindex", "0");
  charactersContainer.appendChild(newCharacterTemplate);
  
  if (pseudo) {
    spanInput.textContent = pseudo;
    svgContainer.setAttribute("data-pseudo", pseudo);
  }
  
  newCharacterTemplate.addEventListener("click", function(event) {
    var currentTarget = event.currentTarget;
    var target = event.target;
    
    if (target.tagName === "path" || target.tagName === "svg") {
      handleClickOnCharacter(target, characters, battle);
    } else {
      handleClickOnCharacter(currentTarget, characters, battle, true);
    }
  });
  
  newCharacterTemplate.addEventListener("keydown", function(event) {
    if (event.keyCode === 13) {
      handleClickOnCharacter(event.currentTarget, characters, battle, true);
    }
  });
  
  return [newCharacterTemplate, spanInput, svgContainer];
}


function validPseudo(pseudo) {
  
  var newPseudo = pseudo.replace(/[^A-Za-z0-9]+/g, "");
  
  if (!newPseudo) {
    return "Pseudo"
  }
  
  return newPseudo;
}


function addNewCharacter(characters, characterTemplate, charactersContainer, battle, pseudoToDuplicate) {

  function editAndSetCharacterPseudoInput(selectedCharacter, spanInput, svgContainer) {
    
    var maxPseudoLength = 15;
    
    var selection = window.getSelection();
    var range = document.createRange();
    
    if (pseudoToDuplicate) {
      spanInput.textContent = pseudoToDuplicate;
    }
    
    spanInput.contentEditable = true;
    spanInput.focus();
    range.selectNodeContents(spanInput);
    selection.removeAllRanges();
    selection.addRange(range);
    
    function pseudoValidation() {
      
      var characterPseudo = validPseudo(spanInput.textContent);
      var characterDataObject = {name: characterPseudo};
      
      if (pseudoToDuplicate) {
        characterDataObject = copyObject(characters.savedCharacters[pseudoToDuplicate]);
        characterDataObject.name = characterPseudo;
      }
      
      [characterDataObject, characterPseudo] = addUniquePseudo(characterDataObject, Object.keys(characters.savedCharacters));
      
      svgContainer.setAttribute("data-pseudo", characterPseudo);
      selection.removeAllRanges();
      spanInput.contentEditable = false;
      spanInput.textContent = characterPseudo;
      
      updateForm(characterDataObject, characters.characterCreation, characters, selectedCharacter);
      saveCharacter(characters.savedCharacters, characters.characterCreation, battle, true);
    }
    
    function handleMaxLength(event) {
      if (spanInput.textContent.length > maxPseudoLength) {
        spanInput.textContent = spanInput.textContent.slice(0, maxPseudoLength);
        range.setStart(spanInput.childNodes[0], maxPseudoLength);
        selection.removeAllRanges();
        selection.addRange(range);
      }
    }

    function handleBlur() {
      spanInput.removeEventListener("blur", handleBlur);
      spanInput.removeEventListener("input", handleMaxLength);
      pseudoValidation();
    }
    
    function handleKeyDown(event) {

      if (event.key === "Enter") {
        
        event.preventDefault();
        
        spanInput.removeEventListener("keydown", handleKeyDown);
        spanInput.removeEventListener("blur", handleBlur);
        spanInput.removeEventListener("input", handleMaxLength);
        
        pseudoValidation();
      }
    }
    
    spanInput.addEventListener("input", handleMaxLength);
    spanInput.addEventListener("keydown", handleKeyDown);
    spanInput.addEventListener("blur", handleBlur);
  }
  
  hideElement(characters.characterCreation);
  var [selectedCharacter, spanInput, svgContainer] = handleNewCharacter(characters, characterTemplate, charactersContainer, battle);

  editAndSetCharacterPseudoInput(selectedCharacter, spanInput, svgContainer);
}


function characterManagement(characters, battle) {
  
  var characterTemplate = characters.newCharacterTemplate;
  var charactersContainer = characters.charactersContainer;
  
  Object.keys(characters.savedCharacters).forEach(function(pseudo) {
    handleNewCharacter(characters, characterTemplate, charactersContainer, battle, pseudo);
  });

  characters.addNewCharacterButton.addEventListener("click", function(event) {
    if (!characters.unsavedChanges) {
      addNewCharacter(characters, characterTemplate, charactersContainer, battle);
      
    } else {
      var result = confirm("Voulez-vous continuer ? Les dernières modifications ne seront pas sauvegardées.");
      
      if (result) {
        addNewCharacter(characters, characterTemplate, charactersContainer, battle);
        saveButtonGreen(characters);
        characters.unsavedChanges = false;
      }
    }
  });
  
  characters.uploadCharacter.addEventListener("click", function(event) {
    uploadCharacter(characters, characterTemplate, charactersContainer, battle);
  });
  
  characters.characterCreation.addEventListener("change", function() {
    saveButtonOrange(characters)
    characters.unsavedChanges = true;
  })
  
  filterForm(characters);
  characterCreationListener(characters, battle);
  
  window.addEventListener('beforeunload', function(event) {
    if (characters.unsavedChanges) {
      event.preventDefault();
      event.returnValue = '';
      return '';
    }
  });
}


function removeBattleChoice(battle, name) {
  
  var battleSelects = [battle.attackerSelection, battle.victimSelection];
  
  battleSelects.forEach(function(battleSelect) {
    for (var optionIndex = 0; optionIndex < battleSelect.options.length; optionIndex++) {
      if (battleSelect.options[optionIndex].value === name) {
        battleSelect.remove(optionIndex);
        break;
      }
    }
  });
}


function addBattleChoice(battle, name) {
  
  function createOption(text) {
    var option = document.createElement("option");
    option.textContent = text;
    option.value = text;
    return option;
  }
  
  battle.attackerSelection.appendChild(createOption(name));
  battle.victimSelection.appendChild(createOption(name));
}


function updateBattleChoice(savedCharacters, battle) {
  
  addBattleChoice(battle, "Chien errant");

  Object.keys(savedCharacters).forEach(function(pseudo) {
    addBattleChoice(battle, pseudo);
  });
}


function isPC(character) {
  if (character.race === "monster") {
    return false;
  }
  return true;
}


function calcAttackFactor(attacker, victim) {

  function calcCoeffK(dex, level) {
    return Math.min(90, Math.floor((2 * dex + level) / 3))
  }

  var K1 = calcCoeffK(attacker.dex, attacker.level);
  var K2 = calcCoeffK(victim.dex, attacker.level);
  
  var AR = (K1 + 210) / 300;
  var ER = (2 * K2 + 5) / (K2 + 95) * 3 / 10;
  
  return AR - ER
}


function calcMainAttackValue(attacker, attackerWeapon) {
  
  var leadership = 0;
  var rawWeaponAttackValue = 0;
  
  if (isPC(attacker)) {
    
    var rawWeaponAttackValue = attackerWeapon[3][attacker.upgrade];

    if (!rawWeaponAttackValue) {
      rawWeaponAttackValue = 0;
    }
    
    leadership = attacker.leadership;
  }
  
  return 2 * (attacker.level + rawWeaponAttackValue) + leadership;
}


function calcStatAttackValue(character) {
  
  switch (character.race) {
    case 'warrior':
    case 'sura':
      return 2 * character.str
    case 'ninja':
      return parseInt(1 / 4 * (character.str + 7 * character.dex))
    case 'shaman':
      return parseInt(1 / 3 * (5 * character.int + character.dex))
    case 'lycan':
      return character.vit + 2 * character.dex;
    default:
      return 2 * character.str
  }
}


function calcSecondaryAttackValue(attacker, attackerWeapon) {
  
  var attackValues = [];
  var weights = [];
  
  var attackValueOther = 0;
  
  var minAttackValue = 0;
  var maxAttackValue = 0;
  
  var minAttackValueSlash = 0;
  var maxAttackValueSlash = 0;
  
  if (isPC(attacker)) {
    
    if (!isValueInArray("serpent", attacker.weapon.toLowerCase())) {

      minAttackValue += attackerWeapon[2][2];
      maxAttackValue += attackerWeapon[2][3];

    } else {

      var rawAttackValue = attackerWeapon[3][attacker.upgrade];
      
      minAttackValue += attacker.randomAttackValueMin - rawAttackValue;
      maxAttackValue += attacker.randomAttackValueMax  - rawAttackValue;
      
      minAttackValue = Math.max(0, minAttackValue);
      maxAttackValue = Math.max(minAttackValue, maxAttackValue);
    }
    
    minAttackValueSlash += Math.min(attacker.slashMin, attacker.slashMax);
    maxAttackValueSlash += Math.max(attacker.slashMin, attacker.slashMax);
    
    attackValueOther += attacker.attackValue;
    
  } else {
    
    minAttackValue += attacker.randomAttackValueMin;
    maxAttackValue += attacker.randomAttackValueMax;
  }
  
  attackValueOther += calcStatAttackValue(attacker);

  var weaponRange = maxAttackValue - minAttackValue;
  var slashRange = maxAttackValueSlash - minAttackValueSlash;
  
  var totalCardinal = (weaponRange + 1) * (slashRange + 1);
  var minRange = Math.min(weaponRange, slashRange) + 1;
  
  minAttackValue += minAttackValueSlash;
  maxAttackValue += maxAttackValueSlash;

  return [minAttackValue, maxAttackValue, attackValueOther, minRange, totalCardinal];
}


function calcDamageWithPrimaryBonuses(attacker, victim, damages) {
  
  if (isPC(attacker)) {
    damages = parseInt(damages * (1 + (attacker.attackValuePercent + Math.min(100, attacker.attackMeleeMagic)) / 100));
  }
  
  damages -= victim.defense;
  
  return damages;
}


function calcDamageWithSecondaryBonuses(attacker, victim, damages, battleValues) {
  
  damages = parseInt(damages * battleValues.weaponDefenseCoeff);
  damages = parseInt(damages * battleValues.averageDamageCoeff);
  damages = parseInt(damages * battleValues.averageDamageResistanceCoeff);
  damages += Math.floor(battleValues.defensePercent);
  
  return damages;
}


function createBattleValues(attacker, victim, mapping) {
  
  var weaponDefense = 0;
  var averageDamage = 0;
  var averageDamageResistance = 0;
  var defensePercent = 0;
  
  if (isPC(attacker)) {
    
    var weaponType = 8;
    
    if (weaponData.hasOwnProperty(attacker.weapon))
      weaponType = weaponData[attacker.weapon][1];
    
    var weaponDefenseName = mapping.defense[weaponType];
    var weaponDefenseBreakName = mapping.break[weaponType]
    
    if (victim.hasOwnProperty(weaponDefenseName)) {
      weaponDefense = victim[weaponDefenseName];
    }
    
    if (isPC(victim) && attacker.hasOwnProperty(weaponDefenseBreakName)) {
      weaponDefense -= attacker[weaponDefenseBreakName];
    }
    
    averageDamage = attacker.averageDamage;
  }
  
  if (isPC(victim)) {
    
    averageDamageResistance = victim.averageDamageResistance;
    
    if (victim.race === "shaman" || victim.class === "black_magic") {
      defensePercent = -2 * victim.magicDefense * victim.defensePercent / 100;

    } else {
      defensePercent = -2 * victim.defense * victim.defensePercent / 100;
    }
  }
  
  var battleValues = {
    weaponDefenseCoeff: (1 - weaponDefense / 100),
    averageDamageCoeff: (1 + averageDamage / 100),
    averageDamageResistanceCoeff: (1 - Math.min(99, averageDamageResistance) / 100),
    defensePercent: defensePercent
  }

  return battleValues;
}


function calcBattleDamages(attacker, victim, mapping) {
  
  var allDamages = [];
  var weights = [];
  var attackerWeapon = null;
  var minDamageIsReached = false;
  var battleValues = createBattleValues(attacker, victim, mapping);
  
  var maxDamages = 0;
  
  if (isPC(attacker)) {
     attackerWeapon = weaponData[attacker.weapon];
  }

  var attackFactor = calcAttackFactor(attacker, victim);
  var mainAttackValue = calcMainAttackValue(attacker, attackerWeapon);
  var [minAttackValue, maxAttackValue, attackValueOther, minRange, totalCardinal] = calcSecondaryAttackValue(attacker, attackerWeapon);

  var lastWeightsLimit = maxAttackValue - minRange + 1;
  var firstWeightLimit = minAttackValue + minRange - 1;
  
  for (var attackValue = maxAttackValue; attackValue >= minAttackValue; attackValue--) {

    var weight;
    
    if (attackValue > lastWeightsLimit) {
      weight = maxAttackValue - attackValue + 1;
      
    } else if (attackValue < firstWeightLimit) {
      weight = attackValue - minAttackValue + 1;
      
    } else {
      weight = minRange;
    }
    
    if (!minDamageIsReached) {
      
      var secondaryAttackValue = 2 * attackValue + attackValueOther;
      var rawDamages = mainAttackValue + parseInt(attackFactor * secondaryAttackValue);
      var damages = calcDamageWithPrimaryBonuses(attacker, victim, rawDamages);
      
      if (damages <= 2) {
        
        for (damages = 5; damages >= 1; damages--) {
          
          var finalDamages = calcDamageWithSecondaryBonuses(attacker, victim, damages, battleValues);
          
          allDamages.push(finalDamages);
          weights.push(weight / 5);
          
          minDamageIsReached = true;
          
          if (damages == 5 && allDamages.length) {
            
            if (finalDamages > allDamages[0]) {
              maxDamages = finalDamages;
            }
          }
        }
        
      } else {
        
        finalDamages = calcDamageWithSecondaryBonuses(attacker, victim, damages, battleValues);
        allDamages.push(finalDamages);
        weights.push(weight);
      }
      
    } else {
      
      var weightsLength = weights.length;
      
      for (var index = 1; index <= 5; index++) {
        weights[weightsLength - index] += weight / 5;
      }
      
    }
  }

  return [averageArray(allDamages, weights, totalCardinal), allDamages[allDamages.length - 1], Math.max(maxDamages, allDamages[0])];
}


function damageFormat(damage, precision) {
  return Math.max(0, Math.round((damage) * 10**precision) / 10**precision);
}


function createMonster(name) {
  
  var data = monsterData[name];

  var monster = {
    "name": name,
    "level": data[0],
    "race": "monster",
    "vit": data[3],
    "int": data[4],
    "str": data[1],
    "dex": data[2],
    "randomAttackValueMin": data[5],
    "randomAttackValueMax": data[6],
    "defense": data[7] + data[0] + data[3],
    "fistDefense": data[8],
    "swordDefense": data[9],
    "twoHandedDefense": data[10],
    "daggerDefense": data[11],
    "bellDefense": data[12],
    "fanDefense": data[13],
    "bowDefense": data[14],
    "clawDefense": data[15]
  }
  
  return monster;
}


function createBattle(characters, battle) {
  
  battle.battleForm.addEventListener("submit", function(event) {
    
    event.preventDefault();
    
    var battleInfo = new FormData(event.target)
    var attackerName = battleInfo.get("attacker");
    var victimName = battleInfo.get("victim");
    
    if (attackerName === 'Chien errant') {
      var attacker = createMonster('Chien errant');
      
    } else {
      var attacker = characters.savedCharacters[attackerName];
    }
    
    if (victimName === 'Chien errant') {
      var victim = createMonster('Chien errant');
      
    } else {
      var victim = characters.savedCharacters[victimName];
    }

    var [meanDamages, minDamages, maxDamages] = calcBattleDamages(attacker, victim, battle.mapping);

    battle.damageResult.textContent = attacker.name + " inflige " + damageFormat(meanDamages, 1) + " dégâts en moyenne à " + victim.name + " (minimum : " + minDamages + ", maximum : " + maxDamages + ").";
  });
  
}


function createDamageCalculatorInformation() {
  
  var characters = {
    unsavedChanges: false,
    savedCharacters: {},
    currentCharacter: {element: null},
    characterCreation: document.getElementById("character-creation"),
    addNewCharacterButton: document.getElementById("add-new-character"),
    uploadCharacter: document.getElementById("upload-character"),
    newCharacterTemplate: document.getElementById("new-character-template").children[0],
    charactersContainer: document.getElementById("characters-container"),
    saveButton: document.getElementById("save-button"),
    saveBar: document.getElementById("save-bar"),
    raceChoice: document.getElementById("race-choice"),
    classChoice: document.getElementById("class-choice"),
    weaponChoice: document.getElementById("weapon-choice"),
    weaponUpgrade: document.getElementById("upgrade-choice"),
    randomAttackValue: document.getElementById("random-attack-value")
  }

  characters.currentCharacter.normal = characters.newCharacterTemplate.getAttribute("style");
  characters.currentCharacter.onClick = characters.currentCharacter.normal + ' ' + characters.newCharacterTemplate.dataset.click;
  
  delete characters.newCharacterTemplate.dataset.click;

  var savedCharacters = getSavedCharacters();

  for (var [pseudo, character] of Object.entries(savedCharacters)) {
    characters.savedCharacters[pseudo] = character;
  }
  
  var mapping = {
    defense: {
      0: "swordDefense",
      1: "daggerDefense",
      2: "bowDefense",
      3: "twoHandedSwordDefense",
      4: "bellDefense",
      5: "clawDefense",
      6: "fanDefense",
      7: "swordDefense",
      8: "fistDefense"
    },
    break: {
      0: "breakSwordDefense",
      1: "breakDaggerDefense",
      2: "breakBowDefense",
      3: "breakTwoHandedSwordDefense",
      4: "breakBellDefense",
      5: "breakClawDefense",
      6: "breakFanDefense",
      7: "breakSwordDefense"
    }
  }
  
  var battle = {
    battleForm: document.getElementById("create-battle"),
    attackerSelection: document.getElementById("attacker-selection"),
    victimSelection: document.getElementById("victim-selection"),
    damageResult: document.getElementById("result-damage"),
    mapping: mapping
  }

  return [characters, battle];
}


function loadScript(src, callback) {

  var script = document.createElement("script");
  script.src = src;
  
  function onComplete() {
    if (script.parentNode) {
      script.parentNode.removeChild(script);
    }
    callback();
  }
  
  document.head.appendChild(script);

  script.onload = onComplete;
  script.onerror = onComplete;
}


(function(){
  
  var src = "/index.php?title=Utilisateur:Ankhseram/Calculator.js&action=raw&ctype=text/javascript";
  
  function main() {
  
    var [characters, battle] = createDamageCalculatorInformation();

    characterManagement(characters, battle);

    updateBattleChoice(characters.savedCharacters, battle);
    createBattle(characters, battle);
  }
  
  loadScript(src, main);

})();