Widget:BoostCalcForm

Revisão de 12h24min de 2 de abril de 2023 por Rag (discussão | contribs)

<form class="boostCalc__form" data-target="calcBoostForm">

           <select name="boostType" class="boostCalc__select" id="boostType">
               <option value="2">2</option>
               <option value="3">3</option>
               <option value="5">5</option>
               <option value="10">10</option>
           </select>
           <label class="boostCalc__label" for="boostType">Selecione o tipo do boost</label>
           <input placeholder=" " type="checkbox" name="specialBoost" id="specialBoostCheckbox">
           <label for="specialBoostCheckbox">Você irá usar metal, crystal ou ancient stone ?</label>


           <input placeholder=" " name="stonePrice" id="stonePrice" class="boostCalc__input" type="number">
           <label class="boostCalc__label" for="stonePrice">Digite o preço da stone (Opcional)</label>
           <input placeholder=" " name="stonePrice" id="stonePrice" class="boostCalc__input" type="number">
           <label class="boostCalc__label" for="stonePrice">Digite o preço da Boost stone (Opcional)</label>
           <input placeholder=" " name="startBoost" id="startBoost" class="boostCalc__input" required min="0" max="49"
               type="number">
           <label class="boostCalc__label" for="startBoost">Digite o boost atual</label>
           <input placeholder=" " name="endBoost" id="endBoost" class="boostCalc__input" required min="1" max="50"
               type="number">
           <label class="boostCalc__label" for="endBoost">Digite o boost desejado</label>

<button type="submit" class="hover-minimize" style="background:none;border:none;margin-top:1.5rem;cursor:pointer;"> <img style="border-radius:12px;" src="http://51.161.108.38/images/d/d3/Banner_calculadora.png" alt="Imagem botão calcular boost" /> </button>

   </form>

<script>

     // Função para formatar numeros para o padrão decimal
       const formatNumber = (number) => Number(number).toLocaleString("pt-BR", { style: "decimal" });
       // Função que retorna o calculo de gasto para boostar um pokemon (Forma normal) 
       function calcNormalBoost(initBoost, endBoost, boostType, stonePrice, boostStonePrice) {
           const responseBoost = [];
           let usedNormalStones = 0;
           let usedBoostStones = 0;
           let stonesForBoost = 0;
           for (i = 1; i <= initBoost; i++) {
               if (i % boostType === 0) stonesForBoost++;
           }
           for (i = initBoost + 1; i <= endBoost; i++) {
               if (i % boostType != 0 && i != initBoost) stonesForBoost++;
               if ((stonesForBoost * stonePrice) < boostStonePrice) {
                   usedNormalStones += stonesForBoost;
               } else {
                   usedBoostStones++;
               }
               responseBoost.push(
                   {
                       price: formatNumber((usedNormalStones * stonePrice) + (usedBoostStones * boostStonePrice)),
                       normal_stones: usedNormalStones,
                       boost_stones: usedBoostStones,
                       boost: i
                   }
               );
           }
           return responseBoost;
       }
       // Função que retorna o calculo de gasto para boostar um pokemon (Exceção) 
       function calcExceptionBoost(initBoost, endBoost, boostType, stonePrice, boostStonePrice) {
           const responseBoost = [];
           let usedStones = 0;
           let usedNormalStones = 0;
           let usedBoostStones = 0;
           let stonesForBoost = 1;
           for (i = 1; i <= initBoost; i++) {
               if (i < 10) continue;
               if (i % boostType === 0) stonesForBoost++;
           }
           for (i = initBoost + 1; i <= endBoost; i++) {
               if (i < 10) {
                   if (!(i % 2 === 0)) {
                       responseBoost.push(
                           {
                               price: null,
                               necessary_stones: null,
                               boost: i
                           }
                       );
                       continue;
                   }
                   if ((stonesForBoost * stonePrice) < boostStonePrice) {
                       usedNormalStones += stonesForBoost;
                   } else {
                       usedBoostStones++;
                   }
                   responseBoost.push(
                       {
                           price: formatNumber((usedNormalStones * stonePrice) + (usedBoostStones * boostStonePrice)),
                           normal_stones: usedNormalStones,
                           boost_stones: usedBoostStones,
                           boost: i
                       }
                   );
                   continue;
               }
               if ((stonesForBoost * stonePrice) < boostStonePrice) {
                   usedNormalStones += stonesForBoost;
               } else {
                   usedBoostStones++;
               }
               if (i % boostType === 0) stonesForBoost++;
               responseBoost.push(
                   {
                       price: formatNumber((usedNormalStones * stonePrice) + (usedBoostStones * boostStonePrice)),
                       normal_stones: usedNormalStones,
                       boost_stones: usedBoostStones,
                       boost: i
                   }
               );
           }
           return responseBoost;
       }
       document.addEventListener('DOMContentLoaded', function () {
           const form = document.querySelector("[data-target='calcBoostForm']");
           form.addEventListener("submit", function (e) {
               e.preventDefault();
               const formData = new FormData(form);
               const boostType = formData.get("boostType");
               const specialBoost = formData.get("specialBoost");
               const stonePrice = formData.get("stonePrice") ? Number(formData.get("stonePrice")) > 1000 ? Number(formData.get("stonePrice")) / 1000 : Number(formData.get("stonePrice")) : 0;
               const boostStonePrice = formData.get("boostStonePrice") ? Number(formData.get("boostStonePrice")) > 1000 ? Number(formData.get("boostStonePrice")) / 1000 : Number(formData.get("boostStonePrice")) : 9999999;
               const startBoost = formData.get("startBoost");
               const endBoost = formData.get("endBoost");
               if (startBoost > endBoost) {
                   alert("O boost inicial não pode ser maior do que o final");
                   return;
               }
               if (specialBoost == "on") {
                   console.log(calcExceptionBoost(startBoost, endBoost, boostType, stonePrice, boostStonePrice));
               } else {
                   console.log(calcNormalBoost(startBoost, endBoost, boostType, stonePrice, boostStonePrice));
               }
           });
       });        

</script>