UNPKG

@randsum/daggerheart

Version:

A flexible, type-safe dice roller for building Daggerheart-compatible applications

12 lines 3.74 kB
{ "version": 3, "sources": ["../src/index.ts", "../src/rollDaggerheart/index.ts", "../src/rollDaggerheart/calculateType.ts"], "sourcesContent": [ "export { roll, validateNotation } from '@randsum/roller'\nexport type { RollArgument, RollerRollResult, ValidationResult } from '@randsum/roller'\n\nexport { rollDaggerheart } from './rollDaggerheart'\nexport type {\n DaggerheartRollArgument,\n DaggerheartRollResult,\n DaggerheartRollResultType,\n DaggerheartAdvantageDisadvantage\n} from './types'\n", "import type { RollRecord, RollResult } from '@randsum/roller'\nimport { roll } from '@randsum/roller'\nimport type { DaggerheartRollArgument, DaggerheartRollResult } from '../types'\nimport { calculateType } from './calculateType'\n\nexport function rollDaggerheart({\n rollingWith,\n amplifyHope = false,\n amplifyFear = false,\n modifier = 0\n}: DaggerheartRollArgument): RollResult<DaggerheartRollResult> {\n const isAdvantage = rollingWith === 'Advantage'\n const hopeRollOptions = {\n sides: amplifyHope ? 20 : 12,\n key: 'hope'\n }\n const fearRollOptions = {\n sides: amplifyFear ? 20 : 12,\n key: 'fear'\n }\n const advDisadvantageOptions = {\n sides: 6,\n key: rollingWith,\n arithmetic: isAdvantage ? ('add' as const) : ('subtract' as const)\n }\n\n const rollResult = roll(\n ...[hopeRollOptions, fearRollOptions, rollingWith ? advDisadvantageOptions : undefined].filter(\n a => !!a\n )\n )\n\n const hopeRoll = rollResult.rolls.find(roll => roll.parameters.key === 'hope')\n const fearRoll = rollResult.rolls.find(roll => roll.parameters.key === 'fear')\n const advDisadvantageRoll = rollResult.rolls.find(\n roll => rollingWith && roll.parameters.key === rollingWith\n )\n\n if (!hopeRoll || !fearRoll) {\n throw new Error('Failed to properly roll.')\n }\n\n const advantage = advDisadvantageRoll\n ? {\n roll: advDisadvantageRoll.appliedTotal\n }\n : undefined\n\n return {\n ...rollResult,\n result: {\n total: rollResult.total + modifier,\n type: calculateType(hopeRoll.total, fearRoll.total),\n details: {\n hope: digestHopeFearTotal(hopeRoll),\n fear: digestHopeFearTotal(fearRoll),\n advantage,\n modifier\n }\n }\n }\n}\n\nfunction digestHopeFearTotal(roll: RollRecord): {\n roll: number\n amplified: boolean\n} {\n return {\n roll: roll.total,\n amplified: roll.parameters.sides === 20\n }\n}\n", "import type { DaggerheartRollResultType } from '../types'\n\nexport function calculateType(hope: number, fear: number): DaggerheartRollResultType {\n if (hope === fear) {\n return 'critical hope'\n }\n if (hope > fear) {\n return 'hope'\n }\n return 'fear'\n}\n" ], "mappings": "wlBAAuC,IAAvC,6BCCqB,IAArB,6BCCO,SAAS,CAAa,CAAC,EAAc,EAAyC,CACnF,GAAI,IAAS,EACX,MAAO,gBAET,GAAI,EAAO,EACT,MAAO,OAET,MAAO,ODJF,SAAS,CAAe,EAC7B,cACA,cAAc,GACd,cAAc,GACd,WAAW,GACkD,CAgB7D,IAAM,EAAa,OACjB,GAAG,CAfmB,CACtB,MAAO,EAAc,GAAK,GAC1B,IAAK,MACP,EACwB,CACtB,MAAO,EAAc,GAAK,GAC1B,IAAK,MACP,EAQwC,EAPT,CAC7B,MAAO,EACP,IAAK,EACL,WAZkB,IAAgB,YAYP,MAAmB,UAChD,EAG+E,MAAS,EAAE,OACtF,OAAO,CACT,CACF,EAEM,EAAW,EAAW,MAAM,KAAK,KAAQ,EAAK,WAAW,MAAQ,MAAM,EACvE,EAAW,EAAW,MAAM,KAAK,KAAQ,EAAK,WAAW,MAAQ,MAAM,EACvE,EAAsB,EAAW,MAAM,KAC3C,KAAQ,GAAe,EAAK,WAAW,MAAQ,CACjD,EAEA,IAAK,IAAa,EAChB,MAAM,IAAI,MAAM,0BAA0B,EAG5C,IAAM,EAAY,EACd,CACE,KAAM,EAAoB,YAC5B,EACA,OAEJ,MAAO,IACF,EACH,OAAQ,CACN,MAAO,EAAW,MAAQ,EAC1B,KAAM,EAAc,EAAS,MAAO,EAAS,KAAK,EAClD,QAAS,CACP,KAAM,EAAoB,CAAQ,EAClC,KAAM,EAAoB,CAAQ,EAClC,YACA,UACF,CACF,CACF,EAGF,SAAS,CAAmB,CAAC,EAG3B,CACA,MAAO,CACL,KAAM,EAAK,MACX,UAAW,EAAK,WAAW,QAAU,EACvC", "debugId": "6EB504D14C76A58864756E2164756E21", "names": [] }