@platonic-dice/core 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +57 -0
- package/dist/entities/DieType.d.ts +35 -0
- package/dist/entities/DieType.d.ts.map +1 -0
- package/dist/entities/DieType.js +43 -0
- package/dist/entities/Outcome.d.ts +26 -0
- package/dist/entities/Outcome.d.ts.map +1 -0
- package/dist/entities/Outcome.js +37 -0
- package/dist/entities/RollModifier.d.ts +115 -0
- package/dist/entities/RollModifier.d.ts.map +1 -0
- package/dist/entities/RollModifier.js +147 -0
- package/dist/entities/RollType.d.ts +24 -0
- package/dist/entities/RollType.d.ts.map +1 -0
- package/dist/entities/RollType.js +35 -0
- package/dist/entities/TestConditions.d.ts +97 -0
- package/dist/entities/TestConditions.d.ts.map +1 -0
- package/dist/entities/TestConditions.js +324 -0
- package/dist/entities/TestType.d.ts +28 -0
- package/dist/entities/TestType.d.ts.map +1 -0
- package/dist/entities/TestType.js +39 -0
- package/dist/entities/index.d.ts +16 -0
- package/dist/entities/index.d.ts.map +1 -0
- package/dist/entities/index.js +46 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +42 -0
- package/dist/roll.d.ts +66 -0
- package/dist/roll.d.ts.map +1 -0
- package/dist/roll.js +135 -0
- package/dist/rollDice.d.ts +48 -0
- package/dist/rollDice.d.ts.map +1 -0
- package/dist/rollDice.js +92 -0
- package/dist/rollDiceMod.d.ts +54 -0
- package/dist/rollDiceMod.d.ts.map +1 -0
- package/dist/rollDiceMod.js +137 -0
- package/dist/rollMod.d.ts +52 -0
- package/dist/rollMod.d.ts.map +1 -0
- package/dist/rollMod.js +114 -0
- package/dist/rollTest.d.ts +40 -0
- package/dist/rollTest.d.ts.map +1 -0
- package/dist/rollTest.js +100 -0
- package/dist/utils/determineOutcome.d.ts +45 -0
- package/dist/utils/determineOutcome.d.ts.map +1 -0
- package/dist/utils/determineOutcome.js +115 -0
- package/dist/utils/generateResult.d.ts +30 -0
- package/dist/utils/generateResult.d.ts.map +1 -0
- package/dist/utils/generateResult.js +51 -0
- package/dist/utils/index.d.ts +5 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +26 -0
- package/dist-types.d.ts +48 -0
- package/package.json +46 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rollTest.d.ts","sourceRoot":"","sources":["../src/rollTest.js"],"names":[],"mappings":";;;;;;;oBA6Ba,OAAO,oBAAoB,EAAE,YAAY;oBACzC,OAAO,oBAAoB,EAAE,YAAY;qBACzC,OAAO,qBAAqB,EAAE,aAAa;qBAC3C,OAAO,qBAAqB,EAAE,aAAa;8BAC3C,OAAO,2BAA2B,EAAE,sBAAsB;AALvE;;;;;;GAMG;AAEH;;;;;;;;;;;;GAYG;AACH,mCATW,YAAY,kBACZ,sBAAsB,GAAC;IAAE,QAAQ,EAAE,aAAa,CAAC;IAAC,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;CAAE,aAItE,aAAa,GACX;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,YAAY,CAAA;CAAE,CAgBnD"}
|
package/dist/rollTest.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @platonic-dice/core/src/rollTest
|
|
3
|
+
* @description
|
|
4
|
+
* Rolls a die and evaluates it against a set of test conditions.
|
|
5
|
+
* Supports either a `TestConditions` instance or a plain object
|
|
6
|
+
* with `{ testType, ...conditions }`.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* import { rollTest, TestType, DieType, RollType } from "@platonic-dice/core";
|
|
10
|
+
*
|
|
11
|
+
* // Basic "at least" test
|
|
12
|
+
* const result = rollTest(DieType.D20, { testType: TestType.AtLeast, target: 15 });
|
|
13
|
+
* console.log(result.base); // e.g., 12
|
|
14
|
+
* console.log(result.outcome); // e.g., "failure"
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* // Roll with advantage
|
|
18
|
+
* const resultAdv = rollTest(
|
|
19
|
+
* DieType.D20,
|
|
20
|
+
* { testType: TestType.AtMost, target: 5 },
|
|
21
|
+
* RollType.Advantage
|
|
22
|
+
* );
|
|
23
|
+
*/
|
|
24
|
+
const { DieType, TestType } = require("./entities");
|
|
25
|
+
const tc = require("./entities/TestConditions.js");
|
|
26
|
+
const r = require("./roll.js");
|
|
27
|
+
const utils = require("./utils");
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @typedef {import("./entities/DieType").DieTypeValue} DieTypeValue
|
|
31
|
+
* @typedef {import("./entities/Outcome").OutcomeValue} OutcomeValue
|
|
32
|
+
* @typedef {import("./entities/RollType").RollTypeValue} RollTypeValue
|
|
33
|
+
* @typedef {import("./entities/TestType").TestTypeValue} TestTypeValue
|
|
34
|
+
* @typedef {import("./entities/TestConditions").TestConditionsInstance} TestConditionsInstance
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Rolls a die and evaluates it against specified test conditions.
|
|
39
|
+
*
|
|
40
|
+
* @function rollTest
|
|
41
|
+
* @param {DieTypeValue} dieType - The type of die to roll (e.g., `DieType.D6`, `DieType.D20`).
|
|
42
|
+
* @param {TestConditionsInstance|{ testType: TestTypeValue, [key: string]: any }} testConditions
|
|
43
|
+
* Can be:
|
|
44
|
+
* - A `TestConditions` instance.
|
|
45
|
+
* - A plain object `{ testType, ...conditions }`.
|
|
46
|
+
* @param {RollTypeValue} [rollType=undefined] - Optional roll mode (`RollType.Advantage` or `RollType.Disadvantage`).
|
|
47
|
+
* @returns {{ base: number, outcome: OutcomeValue }} The raw roll and its evaluated outcome.
|
|
48
|
+
* @throws {TypeError} If `dieType` or `testConditions` are invalid.
|
|
49
|
+
*/
|
|
50
|
+
function rollTest(dieType, testConditions, rollType = undefined) {
|
|
51
|
+
if (!dieType) throw new TypeError("dieType is required.");
|
|
52
|
+
|
|
53
|
+
// Normalise testConditions
|
|
54
|
+
const conditionSet = tc.normaliseTestConditions(testConditions, dieType);
|
|
55
|
+
|
|
56
|
+
// Perform the roll
|
|
57
|
+
const base = r.roll(dieType, rollType);
|
|
58
|
+
|
|
59
|
+
// Determine the outcome using the normalized conditions
|
|
60
|
+
const outcome = utils.determineOutcome(base, conditionSet);
|
|
61
|
+
|
|
62
|
+
return { base, outcome };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
//
|
|
66
|
+
// --- Convenience Aliases ---
|
|
67
|
+
//
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Generates a DieType + TestType-specific alias for `rollTest`.
|
|
71
|
+
*
|
|
72
|
+
* @private
|
|
73
|
+
* @param {DieTypeValue} dieType
|
|
74
|
+
* @param {TestTypeValue} testType
|
|
75
|
+
* @returns {(target: number, rollType?: RollTypeValue|undefined) => { base: number, outcome: OutcomeValue }}
|
|
76
|
+
*/
|
|
77
|
+
function alias(dieType, testType) {
|
|
78
|
+
return (target, rollType = undefined) =>
|
|
79
|
+
rollTest(dieType, { testType, target }, rollType);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Container for all dynamically generated aliases.
|
|
84
|
+
* @type {Record<string, (target: number, rollType?: RollTypeValue|undefined) => { base: number, outcome: OutcomeValue }>}
|
|
85
|
+
*/
|
|
86
|
+
const aliases = {};
|
|
87
|
+
|
|
88
|
+
// Dynamically generate aliases for all DieTypes × TestTypes
|
|
89
|
+
for (const [dieKey, dieValue] of Object.entries(DieType)) {
|
|
90
|
+
for (const [testKey, testValue] of Object.entries(TestType)) {
|
|
91
|
+
const aliasName = `roll${dieKey}${testKey}`; // e.g., rollD20AtLeast
|
|
92
|
+
aliases[aliasName] = alias(dieValue, testValue);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Export all generated aliases
|
|
97
|
+
module.exports = {
|
|
98
|
+
rollTest,
|
|
99
|
+
...aliases,
|
|
100
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export type OutcomeValue = import("../entities/Outcome").OutcomeValue;
|
|
2
|
+
export type TestConditionsInstance = import("../entities/TestConditions").TestConditionsInstance;
|
|
3
|
+
export type Conditions = import("../entities/TestConditions").Conditions;
|
|
4
|
+
export type TestTypeValue = import("../entities/TestType").TestTypeValue;
|
|
5
|
+
export type DieTypeValue = import("../entities/DieType").DieTypeValue;
|
|
6
|
+
export type TestConditionsLike = {
|
|
7
|
+
testType: TestTypeValue;
|
|
8
|
+
dieType: DieTypeValue;
|
|
9
|
+
} & Conditions;
|
|
10
|
+
/**
|
|
11
|
+
* @typedef {import("../entities/Outcome").OutcomeValue} OutcomeValue
|
|
12
|
+
* @typedef {import("../entities/TestConditions").TestConditionsInstance} TestConditionsInstance
|
|
13
|
+
* @typedef {import("../entities/TestConditions").Conditions} Conditions
|
|
14
|
+
* @typedef {import("../entities/TestType").TestTypeValue} TestTypeValue
|
|
15
|
+
* @typedef {import("../entities/DieType").DieTypeValue} DieTypeValue
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* @private
|
|
19
|
+
* @typedef {{ testType: TestTypeValue, dieType: DieTypeValue } & Conditions} TestConditionsLike
|
|
20
|
+
*/
|
|
21
|
+
/**
|
|
22
|
+
* Determines the outcome of a roll based on provided {@link TestConditions}.
|
|
23
|
+
* Returns standard {@link Outcome} values including success, failure, and criticals.
|
|
24
|
+
*
|
|
25
|
+
* @function determineOutcome
|
|
26
|
+
* @param {number} value - The rolled (possibly modified) result.
|
|
27
|
+
* @param {TestConditionsInstance|TestConditionsLike} testConditions - The conditions defining success/failure thresholds.
|
|
28
|
+
* @returns {OutcomeValue} The resulting outcome.
|
|
29
|
+
* @throws {TypeError} If the provided conditions or test type are invalid.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* const test = new TestConditions(TestType.AtLeast, { target: 12 });
|
|
33
|
+
* const outcome = determineOutcome(14, test);
|
|
34
|
+
* console.log(outcome); // "success"
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* const skill = new TestConditions(TestType.Skill, {
|
|
38
|
+
* target: 10,
|
|
39
|
+
* critical_success: 20,
|
|
40
|
+
* critical_failure: 1
|
|
41
|
+
* });
|
|
42
|
+
* console.log(determineOutcome(1, skill)); // "critical_failure"
|
|
43
|
+
*/
|
|
44
|
+
export function determineOutcome(value: number, testConditions: TestConditionsInstance | TestConditionsLike): OutcomeValue;
|
|
45
|
+
//# sourceMappingURL=determineOutcome.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"determineOutcome.d.ts","sourceRoot":"","sources":["../../src/utils/determineOutcome.js"],"names":[],"mappings":"2BAWa,OAAO,qBAAqB,EAAE,YAAY;qCAC1C,OAAO,4BAA4B,EAAE,sBAAsB;yBAC3D,OAAO,4BAA4B,EAAE,UAAU;4BAC/C,OAAO,sBAAsB,EAAE,aAAa;2BAC5C,OAAO,qBAAqB,EAAE,YAAY;iCAK1C;IAAE,QAAQ,EAAE,aAAa,CAAC;IAAC,OAAO,EAAE,YAAY,CAAA;CAAE,GAAG,UAAU;AAV5E;;;;;;GAMG;AAEH;;;GAGG;AAEH;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wCAlBW,MAAM,kBACN,sBAAsB,GAAC,kBAAkB,GACvC,YAAY,CAgFxB"}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @platonic-dice/core/src/utils/determineOutcome
|
|
3
|
+
* @description
|
|
4
|
+
* Determines the outcome of a roll based on provided test conditions.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
function getEntities() {
|
|
8
|
+
return require("../entities");
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @typedef {import("../entities/Outcome").OutcomeValue} OutcomeValue
|
|
13
|
+
* @typedef {import("../entities/TestConditions").TestConditionsInstance} TestConditionsInstance
|
|
14
|
+
* @typedef {import("../entities/TestConditions").Conditions} Conditions
|
|
15
|
+
* @typedef {import("../entities/TestType").TestTypeValue} TestTypeValue
|
|
16
|
+
* @typedef {import("../entities/DieType").DieTypeValue} DieTypeValue
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @private
|
|
21
|
+
* @typedef {{ testType: TestTypeValue, dieType: DieTypeValue } & Conditions} TestConditionsLike
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Determines the outcome of a roll based on provided {@link TestConditions}.
|
|
26
|
+
* Returns standard {@link Outcome} values including success, failure, and criticals.
|
|
27
|
+
*
|
|
28
|
+
* @function determineOutcome
|
|
29
|
+
* @param {number} value - The rolled (possibly modified) result.
|
|
30
|
+
* @param {TestConditionsInstance|TestConditionsLike} testConditions - The conditions defining success/failure thresholds.
|
|
31
|
+
* @returns {OutcomeValue} The resulting outcome.
|
|
32
|
+
* @throws {TypeError} If the provided conditions or test type are invalid.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* const test = new TestConditions(TestType.AtLeast, { target: 12 });
|
|
36
|
+
* const outcome = determineOutcome(14, test);
|
|
37
|
+
* console.log(outcome); // "success"
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* const skill = new TestConditions(TestType.Skill, {
|
|
41
|
+
* target: 10,
|
|
42
|
+
* critical_success: 20,
|
|
43
|
+
* critical_failure: 1
|
|
44
|
+
* });
|
|
45
|
+
* console.log(determineOutcome(1, skill)); // "critical_failure"
|
|
46
|
+
*/
|
|
47
|
+
function determineOutcome(value, testConditions) {
|
|
48
|
+
const { Outcome, TestType, TestConditions } = getEntities();
|
|
49
|
+
|
|
50
|
+
if (typeof value !== "number" || Number.isNaN(value)) {
|
|
51
|
+
throw new TypeError("value must be a valid number.");
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Normalise plain object input into a TestConditions instance
|
|
55
|
+
if (!(testConditions instanceof TestConditions)) {
|
|
56
|
+
const { testType, dieType, ...rest } = /** @type {TestConditionsLike} */ (
|
|
57
|
+
testConditions
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
// Inject dieType into the conditions object to satisfy TestConditions
|
|
61
|
+
const fullConditions = { ...rest, dieType };
|
|
62
|
+
|
|
63
|
+
testConditions = new TestConditions(testType, fullConditions, dieType);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** @type {TestConditionsInstance} */
|
|
67
|
+
const { testType, conditions } = testConditions;
|
|
68
|
+
|
|
69
|
+
switch (testType) {
|
|
70
|
+
case TestType.AtLeast:
|
|
71
|
+
case TestType.AtMost:
|
|
72
|
+
case TestType.Exact: {
|
|
73
|
+
const { target } = /** @type {{ target: number }} */ (conditions);
|
|
74
|
+
if (testType === TestType.AtLeast)
|
|
75
|
+
return value >= target ? Outcome.Success : Outcome.Failure;
|
|
76
|
+
if (testType === TestType.AtMost)
|
|
77
|
+
return value <= target ? Outcome.Success : Outcome.Failure;
|
|
78
|
+
return value === target ? Outcome.Success : Outcome.Failure;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
case TestType.Within: {
|
|
82
|
+
const { min, max } = /** @type {{ min: number, max: number }} */ (
|
|
83
|
+
conditions
|
|
84
|
+
);
|
|
85
|
+
return value >= min && value <= max ? Outcome.Success : Outcome.Failure;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
case TestType.InList: {
|
|
89
|
+
const { values } = /** @type {{ values: number[] }} */ (conditions);
|
|
90
|
+
return Array.isArray(values) && values.includes(value)
|
|
91
|
+
? Outcome.Success
|
|
92
|
+
: Outcome.Failure;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
case TestType.Skill: {
|
|
96
|
+
const { target, critical_success, critical_failure } =
|
|
97
|
+
/** @type {{ target: number, critical_success?: number, critical_failure?: number }} */ (
|
|
98
|
+
conditions
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
if (critical_failure != null && value <= critical_failure)
|
|
102
|
+
return Outcome.CriticalFailure;
|
|
103
|
+
if (critical_success != null && value >= critical_success)
|
|
104
|
+
return Outcome.CriticalSuccess;
|
|
105
|
+
return value >= target ? Outcome.Success : Outcome.Failure;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
default:
|
|
109
|
+
throw new TypeError(`Unknown or unsupported testType '${testType}'.`);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
module.exports = {
|
|
114
|
+
determineOutcome,
|
|
115
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export type DieTypeValue = import("../entities/DieType").DieTypeValue;
|
|
2
|
+
/**
|
|
3
|
+
* Generates a single roll result for a given die type.
|
|
4
|
+
*
|
|
5
|
+
* @function generateResult
|
|
6
|
+
* @param {DieTypeValue} dieType - The type of die to roll (e.g., "d6", "d20").
|
|
7
|
+
* @returns {number} The result of rolling the die.
|
|
8
|
+
* @throws {TypeError} If the die type is invalid.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* const result = generateResult("d6");
|
|
12
|
+
* console.log(result); // 1..6
|
|
13
|
+
*/
|
|
14
|
+
export function generateResult(dieType: DieTypeValue): number;
|
|
15
|
+
/**
|
|
16
|
+
* @typedef {import("../entities/DieType").DieTypeValue} DieTypeValue
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* Returns the number of sides on a die given its type.
|
|
20
|
+
*
|
|
21
|
+
* @function numSides
|
|
22
|
+
* @param {DieTypeValue} dieType - The die type (e.g., "d6", "d20").
|
|
23
|
+
* @returns {number} Number of sides.
|
|
24
|
+
* @throws {TypeError} If the die type is invalid.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* console.log(numSides("d6")); // 6
|
|
28
|
+
*/
|
|
29
|
+
export function numSides(dieType: DieTypeValue): number;
|
|
30
|
+
//# sourceMappingURL=generateResult.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generateResult.d.ts","sourceRoot":"","sources":["../../src/utils/generateResult.js"],"names":[],"mappings":"2BASa,OAAO,qBAAqB,EAAE,YAAY;AAqBvD;;;;;;;;;;;GAWG;AACH,wCARW,YAAY,GACV,MAAM,CAUlB;AArCD;;GAEG;AAEH;;;;;;;;;;GAUG;AACH,kCAPW,YAAY,GACV,MAAM,CAWlB"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @platonic-dice/core/src/utils/generateResult
|
|
3
|
+
* @description
|
|
4
|
+
* Generates a single roll result for a given die type.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const { isValidDieType } = require("../entities/DieType");
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @typedef {import("../entities/DieType").DieTypeValue} DieTypeValue
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Returns the number of sides on a die given its type.
|
|
15
|
+
*
|
|
16
|
+
* @function numSides
|
|
17
|
+
* @param {DieTypeValue} dieType - The die type (e.g., "d6", "d20").
|
|
18
|
+
* @returns {number} Number of sides.
|
|
19
|
+
* @throws {TypeError} If the die type is invalid.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* console.log(numSides("d6")); // 6
|
|
23
|
+
*/
|
|
24
|
+
function numSides(dieType) {
|
|
25
|
+
if (!isValidDieType(dieType)) {
|
|
26
|
+
throw new TypeError(`Invalid die type: ${dieType}`);
|
|
27
|
+
}
|
|
28
|
+
return parseInt(dieType.slice(1));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Generates a single roll result for a given die type.
|
|
33
|
+
*
|
|
34
|
+
* @function generateResult
|
|
35
|
+
* @param {DieTypeValue} dieType - The type of die to roll (e.g., "d6", "d20").
|
|
36
|
+
* @returns {number} The result of rolling the die.
|
|
37
|
+
* @throws {TypeError} If the die type is invalid.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* const result = generateResult("d6");
|
|
41
|
+
* console.log(result); // 1..6
|
|
42
|
+
*/
|
|
43
|
+
function generateResult(dieType) {
|
|
44
|
+
const sides = numSides(dieType); // validation of dieType happens here
|
|
45
|
+
return Math.floor(Math.random() * sides) + 1;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
module.exports = {
|
|
49
|
+
generateResult,
|
|
50
|
+
numSides,
|
|
51
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.js"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @platonic-dice/core/src/utils
|
|
3
|
+
* @description
|
|
4
|
+
* Internal utility functions for core dice logic.
|
|
5
|
+
*
|
|
6
|
+
* These functions provide shared, low-level logic such as
|
|
7
|
+
* generating random die results, computing number of sides,
|
|
8
|
+
* and evaluating roll outcomes.
|
|
9
|
+
*
|
|
10
|
+
* They are used internally by the `@platonic-dice/core/src` roll functions
|
|
11
|
+
* and entity validation routines.
|
|
12
|
+
*
|
|
13
|
+
* @private
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* // Internal usage only — not part of the public API
|
|
17
|
+
* import { generateDieResult, determineOutcome } from "../utils";
|
|
18
|
+
*/
|
|
19
|
+
const { determineOutcome } = require("./determineOutcome.js");
|
|
20
|
+
const { generateResult, numSides } = require("./generateResult.js");
|
|
21
|
+
|
|
22
|
+
module.exports = {
|
|
23
|
+
determineOutcome,
|
|
24
|
+
generateResult,
|
|
25
|
+
numSides,
|
|
26
|
+
};
|
package/dist-types.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// Convenience type re-exports for consumers. This file points at the generated
|
|
2
|
+
// declaration files in `dist/entities` so TypeScript users can import type
|
|
3
|
+
// aliases directly from the package root via the `types` field.
|
|
4
|
+
|
|
5
|
+
export type {
|
|
6
|
+
RollModifierFunction,
|
|
7
|
+
RollModifierInstance,
|
|
8
|
+
} from "./dist/entities/RollModifier";
|
|
9
|
+
export type { OutcomeValue } from "./dist/entities/Outcome";
|
|
10
|
+
export type { TestConditionsInstance } from "./dist/entities/TestConditions";
|
|
11
|
+
export type { RollTypeValue } from "./dist/entities/RollType";
|
|
12
|
+
export type { TestTypeValue } from "./dist/entities/TestType";
|
|
13
|
+
export type { DieTypeValue } from "./dist/entities/DieType";
|
|
14
|
+
|
|
15
|
+
// Re-export other commonly used entity types
|
|
16
|
+
export * from "./dist/entities/index";
|
|
17
|
+
|
|
18
|
+
// Re-export top-level runtime APIs (roll helpers) so consumers can import
|
|
19
|
+
// functions like `roll`, `rollMod`, and `rollTest` from the package root.
|
|
20
|
+
export * from "./dist/roll";
|
|
21
|
+
export * from "./dist/rollMod";
|
|
22
|
+
export * from "./dist/rollDice";
|
|
23
|
+
export * from "./dist/rollDiceMod";
|
|
24
|
+
export * from "./dist/rollTest";
|
|
25
|
+
|
|
26
|
+
// The `rollMod`/`rollTest` d.ts files are emitted using a CommonJS `export =`
|
|
27
|
+
// shape which doesn't always expose named exports for consumers under some
|
|
28
|
+
// moduleResolution strategies. Provide explicit ESM-style type declarations
|
|
29
|
+
// here so TypeScript imports like `import { rollMod } from '@platonic-dice/core'`
|
|
30
|
+
// resolve correctly.
|
|
31
|
+
export function rollMod(
|
|
32
|
+
dieType: import("./dist/entities/DieType").DieTypeValue,
|
|
33
|
+
modifier:
|
|
34
|
+
| import("./dist/entities/RollModifier").RollModifierFunction
|
|
35
|
+
| import("./dist/entities/RollModifier").RollModifierInstance,
|
|
36
|
+
rollType?: import("./dist/entities/RollType").RollTypeValue
|
|
37
|
+
): { base: number; modified: number };
|
|
38
|
+
|
|
39
|
+
export function rollTest(
|
|
40
|
+
dieType: import("./dist/entities/DieType").DieTypeValue,
|
|
41
|
+
testConditions:
|
|
42
|
+
| import("./dist/entities/TestConditions").TestConditionsInstance
|
|
43
|
+
| {
|
|
44
|
+
testType: import("./dist/entities/TestType").TestTypeValue;
|
|
45
|
+
[k: string]: any;
|
|
46
|
+
},
|
|
47
|
+
rollType?: import("./dist/entities/RollType").RollTypeValue
|
|
48
|
+
): { base: number; outcome: import("./dist/entities/Outcome").OutcomeValue };
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@platonic-dice/core",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "Core JavaScript logic for simulating TTRPG dice rolls.",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist-types.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"require": "./dist/index.js",
|
|
10
|
+
"types": "./dist-types.d.ts"
|
|
11
|
+
},
|
|
12
|
+
"./entities": {
|
|
13
|
+
"require": "./dist/entities/index.js",
|
|
14
|
+
"types": "./dist/entities/index.d.ts"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"files": ["dist", "dist-types.d.ts"],
|
|
18
|
+
"keywords": [
|
|
19
|
+
"dice",
|
|
20
|
+
"TTRPG",
|
|
21
|
+
"random",
|
|
22
|
+
"game",
|
|
23
|
+
"d20",
|
|
24
|
+
"dnd",
|
|
25
|
+
"core"
|
|
26
|
+
],
|
|
27
|
+
"author": "Stephen James Saunders",
|
|
28
|
+
"license": "MIT",
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "https://github.com/sjs2k20/platonic-dice.git"
|
|
32
|
+
},
|
|
33
|
+
"bugs": {
|
|
34
|
+
"url": "https://github.com/sjs2k20/platonic-dice.git/issues"
|
|
35
|
+
},
|
|
36
|
+
"homepage": "https://github.com/sjs2k20/platonic-dice.git#readme",
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "rm -rf dist && mkdir -p dist && cp -R src/* dist/ && tsc -p tsconfig.json --emitDeclarationOnly",
|
|
39
|
+
"prepublishOnly": "npm run build",
|
|
40
|
+
"test": "jest",
|
|
41
|
+
"test:types": "tsd"
|
|
42
|
+
},
|
|
43
|
+
"tsd": {
|
|
44
|
+
"directory": "src/test-d"
|
|
45
|
+
}
|
|
46
|
+
}
|