Add range and binomial options

This commit is contained in:
Misode
2019-06-17 14:54:41 +02:00
parent dbe4049e06
commit 74c47e2f66
2 changed files with 75 additions and 15 deletions

View File

@@ -19,7 +19,7 @@ function addPool(el) {
}
function removePool(el) {
let $pool = $(el).parent().parent();
let $pool = $(el).closest('.pool');
table.pools.pop($pool.attr('data-index'));
$('#structure .pool').each((i, el) => {
if ($(el).attr('data-index') > $pool.attr('data-index')) {
@@ -30,6 +30,59 @@ function removePool(el) {
invalidated();
}
function updateRollsField(el) {
let $pool = $(el).closest('.pool')
let $rolls = $(el).closest('.rolls');
let data = parseInt($rolls.find('.exact').val());
if ($rolls.attr('data-type') === 'range') {
data = {};
let min = $rolls.find('.range.min').val();
let max = $rolls.find('.range.max').val();
if (min) data.min = parseInt(min);
if (max) data.max = parseInt(max);
} else if ($rolls.attr('data-type') === 'binomial') {
data = {type: "minecraft:binomial"};
let n = $rolls.find('.binomial.n').val();
let p = $rolls.find('.binomial.p').val();
if (n) data.n = parseInt(n);
if (p) data.p = parseFloat(p);
}
table.pools[$pool.attr('data-index')].rolls = data;
invalidated();
}
function switchExact(el) {
let $rolls = $(el).closest('.rolls');
$rolls.attr('data-type', 'exact');
$rolls.find('.exact').removeClass('d-none');
$rolls.find('.range').addClass('d-none');
$rolls.find('.binomial').addClass('d-none');
updateRollsField(el);
}
function switchRange(el) {
let $rolls = $(el).closest('.rolls');
$rolls.attr('data-type', 'range');
$rolls.find('.exact').addClass('d-none');
$rolls.find('.range').removeClass('d-none');
$rolls.find('.binomial').addClass('d-none');
updateRollsField(el);
}
function switchBinomial(el) {
let $rolls = $(el).closest('.rolls');
$rolls.attr('data-type', 'binomial');
$rolls.find('.exact').addClass('d-none');
$rolls.find('.range').addClass('d-none');
$rolls.find('.binomial').removeClass('d-none');
updateRollsField(el);
}
function invalidated() {
$('#source').val(JSON.stringify(table, null, indentation));
$('#source').autogrow();
}
function updateIndentation(el) {
if (el.value === 'tab') {
indentation = '\t';
@@ -39,18 +92,6 @@ function updateIndentation(el) {
invalidated();
}
function updateRollsField(el) {
let $pool = $(el).parent().parent().parent();
let value = parseInt($(el).val());
table.pools[$pool.attr('data-index')].rolls = value;
invalidated();
}
function invalidated() {
$('#source').val(JSON.stringify(table, null, indentation));
$('#source').autogrow();
}
function copySource(el) {
$('#source').get()[0].select();
document.execCommand('copy');