UNPKG

@dice-roller/rpg-dice-roller

Version:

An advanced JS based dice roller that can roll various types of dice and modifiers, along with mathematical equations.

10 lines (9 loc) 53.1 kB
/** * @dice-roller/rpg-dice-roller - An advanced JS based dice roller that can roll various types of dice and modifiers, along with mathematical equations. * * @version 5.5.1 * @license MIT * @author GreenImp Media <info@greenimp.co.uk> (https://greenimp.co.uk) * @link https://dice-roller.github.io/documentation */ import{evaluate as t}from"mathjs";class CompareOperatorError extends TypeError{constructor(t){super(`Operator "${t}" is invalid`),TypeError.captureStackTrace&&TypeError.captureStackTrace(this,CompareOperatorError),this.name="CompareOperatorError",this.operator=t}}class DataFormatError extends Error{constructor(t){super(`Invalid data format: ${t}`),Error.captureStackTrace&&Error.captureStackTrace(this,DataFormatError),this.name="ImportError",this.data=t}}class DieActionValueError extends Error{constructor(t,e=null){super(`Die "${t}" must have more than 1 possible value to ${e||"do this action"}`),Error.captureStackTrace&&Error.captureStackTrace(this,DieActionValueError),this.name="DieActionValueError",this.action=e,this.die=t}}class NotationError extends Error{constructor(t){super(`Notation "${t}" is invalid`),Error.captureStackTrace&&Error.captureStackTrace(this,NotationError),this.name="NotationError",this.notation=t}}class RequiredArgumentError extends Error{constructor(t=null){super("Missing argument"+(t?` "${t}"`:"")),Error.captureStackTrace&&Error.captureStackTrace(this,RequiredArgumentError),this.argumentName=t}}var e=Object.freeze({__proto__:null,CompareOperatorError:CompareOperatorError,DataFormatError:DataFormatError,DieActionValueError:DieActionValueError,NotationError:NotationError,RequiredArgumentError:RequiredArgumentError});const r=e=>t(e),n=t=>("number"==typeof t||"string"==typeof t)&&(!Number.isNaN(t)&&Number.isFinite(Number(t))),i=t=>{if(!n(t))return!1;const e=Number(t);return e<=Number.MAX_SAFE_INTEGER&&e>=Number.MIN_SAFE_INTEGER},o=9007199254740992,s=9007199254740991,a=-1>>>0,u=4294967296,l=2147483648,c=2147483647,h=1<<21,f=2097151;function p(t){return 0|t.next()}function d(t,e){return 0===e?t:r=>t(r)+e}function m(t){const e=0|t.next(),r=t.next()>>>0;return(e&f)*u+r+(e&h?-9007199254740992:0)}function g(t){for(;;){const e=0|t.next();if(!(4194304&e)){const r=t.next()>>>0;return(e&f)*u+r+(e&h?-9007199254740992:0)}if(4194304==(8388607&e)&&!(0|t.next()))return o}}function y(t){return t.next()>>>0}function w(t){const e=t.next()&f,r=t.next()>>>0;return e*u+r}function b(t){for(;;){const e=0|t.next();if(!(e&h)){const r=t.next()>>>0;return(e&f)*u+r}if(!(e&f||0|t.next()))return o}}function x(t){return!(t+1&t)}function v(t){return x(t)?(e=t,t=>t.next()&e):function(t){const e=t+1,r=e*Math.floor(u/e);return t=>{let n=0;do{n=t.next()>>>0}while(n>=r);return n%e}}(t);var e}function E(t){const e=t+1;if(!(0|e)){const t=(e/u|0)-1;if(x(t))return r=t,t=>{const e=t.next()&r,n=t.next()>>>0;return e*u+n}}var r;return function(t){const e=t*Math.floor(o/t);return r=>{let n=0;do{const t=r.next()&f,e=r.next()>>>0;n=t*u+e}while(n>=e);return n%t}}(e)}function S(t,e){return r=>{let n=0;do{const t=0|r.next(),e=r.next()>>>0;n=(t&f)*u+e+(t&h?-9007199254740992:0)}while(n<t||n>e);return n}}function A(t,e){if(t=Math.floor(t),e=Math.floor(e),t<-9007199254740992||!isFinite(t))throw new RangeError("Expected min to be at least -9007199254740992");if(e>o||!isFinite(e))throw new RangeError("Expected max to be at most 9007199254740992");const r=e-t;return r<=0||!isFinite(r)?()=>t:r===a?0===t?y:d(p,t+l):r<a?d(v(r),t):r===s?d(w,t):r<s?d(E(r),t):e-1-t===s?d(b,t):-9007199254740992===t&&e===o?g:-9007199254740992===t&&e===s?m:-9007199254740991===t&&e===o?d(m,1):e===o?d(S(t-1,e-1),1):S(t,e)}function R(t){return!(1&~t.next())}function M(t,e){return r=>t(r)<e}function C(t,e){return null==e?null==t?R:function(t){if(t<=0)return()=>!1;if(t>=1)return()=>!0;{const e=t*u;return e%1==0?M(p,e-l|0):M(w,Math.round(t*o))}}(t):t<=0?()=>!1:t>=e?()=>!0:M(A(0,e-1),t)}function T(t){return A(1,t)}function N(t="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-"){const e=t.length;if(!e)throw new Error("Expected pool not to be an empty string");const r=A(0,e-1);return(e,n)=>{let i="";for(let o=0;o<n;++o){const n=r(e);i+=t.charAt(n)}return i}}const O="0123456789abcdef",$=N(O),D=N(O.toUpperCase());function F(t,e){return t<0?Math.max(t+e,0):Math.min(t,e)}function P(t){const e=+t;return e<0?Math.ceil(e):Math.floor(e)}function I(t){return w(t)/o}function j(t){return b(t)/o}const J=Array.prototype.slice;function q(t,e,r=0){const n=e.length;if(n)for(let i=n-1>>>0;i>r;--i){const r=A(0,i)(t);if(i!==r){const t=e[i];e[i]=e[r],e[r]=t}}return e}const G=(()=>{try{if("xxx"==="x".repeat(3))return(t,e)=>t.repeat(e)}catch(t){}return(t,e)=>{let r="";for(;e>0;)1&e&&(r+=t),e>>=1,t+=t;return r}})();function V(t,e){return G("0",e-t.length)+t}const k={next:()=>Math.random()*u|0};class Random{constructor(t=k){this.engine=t}int32(){return p(this.engine)}uint32(){return y(this.engine)}uint53(){return w(this.engine)}uint53Full(){return b(this.engine)}int53(){return m(this.engine)}int53Full(){return g(this.engine)}integer(t,e){return A(t,e)(this.engine)}realZeroToOneInclusive(){return j(this.engine)}realZeroToOneExclusive(){return I(this.engine)}real(t,e,r=!1){return function(t,e,r=!1){if(!isFinite(t))throw new RangeError("Expected min to be a finite number");if(!isFinite(e))throw new RangeError("Expected max to be a finite number");return d((n=r?j:I,1==(i=e-t)?n:0===i?()=>0:t=>n(t)*i),t);var n,i}(t,e,r)(this.engine)}bool(t,e){return C(t,e)(this.engine)}pick(t,e,r){return function(t,e,r,n){const i=e.length;if(0===i)throw new RangeError("Cannot pick from an empty array");const o=null==r?0:F(P(r),i),s=void 0===n?i:F(P(n),i);if(o>=s)throw new RangeError(`Cannot pick between bounds ${o} and ${s}`);return e[A(o,s-1)(t)]}(this.engine,t,e,r)}shuffle(t){return q(this.engine,t)}sample(t,e){return function(t,e,r){if(r<0||r>e.length||!isFinite(r))throw new RangeError("Expected sampleSize to be within 0 and the length of the population");if(0===r)return[];const n=J.call(e),i=n.length;if(i===r)return q(t,n,0);const o=i-r;return q(t,n,o-1).slice(o)}(this.engine,t,e)}die(t){return T(t)(this.engine)}dice(t,e){return function(t,e){const r=T(t);return t=>{const n=[];for(let i=0;i<e;++i)n.push(r(t));return n}}(t,e)(this.engine)}uuid4(){return function(t){const e=t.next()>>>0,r=0|t.next(),n=0|t.next(),i=t.next()>>>0;return V(e.toString(16),8)+"-"+V((65535&r).toString(16),4)+"-"+V((r>>4&4095|16384).toString(16),4)+"-"+V((16383&n|32768).toString(16),4)+"-"+V((n>>4&65535).toString(16),4)+V(i.toString(16),8)}(this.engine)}string(t,e){return N(e)(this.engine,t)}hex(t,e){return function(t){return t?D:$}(e)(this.engine,t)}date(t,e){return function(t,e){const r=A(+t,+e);return t=>new Date(r(t))}(t,e)(this.engine)}}const _=(()=>{try{const t=new ArrayBuffer(4),e=new Int32Array(t);if(e[0]=l,-2147483648===e[0])return Int32Array}catch(t){}return Array})();let B=null;let z=128;const L={next:()=>(z>=128&&(null===B&&(B=new _(128)),crypto.getRandomValues(B),z=0),0|B[z++])};const U=(()=>{try{if(-5===Math.imul(a,5))return Math.imul}catch(t){}const t=65535;return(e,r)=>{const n=e&t,i=r&t;return n*i+((e>>>16&t)*i+n*(r>>>16&t)<<16>>>0)|0}})(),K=624,W=623,H=2567483615;class MersenneTwister19937{constructor(){this.data=new _(K),this.index=0,this.uses=0}static seed(t){return(new MersenneTwister19937).seed(t)}static seedWithArray(t){return(new MersenneTwister19937).seedWithArray(t)}static autoSeed(){return MersenneTwister19937.seedWithArray(function(t=k,e=16){const r=[];r.push(0|(new Date).getTime());for(let n=1;n<e;++n)r[n]=0|t.next();return r}())}next(){(0|this.index)>=K&&(Z(this.data),this.index=0);const t=this.data[this.index];return this.index=this.index+1|0,this.uses+=1,0|function(t){return t^=t>>>11,t^=t<<7&2636928640,t^=t<<15&4022730752,t^t>>>18}(t)}getUseCount(){return this.uses}discard(t){if(t<=0)return this;for(this.uses+=t,(0|this.index)>=K&&(Z(this.data),this.index=0);t+this.index>K;)t-=K-this.index,Z(this.data),this.index=0;return this.index=this.index+t|0,this}seed(t){let e=0;this.data[0]=e=0|t;for(let t=1;t<K;t=t+1|0)this.data[t]=e=U(e^e>>>30,1812433253)+t|0;return this.index=K,this.uses=0,this}seedWithArray(t){return this.seed(19650218),function(t,e){let r=1,n=0;const i=e.length;let o=0|Math.max(i,K),s=0|t[0];for(;(0|o)>0;--o)t[r]=s=(t[r]^U(s^s>>>30,1664525))+(0|e[n])+(0|n)|0,r=r+1|0,++n,(0|r)>W&&(t[0]=t[623],r=1),n>=i&&(n=0);for(o=W;(0|o)>0;--o)t[r]=s=(t[r]^U(s^s>>>30,1566083941))-r|0,r=r+1|0,(0|r)>W&&(t[0]=t[623],r=1);t[0]=l}(this.data,t),this}}function Z(t){let e=0,r=0;for(;(0|e)<227;e=e+1|0)r=t[e]&l|t[e+1|0]&c,t[e]=t[e+397|0]^r>>>1^(1&r?H:0);for(;(0|e)<W;e=e+1|0)r=t[e]&l|t[e+1|0]&c,t[e]=t[e-227|0]^r>>>1^(1&r?H:0);r=t[623]&l|t[0]&c,t[623]=t[396]^r>>>1^(1&r?H:0)}let X=null;let Q=128;const Y={next:()=>(Q>=128&&(X=new Int32Array(new Int8Array(require("crypto").randomBytes(512)).buffer),Q=0),0|X[Q++])},tt=Symbol("engine"),et=Symbol("random"),rt={browserCrypto:L,nodeCrypto:Y,MersenneTwister19937:MersenneTwister19937,nativeMath:k,min:{next:()=>0},max:{range:[],next(){return this.range[1]-this.range[0]}}};const nt=new class NumberGenerator{constructor(t=k){this.engine=t||k}get engine(){return this[tt]}set engine(t){if(t&&"function"!=typeof t.next)throw new TypeError("engine must have function `next()`");this[tt]=t||k,this[et]=new Random(this[tt])}integer(t,e){return this[tt].range=[t,e],this[et].integer(t,e)}real(t,e,r=!1){return this[tt].range=[t,e],this[et].real(t,e,r)}};var it=Object.freeze({__proto__:null,engines:rt,generator:nt});const ot=Symbol("text"),st=Symbol("type");class Description{static types={MULTILINE:"multiline",INLINE:"inline"};constructor(t,e=this.constructor.types.INLINE){this.text=t,this.type=e}get text(){return this[ot]}set text(t){if("object"==typeof t)throw new TypeError("Description text is invalid");if(!t&&0!==t||""===`${t}`.trim())throw new TypeError("Description text cannot be empty");this[ot]=`${t}`.trim()}get type(){return this[st]}set type(t){const e=Object.values(this.constructor.types);if("string"!=typeof t)throw new TypeError("Description type must be a string");if(!e.includes(t))throw new RangeError(`Description type must be one of; ${e.join(", ")}`);this[st]=t}toJSON(){const{text:t,type:e}=this;return{text:t,type:e}}toString(){return this.type===this.constructor.types.INLINE?`# ${this.text}`:`[${this.text}]`}}const at=Symbol("description");class HasDescription{constructor(t=null){this.description=t}get description(){return this[at]||null}set description(t){if(t||0===t)if(t instanceof Description)this[at]=t;else{if("string"!=typeof t)throw new TypeError("description must be of type Description, string or null. Received "+typeof t);this[at]=new Description(t)}else this[at]=null}toJSON(){const{description:t}=this;return{description:t}}toString(){return this.description?`${this.description}`:""}}class Modifier{static order=999;constructor(){this.order=this.constructor.order}get name(){return"modifier"}get notation(){return""}get maxIterations(){return 1e3}defaults(t){return{}}useDefaultsIfNeeded(t){Object.entries(this.defaults(t)).forEach((([t,e])=>{void 0===this[t]&&(this[t]=e)}))}run(t,e){return this.useDefaultsIfNeeded(e),t}toJSON(){const{notation:t,name:e}=this;return{name:e,notation:t,type:"modifier"}}toString(){return this.notation}}const ut={compound:"!",explode:"!","critical-failure":"__","critical-success":"**",drop:"d",max:"v",min:"^",penetrate:"p","re-roll":"r","re-roll-once":"ro","target-failure":"_","target-success":"*",unique:"u","unique-once":"uo"},lt=(...t)=>[...t].reduce(((t,e)=>{let r;return r=e instanceof Modifier?e.name:e,t+(ut[r]||r)}),""),ct=Symbol("calculation-value"),ht=Symbol("modifiers"),ft=Symbol("initial-value"),pt=Symbol("use-in-total"),dt=Symbol("value");class RollResult{constructor(t,e=[],r=!0){if(n(t))this[ft]=Number(t),this.modifiers=e||[],this.useInTotal=r;else{if(!t||"object"!=typeof t||Array.isArray(t))throw t===1/0?new RangeError("Result value must be a finite number"):new TypeError(`Result value is invalid: ${t}`);{const i=n(t.initialValue)?t.initialValue:t.value;if(!n(i))throw new TypeError(`Result value is invalid: ${i}`);this[ft]=Number(i),n(t.value)&&Number(t.value)!==this[ft]&&(this.value=t.value),n(t.calculationValue)&&parseFloat(`${t.calculationValue}`)!==this.value&&(this.calculationValue=t.calculationValue),this.modifiers=t.modifiers||e||[],this.useInTotal="boolean"==typeof t.useInTotal?t.useInTotal:r||!1}}}get calculationValue(){return n(this[ct])?parseFloat(this[ct]):this.value}set calculationValue(t){const e=n(t);if(t===1/0)throw new RangeError("Result calculation value must be a finite number");if(t&&!e)throw new TypeError(`Result calculation value is invalid: ${t}`);this[ct]=e?parseFloat(`${t}`):null}get initialValue(){return this[ft]}get modifierFlags(){return lt(...this.modifiers)}get modifiers(){return this[ht]}set modifiers(t){if((Array.isArray(t)||t instanceof Set)&&[...t].every((t=>"string"==typeof t)))this[ht]=new Set([...t]);else{if(t||0===t)throw new TypeError(`modifiers must be a Set or array of modifier names: ${t}`);this[ht]=new Set}}get useInTotal(){return!!this[pt]}set useInTotal(t){this[pt]=!!t}get value(){return n(this[dt])?this[dt]:this[ft]}set value(t){if(t===1/0)throw new RangeError("Result value must be a finite number");if(!n(t))throw new TypeError(`Result value is invalid: ${t}`);this[dt]=Number(t)}toJSON(){const{calculationValue:t,initialValue:e,modifierFlags:r,modifiers:n,useInTotal:i,value:o}=this;return{calculationValue:t,initialValue:e,modifierFlags:r,modifiers:[...n],type:"result",useInTotal:i,value:o}}toString(){return this.value+this.modifierFlags}}const mt=Symbol("rolls");class RollResults{constructor(t=[]){this.rolls=t}get length(){return this.rolls.length||0}get rolls(){return[...this[mt]]}set rolls(t){if(!t||!Array.isArray(t))throw new TypeError(`rolls must be an array: ${t}`);this[mt]=[],t.forEach((t=>{this.addRoll(t)}))}get value(){return this.rolls.reduce(((t,e)=>t+(e.useInTotal?e.calculationValue:0)),0)}addRoll(t){const e=t instanceof RollResult?t:new RollResult(t);this[mt].push(e)}toJSON(){const{rolls:t,value:e}=this;return{rolls:t,type:"roll-results",value:e}}toString(){return`[${this.rolls.join(", ")}]`}}const gt=Symbol("modifiers"),yt=Symbol("qty"),wt=Symbol("sides"),bt=Symbol("min-value"),xt=Symbol("max-value");class StandardDice extends HasDescription{constructor(t,e=1,r=null,o=1,s=null,a=null){if(super(a),!t&&0!==t)throw new RequiredArgumentError("sides");if(t===1/0)throw new RangeError("numerical sides must be finite number");if(n(t)){if(t<1||!i(t))throw new RangeError("numerical sides must be a positive finite number")}else if("string"!=typeof t)throw new TypeError("non-numerical sides must be a string");if(!n(e))throw new TypeError("qty must be a positive finite integer");if(e<1||e>999)throw new RangeError("qty must be between 1 and 999");let u=o;if(null==u)u=1;else{if(!n(u))throw new TypeError("min must a finite number");if(!i(u))throw new RangeError("min must a finite number")}if(s&&!n(s))throw new TypeError("max must a finite number");if(s&&!i(s))throw new RangeError("max must a finite number");this[yt]=parseInt(`${e}`,10),this[wt]=t,r&&(this.modifiers=r),this[bt]=parseInt(u,10),this[xt]=s?parseInt(`${s}`,10):t}get average(){return(this.min+this.max)/2}get modifiers(){return this[gt]?new Map([...this[gt]].sort(((t,e)=>t[1].order-e[1].order))):null}set modifiers(t){let e;if(t instanceof Map)e=t;else if(Array.isArray(t))e=new Map(t.map((t=>[t.name,t])));else{if("object"!=typeof t)throw new TypeError("modifiers should be a Map, array, or an Object containing Modifiers");e=new Map(Object.entries(t))}if(e.size&&[...e.entries()].some((t=>!(t[1]instanceof Modifier))))throw new TypeError("modifiers must only contain Modifier instances");this[gt]=e}get max(){return this[xt]}get min(){return this[bt]}get name(){return"standard"}get notation(){let t=`${this.qty}d${this.sides}`;return this.modifiers&&this.modifiers.size&&(t+=[...this.modifiers.values()].reduce(((t,e)=>t+e.notation),"")),t}get qty(){return this[yt]}get sides(){return this[wt]}roll(){const t=new RollResults;for(let e=0;e<this.qty;e++)t.addRoll(this.rollOnce());return(this.modifiers||[]).forEach((e=>{e.run(t,this)})),t}rollOnce(){return new RollResult(nt.integer(this.min,this.max))}toJSON(){const{average:t,max:e,min:r,modifiers:n,name:i,notation:o,qty:s,sides:a}=this;return Object.assign(super.toJSON(),{average:t,max:e,min:r,modifiers:n,name:i,notation:o,qty:s,sides:a,type:"die"})}toString(){return`${this.notation}${this.description?` ${this.description}`:""}`}}class FudgeDice extends StandardDice{constructor(t=2,e=1,r=null,n=null){let i=t;if(i||0===i){if(1!==i&&2!==i)throw new RangeError("nonBlanks must be 1 or 2")}else i=2;super(i,e,r,-1,1,n)}get name(){return"fudge"}get nonBlanks(){return super.sides}get sides(){return`F.${this.nonBlanks}`}rollOnce(){let t=0;if(2===this.nonBlanks)t=nt.integer(1,3)-2;else if(1===this.nonBlanks){const e=nt.integer(1,6);1===e?t=-1:6===e&&(t=1)}return new RollResult(t)}}class PercentileDice extends StandardDice{constructor(t=1,e=null,r=!1,n=null){super(100,t,e,null,null,n),this.sidesAsNumber=!!r}get name(){return"percentile"}get sides(){return this.sidesAsNumber?super.sides:"%"}}var vt=Object.freeze({__proto__:null,FudgeDice:FudgeDice,PercentileDice:PercentileDice,StandardDice:StandardDice});const Et=Symbol("operator"),St=Symbol("value");class ComparePoint{constructor(t,e){if(!t)throw new RequiredArgumentError("operator");if(!e&&0!==e)throw new RequiredArgumentError("value");this.operator=t,this.value=e}static isValidOperator(t){return"string"==typeof t&&/^(?:[<>!]?=|[<>]|<>)$/.test(t)}set operator(t){if(!this.constructor.isValidOperator(t))throw new CompareOperatorError(t);this[Et]=t}get operator(){return this[Et]}set value(t){if(!n(t))throw new TypeError("value must be a finite number");this[St]=Number(t)}get value(){return this[St]}isMatch(t){return((t,e,r)=>{const n=Number(t),i=Number(e);let o;if(Number.isNaN(n)||Number.isNaN(i))return!1;switch(r){case"=":case"==":o=n===i;break;case"<":o=n<i;break;case">":o=n>i;break;case"<=":o=n<=i;break;case">=":o=n>=i;break;case"!":case"!=":case"<>":o=n!==i;break;default:o=!1}return o})(t,this.value,this.operator)}toJSON(){const{operator:t,value:e}=this;return{operator:t,type:"compare-point",value:e}}toString(){return`${this.operator}${this.value}`}}const At=Symbol("compare-point");class ComparisonModifier extends Modifier{constructor(t){super(),t&&(this.comparePoint=t)}get comparePoint(){return this[At]}set comparePoint(t){if(!(t instanceof ComparePoint))throw new TypeError("comparePoint must be instance of ComparePoint");this[At]=t}get name(){return"comparison"}get notation(){return`${this.comparePoint||""}`}defaultComparePoint(t){return{}}defaults(t){const e=this.defaultComparePoint(t);return"object"==typeof e&&2===e.length?{comparePoint:new ComparePoint(...e)}:{}}isComparePoint(t){return!!this.comparePoint&&this.comparePoint.isMatch(t)}toJSON(){const{comparePoint:t}=this;return Object.assign(super.toJSON(),{comparePoint:t})}}class CriticalFailureModifier extends ComparisonModifier{static order=10;constructor(t){super(t)}get name(){return"critical-failure"}get notation(){return`cf${super.notation}`}defaultComparePoint(t){return["=",t.min]}run(t,e){return super.run(t,e),t.rolls.forEach((t=>(this.isComparePoint(t.value)&&t.modifiers.add("critical-failure"),t))),t}}class CriticalSuccessModifier extends ComparisonModifier{static order=9;constructor(t){super(t)}get name(){return"critical-success"}get notation(){return`cs${super.notation}`}defaultComparePoint(t){return["=",t.max]}run(t,e){return super.run(t,e),t.rolls.forEach((t=>(this.isComparePoint(t.value)&&t.modifiers.add("critical-success"),t))),t}}const Rt=Symbol("calculation-value"),Mt=Symbol("is-roll-group"),Ct=Symbol("modifiers"),Tt=Symbol("results"),Nt=Symbol("use-in-total");class ResultGroup{constructor(t=[],e=[],r=!1,n=!0){this.isRollGroup=r,this.modifiers=e,this.results=t,this.useInTotal=n}get calculationValue(){return n(this[Rt])?parseFloat(this[Rt]):this.value}set calculationValue(t){const e=n(t);if(t===1/0)throw new RangeError("Results calculation value must be a finite number");if(t&&!e)throw new TypeError(`Results calculation value is invalid: ${t}`);this[Rt]=e?parseFloat(`${t}`):null}get isRollGroup(){return this[Mt]}set isRollGroup(t){this[Mt]=!!t}get length(){return this.results.length||0}get modifierFlags(){return lt(...this.modifiers)}get modifiers(){return this[Ct]}set modifiers(t){if((Array.isArray(t)||t instanceof Set)&&[...t].every((t=>"string"==typeof t)))this[Ct]=new Set([...t]);else{if(t||0===t)throw new TypeError(`modifiers must be a Set or array of modifier names: ${t}`);this[Ct]=new Set}}get results(){return[...this[Tt]]}set results(t){if(!t||!Array.isArray(t))throw new TypeError(`results must be an array: ${t}`);this[Tt]=[],t.forEach((t=>{this.addResult(t)}))}get useInTotal(){return!!this[Nt]}set useInTotal(t){this[Nt]=!!t}get value(){if(!this.results.length)return 0;const t=this.results.reduce(((t,e)=>{let r=e;return e instanceof ResultGroup?r=e.useInTotal?e.calculationValue:0:e instanceof RollResults&&(r=e.value),t+r}),"string"==typeof this.results[0]?"":0);return"string"==typeof t?r(t):t}addResult(t){let e;if(t instanceof ResultGroup||t instanceof RollResults)e=t;else{if("string"!=typeof t&&!n(t))throw new TypeError("value must be one of ResultGroup, RollResults, string, or number");e=t}this[Tt].push(e)}toJSON(){const{calculationValue:t,isRollGroup:e,modifierFlags:r,modifiers:n,results:i,useInTotal:o,value:s}=this;return{calculationValue:t,isRollGroup:e,modifierFlags:r,modifiers:[...n],results:i,type:"result-group",useInTotal:o,value:s}}toString(){let t;return t=this.isRollGroup?`{${this.results.join(", ")}}`:this.results.join(""),this.modifierFlags&&(t=`(${t})${this.modifierFlags}`),t}}const Ot=Symbol("end"),$t=Symbol("qty");class KeepModifier extends Modifier{static order=6;constructor(t="h",e=1){super(),this.end=t,this.qty=e}get end(){return this[Ot]}set end(t){if("h"!==t&&"l"!==t)throw new RangeError('End must be "h" or "l"');this[Ot]=t}get name(){return`keep-${this.end}`}get notation(){return`k${this.end}${this.qty}`}get qty(){return this[$t]}set qty(t){if(t===1/0)throw new RangeError("qty must be a finite number");if(!n(t)||t<1)throw new TypeError("qty must be a positive finite integer");this[$t]=Math.floor(t)}rangeToDrop(t){return"h"===this.end?[0,t.length-this.qty]:[this.qty,t.length]}run(t,e){let r,n;return t instanceof ResultGroup?(r=t.results,n=1===r.length&&r[0]instanceof ResultGroup?r[0].results.map(((t,e)=>t instanceof RollResults?t.rolls.map(((t,r)=>({value:t.value,index:[e,r]}))):null)).flat().filter(Boolean):[...r].map(((t,e)=>({value:t.value,index:e})))):(r=t.rolls,n=[...r].map(((t,e)=>({value:t.value,index:e})))),n=n.sort(((t,e)=>t.value-e.value)).map((t=>t.index)).slice(...this.rangeToDrop(n)),n.forEach((t=>{let e;e=Array.isArray(t)?r[0].results[t[0]].rolls[t[1]]:r[t],e.modifiers.add("drop"),e.useInTotal=!1})),t}toJSON(){const{end:t,qty:e}=this;return Object.assign(super.toJSON(),{end:t,qty:e})}}class DropModifier extends KeepModifier{static order=7;constructor(t="l",e=1){super(t,e)}get name(){return`drop-${this.end}`}get notation(){return`d${this.end}${this.qty}`}rangeToDrop(t){return"h"===this.end?[t.length-this.qty,t.length]:[0,this.qty]}}const Dt=Symbol("compound"),Ft=Symbol("penetrate");class ExplodeModifier extends ComparisonModifier{static order=3;constructor(t=null,e=!1,r=!1){super(t),this[Dt]=!!e,this[Ft]=!!r}get compound(){return this[Dt]}get name(){return"explode"}get notation(){return`!${this.compound?"!":""}${this.penetrate?"p":""}${super.notation}`}get penetrate(){return this[Ft]}defaultComparePoint(t){return["=",t.max]}run(t,e){if(super.run(t,e),e.min===e.max)throw new DieActionValueError(e,"explode");const r=t;return r.rolls=t.rolls.map((t=>{const r=[t];let i=t.value;for(let t=0;t<this.maxIterations&&this.isComparePoint(i);t++){const t=r[r.length-1],n=e.rollOnce();i=n.value,t.modifiers.add("explode"),this.penetrate&&(t.modifiers.add("penetrate"),n.value-=1),r.push(n)}return this.compound&&r.length>1?(t.value=(o=r.map((t=>t.value)),Array.isArray(o)?o.reduce(((t,e)=>t+(n(e)?parseFloat(`${e}`):0)),0):0),t.modifiers=["explode","compound"],this.penetrate&&t.modifiers.add("penetrate"),t):r;var o})).flat(),r}toJSON(){const{compound:t,penetrate:e}=this;return Object.assign(super.toJSON(),{compound:t,penetrate:e})}}const Pt=Symbol("max");class MaxModifier extends Modifier{static order=2;constructor(t){super(),this.max=t}get max(){return this[Pt]}set max(t){if(!n(t))throw new TypeError("max must be a number");this[Pt]=parseFloat(`${t}`)}get name(){return"max"}get notation(){return`max${this.max}`}run(t,e){const r=t;return r.rolls=t.rolls.map((t=>{const e=t;return t.value>this.max&&(e.value=this.max,e.modifiers.add("max")),e})),r}toJSON(){const{max:t}=this;return Object.assign(super.toJSON(),{max:t})}}const It=Symbol("min");class MinModifier extends Modifier{static order=1;constructor(t){super(),this.min=t}get min(){return this[It]}set min(t){if(!n(t))throw new TypeError("min must be a number");this[It]=parseFloat(`${t}`)}get name(){return"min"}get notation(){return`min${this.min}`}run(t,e){const r=t;return r.rolls=t.rolls.map((t=>{const e=t;return t.value<this.min&&(e.value=this.min,e.modifiers.add("min")),e})),r}toJSON(){const{min:t}=this;return Object.assign(super.toJSON(),{min:t})}}const jt=Symbol("once");class ReRollModifier extends ComparisonModifier{static order=4;constructor(t=!1,e=null){super(e),this.once=!!t}get name(){return"re-roll"}get notation(){return`r${this.once?"o":""}${super.notation}`}get once(){return!!this[jt]}set once(t){this[jt]=!!t}defaultComparePoint(t){return["=",t.min]}run(t,e){if(super.run(t,e),e.min===e.max)throw new DieActionValueError(e,"re-roll");return t.rolls.map((t=>{for(let r=0;r<this.maxIterations&&this.isComparePoint(t.value);r++){const r=e.rollOnce();if(t.value=r.value,t.modifiers.add("re-roll"+(this.once?"-once":"")),this.once)break}return t})),t}toJSON(){const{once:t}=this;return Object.assign(super.toJSON(),{once:t})}}const Jt=Symbol("direction");class SortingModifier extends Modifier{static order=11;constructor(t="a"){super(),this.direction=t}get direction(){return this[Jt]}set direction(t){if("a"!==t&&"d"!==t)throw new RangeError('Direction must be "a" (Ascending) or "d" (Descending)');this[Jt]=t}get name(){return"sorting"}get notation(){return`s${this.direction}`}run(t,e){let r;return r=t instanceof ResultGroup?"results":"rolls",t[r]=t[r].sort(((t,e)=>"d"===this.direction?e.value-t.value:t.value-e.value)),t instanceof ResultGroup&&(t[r]=t[r].map((t=>t instanceof ResultGroup||t instanceof RollResults?this.run(t,e):t))),t}toJSON(){const{direction:t}=this;return Object.assign(super.toJSON(),{direction:t})}}const qt=Symbol("failure-cp");class TargetModifier extends ComparisonModifier{static order=8;constructor(t,e=null){super(t),this.failureComparePoint=e}get failureComparePoint(){return this[qt]}set failureComparePoint(t){if(t&&!(t instanceof ComparePoint))throw new TypeError("failure comparePoint must be instance of ComparePoint or null");this[qt]=t||null}get name(){return"target"}get notation(){return`${super.notation}${this.failureComparePoint?`f${this.failureComparePoint}`:""}`}get successComparePoint(){return this.comparePoint}set successComparePoint(t){super.comparePoint=t}getStateValue(t){return this.isSuccess(t)?1:this.isFailure(t)?-1:0}isFailure(t){return!!this.failureComparePoint&&this.failureComparePoint.isMatch(t)}isNeutral(t){return!this.isSuccess(t)&&!this.isFailure(t)}isSuccess(t){return this.isComparePoint(t)}run(t,e){let r;return r=t instanceof ResultGroup?t.results:t.rolls,r.forEach((t=>{this.isSuccess(t.value)?t.modifiers.add("target-success"):this.isFailure(t.value)&&t.modifiers.add("target-failure"),t.calculationValue=this.getStateValue(t.value)})),t}toJSON(){const{failureComparePoint:t,successComparePoint:e}=this,r=super.toJSON();return delete r.comparePoint,Object.assign(r,{failureComparePoint:t,successComparePoint:e})}}const Gt=Symbol("once"),Vt=(t,e,r,n=!1)=>{const i=r.map((t=>t.value)).indexOf(t.value);return n?i<e:i!==e};class UniqueModifier extends ComparisonModifier{static order=5;constructor(t=!1,e=null){super(e),this.once=!!t}get name(){return"unique"}get notation(){return`u${this.once?"o":""}${super.notation}`}get once(){return!!this[Gt]}set once(t){this[Gt]=!!t}run(t,e){if(e.min===e.max)throw new DieActionValueError(e,"re-roll");return t.rolls.forEach(((t,r,n)=>{if(0!==r)for(let i=0;i<this.maxIterations&&(!this.comparePoint||this.isComparePoint(t.value))&&Vt(t,r,n,!0);i++){const r=e.rollOnce();if(t.value=r.value,t.modifiers.add("unique"+(this.once?"-once":"")),this.once)break}})),t}toJSON(){const{once:t}=this;return Object.assign(super.toJSON(),{once:t})}}var kt=Object.freeze({__proto__:null,ComparisonModifier:ComparisonModifier,CriticalFailureModifier:CriticalFailureModifier,CriticalSuccessModifier:CriticalSuccessModifier,DropModifier:DropModifier,ExplodeModifier:ExplodeModifier,KeepModifier:KeepModifier,MaxModifier:MaxModifier,MinModifier:MinModifier,Modifier:Modifier,ReRollModifier:ReRollModifier,SortingModifier:SortingModifier,TargetModifier:TargetModifier,UniqueModifier:UniqueModifier}),_t=Object.freeze({__proto__:null,ResultGroup:ResultGroup,RollResult:RollResult,RollResults:RollResults});const Bt=t=>{try{return!(!t||btoa(atob(t))!==t)}catch(t){return!1}},zt=t=>{try{const e=!!t&&JSON.parse(t);return!(!e||"object"!=typeof e)}catch(t){return!1}},Lt=Symbol("expressions"),Ut=Symbol("modifiers");class RollGroup extends HasDescription{constructor(t=[],e=[],r=null){super(r),this.expressions=t,this.modifiers=e}get expressions(){return[...this[Lt]||[]]}set expressions(t){if(!t)throw new RequiredArgumentError("expressions");if(!Array.isArray(t))throw new TypeError(`expressions must be an array: ${t}`);this[Lt]=[],t.forEach((e=>{if(!e||!Array.isArray(e))throw new TypeError(`Expressions must be an array of arrays: ${t}`);if(0===e.length)throw new TypeError(`Sub expressions cannot be empty: ${t}`);if(!e.every((t=>t instanceof StandardDice||"string"==typeof t||"number"==typeof t)))throw new TypeError("Sub expression items must be Dice, numbers, or strings");this[Lt].push(e)}))}get modifiers(){return this[Ut]?new Map([...this[Ut]].sort(((t,e)=>t[1].order-e[1].order))):null}set modifiers(t){let e;if(t instanceof Map)e=t;else if(Array.isArray(t))e=new Map(t.map((t=>[t.name,t])));else{if("object"!=typeof t)throw new TypeError("modifiers should be a Map, array, or an Object containing Modifiers");e=new Map(Object.entries(t))}if(e.size&&[...e.entries()].some((t=>!(t[1]instanceof Modifier))))throw new TypeError("modifiers must only contain Modifier instances");this[Ut]=e}get notation(){let t=this.expressions.map((t=>t.reduce(((t,e)=>t+e),""))).join(", ");return t=`{${t}}`,this.modifiers&&this.modifiers.size&&(t+=[...this.modifiers.values()].reduce(((t,e)=>t+e.notation),"")),t}roll(){const t=new ResultGroup(this.expressions.map((t=>{const e=t.map((t=>t instanceof StandardDice?t.roll():t));return new ResultGroup(e)})));return t.isRollGroup=!0,(this.modifiers||[]).forEach((e=>{e.run(t,this)})),t}toJSON(){const{modifiers:t,notation:e,expressions:r}=this;return Object.assign(super.toJSON(),{expressions:r,modifiers:t,notation:e,type:"group"})}toString(){return`${this.notation}${this.description?` ${this.description}`:""}`}}function Kt(t,e,r,n){var i=Error.call(this,t);return Object.setPrototypeOf&&Object.setPrototypeOf(i,Kt.prototype),i.expected=e,i.found=r,i.location=n,i.name="SyntaxError",i}function Wt(t,e,r){return r=r||" ",t.length>e?t:(e-=t.length,t+(r+=r.repeat(e)).slice(0,e))}function Ht(t,e){var n,i,o,s,a={},u=(e=void 0!==e?e:{}).grammarSource,l={Main:vr},c=vr,h="{",f=",",p="}",d="d",m="d%",g="dF",y=".",w="!",b="p",x="k",v="max",E="min",S="r",A="o",R="u",M="cs",C="cf",T="s",N="a",O="f",$="!=",D="<=",F=">=",P="=",I="<>",j=">",J="<",q="(",G=")",V="abs",k="ceil",_="cos",B="exp",z="floor",L="log",U="round",K="sign",W="sin",H="sqrt",Z="tan",X="pow",Q="-",Y="**",tt="*",et="^",rt="%",nt="/",it="+",ot="/*",st="*/",at="[",ut="]",lt="//",ct="#",ht=/^[12]/,ft=/^[lh]/,pt=/^[.]/,dt=/^[1-9]/,mt=/^[0-9]/,gt=/^[^\]]/,yt=/^[\n\r\u2028\u2029]/,wt=/^[ \t\n\r]/,bt=mr("{",!1),xt=mr(",",!1),vt=mr("}",!1),Et=mr("d",!1),St=mr("d%",!1),At=mr("dF",!1),Rt=mr(".",!1),Mt=gr(["1","2"],!1,!1),Ct=mr("!",!1),Tt=mr("p",!1),Nt=gr(["l","h"],!1,!1),Ot=mr("k",!1),$t=mr("max",!1),Dt=mr("min",!1),Ft=mr("r",!1),Pt=mr("o",!1),It=mr("u",!1),jt=mr("cs",!1),Jt=mr("cf",!1),qt=mr("s",!1),Gt=mr("a",!1),Vt=mr("f",!1),kt=mr("!=",!1),_t=mr("<=",!1),Bt=mr(">=",!1),zt=mr("=",!1),Lt=mr("<>",!1),Ut=mr(">",!1),Wt=mr("<",!1),Ht=mr("(",!1),Zt=mr(")",!1),Xt=mr("abs",!1),Qt=mr("ceil",!1),Yt=mr("cos",!1),te=mr("exp",!1),ee=mr("floor",!1),re=mr("log",!1),ne=mr("round",!1),ie=mr("sign",!1),oe=mr("sin",!1),se=mr("sqrt",!1),ae=mr("tan",!1),ue=mr("pow",!1),le=mr("-",!1),ce=gr(["."],!1,!1),he=gr([["1","9"]],!1,!1),fe=gr([["0","9"]],!1,!1),pe=mr("**",!1),de=mr("*",!1),me=mr("^",!1),ge=mr("%",!1),ye=mr("/",!1),we=mr("+",!1),be=yr("comment"),xe=mr("/*",!1),ve=mr("*/",!1),Ee={type:"any"},Se=mr("[",!1),Ae=gr(["]"],!0,!1),Re=mr("]",!1),Me=mr("//",!1),Ce=mr("#",!1),Te=gr(["\n","\r","\u2028","\u2029"],!1,!1),Ne=yr("whitespace"),Oe=gr([" ","\t","\n","\r"],!1,!1),$e=yr("whitespace or comment"),De=function(t,e,r,n){return new RollGroup([t,...e.map((t=>t[3]))],Object.assign({},...r.map((t=>({[t.name]:t})))),n.find((t=>t instanceof Description)))},Fe=function(t,e,r){return t.modifiers=Object.assign({},...e.map((t=>({[t.name]:t})))),t.description=r.find((t=>t instanceof Description)),t},Pe=function(t,e){return new StandardDice(e,t||1)},Ie=function(t){return new PercentileDice(t||1)},je=function(t,e){return new FudgeDice(e?parseInt(e[1],10):2,t||1)},Je=function(t,e,r){return new ExplodeModifier(r,!!t,!!e)},qe=function(t,e){return new TargetModifier(t,e)},Ge=function(t,e){return new DropModifier(t||"l",e)},Ve=function(t,e){return new KeepModifier(t||"h",e)},ke=function(t){return new MaxModifier(t)},_e=function(t){return new MinModifier(t)},Be=function(t,e){return new ReRollModifier(!!t,e)},ze=function(t,e){return new UniqueModifier(!!t,e)},Le=function(t){return new CriticalSuccessModifier(t)},Ue=function(t){return new CriticalFailureModifier(t)},Ke=function(t){return new SortingModifier(t||"a")},We=function(t){return t},He=function(t,e){return new ComparePoint(t,e)},Ze=function(t,e,n){return r(dr())},Xe=function(t,e){return t=Array.isArray(t)?t:[t],[...t,...e.map((([,t,,e])=>[t,e])).flat(2)]},Qe=function(t,e,r){return[t,...e,r]},Ye=function(t,e){return[`${t}(`,...e,")"]},tr=function(t,e,r){return[`${t}(`,...e,",",...r,")"]},er=function(){return parseFloat(dr())},rr=function(){return parseInt(dr(),10)},nr=function(){return parseInt(dr(),10)},ir=function(){return"^"},or=function(t){return new Description(t.flat().join(""),Description.types.MULTILINE)},sr=function(t){return new Description(t.flat().join(""),Description.types.MULTILINE)},ar=function(t){return new Description(t.flat().join(""),Description.types.INLINE)},ur=0,lr=0,cr=[{line:1,column:1}],hr=0,fr=[],pr=0;if("startRule"in e){if(!(e.startRule in l))throw new Error("Can't start parsing from rule \""+e.startRule+'".');c=l[e.startRule]}function dr(){return t.substring(lr,ur)}function mr(t,e){return{type:"literal",text:t,ignoreCase:e}}function gr(t,e,r){return{type:"class",parts:t,inverted:e,ignoreCase:r}}function yr(t){return{type:"other",description:t}}function wr(e){var r,n=cr[e];if(n)return n;for(r=e-1;!cr[r];)r--;for(n={line:(n=cr[r]).line,column:n.column};r<e;)10===t.charCodeAt(r)?(n.line++,n.column=1):n.column++,r++;return cr[e]=n,n}function br(t,e,r){var n=wr(t),i=wr(e),o={source:u,start:{offset:t,line:n.line,column:n.column},end:{offset:e,line:i.line,column:i.column}};return r&&u&&"function"==typeof u.offset&&(o.start=u.offset(o.start),o.end=u.offset(o.end)),o}function xr(t){ur<hr||(ur>hr&&(hr=ur,fr=[]),fr.push(t))}function vr(){return Mr()}function Er(){var e,r,n,i;if(e=ur,r=function(){var e,r,n,i;e=ur,r=Rr(),r===a&&(r=null);100===t.charCodeAt(ur)?(n=d,ur++):(n=a,0===pr&&xr(Et));n!==a&&(i=Rr())!==a?(lr=e,e=Pe(r,i)):(ur=e,e=a);return e}(),r===a&&(r=function(){var e,r,n;e=ur,r=Rr(),r===a&&(r=null);t.substr(ur,2)===m?(n=m,ur+=2):(n=a,0===pr&&xr(St));n!==a?(lr=e,e=Ie(r)):(ur=e,e=a);return e}(),r===a&&(r=function(){var e,r,n,i,o,s;e=ur,r=Rr(),r===a&&(r=null);t.substr(ur,2)===g?(n=g,ur+=2):(n=a,0===pr&&xr(At));n!==a?(i=ur,46===t.charCodeAt(ur)?(o=y,ur++):(o=a,0===pr&&xr(Rt)),o!==a?(ht.test(t.charAt(ur))?(s=t.charAt(ur),ur++):(s=a,0===pr&&xr(Mt)),s!==a?i=o=[o,s]:(ur=i,i=a)):(ur=i,i=a),i===a&&(i=null),lr=e,e=je(r,i)):(ur=e,e=a);return e}())),r!==a){for(n=[],i=Sr();i!==a;)n.push(i),i=Sr();i=jr(),lr=e,e=Fe(r,n,i)}else ur=e,e=a;return e}function Sr(){var e;return(e=function(){var e,r,n,i,o;e=ur,33===t.charCodeAt(ur)?(r=w,ur++):(r=a,0===pr&&xr(Ct));r!==a?(33===t.charCodeAt(ur)?(n=w,ur++):(n=a,0===pr&&xr(Ct)),n===a&&(n=null),112===t.charCodeAt(ur)?(i=b,ur++):(i=a,0===pr&&xr(Tt)),i===a&&(i=null),(o=Ar())===a&&(o=null),lr=e,e=Je(n,i,o)):(ur=e,e=a);return e}())===a&&(e=function(){var e,r,n;e=ur,(r=Ar())!==a?(n=function(){var e,r,n;e=ur,102===t.charCodeAt(ur)?(r=O,ur++):(r=a,0===pr&&xr(Vt));r!==a&&(n=Ar())!==a?(lr=e,e=We(n)):(ur=e,e=a);return e}(),n===a&&(n=null),lr=e,e=qe(r,n)):(ur=e,e=a);return e}())===a&&(e=function(){var e,r,n,i;e=ur,100===t.charCodeAt(ur)?(r=d,ur++):(r=a,0===pr&&xr(Et));r!==a?(ft.test(t.charAt(ur))?(n=t.charAt(ur),ur++):(n=a,0===pr&&xr(Nt)),n===a&&(n=null),(i=Nr())!==a?(lr=e,e=Ge(n,i)):(ur=e,e=a)):(ur=e,e=a);return e}())===a&&(e=function(){var e,r,n,i;e=ur,107===t.charCodeAt(ur)?(r=x,ur++):(r=a,0===pr&&xr(Ot));r!==a?(ft.test(t.charAt(ur))?(n=t.charAt(ur),ur++):(n=a,0===pr&&xr(Nt)),n===a&&(n=null),(i=Nr())!==a?(lr=e,e=Ve(n,i)):(ur=e,e=a)):(ur=e,e=a);return e}())===a&&(e=function(){var e,r,n,i;e=ur,114===t.charCodeAt(ur)?(r=S,ur++):(r=a,0===pr&&xr(Ft));r!==a?(111===t.charCodeAt(ur)?(n=A,ur++):(n=a,0===pr&&xr(Pt)),n===a&&(n=null),(i=Ar())===a&&(i=null),lr=e,e=Be(n,i)):(ur=e,e=a);return e}())===a&&(e=function(){var e,r,n,i;e=ur,117===t.charCodeAt(ur)?(r=R,ur++):(r=a,0===pr&&xr(It));r!==a?(111===t.charCodeAt(ur)?(n=A,ur++):(n=a,0===pr&&xr(Pt)),n===a&&(n=null),(i=Ar())===a&&(i=null),lr=e,e=ze(n,i)):(ur=e,e=a);return e}())===a&&(e=function(){var e,r,n;e=ur,t.substr(ur,2)===M?(r=M,ur+=2):(r=a,0===pr&&xr(jt));r!==a?((n=Ar())===a&&(n=null),lr=e,e=Le(n)):(ur=e,e=a);return e}())===a&&(e=function(){var e,r,n;e=ur,t.substr(ur,2)===C?(r=C,ur+=2):(r=a,0===pr&&xr(Jt));r!==a?((n=Ar())===a&&(n=null),lr=e,e=Ue(n)):(ur=e,e=a);return e}())===a&&(e=function(){var e,r,n;e=ur,115===t.charCodeAt(ur)?(r=T,ur++):(r=a,0===pr&&xr(qt));r!==a?(97===t.charCodeAt(ur)?(n=N,ur++):(n=a,0===pr&&xr(Gt)),n===a&&(100===t.charCodeAt(ur)?(n=d,ur++):(n=a,0===pr&&xr(Et))),n===a&&(n=null),lr=e,e=Ke(n)):(ur=e,e=a);return e}())===a&&(e=function(){var e,r,n;e=ur,t.substr(ur,3)===v?(r=v,ur+=3):(r=a,0===pr&&xr($t));r!==a&&(n=Tr())!==a?(lr=e,e=ke(n)):(ur=e,e=a);return e}())===a&&(e=function(){var e,r,n;e=ur,t.substr(ur,3)===E?(r=E,ur+=3):(r=a,0===pr&&xr(Dt));r!==a&&(n=Tr())!==a?(lr=e,e=_e(n)):(ur=e,e=a);return e}()),e}function Ar(){var e,r,n;return e=ur,r=function(){var e;t.substr(ur,2)===$?(e=$,ur+=2):(e=a,0===pr&&xr(kt));e===a&&(t.substr(ur,2)===D?(e=D,ur+=2):(e=a,0===pr&&xr(_t)),e===a&&(t.substr(ur,2)===F?(e=F,ur+=2):(e=a,0===pr&&xr(Bt)),e===a&&(61===t.charCodeAt(ur)?(e=P,ur++):(e=a,0===pr&&xr(zt)),e===a&&(t.substr(ur,2)===I?(e=I,ur+=2):(e=a,0===pr&&xr(Lt)),e===a&&(62===t.charCodeAt(ur)?(e=j,ur++):(e=a,0===pr&&xr(Ut)),e===a&&(60===t.charCodeAt(ur)?(e=J,ur++):(e=a,0===pr&&xr(Wt))))))));return e}(),r!==a&&(n=Tr())!==a?(lr=e,e=He(r,n)):(ur=e,e=a),e}function Rr(){var e,r,n,i,o,s,u,l,c,h;if((e=Nr())===a)if(e=ur,40===t.charCodeAt(ur)?(r=q,ur++):(r=a,0===pr&&xr(Ht)),r!==a){if(Ir(),n=ur,(i=Tr())!==a){if(o=[],s=ur,u=Ir(),(l=$r())!==a?(c=Ir(),(h=Tr())!==a?s=u=[u,l,c,h]:(ur=s,s=a)):(ur=s,s=a),s!==a)for(;s!==a;)o.push(s),s=ur,u=Ir(),(l=$r())!==a?(c=Ir(),(h=Tr())!==a?s=u=[u,l,c,h]:(ur=s,s=a)):(ur=s,s=a);else o=a;o!==a?n=i=[i,o]:(ur=n,n=a)}else ur=n,n=a;n!==a?(i=Ir(),41===t.charCodeAt(ur)?(o=G,ur++):(o=a,0===pr&&xr(Zt)),o!==a?(lr=e,e=Ze()):(ur=e,e=a)):(ur=e,e=a)}else ur=e,e=a;return e}function Mr(){var t,e,r,n,i,o,s,u;if(t=ur,(e=Cr())!==a){for(r=[],n=ur,i=Ir(),(o=$r())!==a?(s=Ir(),(u=Cr())!==a?n=i=[i,o,s,u]:(ur=n,n=a)):(ur=n,n=a);n!==a;)r.push(n),n=ur,i=Ir(),(o=$r())!==a?(s=Ir(),(u=Cr())!==a?n=i=[i,o,s,u]:(ur=n,n=a)):(ur=n,n=a);lr=t,t=Xe(e,r)}else ur=t,t=a;return t}function Cr(){var e,r,n,i;return e=function(){var e,r,n,i,o,s,u;e=ur,t.substr(ur,3)===V?(r=V,ur+=3):(r=a,0===pr&&xr(Xt));r===a&&(t.substr(ur,4)===k?(r=k,ur+=4):(r=a,0===pr&&xr(Qt)),r===a&&(t.substr(ur,3)===_?(r=_,ur+=3):(r=a,0===pr&&xr(Yt)),r===a&&(t.substr(ur,3)===B?(r=B,ur+=3):(r=a,0===pr&&xr(te)),r===a&&(t.substr(ur,5)===z?(r=z,ur+=5):(r=a,0===pr&&xr(ee)),r===a&&(t.substr(ur,3)===L?(r=L,ur+=3):(r=a,0===pr&&xr(re)),r===a&&(t.substr(ur,5)===U?(r=U,ur+=5):(r=a,0===pr&&xr(ne)),r===a&&(t.substr(ur,4)===K?(r=K,ur+=4):(r=a,0===pr&&xr(ie)),r===a&&(t.substr(ur,3)===W?(r=W,ur+=3):(r=a,0===pr&&xr(oe)),r===a&&(t.substr(ur,4)===H?(r=H,ur+=4):(r=a,0===pr&&xr(se)),r===a&&(t.substr(ur,3)===Z?(r=Z,ur+=3):(r=a,0===pr&&xr(ae))))))))))));r!==a?(40===t.charCodeAt(ur)?(n=q,ur++):(n=a,0===pr&&xr(Ht)),n!==a?(Ir(),(i=Mr())!==a?(Ir(),41===t.charCodeAt(ur)?(o=G,ur++):(o=a,0===pr&&xr(Zt)),o!==a?(lr=e,e=Ye(r,i)):(ur=e,e=a)):(ur=e,e=a)):(ur=e,e=a)):(ur=e,e=a);e===a&&(e=ur,t.substr(ur,3)===X?(r=X,ur+=3):(r=a,0===pr&&xr(ue)),r===a&&(t.substr(ur,3)===v?(r=v,ur+=3):(r=a,0===pr&&xr($t)),r===a&&(t.substr(ur,3)===E?(r=E,ur+=3):(r=a,0===pr&&xr(Dt)))),r!==a?(40===t.charCodeAt(ur)?(n=q,ur++):(n=a,0===pr&&xr(Ht)),n!==a?(Ir(),(i=Mr())!==a?(Ir(),44===t.charCodeAt(ur)?(o=f,ur++):(o=a,0===pr&&xr(xt)),o!==a?(Ir(),(s=Mr())!==a?(Ir(),41===t.charCodeAt(ur)?(u=G,ur++):(u=a,0===pr&&xr(Zt)),u!==a?(lr=e,e=tr(r,i,s)):(ur=e,e=a)):(ur=e,e=a)):(ur=e,e=a)):(ur=e,e=a)):(ur=e,e=a)):(ur=e,e=a));return e}(),e===a&&(e=Er())===a&&(e=Tr())===a&&(e=ur,40===t.charCodeAt(ur)?(r=q,ur++):(r=a,0===pr&&xr(Ht)),r!==a?(Ir(),(n=Mr())!==a?(Ir(),41===t.charCodeAt(ur)?(i=G,ur++):(i=a,0===pr&&xr(Zt)),i!==a?(lr=e,e=Qe(r,n,i)):(ur=e,e=a)):(ur=e,e=a)):(ur=e,e=a),e===a&&(e=function(){var e,r,n,i,o,s,u,l,c;if(e=ur,123===t.charCodeAt(ur)?(r=h,ur++):(r=a,0===pr&&xr(bt)),r!==a)if(Ir(),(n=Mr())!==a){for(i=[],o=ur,s=Ir(),44===t.charCodeAt(ur)?(u=f,ur++):(u=a,0===pr&&xr(xt)),u!==a?(l=Ir(),(c=Mr())!==a?o=s=[s,u,l,c]:(ur=o,o=a)):(ur=o,o=a);o!==a;)i.push(o),o=ur,s=Ir(),44===t.charCodeAt(ur)?(u=f,ur++):(u=a,0===pr&&xr(xt)),u!==a?(l=Ir(),(c=Mr())!==a?o=s=[s,u,l,c]:(ur=o,o=a)):(ur=o,o=a);if(o=Ir(),125===t.charCodeAt(ur)?(s=p,ur++):(s=a,0===pr&&xr(vt)),s!==a){for(u=[],l=Sr();l!==a;)u.push(l),l=Sr();l=jr(),lr=e,e=De(n,i,u,l)}else ur=e,e=a}else ur=e,e=a;else ur=e,e=a;return e}())),e}function Tr(){var e,r,n,i;return e=ur,45===t.charCodeAt(ur)?ur++:0===pr&&xr(le),Or()!==a?(r=ur,pt.test(t.charAt(ur))?(n=t.charAt(ur),ur++):(n=a,0===pr&&xr(ce)),n!==a&&(i=Or())!==a?r=n=[n,i]:(ur=r,r=a),r===a&&(r=null),lr=e,e=er()):(ur=e,e=a),e}function Nr(){var e,r,n,i;if(e=ur,dt.test(t.charAt(ur))?(r=t.charAt(ur),ur++):(r=a,0===pr&&xr(he)),r!==a){for(n=[],mt.test(t.charAt(ur))?(i=t.charAt(ur),ur++):(i=a,0===pr&&xr(fe));i!==a;)n.push(i),mt.test(t.charAt(ur))?(i=t.charAt(ur),ur++):(i=a,0===pr&&xr(fe));lr=e,e=rr()}else ur=e,e=a;return e}function Or(){var e,r,n;if(e=ur,r=[],mt.test(t.charAt(ur))?(n=t.charAt(ur),ur++):(n=a,0===pr&&xr(fe)),n!==a)for(;n!==a;)r.push(n),mt.test(t.charAt(ur))?(n=t.charAt(ur),ur++):(n=a,0===pr&&xr(fe));else r=a;return r!==a&&(lr=e,r=nr()),e=r}function $r(){var e,r;return e=ur,t.substr(ur,2)===Y?(r=Y,ur+=2):(r=a,0===pr&&xr(pe)),r!==a&&(lr=e,r=ir()),(e=r)===a&&(42===t.charCodeAt(ur)?(e=tt,ur++):(e=a,0===pr&&xr(de)),e===a&&(94===t.charCodeAt(ur)?(e=et,ur++):(e=a,0===pr&&xr(me)),e===a&&(37===t.charCodeAt(ur)?(e=rt,ur++):(e=a,0===pr&&xr(ge)),e===a&&(47===t.charCodeAt(ur)?(e=nt,ur++):(e=a,0===pr&&xr(ye)),e===a&&(43===t.charCodeAt(ur)?(e=it,ur++):(e=a,0===pr&&xr(we)),e===a&&(45===t.charCodeAt(ur)?(e=Q,ur++):(e=a,0===pr&&xr(le)))))))),e}function Dr(){var e;return pr++,(e=function(){var e,r,n,i,o,s;e=ur,t.substr(ur,2)===ot?(r=ot,ur+=2):(r=a,0===pr&&xr(xe));if(r!==a){for(n=[],i=ur,o=ur,pr++,t.substr(ur,2)===st?(s=st,ur+=2):(s=a,0===pr&&xr(ve)),pr--,s===a?o=void 0:(ur=o,o=a),o!==a?(t.length>ur?(s=t.charAt(ur),ur++):(s=a,0===pr&&xr(Ee)),s!==a?i=o=[o,s]:(ur=i,i=a)):(ur=i,i=a);i!==a;)n.push(i),i=ur,o=ur,pr++,t.substr(ur,2)===st?(s=st,ur+=2):(s=a,0===pr&&xr(ve)),pr--,s===a?o=void 0:(ur=o,o=a),o!==a?(t.length>ur?(s=t.charAt(ur),ur++):(s=a,0===pr&&xr(Ee)),s!==a?i=o=[o,s]:(ur=i,i=a)):(ur=i,i=a);t.substr(ur,2)===st?(i=st,ur+=2):(i=a,0===pr&&xr(ve)),i!==a?(lr=e,e=or(n)):(ur=e,e=a)}else ur=e,e=a;if(e===a)if(e=ur,91===t.charCodeAt(ur)?(r=at,ur++):(r=a,0===pr&&xr(Se)),r!==a){for(n=[],gt.test(t.charAt(ur))?(i=t.charAt(ur),ur++):(i=a,0===pr&&xr(Ae));i!==a;)n.push(i),gt.test(t.charAt(ur))?(i=t.charAt(ur),ur++):(i=a,0===pr&&xr(Ae));93===t.charCodeAt(ur)?(i=ut,ur++):(i=a,0===pr&&xr(Re)),i!==a?(lr=e,e=sr(n)):(ur=e,e=a)}else ur=e,e=a;return e}())===a&&(e=function(){var e,r,n,i,o,s;e=ur,t.substr(ur,2)===lt?(r=lt,ur+=2):(r=a,0===pr&&xr(Me));r===a&&(35===t.charCodeAt(ur)?(r=ct,ur++):(r=a,0===pr&&xr(Ce)));if(r!==a){for(n=[],i=ur,o=ur,pr++,s=Fr(),pr--,s===a?o=void 0:(ur=o,o=a),o!==a?(t.length>ur?(s=t.charAt(ur),ur++):(s=a,0===pr&&xr(Ee)),s!==a?i=o=[o,s]:(ur=i,i=a)):(ur=i,i=a);i!==a;)n.push(i),i=ur,o=ur,pr++,s=Fr(),pr--,s===a?o=void 0:(ur=o,o=a),o!==a?(t.length>ur?(s=t.charAt(ur),ur++):(s=a,0===pr&&xr(Ee)),s!==a?i=o=[o,s]:(ur=i,i=a)):(ur=i,i=a);lr=e,e=ar(n)}else ur=e,e=a;return e}()),pr--,e===a&&0===pr&&xr(be),e}function Fr(){var e;return yt.test(t.charAt(ur))?(e=t.charAt(ur),ur++):(e=a,0===pr&&xr(Te)),e}function Pr(){var e;return pr++,wt.test(t.charAt(ur))?(e=t.charAt(ur),ur++):(e=a,0===pr&&xr(Oe)),pr--,e===a&&0===pr&&xr(Ne),e}function Ir(){var t,e;for(t=[],e=Pr();e!==a;)t.push(e),e=Pr();return t}function jr(){var t,e;for(pr++,t=[],(e=Pr())===a&&(e=Dr());e!==a;)t.push(e),(e=Pr())===a&&(e=Dr());return pr--,e=a,0===pr&&xr($e),t}if((n=c())!==a&&ur===t.length)return n;throw n!==a&&ur<t.length&&xr({type:"end"}),i=fr,o=hr<t.length?t.charAt(hr):null,s=hr<t.length?br(hr,hr+1):br(hr,hr),new Kt(Kt.buildMessage(i,o),i,o,s)}!function(t,e){function r(){this.constructor=t}r.prototype=e.prototype,t.prototype=new r}(Kt,Error),Kt.prototype.format=function(t){var e="Error: "+this.message;if(this.location){var r,n=null;for(r=0;r<t.length;r++)if(t[r].source===this.location.source){n=t[r].text.split(/\r\n|\n|\r/g);break}var i=this.location.start,o=this.location.source&&"function"==typeof this.location.source.offset?this.location.source.offset(i):i,s=this.location.source+":"+o.line+":"+o.column;if(n){var a=this.location.end,u=Wt("",o.line.toString().length," "),l=n[i.line-1],c=(i.line===a.line?a.column:l.length+1)-i.column||1;e+="\n --\x3e "+s+"\n"+u+" |\n"+o.line+" | "+l+"\n"+u+" | "+Wt("",i.column-1," ")+Wt("",c,"^")}else e+="\n at "+s}return e},Kt.buildMessage=function(t,e){var r={literal:function(t){return'"'+i(t.text)+'"'},class:function(t){var e=t.parts.map((function(t){return Array.isArray(t)?o(t[0])+"-"+o(t[1]):o(t)}));return"["+(t.inverted?"^":"")+e.join("")+"]"},any:function(){return"any character"},end:function(){return"end of input"},other:function(t){return t.description}};function n(t){return t.charCodeAt(0).toString(16).toUpperCase()}function i(t){return t.replace(/\\/g,"\\\\").replace(/"/g,'\\"').replace(/\0/g,"\\0").replace(/\t/g,"\\t").replace(/\n/g,"\\n").replace(/\r/g,"\\r").replace(/[\x00-\x0F]/g,(function(t){return"\\x0"+n(t)})).replace(/[\x10-\x1F\x7F-\x9F]/g,(function(t){return"\\x"+n(t)}))}function o(t){return t.replace(/\\/g,"\\\\").replace(/\]/g,"\\]").replace(/\^/g,"\\^").replace(/-/g,"\\-").replace(/\0/g,"\\0").replace(/\t/g,"\\t").replace(/\n/g,"\\n").replace(/\r/g,"\\r").replace(/[\x00-\x0F]/g,(function(t){return"\\x0"+n(t)})).replace(/[\x10-\x1F\x7F-\x9F]/g,(function(t){return"\\x"+n(t)}))}function s(t){return r[t.type](t)}return"Expected "+function(t){var e,r,n=t.map(s);if(n.sort(),n.length>0){for(e=1,r=1;e<n.length;e++)n[e-1]!==n[e]&&(n[r]=n[e],r++);n.length=r}switch(n.length){case 1:return n[0];case 2:return n[0]+" or "+n[1];default:return n.slice(0,-1).join(", ")+", or "+n[n.length-1]}}(t)+" but "+function(t){return t?'"'+i(t)+'"':"end of input"}(e)+" found."};class Parser{static parse(t){if(!t)throw new RequiredArgumentError("notation");if("string"!=typeof t)throw new TypeError("Notation must be a string");return Ht(t)}}const Zt=Object.freeze({BASE_64:1,JSON:0,OBJECT:2}),Xt=Symbol("notation"),Qt=Symbol("maxTotal"),Yt=Symbol("minTotal"),te=Symbol("expressions"),ee=Symbol("roll-method"),re=Symbol("rolls"),ne=Symbol("set-rolls"),ie=Symbol("total"),oe=t=>((t,e=0)=>parseFloat(parseFloat(`${t}`).toFixed(e||0)))(t.calculationValue,2);class DiceRoll{constructor(t){if(!t)throw new RequiredArgumentError("notation");if(this[te]=[],t instanceof Object&&!Array.isArray(t)){if(!t.notation)throw new RequiredArgumentError("notation");if("string"!=typeof t.notation)throw new NotationError(t.notation);t.rolls&&this[ne](t.rolls),this[Xt]=t.notation,this[te]=Parser.parse(this.notation),this.hasRolls()||this.roll()}else{if("string"!=typeof t)throw new NotationError(t);this[Xt]=t,this[te]=Parser.parse(this.notation),this.roll()}}get averageTotal(){return(this.maxTotal+this.minTotal)/2}get maxTotal(){if(!this.hasExpressions())return 0;if(!this[Qt]){const t=this[ee](rt.max);this[Qt]=oe(t)}return this[Qt]}get minTotal(){if(!this.hasExpressions())return 0;if(!this[Yt]){const t=this[ee](rt.min);this[Yt]=oe(t)}return this[Yt]}get notation(){return this[Xt]}get output(){let t=`${this.notation}: `;return this.hasRolls()?t+=`${this[re]} = ${this.total}`:t+="No dice rolled",t}get rolls(){return this[re]?this[re].results:[]}get total(){ret