Skip to content

Instantly share code, notes, and snippets.

@jonaslindmark
Last active April 9, 2021 10:26
Show Gist options
  • Save jonaslindmark/764cc3c150a508e2ea86ae5105c0a5b2 to your computer and use it in GitHub Desktop.
Save jonaslindmark/764cc3c150a508e2ea86ae5105c0a5b2 to your computer and use it in GitHub Desktop.
// this macro gets all the d20 rolled in a session and spits back what is the avg roll and all rolled d20 results.
// // step one, get all the messages with a d20 roll in them.
let arrays = {}
//let rollArray = []
let rollResult = 0;
let rollMessages = game.messages.entities.filter(m => m.roll?.formula?.includes("d20") == true)
// // step two, get the rolled dice.
for (let message of rollMessages) { // takes an individual message from the rollMessages object.
let player = message.user.data.name;
for (let dice of message.roll.dice) { // allows us to check when there is multiple different die used in the formula if we are actually checking a d20.
let array = arrays[player] || []
if (dice.faces == 20) {
for (let rollDone of dice.results) { // here we seperate the die rolls of multiple dice of the same kind. eg. when advantage was rolled
rollResult = rollDone.result;
array.push(rollResult); // here we combine all the results in an array.
}
}
arrays[player] = array;
}
}
// step three, the results.
let result = {};
for (let name in arrays) {
let diceRolls = arrays[name];
let sumRolls = diceRolls.reduce(function(a, b) {
return a + b;
});
let average = sumRolls / diceRolls.length;
let rounded = (Math.round(average * 100) / 100).toFixed(2);
result[name] = {"average": rounded, "nr_of_rolls": diceRolls.length}
}
let message = "<br>";
for (let name in result) {
message = message + `${name}: rolled ${result[name].nr_of_rolls} dies, average of ${result[name].average}<br>`
}
console.log(message);
ChatMessage.create({flavor: 'Dice stats:', content: message});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment