HotS Damage Multiplier
by MrPolywhirl
HTML
<link rel="stylesheet" href="https://cdn.rawgit.com/davatron5000/Lettering.js/master/examples/style.css">
<h2>HotS Damage Calculator</h2>
<form name="dmg-calc">
<div class="field">
<label>Base Damage</label>
<input type="text" name="in-dmg-base" placeholder="1" size="4" />
</div>
<div class="field">
<label>Multiplier</label>
<input type="text" name="in-dmg-multiplier" placeholder="0%" size="4" /> %
</div>
<div class="field">
<label>Level</label>
<input type="number" name="in-dmg-level" placeholder="1" min="1" max="30" maxlength="2" />
<div class="tiers">
<label class="tier">1<br /><input type="radio" name="quick-lvl" value="1" /></label>
<label class="tier">4<br /><input type="radio" name="quick-lvl" value="4" /></label>
<label class="tier">7<br /><input type="radio" name="quick-lvl" value="7" /></label>
<label class="tier"><strong>10</strong><br /><input type="radio" name="quick-lvl" value="10" /></label>
<label class="tier">13<br /><input type="radio" name="quick-lvl" value="13" /></label>
<label class="tier">16<br /><input type="radio" name="quick-lvl" value="16" /></label>
<label class="tier"><strong>20</strong><br /><input type="radio" name="quick-lvl" value="20" /></label>
<label class="tier">30<br /><input type="radio" name="quick-lvl" value="30" /></label>
</div>
</div>
<br />
<div class="field">
<label>Max Damage</label>
<input type="text" name="out-dmg-max" placeholder="0" readonly="readonly" size="4" />
<span class="tip-text">Rounded (<span id="estimated"></span>)</span>
</div>
</form>
CSS
body { background: #FFF; }
form { padding: 0.5em; }
.field label {
display: inline-block;
width: 7em;
font-weight: bold;
margin-bottom: 0.25em;
}
div.tiers {
display: inline-block;
margin-top: 1em;
}
label.tier {
text-align: center;
font-weight: normal;
width: 2em;
}
.tip-text { font-style: italic; }
JavaScript
(function($) {
// http://stackoverflow.com/questions/7298364
$.fn.populateForm = function(data) {
var $form = $(this);
$.each(data, function(key, value) {
var $field = $form.find('[name=' + mixedCaseToKebabCase(key) + ']');
switch ($field.attr('type')) {
case 'text' : case 'hidden' :
$field.val(value); break;
case 'radio' : case 'checkbox' :
$field.each(function() {
if ($(this).attr('value') === value) $(this).attr('checked', value);
});
break;
default: $field.val(value);
}
});
};
$.fn.formParams = function(autoCast) {
return $(this).serializeArray().reduce((o, e) => {
o[kebabCaseToMixedCase(e.name)] = autoCast ? $.autoCastType(e.value) : e.value;
return o;
}, {});
};
$.fn.setFormField = function(field, value) {
$(this).find('[name="' + field + '"]').val(value);
return $(this);
};
$.fn.getFormField = function(field, autoCast) {
return (function(value) {
return autoCast ? $.autoCastType(value) : value;
})($(this).find('[name="in-dmg-base"]').val());
};
$.autoCastType = function(input) {
if ($.isNumeric(input)) {
return input.indexOf('.') ? parseFloat(input) : parseInt(input, 10);
}
if ($.isBoolean(input)) {
return input === 'true';
}
return input;
};
$.isBoolean = function() {
return typeof(variable) === 'boolean';
}
}(jQuery));
$(function() {
$('input[name^="in-"').on('change keyup', function(e) {
var $form = $('form[name="dmg-calc"]');
var params = $form.formParams(true);
var dmg = params.inDmgBase;
var mul = params.inDmgMultiplier / 100 + 1;
var lvl = params.inDmgLevel;
var out = calcDmg(dmg, mul, lvl);
$form.setFormField('out-dmg-max', out.toFixed(2));
$('#estimated').text(Math.round(out));
});
$('input[name="quick-lvl"]').on('change', function(e) {
$('input[name="in-dmg-level"]').val(parseInt($(this).val(),...