Modified diceRoll() to return the raw value as well as the dice roll,

updated unit tests, updated code that displays the dice.
This commit is contained in:
Douglas Muth 2017-02-01 21:50:33 -05:00
parent d6f2e9b324
commit d511f8156c
2 changed files with 19 additions and 9 deletions

18
main.js
View file

@ -150,10 +150,12 @@ Diceware.getNumValuesFromNumDice = function(num_dice) {
* Get our maximum number for a random value, turn it into base-6, * Get our maximum number for a random value, turn it into base-6,
* then turn it into a dice roll! * then turn it into a dice roll!
* *
* @return object An object that contains a dice roll and the raw random value.
*
*/ */
Diceware.rollDice = function(num_dice) { Diceware.rollDice = function(num_dice) {
var retval = []; var retval = {};
var max = Diceware.getNumValuesFromNumDice(num_dice); var max = Diceware.getNumValuesFromNumDice(num_dice);
@ -163,7 +165,8 @@ Diceware.rollDice = function(num_dice) {
var dice = Diceware.convertBase6ToDice(base6, num_dice); var dice = Diceware.convertBase6ToDice(base6, num_dice);
retval = dice; retval.value = random;
retval.roll = dice;
return(retval); return(retval);
@ -428,8 +431,11 @@ Diceware.go = function() {
for (var i=0; i<num_dice; i++) { for (var i=0; i<num_dice; i++) {
var roll = {}; var roll = {};
roll.dice = Diceware.roll_dice(); //
roll.word = Diceware.get_word(wordlist, roll.dice.join("")); // Roll 5 dice for 7,776 words.
//
roll.dice = Diceware.rollDice(5);
roll.word = Diceware.get_word(wordlist, roll.dice.value);
rolls.push(roll); rolls.push(roll);
passphrase.push(roll.word); passphrase.push(roll.word);
@ -452,8 +458,8 @@ Diceware.go = function() {
// //
// Clone and append specific dice to this row. // Clone and append specific dice to this row.
// //
for (key2 in roll.dice) { for (key2 in roll.dice.roll) {
var die = roll.dice[key2]; var die = roll.dice.roll[key2];
var classname = ".source .dice" + die; var classname = ".source .dice" + die;
var tmp = jQuery(classname).clone().appendTo(row); var tmp = jQuery(classname).clone().appendTo(row);
} }

View file

@ -79,9 +79,13 @@ QUnit.test("Roll some dice!", function(assert) {
assert.throws(function() {Diceware.getNumValuesFromNumDice(0); }, /zero/, "Zero"); assert.throws(function() {Diceware.getNumValuesFromNumDice(0); }, /zero/, "Zero");
assert.throws(function() {Diceware.getNumValuesFromNumDice(-1); }, /negative/, "Negative value"); assert.throws(function() {Diceware.getNumValuesFromNumDice(-1); }, /negative/, "Negative value");
assert.equal(Diceware.rollDice(1).length, 1); assert.equal(Diceware.rollDice(1).roll.length, 1);
assert.equal(Diceware.rollDice(3).length, 3); assert.equal(Diceware.rollDice(3).roll.length, 3);
assert.equal(Diceware.rollDice(8).length, 8); assert.equal(Diceware.rollDice(8).roll.length, 8);
assert.ok(parseInt(Diceware.rollDice(1).value));
assert.ok(parseInt(Diceware.rollDice(3).value));
assert.ok(parseInt(Diceware.rollDice(8).value));
assert.throws(function() {Diceware.rollDice(0); }, /zero/, "Zero"); assert.throws(function() {Diceware.rollDice(0); }, /zero/, "Zero");
assert.throws(function() {Diceware.rollDice(-1); }, /negative/, "Negative value"); assert.throws(function() {Diceware.rollDice(-1); }, /negative/, "Negative value");