paritybit.ca

Files for paritybit.ca.
git clone https://git.jaderune.net/jbauer/paritybit.ca
Log | Files | Refs | README | LICENSE

roll.js (4193B)


      1 /*roll.js
      2 * Copyright (C) 2019,2022  Jake Bauer
      3 * Licensed under the ISC License.
      4 */
      5 
      6 /* stringRoll()
      7  * @brief This function rolls dice based on the input from the text-field
      8  */
      9 function stringRoll() {
     10     let rollString = document.getElementById("inputString").value;
     11     let result = 0;
     12     let numRolls, die, operation, modifier, resultStr = "";
     13 
     14     // Parse values from string
     15     try {
     16         numRolls = rollString.match(/\d+d/ig)[0];
     17         numRolls = numRolls.substring(0, numRolls.length-1);
     18         die = rollString.match(/d\d+/ig)[0];
     19         die = die.substring(1);
     20     }
     21     catch(err) {
     22         console.log(err);
     23         alert("Invalid text input! See example for proper string format.");
     24         return;
     25     }
     26     try {
     27         operation = rollString.match(/[\+\-]/ig)[0];
     28         modifier = rollString.match(/\d+$/ig)[0];
     29     }
     30     catch(TypeError) {
     31         // Ignore missing operation or modifier strings
     32     }
     33 
     34     // Conduct rolls
     35     for (let i = 0; i < numRolls; i++) {
     36 	let intermediate = Math.floor((Math.random()*die)+1);
     37 	if (i == numRolls-1) {
     38 	    // If it's the last roll, don't add a + symbol to the string
     39             resultStr += intermediate;
     40 	}
     41 	else {
     42             resultStr += intermediate + " + ";
     43 	}
     44 	result += intermediate;
     45     }
     46     // Finalize the result string
     47     resultStr += " = " + result;
     48     if (numRolls == 1) {
     49         resultStr = result;
     50     }
     51     // Apply the modifier
     52     resultStr = "d"+ die + ": " + resultStr;
     53     if (operation === '-') {
     54         result = Number(result) - Number(modifier);
     55         resultStr += " - " + modifier + " = " + result;
     56     }
     57     else if (operation === '+') {
     58         result = Number(result) + Number(modifier);
     59         resultStr += " + " + modifier + " = " + result;
     60     }
     61     // Add the result to the history box
     62     let hist = document.getElementById("history-text");
     63     hist.innerHTML = resultStr + "<br />" + hist.innerHTML;
     64 }
     65 
     66 /* roll()
     67  * @brief This function rolls the dice according to which button was pressed
     68  * @input Element The button representing which dice to roll
     69  */
     70 function roll(element) {
     71     let btnId = (element.id).substring(1);
     72     let numRolls = document.getElementById(('num'+btnId)).value;
     73     let modifier = document.getElementById(('mod'+btnId)).value;
     74     let result = 0;
     75     let resultStr = "";
     76     // Conduct rolls
     77     for (let i = 0; i < numRolls; i++) {
     78 	let intermediate = Math.floor((Math.random()*btnId)+1);
     79 	if (i == numRolls-1) {
     80 	    // If it's the last roll, don't add a + symbol to the string
     81             resultStr += intermediate;
     82 	}
     83 	else {
     84             resultStr += intermediate + " + ";
     85 	}
     86 	result += intermediate;
     87     }
     88     // Finalize the result string
     89     resultStr += " = " + result;
     90     if (numRolls == 1) {
     91         resultStr = result;
     92     }
     93     // Apply the modifier
     94     result = Number(result) + Number(modifier);
     95     resultStr = "d"+ btnId + ": " + resultStr;
     96     if (modifier < 0) {
     97         resultStr += " - " + Math.abs(modifier) + " = " + result;
     98     }
     99     else if (modifier > 0) {
    100         resultStr += " + " + modifier + " = " + result;
    101     }
    102     // Show the result as a number in the die's row
    103     document.getElementById(("d"+btnId+"result")).innerHTML = result;
    104     // Add the result to the history box
    105     let hist = document.getElementById("history-text");
    106     hist.innerHTML = resultStr + "<br />" + hist.innerHTML;
    107 }
    108 
    109 /* clear_history()
    110  * @brief This function clears the history box, restting it back to default
    111  *          values while also clearing results and input boxes.
    112  */
    113 function clear_history() {
    114     let results = document.getElementsByTagName("p");
    115     for (let i = 0; i < Object.keys(results).length; i++) {
    116         element = results[i];
    117 	if (element.getAttribute("class") == "field") {
    118 	    element.innerHTML = "0";
    119 	}
    120     }
    121     let inputs = document.getElementsByTagName("input");
    122     for (let i = 0; i < Object.keys(inputs).length; i++) {
    123 	element = inputs[i];
    124 	if (element.getAttribute("type") == "number") {
    125 	    if (element.getAttribute("min")) {
    126 	        element.value = 1;
    127 	    }
    128 	    else {
    129 	        element.value = 0;
    130 	    }
    131 	}
    132     }
    133     let hist = document.getElementById("history-text");
    134     hist.innerHTML = "";
    135 }