@randsum/dice
Version:
A flexible, type-safe dice roller
2 lines (1 loc) • 4.96 kB
JavaScript
var k=require("node:module");var{defineProperty:z,getOwnPropertyNames:h,getOwnPropertyDescriptor:q}=Object,y=Object.prototype.hasOwnProperty;var v=new WeakMap,w=(t)=>{var n=v.get(t),m;if(n)return n;if(n=z({},"__esModule",{value:!0}),t&&typeof t==="object"||typeof t==="function")h(t).map((R)=>!y.call(n,R)&&z(n,R,{get:()=>t[R],enumerable:!(m=q(t,R))||m.enumerable}));return v.set(t,n),n};var c=(t,n)=>{for(var m in n)z(t,m,{get:n[m],enumerable:!0,configurable:!0,set:(R)=>n[m]=()=>R})};var a={};c(a,{roll:()=>M,fudgeDice:()=>g,coin:()=>Z,alphaNumDie:()=>I,D8:()=>W,D6:()=>S,D4:()=>Q,D20:()=>$,D12:()=>Y,D100:()=>G,D10:()=>X,D:()=>A});module.exports=w(a);function r(t,n=0){if(t.every((m)=>typeof m==="number"))return t.reduce((m,R)=>m+R,n);return t.flat().join(", ")}function f(t){return Math.floor(Math.random()*t)}function F(t){return Array.from({length:Number(t)},(n,m)=>m+1)}function x(t,n,m){let R=m??F(n);return Array.from({length:t},()=>f(n)).map((u)=>{return R[u]})}function E(){return Math.random().toString(36).substring(2,15)}var N=require("@randsum/core"),C=require("@randsum/notation");function H(t){return t instanceof A}function K(t){let n=J(t);return{argument:t,options:n,die:B(t),notation:N.optionsConverter.toNotation(n),description:N.optionsConverter.toDescription(n)}}function J(t){if(t instanceof A)return t.toOptions;if(C.isDiceNotation(t)){let m=(t.match(C.coreNotationPattern)??"")[0],R=t.replace(m,""),[u,p=""]=m.split(/[Dd]/);if(p.includes("{"))return{quantity:Number(u),sides:[...p.replaceAll(/{|}/g,"")]};return{quantity:Number(u),sides:Number(p),...{modifiers:{...N.DropModifier.parse(R),...N.ExplodeModifier.parse(R),...N.UniqueModifier.parse(R),...N.ReplaceModifier.parse(R),...N.RerollModifier.parse(R),...N.CapModifier.parse(R),...N.PlusModifier.parse(R),...N.MinusModifier.parse(R)}}}}if(Array.isArray(t))return{quantity:1,sides:t.map(String)};if(typeof t==="string"||typeof t==="number")return{quantity:1,sides:Number(t)};return t}function B(t){if(H(t))return t;let n=J(t);return new A(n.sides)}var b=require("@randsum/core");function L(t){return typeof t.sides==="number"}function _(t){let n=l(t.dicePools),m=o(t,n),R=Object.values(m);return{...t,rawRolls:n,modifiedRolls:m,rawResult:Object.values(n).flat(),result:R.map((u)=>u.rolls).flat(),type:P(t.dicePools),total:r(R.map((u)=>u.total))}}function P(t){let n=Object.values(t);if(n.every((m)=>typeof m.options.sides==="number"))return"numerical";if(n.every((m)=>Array.isArray(m.options.sides)))return"custom";return"mixed"}function e(t){return Array.isArray(t.options.sides)}function i(t,n,m,R){let p={reroll:()=>new b.RerollModifier(n.reroll).apply(m,void 0,R.rollOne),unique:()=>new b.UniqueModifier(n.unique).apply(m,{sides:R.sides,quantity:R.quantity},R.rollOne),replace:()=>new b.ReplaceModifier(n.replace).apply(m),cap:()=>new b.CapModifier(n.cap).apply(m),drop:()=>new b.DropModifier(n.drop).apply(m),explode:()=>new b.ExplodeModifier(n.explode).apply(m,{sides:R.sides,quantity:R.quantity},R.rollOne),plus:()=>new b.PlusModifier(n.plus).apply(m),minus:()=>new b.MinusModifier(n.minus).apply(m)}[t];if(!p)throw new Error(`Unknown modifier: ${t}`);return p()}function o(t,n){return Object.fromEntries(Object.entries(t.dicePools).map(([m,R])=>{let u=n[m]??[];if(e(R))return[m,{total:r(u),rolls:u}];let{options:{sides:p,quantity:U=1,modifiers:j={}}}=R,V=()=>f(p),O=Object.keys(j).reduce((D,T)=>i(T,j,D,{sides:p,quantity:U,rollOne:V}),{simpleMathModifier:0,rolls:u});return[m,{rolls:O.rolls,total:r(O.rolls,O.simpleMathModifier)}]}))}function l(t){return Object.fromEntries(Object.entries(t).map(([n,m])=>{let{options:R}=m,u=R.quantity??1;if(L(R))return[n,x(u,R.sides)];return[n,x(u,R.sides.length,R.sides)]}))}function M(...t){let n={dicePools:Object.fromEntries(t.map((m)=>[E(),K(m)]))};return _(n)}class A{sides;faces;type;isCustom;constructor(t){if(typeof t==="number"){if(!Number.isInteger(t)||t<1)throw new Error("Die must have at least one side with a positive integer value");this.sides=t,this.faces=F(t),this.type="numerical",this.isCustom=!1}else{if(!t.length)throw new Error("Custom die must have at least one face");this.sides=t.length,this.faces=[...t],this.type="custom",this.isCustom=!0}}roll(t=1){let n=this.rollSpread(t);if(this.type==="numerical")return n.reduce((m,R)=>m+R,0);return n.join(", ")}rollSpread(t=1){return x(t,this.sides,this.faces)}rollModified(t=1,n={}){return M({...this.toOptions,quantity:t,modifiers:n})}get toOptions(){if(this.type==="numerical")return{quantity:1,sides:this.sides};return{quantity:1,sides:[...this.faces]}}}var Q=new A(4),S=new A(6),W=new A(8),X=new A(10),Y=new A(12),$=new A(20),G=new A(100),Z=new A(["Heads","Tails"]),g=new A(["+","+","+","-"," "," "]),d=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9"],I=new A(d);