mardi 15 décembre 2015

Javascrit to calculate multiple checkbox values with precalculation

Currently I am using below script and that is working fine, but condition have changed and I need to pre-calculate some values first. At the moment the form is used by players that can check percentage buffs they get in the game to give them time discounts. As seen below buff 0 to 3 are percentage discounts on the total time an event last. Currently: factor 1 - 20% - 10% - 5% = factor Calculated as: 1 x 0.8 x 0.9 x 0.95 = 0.684 This factor is used with the milliseconds an event lasts and then converted into readable time.

But now this has to have buff 1 + 2 + 3 added to each other before the calculation happens. ie. factor 1 - 20% - (10% + 5% + 5%) = factor Calculated as: 1 x 0.8 x (1 - (10% + 5%)) = 0.68

As Javascripts are pretty new to me I am not sure how to do this (to get a wrap around only certain elements first). I would say I have to convert it to (% / 100 * factor) on each line to work properly. What I have found is that I most likely need getelementsbytagname, but I do not really understand how that works :(

function Clear() {
    var frm = document.getElementById("time");
    frm.reset();
    frm.elements.buff[0].focus();
}
        
function Calculate() {
// First check if only 2 crafting products are selected
    var frm = document.getElementById("time");
        if (frm.elements.buff[2].checked &&
        frm.elements.buff[3].checked &&
        frm.elements.buff[4].checked) {
            alert('You only have 2 Golden Buff Slots. \nSo you can not activate 3 Crafting Products in your game \n Please de-select one to go further.');
                frm.elements.time0.value = '';
                frm.elements.time1.value = '';
                frm.elements.time2.value = '';
    }
        else {

        factor = 1;
        if (frm.elements.buff[0].checked) {
                factor = factor * 0.8; // water
        }       
        if (frm.elements.buff[1].checked) {
                factor = factor * 0.5; // pf
        }       
        if (frm.elements.buff[2].checked) {
                factor = factor * 0.9;  // rune
        }       
        if (frm.elements.buff[3].checked) {
                factor = factor * 0.95; // egg
        }       
        if (frm.elements.buff[4].checked) {
                factor = factor * 0.95;  // cb
        }   

        }
        if (frm.elements.stable[0].checked) {
                frm.elements.stable[1].checked = true;  
                frm.elements.stable[2].checked = true;  


        if (frm.elements.stable[0].checked) {
                        time0 = msTime(5400000 * factor);
                frm.elements.time0.value = time0;
        }
        else {
            frm.elements.time0.value = '';
        }    

        if (frm.elements.stable[1].checked) {
                        time1 = msTime(4800000 * factor);
                frm.elements.time1.value = time1;
        }
        else {
            frm.elements.time1.value = '';
        }

        if (frm.elements.stable[2].checked) {
                        time2 = msTime(7200000 * factor);
                frm.elements.time2.value = time2;
        }
        else {
            frm.elements.time2.value = '';
        }      
}}

// function milliseconds to time
function msTime(s) {
  var ms = s % 1000;
  s = (s - ms) / 1000;
  var secs = s % 60;
  s = (s - secs) / 60;
  var mins = s % 60;
  var hrs = (s - mins) / 60;

        mins = ("00" + mins).substr(-2);
        secs = ("00" + secs).substr(-2);

  return ' ' + hrs + 'h ' + ' ' + mins + 'm ' + ' ' + secs + 's';
}



Aucun commentaire:

Enregistrer un commentaire