Difference between revisions of "MediaWiki:Calculator.js/Luck.js"

m
m
 
(3 intermediate revisions by the same user not shown)
Line 2: Line 2:
 
  * Calculator that calculates your percentage chance for more and better loot given a specified Luck value.
 
  * Calculator that calculates your percentage chance for more and better loot given a specified Luck value.
 
  */
 
  */
var LuckToChanceCalculator = {
+
function LuckToChanceCalculator() {
     calculate: function(input, label) {
+
     var calculate = function(input, label) {
 
         if (input.value) {
 
         if (input.value) {
 
             if (isNumeric(input.value)) {
 
             if (isNumeric(input.value)) {
Line 15: Line 15:
 
             }
 
             }
 
         }
 
         }
     },
+
     };
    display: function() {
+
        var calcTableRow = document.createElement('tr');
+
        var calcTableCell = document.createElement('td');
+
        var luckInput = document.createElement('input');
+
        var calculateButton = document.createElement('input');
+
        var percentChanceLabel = document.createElement('div');
+
  
        calcTableCell.style.textAlign = 'right';
+
    var calcTableRow = document.createElement('tr');
        calcTableCell.appendChild(document.createTextNode('Luck:'));
+
    var calcTableCell = document.createElement('td');
        calcTableRow.appendChild(calcTableCell);
+
    var luckInput = document.createElement('input');
 +
    var calculateButton = document.createElement('input');
 +
    var percentChanceLabel = document.createElement('div');
  
        luckInput.id = 'luckInput';
+
    calcTableCell.style.textAlign = 'right';
        luckInput.maxLength = '4';
+
    calcTableCell.appendChild(document.createTextNode('Luck:'));
        luckInput.type = 'text';
+
    calcTableRow.appendChild(calcTableCell);
        luckInput.style.width = '35px';
+
        luckInput.onkeyup = function(keyEvent) {
+
            var keyID = window.event ? event.keyCode : keyEvent.keyCode;
+
            if (keyID == 13)
+
                calculate(luckInput, percentChanceLabel);
+
        };
+
calcTableCell = document.createElement('td');
+
        calcTableCell.appendChild(luckInput);
+
        calcTableRow.appendChild(calcTableCell);
+
  
        calculateButton.type = 'button';
+
    luckInput.id = 'luckInput';
        calculateButton.value = 'Calculate >>';
+
    luckInput.maxLength = '4';
        calculateButton.onclick = function() {
+
    luckInput.type = 'text';
            calculate(luckInput, percentChanceLabel);
+
    luckInput.style.width = '35px';
        };
+
    luckInput.onkeyup = function(keyEvent) {
calcTableCell = document.createElement('td');
+
        var keyID = window.event ? event.keyCode : keyEvent.keyCode;
        calcTableCell.appendChild(calculateButton);
+
        if (keyID == 13)
        calcTableRow.appendChild(calcTableCell);
+
          calculate(luckInput, percentChanceLabel);
 +
    };
 +
    calcTableCell = document.createElement('td');
 +
    calcTableCell.appendChild(luckInput);
 +
    calcTableRow.appendChild(calcTableCell);
  
        percentChanceLabel.appendChild(document.createTextNode('0.000% Chance'));
+
    calculateButton.type = 'button';
        percentChanceLabel.style.textAlign = 'right';
+
    calculateButton.value = 'Calculate >>';
         percentChanceLabel.style.width = '110px';
+
    calculateButton.onclick = function() {
calcTableCell = document.createElement('td');
+
         calculate(luckInput, percentChanceLabel);
        calcTableCell.appendChild(percentChanceLabel);
+
    };
        calcTableRow.appendChild(calcTableCell);
+
    calcTableCell = document.createElement('td');
 +
    calcTableCell.appendChild(calculateButton);
 +
    calcTableRow.appendChild(calcTableCell);
  
        return calcTableRow;
+
    percentChanceLabel.appendChild(document.createTextNode('0.000% Chance'));
     }
+
    percentChanceLabel.style.textAlign = 'right';
};
+
    percentChanceLabel.style.width = '110px';
 +
    calcTableCell = document.createElement('td');
 +
    calcTableCell.appendChild(percentChanceLabel);
 +
    calcTableRow.appendChild(calcTableCell);
 +
 
 +
     return calcTableRow;
 +
}
  
 
/**
 
/**
 
  * Calculator that calculates the amount of Luck required to achieve a specified percentage chance for more and better loot.
 
  * Calculator that calculates the amount of Luck required to achieve a specified percentage chance for more and better loot.
 
  */
 
  */
