pvgttm-web/src/assets/scripts/tools.js

87 lines
3.2 KiB
JavaScript

// @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT
/****************************************************************************
* Planar Vagabond's Guide to the Multiverse (planarvagabond.com)
* Referee Tools
*
* Copyright 2023 Eric Woodward
* Source released under MIT License
* https://www.itsericwoodward.com/licenses/mit
****************************************************************************/
const // Scripts for the Die Roller
addRollerForm = () => {
const rollerForm = document.getElementById('js-rollerForm'),
rollerInput = document.getElementById('js-rollerInput'),
rollerOutput = document.getElementById('js-rollerOutput');
// double-click [x] to clear list
rollerForm.addEventListener('reset', (e) => {
if (rollerInput.value === '') rollerOutput.replaceChildren();
});
// do the roll(s) and show the (cleaned-up) result(s)
rollerForm.addEventListener('submit', (e) => {
e.preventDefault();
if (!window.dice) return;
const newEl = document.createElement('li'),
// support multiple sets of dice
rolls = rollerInput.value.split(/,\s+/);
newEl.innerText = rolls
.map((roll) => {
const result = dice.roll(roll),
stringifiedResult = dice.stringify(result);
return `${stringifiedResult.replaceAll(
'!!!mods listing not yet complete!!!',
''
)}${
// only show total if there's multiple dice thrown in a set
stringifiedResult.includes(',') ? ` = ${+result}` : ''
}`;
})
.join(', ');
rollerOutput.prepend(newEl);
});
// clear on escape key
rollerForm.addEventListener('keydown', (event) => {
if (event.key === 'Escape') rollerForm.reset();
});
},
// Scripts for the Complication Randomizers
shuffleContainer = (parent) => {
const container = document.getElementById(parent),
children = container.children,
length = children.length,
shuffled = [...children];
for (let i = 0; i < length * length; i++)
shuffled.sort(() => 0.5 - Math.random());
for (let i = 0; i < length; i++) container.appendChild(shuffled[i]);
},
addComplicationForm = () => {
const complicationForm = document.getElementById('js-complicationForm');
complicationForm.addEventListener('submit', (e) => {
e.preventDefault();
shuffleContainer('js-complicationList');
});
},
addAstralComplicationForm = () => {
const complicationForm = document.getElementById(
'js-astralComplicationForm'
);
complicationForm.addEventListener('submit', (e) => {
e.preventDefault();
shuffleContainer('js-astralComplicationList');
});
};
export default (() => {
addRollerForm();
addComplicationForm();
addAstralComplicationForm();
})();
// @license-end