Difference between revisions of "MediaWiki:Calculator.js/Mana Regeneration.js"

m
m
Line 4: Line 4:
 
var ManaPerSecondCalculator = Calculator.extend({
 
var ManaPerSecondCalculator = Calculator.extend({
 
     init: function() {
 
     init: function() {
 +
        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 calcTableRow = document.createElement('tr');
 
         var calcTableCell = document.createElement('td');
 
         var calcTableCell = document.createElement('td');
         var meditationInput = document.createElement('input');
+
         var luckInput = document.createElement('input');
        var focusInput = document.createElement('input');
+
        var intInput = document.createElement('input');
+
        var mrInput = document.createElement('input');
+
 
         var calculateButton = document.createElement('input');
 
         var calculateButton = document.createElement('input');
         var manaPerSecondLabel = document.createElement('div');
+
         var percentChanceLabel = document.createElement('div');
  
 
         calcTableCell.style.textAlign = 'right';
 
         calcTableCell.style.textAlign = 'right';
         calcTableCell.appendChild(document.createTextNode('Meditation:'));
+
         calcTableCell.appendChild(document.createTextNode('Luck:'));
 
         calcTableRow.appendChild(calcTableCell);
 
         calcTableRow.appendChild(calcTableCell);
  
         meditationInput.maxLength = '5';
+
         luckInput.id = 'luckInput';
         meditationInput.type = 'text';
+
        luckInput.maxLength = '4';
         meditationInput.style.width = '45px';
+
         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 = document.createElement('td');
         calcTableCell.appendChild(meditationInput);
+
         calcTableCell.appendChild(luckInput);
 
         calcTableRow.appendChild(calcTableCell);
 
         calcTableRow.appendChild(calcTableCell);
  
 
         calculateButton.type = 'button';
 
         calculateButton.type = 'button';
 
         calculateButton.value = 'Calculate >>';
 
         calculateButton.value = 'Calculate >>';
 +
        calculateButton.onclick = function() {
 +
            calculate(luckInput, percentChanceLabel);
 +
        };
 
calcTableCell = document.createElement('td');
 
calcTableCell = document.createElement('td');
 
         calcTableCell.appendChild(calculateButton);
 
         calcTableCell.appendChild(calculateButton);
 
         calcTableRow.appendChild(calcTableCell);
 
         calcTableRow.appendChild(calcTableCell);
  
         manaPerSecondLabel.appendChild(document.createTextNode('0 Mana Per Second'));
+
         percentChanceLabel.appendChild(document.createTextNode('0.000% Chance'));
         manaPerSecondLabel.style.textAlign = 'right';
+
         percentChanceLabel.style.textAlign = 'right';
         manaPerSecondLabel.style.width = '120px';
+
         percentChanceLabel.style.width = '110px';
 
calcTableCell = document.createElement('td');
 
calcTableCell = document.createElement('td');
 
         calcTableCell.appendChild(percentChanceLabel);
 
         calcTableCell.appendChild(percentChanceLabel);

Revision as of 12:00, 4 May 2010

/**
 * Calculator that calculates your mana regenerated per second based on Meditation, Focus, Intelligence, and equipped MR.
 */
var ManaPerSecondCalculator = Calculator.extend({
    init: function() {
        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);

        this._super(calcTableRow, 'ManaPerSecond', 'Mana Per Second', 4);
    }
});

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

    for (var i in calculators) {
         if (calculators[i].id == 'ManaPerSecond') {
             new ManaPerSecondCalculator();
         }
    }
}
addOnloadHook(searchForCalculators);