var ChanceToLuckCalculator = {
+
function ChanceToLuckCalculator() {
     calculate: function(input, label) {
+
     var calculate = function(input, label) {
 
         if (input.value) {
 
         if (input.value) {
 
             if (input.value > 100) {
 
             if (input.value > 100) {
Line 75: Line 74:
 
             }
 
             }
 
         }
 
         }
     },
+
     };
    display: function() {
+
        var calcTableRow = document.createElement('tr');
+
        var calcTableCell = document.createElement('td');
+
        var chanceInput = document.createElement('input');
+
        var calculateButton = document.createElement('input');
+
        var luckAmountLabel = document.createElement('div');
+
  
        calcTableCell.style.textAlign = 'right';
+
    var calcTableRow = document.createElement('tr');
        calcTableCell.appendChild(document.createTextNode('Chance:'));
+
    var calcTableCell = document.createElement('td');
        calcTableRow.appendChild(calcTableCell);
+
    var chanceInput = document.createElement('input');
 +
    var calculateButton = document.createElement('input');
 +
    var luckAmountLabel = document.createElement('div');
  
        chanceInput.id = 'chanceInput';
+
    calcTableCell.style.textAlign = 'right';
        chanceInput.maxLength = '3';
+
    calcTableCell.appendChild(document.createTextNode('Chance:'));
        chanceInput.type = 'text';
+
    calcTableRow.appendChild(calcTableCell);
        chanceInput.style.width = '25px';
+
        chanceInput.onkeyup = function(keyEvent) {
+
            var keyID = window.event ? event.keyCode : keyEvent.keyCode;
+
            if (keyID == 13)
+
                calculate(chanceInput, luckAmountLabel);
+
        };
+
calcTableCell = document.createElement('td');
+
        calcTableCell.appendChild(chanceInput);
+
        calcTableRow.appendChild(calcTableCell);
+
  
        calculateButton.type = 'button';
+
    chanceInput.id = 'chanceInput';
        calculateButton.value = 'Calculate >>';
+
    chanceInput.maxLength = '3';
        calculateButton.onclick = function() {
+
    chanceInput.type = 'text';
 +
    chanceInput.style.width = '25px';
 +
    chanceInput.onkeyup = function(keyEvent) {
 +
        var keyID = window.event ? event.keyCode : keyEvent.keyCode;
 +
        if (keyID == 13)
 
             calculate(chanceInput, luckAmountLabel);
 
             calculate(chanceInput, luckAmountLabel);
        };
+
    };
calcTableCell = document.createElement('td');
+
    calcTableCell = document.createElement('td');
         calcTableCell.appendChild(calculateButton);
+
    calcTableCell.appendChild(chanceInput);
        calcTableRow.appendChild(calcTableCell);
+
    calcTableRow.appendChild(calcTableCell);
 +
 
 +
    calculateButton.type = 'button';
 +
    calculateButton.value = 'Calculate >>';
 +
    calculateButton.onclick = function() {
 +
         calculate(chanceInput, luckAmountLabel);
 +
    };
 +
    calcTableCell = document.createElement('td');
 +
    calcTableCell.appendChild(calculateButton);
 +
    calcTableRow.appendChild(calcTableCell);
  
        luckAmountLabel.appendChild(document.createTextNode('0 Luck'));
+
    luckAmountLabel.appendChild(document.createTextNode('0 Luck'));
        luckAmountLabel.style.textAlign = 'right';
+
    luckAmountLabel.style.textAlign = 'right';
        luckAmountLabel.style.width = '60px';
+
    luckAmountLabel.style.width = '60px';
calcTableCell = document.createElement('td');
+
    calcTableCell = document.createElement('td');
        calcTableCell.appendChild(luckAmountLabel);
+
    calcTableCell.appendChild(luckAmountLabel);
        calcTableRow.appendChild(calcTableCell);
+
    calcTableRow.appendChild(calcTableCell);
  
        return calcTableRow;
+
    return calcTableRow;
    }
+
}
};
+
  
 
function searchForCalculators() {
 
function searchForCalculators() {
Line 125: Line 123:
 
     for (var i in calculators) {
 
     for (var i in calculators) {
 
         if (calculators[i].id == 'LuckToChance') {
 
         if (calculators[i].id == 'LuckToChance') {
             var luckToChance = new Calculator();
+
             Calculator(LuckToChanceCalculator(), 'LuckToChance', 'Luck to Chance', 4);
            luckToChance.display(LuckToChanceCalculator.display(), 'LuckToChance', 'Luck to Chance', 4);
+
 
         } else if (calculators[i].id == 'ChanceToLuck') {
 
         } else if (calculators[i].id == 'ChanceToLuck') {
             var chanceToLuck = new Calculator();
+
             Calculator(ChanceToLuckCalculator(), 'ChanceToLuck', 'Chance to Luck', 4);
            chanceToLuck.display(ChanceToLuckCalculator.display(), 'ChanceToLuck', 'Chance to Luck', 4);
+
 
         }
 
         }
 
     }
 
     }
 
}
 
}
 
addOnloadHook(searchForCalculators);
 
addOnloadHook(searchForCalculators);

