2015-04-25 23:02:05 -04:00
|
|
|
/**
|
|
|
|
* Our main Javascript file.
|
|
|
|
*/
|
|
|
|
(function() {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Roll a die.
|
|
|
|
*
|
|
|
|
* @return integer A random number between 1 and 6, inclusive.
|
|
|
|
*/
|
|
|
|
function die_roll() {
|
|
|
|
return(Math.floor(Math.random() * 6) + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2015-04-26 02:08:58 -04:00
|
|
|
* Roll a die 5 times.
|
2015-04-25 23:02:05 -04:00
|
|
|
*
|
2015-04-26 02:08:58 -04:00
|
|
|
* @return array an Array of 5 dice rolls
|
2015-04-25 23:02:05 -04:00
|
|
|
*/
|
|
|
|
function roll_dice() {
|
2015-04-26 02:08:58 -04:00
|
|
|
var retval = new Array();
|
|
|
|
retval.push(die_roll());
|
|
|
|
retval.push(die_roll());
|
|
|
|
retval.push(die_roll());
|
|
|
|
retval.push(die_roll());
|
|
|
|
retval.push(die_roll());
|
2015-04-25 23:02:05 -04:00
|
|
|
return(retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Look up a word from our wordlist.
|
|
|
|
*
|
|
|
|
* @param object wordlist Our hash table of dice rolls and their corresponding words.
|
|
|
|
* @param integer index
|
|
|
|
*
|
|
|
|
* @return string The word from the dicelist
|
|
|
|
*/
|
|
|
|
function get_word(wordlist, index) {
|
|
|
|
var retval = wordlist[index];
|
|
|
|
return(retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-26 00:01:29 -04:00
|
|
|
//
|
|
|
|
// Handler to mark the clicked number of dice button as active.
|
|
|
|
//
|
|
|
|
jQuery(".dice_button").on("click", function(e) {
|
|
|
|
jQuery(".dice_button").removeClass("active");
|
|
|
|
jQuery(e.target).addClass("active");
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2015-04-26 15:32:56 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This function displays each dice roll.
|
|
|
|
*
|
2015-04-26 16:02:56 -04:00
|
|
|
* @param array rows Array of rows of dice rolls that we had.
|
|
|
|
* @param object cb Our callback to fire when done
|
2015-04-26 15:32:56 -04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
function display_row(rows, cb) {
|
|
|
|
|
2015-04-26 16:02:56 -04:00
|
|
|
var duration = 250;
|
|
|
|
var fadein_delay = 500;
|
|
|
|
|
2015-04-26 15:32:56 -04:00
|
|
|
if (rows.length) {
|
|
|
|
//
|
|
|
|
// Display a row, then call ourselves again then done.
|
|
|
|
//
|
|
|
|
var row = rows.shift();
|
2015-04-26 16:02:56 -04:00
|
|
|
var tmp = row.hide().appendTo(".results")
|
|
|
|
.delay(fadein_delay)
|
|
|
|
.fadeIn(duration, function() {
|
|
|
|
jQuery(".results").append("<br clear=\"all\" />");
|
|
|
|
display_row(rows, cb);
|
|
|
|
})
|
|
|
|
.delay(1000).fadeOut(duration);
|
2015-04-26 15:32:56 -04:00
|
|
|
|
|
|
|
} else {
|
|
|
|
//
|
|
|
|
// All done with displaying rows, fire our callback and get outta here.
|
|
|
|
//
|
|
|
|
cb();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} // End of display_row()
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the actual results.
|
|
|
|
*/
|
|
|
|
function display_results() {
|
|
|
|
|
|
|
|
jQuery(".results_words_key").hide().clone().appendTo(".results");
|
|
|
|
jQuery(".results_words_value").hide().clone().appendTo(".results");
|
|
|
|
jQuery(".results").append("<br clear=\"all\" />");
|
|
|
|
jQuery(".results_phrase_key").hide().clone().appendTo(".results");
|
|
|
|
jQuery(".results_phrase_value").hide().clone().appendTo(".results");
|
|
|
|
|
|
|
|
jQuery(".results_words_key").fadeIn(500, function() {
|
|
|
|
jQuery(".results_words_value").fadeIn(500, function() {
|
|
|
|
jQuery(".results_phrase_key").fadeIn(500, function() {
|
|
|
|
jQuery(".results_phrase_value").fadeIn(500);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
} // End of display_results()
|
|
|
|
|
|
|
|
|
2015-04-26 00:01:29 -04:00
|
|
|
//
|
|
|
|
// Handler when the "Roll Dice" button is clicked. It gets the
|
|
|
|
// passphrase and updates the HTML with it.
|
|
|
|
//
|
|
|
|
jQuery("#roll_dice").on("click", function(e) {
|
|
|
|
|
2015-04-26 02:08:58 -04:00
|
|
|
//
|
|
|
|
// Remove any old results
|
|
|
|
//
|
|
|
|
jQuery(".results").empty();
|
2015-04-26 00:01:29 -04:00
|
|
|
|
2015-04-26 02:08:58 -04:00
|
|
|
//
|
|
|
|
// Make our dice rolls
|
|
|
|
//
|
2015-04-26 00:01:29 -04:00
|
|
|
var num_dice = jQuery(".dice_button.active").html();
|
|
|
|
var passphrase = new Array();
|
|
|
|
|
2015-04-26 02:08:58 -04:00
|
|
|
var rolls = new Array();
|
2015-04-26 00:01:29 -04:00
|
|
|
for (var i=0; i<num_dice; i++) {
|
|
|
|
var roll = roll_dice();
|
2015-04-26 02:08:58 -04:00
|
|
|
rolls.push(roll);
|
|
|
|
passphrase.push(get_word(wordlist, roll.join("")));
|
2015-04-26 00:01:29 -04:00
|
|
|
}
|
|
|
|
|
2015-04-26 02:08:58 -04:00
|
|
|
//
|
|
|
|
// Populate our results
|
|
|
|
//
|
2015-04-26 00:01:29 -04:00
|
|
|
jQuery(".results_words_value").html(passphrase.join(" "));
|
|
|
|
jQuery(".results_phrase_value").html(passphrase.join(""));
|
|
|
|
|
2015-04-26 02:08:58 -04:00
|
|
|
var rows = new Array();
|
|
|
|
for (key in rolls) {
|
|
|
|
|
|
|
|
var roll = rolls[key];
|
|
|
|
var row = jQuery("<div></div>");
|
|
|
|
|
|
|
|
for (key2 in roll) {
|
|
|
|
var die = roll[key2];
|
|
|
|
var classname = ".source .dice" + die;
|
|
|
|
jQuery(classname).clone().appendTo(row);
|
|
|
|
}
|
|
|
|
|
|
|
|
rows.push(row);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-26 15:32:56 -04:00
|
|
|
display_row(rows, display_results);
|
2015-04-26 00:01:29 -04:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Load our wordlist.
|
|
|
|
//
|
2015-04-25 23:02:05 -04:00
|
|
|
jQuery.getScript("./wordlist.js").done(
|
|
|
|
function(data) {
|
2015-04-26 15:32:56 -04:00
|
|
|
// TEST
|
|
|
|
jQuery("#roll_dice").click(); // Debugging
|
2015-04-25 23:02:05 -04:00
|
|
|
|
|
|
|
}).fail(
|
|
|
|
function(jqxhr, settings, exception) {
|
|
|
|
console.log("Error loading Javascript:", jqxhr.status, settings, exception);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
|