Reformat and modernise code

This commit is contained in:
Florine W. Dekker 2019-06-08 03:57:10 +02:00
parent e013d67d1d
commit e7fe9a9254
Signed by: FWDekker
GPG Key ID: B1B567AF58D6EE0F
1 changed files with 227 additions and 232 deletions

View File

@ -3,15 +3,17 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="author" content="Felix W. Dekker" />
<meta name="application-name" content="Dice probabilities" />
<meta name="description" content="Calculates the probability of throwing a value given a combination of dice." />
<meta name="theme-color" content="#0033cc" />
<meta name="author" content="Felix W. Dekker"/>
<meta name="application-name" content="Dice probabilities"/>
<meta name="description" content="Calculates the probability of throwing a value given a combination of dice."/>
<meta name="theme-color" content="#0033cc"/>
<title>Dice probabilities | FWDekker</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css" integrity="sha256-l85OmPOjvil/SOvVt3HnSSjzF1TUMyT9eV0c2BzEGzU=" crossorigin="anonymous" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/milligram/1.3.0/milligram.min.css" integrity="sha256-Ro/wP8uUi8LR71kwIdilf78atpu8bTEwrK5ZotZo+Zc=" crossorigin="anonymous" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css"
integrity="sha256-l85OmPOjvil/SOvVt3HnSSjzF1TUMyT9eV0c2BzEGzU=" crossorigin="anonymous"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/milligram/1.3.0/milligram.min.css"
integrity="sha256-Ro/wP8uUi8LR71kwIdilf78atpu8bTEwrK5ZotZo+Zc=" crossorigin="anonymous"/>
<style>
body {
@ -43,7 +45,9 @@
</thead>
<tbody>
<tr>
<td><button id="addDieRowButton" class="button-outline" type="button">Add dice</button></td>
<td>
<button id="addDieRowButton" class="button-outline" type="button">Add dice</button>
</td>
</tr>
</tbody>
</table>
@ -57,7 +61,7 @@
<!-- Output -->
<h2>Probabilities</h2>
<div class="row" height="400px">
<div class="row">
<div class="column">
<canvas id="probChart"></canvas>
</div>
@ -65,9 +69,9 @@
</div>
<!-- Scripts -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.min.js" integrity="sha256-oSgtFCCmHWRPQ/JmR4OoZ3Xke1Pw4v50uh6pLcu+fIc=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.min.js"
integrity="sha256-oSgtFCCmHWRPQ/JmR4OoZ3Xke1Pw4v50uh6pLcu+fIc=" crossorigin="anonymous"></script>
<script>
//////
///
@ -75,43 +79,41 @@
///
//////
function addOnLoad(fun) {
var oldOnLoad = window.onload || (() => {});
const doAfterLoad = fun => {
const oldOnLoad = window.onload || (() => {
});
window.onload = (() => {
oldOnLoad();
fun();
});
}
};
function repeat(value, length) {
var zeroArray = [];
for (var i = 0; i < length; i++) {
const repeat = (value, length) => {
const zeroArray = [];
for (let i = 0; i < length; i++)
zeroArray.push(value);
}
return zeroArray;
}
};
function rangeExclusive(from, to) {
var rangeArray = [];
for (var i = from; i < to; i++) {
const rangeExclusive = (from, to) => {
const rangeArray = [];
for (let i = from; i < to; i++)
rangeArray.push(i);
}
return rangeArray;
}
};
function rangeInclusive(from, to) {
var rangeArray = rangeExclusive(from, to);
const rangeInclusive = (from, to) => {
const rangeArray = rangeExclusive(from, to);
rangeArray.push(to);
return rangeArray;
}
};
function iterateNodeList(nodeList, fun) {
for (var i = 0; i < nodeList.length; i++) {
var node = nodeList.item(i);
const iterateNodeList = (nodeList, fun) => {
for (let i = 0; i < nodeList.length; i++) {
const node = nodeList.item(i);
fun(node);
}
}
};
//////
@ -120,30 +122,27 @@
///
//////
var inputTable = {};
const inputTable = {};
// Functions
inputTable.getTable = function() {
return document.querySelector("#dieSettings tbody");
};
inputTable.getTable = () => document.querySelector("#dieSettings tbody");
inputTable.highestDieRowIndex = function() {
var table = inputTable.getTable();
inputTable.highestDieRowIndex = () => {
const table = inputTable.getTable();
var highestDieRowIndex = -1;
let highestDieRowIndex = -1;
iterateNodeList(table.getElementsByTagName("tr"), (node) => {
if (!isNaN(node.dataset.index)) {
if (!isNaN(node.dataset.index))
highestDieRowIndex = Math.max(highestDieRowIndex, node.dataset.index);
}
});
return highestDieRowIndex;
};
inputTable.addDieRow = function() {
function createNumberInput(index, className, value) {
var input = document.createElement("input");
inputTable.addDieRow = () => {
const createNumberInput = (index, className, value) => {
const input = document.createElement("input");
input.id = className + index;
input.className = className;
input.type = "number";
@ -151,29 +150,28 @@
input.step = 1;
input.value = value;
input.addEventListener("keypress", (e) => {
if (e.key == "Enter") {
updateProbGraph();
}
if (e.key === "Enter")
outputChart.updateProbGraph();
});
input.focus();
return input;
}
};
function createRemoveLink(index, className) {
var link = document.createElement("button");
const createRemoveLink = (index, className) => {
const link = document.createElement("button");
link.id = className + index;
link.className = className + " button-clear";
link.type = "button";
link.innerHTML = "Remove";
link.onclick = (() => inputTable.removeDieRow(index));
return link;
}
};
var table = inputTable.getTable();
var newIndex = inputTable.highestDieRowIndex() + 1;
const table = inputTable.getTable();
const newIndex = inputTable.highestDieRowIndex() + 1;
var row = table.insertRow(newIndex);
const row = table.insertRow(newIndex);
row.dataset.index = newIndex;
row.insertCell().appendChild(createNumberInput(newIndex, "dieEyes", 6));
@ -181,26 +179,25 @@
row.insertCell().appendChild(createRemoveLink(newIndex, "dieRemove"));
};
inputTable.removeDieRow = function(index) {
inputTable.removeDieRow = index => {
if (inputTable.highestDieRowIndex() > 0) {
var table = inputTable.getTable();
var row = table.querySelector("tr[data-index=\"" + index + "\"]");
const table = inputTable.getTable();
const row = table.querySelector("tr[data-index=\"" + index + "\"]");
row.parentElement.removeChild(row);
}
};
inputTable.getDice = function() {
var dice = [];
inputTable.getDice = () => {
const dice = [];
var eyesInputs = document.getElementsByClassName("dieEyes");
var countInputs = document.getElementsByClassName("dieCount");
for (var i = 0; i < eyesInputs.length; i++) {
var count = parseInt(countInputs.item(i).value);
const eyesInputs = document.getElementsByClassName("dieEyes");
const countInputs = document.getElementsByClassName("dieCount");
for (let i = 0; i < eyesInputs.length; i++) {
const count = parseInt(countInputs.item(i).value);
for (var j = 0; j < count; j++) {
for (let j = 0; j < count; j++)
dice.push(parseInt(eyesInputs.item(i).value));
}
}
return dice;
};
@ -211,8 +208,7 @@
// Init
addOnLoad(() => document.getElementById("addDieRowButton").click());
doAfterLoad(() => document.getElementById("addDieRowButton").click());
//////
@ -221,20 +217,19 @@
///
//////
var outputChart = {};
const outputChart = {};
// Functions
outputChart.calculateDiceFrequencies = function(dice) {
if (dice.length == 0) {
outputChart.calculateDiceFrequencies = dice => {
if (dice.length === 0)
return [];
}
// Roll dice
var rollFreqs = [0].concat(repeat(1, dice[0]));
let rollFreqs = [0].concat(repeat(1, dice[0]));
dice.slice(1).forEach(die => {
var dieRollFreqs = rollFreqs.concat(repeat(0, die));
const dieRollFreqs = rollFreqs.concat(repeat(0, die));
rollFreqs = repeat(0, dieRollFreqs.length);
rangeInclusive(1, die).forEach(throwValue => {
@ -247,7 +242,7 @@
// Calculate frequencies
var totalRolls = rollFreqs.reduce((a, b) => a + b, 0);
const totalRolls = rollFreqs.reduce((a, b) => a + b, 0);
rangeExclusive(0, rollFreqs.length).forEach(roll => {
rollFreqs[roll] = rollFreqs[roll] / totalRolls;
});
@ -256,9 +251,9 @@
return rollFreqs;
};
outputChart.updateProbGraph = function() {
var dice = inputTable.getDice();
var rollFreqs = outputChart.calculateDiceFrequencies(dice);
outputChart.updateProbGraph = () => {
const dice = inputTable.getDice();
const rollFreqs = outputChart.calculateDiceFrequencies(dice);
probChart.data.labels = rangeInclusive(1, rollFreqs.length);
probChart.data.datasets = [{
@ -274,8 +269,8 @@
// Init
var ctx = document.getElementById("probChart").getContext("2d");
var probChart = new Chart(ctx, {
const ctx = document.getElementById("probChart").getContext("2d");
const probChart = new Chart(ctx, {
type: "line",
data: {},
options: {
@ -293,7 +288,7 @@
}
});
addOnLoad(() => document.getElementById("submit").click());
doAfterLoad(() => document.getElementById("submit").click());
</script>
</body>
</html>