Latest revision as of 13:57, 11 January 2011

/**
 * Calculator that calculates your percentage chance for more and better loot given a specified Luck value.
 */
function LuckToChanceCalculator() {
    var calculate = function(input, label) {
        if (input.value) {
            if (isNumeric(input.value)) {
                var percentChance = Math.pow(input.value, (5 / 9));
                if (percentChance > 100) {
                    percentChance = 100;
                }
                percentChance = percentChance.toFixed(3);
                removeChildren(label);
                label.appendChild(document.createTextNode(percentChance + '% Chance'));
            }
        }
    };

    var calcTableRow = document.createElement('tr');
    var calcTableCell = document.createElement('td');
    var luckInput = document.createElement('input');
    var calculateButton = document.createElement('input');
    var percentChanceLabel = document.createElement('div');

    calcTableCell.style.textAlign = 'right';
    calcTableCell.appendChild(document.createTextNode('Luck:'));
    calcTableRow.appendChild(calcTableCell);

    luckInput.id = 'luckInput';
    luckInput.maxLength = '4';
    luckInput.type = 'text';
    luckInput.style.width = '35px';
    luckInput.onkeyup = function(keyEvent) {
        var keyID = window.event ? event.keyCode : keyEvent.keyCode;
        if (keyID == 13)
           calculate(luckInput, percentChanceLabel);
    };
    calcTableCell = document.createElement('td');
    calcTableCell.appendChild(luckInput);
    calcTableRow.appendChild(calcTableCell);

    calculateButton.type = 'button';
    calculateButton.value = 'Calculate >>';
    calculateButton.onclick = function() {
        calculate(luckInput, percentChanceLabel);
    };
    calcTableCell = document.createElement('td');
    calcTableCell.appendChild(calculateButton);
    calcTableRow.appendChild(calcTableCell);

    percentChanceLabel.appendChild(document.createTextNode('0.000% Chance'));
    percentChanceLabel.style.textAlign = 'right';
    percentChanceLabel.style.width = '110px';
    calcTableCell = document.createElement('td');
    calcTableCell.appendChild(percentChanceLabel);
    calcTableRow.appendChild(calcTableCell);

    return calcTableRow;
}

/**
 * Calculator that calculates the amount of Luck required to achieve a specified percentage chance for more and better loot.
 */
function ChanceToLuckCalculator() {
    var calculate = function(input, label) {
        if (input.value) {
            if (input.value > 100) {
                input.value = '100';
            }
            if (isNumeric(input.value)) {
                var luckAmount = Math.ceil(Math.pow(input.value, (9 / 5)));
                removeChildren(label);
                label.appendChild(document.createTextNode(luckAmount + ' Luck'));
            }
        }
    };

    var calcTableRow = document.createElement('tr');
    var calcTableCell = document.createElement('td');
    var chanceInput = document.createElement('input');
    var calculateButton = document.createElement('input');
    var luckAmountLabel = document.createElement('div');

    calcTableCell.style.textAlign = 'right';
    calcTableCell.appendChild(document.createTextNode('Chance:'));
    calcTableRow.appendChild(calcTableCell);

    chanceInput.id = 'chanceInput';
    chanceInput.maxLength = '3';
    chanceInput.type = 'text';
    chanceInput.style.width = '25px';
    chanceInput.onkeyup = function(keyEvent) {
        var keyID = window.event ? event.keyCode : keyEvent.keyCode;
        if (keyID == 13)
            calculate(chanceInput, luckAmountLabel);
    };
    calcTableCell = document.createElement('td');
    calcTableCell.appendChild(chanceInput);
    calcTableRow.appendChild(calcTableCell);

    calculateButton.type = 'button';
    calculateButton.value = 'Calculate >>';
    calculateButton.onclick = function() {
        calculate(chanceInput, luckAmountLabel);
    };
    calcTableCell = document.createElement('td');
    calcTableCell.appendChild(calculateButton);
    calcTableRow.appendChild(calcTableCell);

    luckAmountLabel.appendChild(document.createTextNode('0 Luck'));
    luckAmountLabel.style.textAlign = 'right';
    luckAmountLabel.style.width = '60px';
    calcTableCell = document.createElement('td');
    calcTableCell.appendChild(luckAmountLabel);
    calcTableRow.appendChild(calcTableCell);

    return calcTableRow;
}

function searchForCalculators() {
    var calculators = document.getElementsByTagName('div');

    for (var i in calculators) {
         if (calculators[i].id == 'LuckToChance') {
             Calculator(LuckToChanceCalculator(), 'LuckToChance', 'Luck to Chance', 4);
         } else if (calculators[i].id == 'ChanceToLuck') {
             Calculator(ChanceToLuckCalculator(), 'ChanceToLuck', 'Chance to Luck', 4);
         }
    }
}
addOnloadHook(searchForCalculators);