@swrpg-online/dice 0.0.0-development

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/dist/cli.js ADDED
@@ -0,0 +1,168 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.main = exports.formatResult = void 0;
5
+ exports.parseDiceNotation = parseDiceNotation;
6
+ const dice_1 = require("./dice");
7
+ // import * as path from 'path';
8
+ function parseDiceNotation(input) {
9
+ const pool = {
10
+ boostDice: 0,
11
+ abilityDice: 0,
12
+ proficiencyDice: 0,
13
+ setBackDice: 0,
14
+ difficultyDice: 0,
15
+ challengeDice: 0,
16
+ forceDice: 0,
17
+ };
18
+ const warnings = [];
19
+ const parts = input
20
+ .toLowerCase()
21
+ .trim()
22
+ .split(" ")
23
+ .filter((p) => p.length > 0);
24
+ for (const part of parts) {
25
+ const count = parseInt(part);
26
+ // Check if parseInt returned NaN
27
+ if (isNaN(count)) {
28
+ warnings.push(`Invalid dice notation: "${part}" - number not found`);
29
+ continue;
30
+ }
31
+ // Check for non-integer values
32
+ if (part.includes(".") || part.includes(",")) {
33
+ warnings.push(`Invalid dice notation: "${part}" - dice count must be a whole number`);
34
+ continue;
35
+ }
36
+ const color = part.slice(String(count).length).toLowerCase();
37
+ // Skip if no color specified
38
+ if (!color) {
39
+ warnings.push(`Invalid dice notation: "${part}" - no dice color specified`);
40
+ continue;
41
+ }
42
+ let recognized = true;
43
+ switch (color) {
44
+ // y/pro = Yellow / Proficiency
45
+ case "y":
46
+ pool.proficiencyDice = count;
47
+ break;
48
+ case "pro":
49
+ pool.proficiencyDice = count;
50
+ break;
51
+ // g/a = Green / Ability
52
+ case "g":
53
+ pool.abilityDice = count;
54
+ break;
55
+ case "a":
56
+ pool.abilityDice = count;
57
+ break;
58
+ // b/boo = Blue / Boost
59
+ case "b":
60
+ pool.boostDice = count;
61
+ break;
62
+ case "boo":
63
+ pool.boostDice = count;
64
+ break;
65
+ // r/c = Red / Challenge
66
+ case "r":
67
+ pool.challengeDice = count;
68
+ break;
69
+ case "c":
70
+ pool.challengeDice = count;
71
+ break;
72
+ // p/diff = Purple / Difficulty
73
+ case "p":
74
+ pool.difficultyDice = count;
75
+ break;
76
+ case "diff":
77
+ pool.difficultyDice = count;
78
+ break;
79
+ // blk/k/sb/s = Black / Setback
80
+ case "blk":
81
+ pool.setBackDice = count;
82
+ break;
83
+ case "k":
84
+ pool.setBackDice = count;
85
+ break;
86
+ case "sb":
87
+ pool.setBackDice = count;
88
+ break;
89
+ case "s":
90
+ pool.setBackDice = count;
91
+ break;
92
+ // w/f = White / Force
93
+ case "w":
94
+ pool.forceDice = count;
95
+ break;
96
+ case "f":
97
+ pool.forceDice = count;
98
+ break;
99
+ default:
100
+ recognized = false;
101
+ warnings.push(`Invalid dice color: "${color}" in "${part}"`);
102
+ }
103
+ }
104
+ // Print warnings to stderr
105
+ if (warnings.length > 0) {
106
+ warnings.forEach((warning) => console.error(`Warning: ${warning}`));
107
+ }
108
+ return pool;
109
+ }
110
+ const formatResult = (result) => {
111
+ const effects = [];
112
+ if (result.summary.successes > 0)
113
+ effects.push(`${result.summary.successes} Success(es)`);
114
+ if (result.summary.failures > 0)
115
+ effects.push(`${result.summary.failures} Failure(s)`);
116
+ if (result.summary.advantages > 0)
117
+ effects.push(`${result.summary.advantages} Advantage(s)`);
118
+ if (result.summary.threats > 0)
119
+ effects.push(`${result.summary.threats} Threat(s)`);
120
+ if (result.summary.triumphs > 0)
121
+ effects.push(`${result.summary.triumphs} Triumph(s)`);
122
+ if (result.summary.despair > 0)
123
+ effects.push(`${result.summary.despair} Despair(s)`);
124
+ const resultText = effects.length > 0 ? effects.join(", ") : "No effects";
125
+ if (result.summary.hints && result.summary.hints.length > 0) {
126
+ return `${resultText}\n\nPossible actions:\n${result.summary.hints.map((hint) => " • " + hint).join("\n")}`;
127
+ }
128
+ return resultText;
129
+ };
130
+ exports.formatResult = formatResult;
131
+ const main = () => {
132
+ const args = process.argv.slice(2);
133
+ const hintsIndex = args.indexOf("--hints");
134
+ const showHints = hintsIndex !== -1;
135
+ const diceNotation = hintsIndex !== -1
136
+ ? args.filter((_, index) => index !== hintsIndex).join(" ")
137
+ : args.join(" ");
138
+ if (!diceNotation.trim()) {
139
+ console.log(`Usage: swrpg-dice <dice-notation> <dice-options>
140
+ Example: swrpg-dice 2y 1g 1p 1b 1sb --hints
141
+ Dice Options:
142
+ - y/pro = Yellow / Proficiency
143
+ - g/a = Green / Ability
144
+ - b/boo = Blue / Boost
145
+ - r/c = Red / Challenge
146
+ - p/diff = Purple / Difficulty
147
+ - blk/k/sb/s = Black / Setback
148
+ - w/f = White/Force
149
+ Options:
150
+ --hints Show possible actions based on roll results`);
151
+ process.exit(1);
152
+ }
153
+ const pool = parseDiceNotation(diceNotation);
154
+ // Check if the pool is empty (all zeros) which might indicate invalid input
155
+ const hasAnyDice = Object.values(pool).some((count) => count > 0);
156
+ if (!hasAnyDice) {
157
+ console.error("\nError: No valid dice found in the notation.");
158
+ console.error("Please check your input and ensure it follows the format: <count><color>");
159
+ console.error("Example: '2y 1g' for 2 yellow and 1 green dice\n");
160
+ process.exit(1);
161
+ }
162
+ const result = (0, dice_1.roll)(pool, { hints: showHints });
163
+ console.log((0, exports.formatResult)(result));
164
+ };
165
+ exports.main = main;
166
+ if (require.main === module) {
167
+ (0, exports.main)();
168
+ }
package/dist/dice.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ import { DicePool, RollResult, RollOptions } from "./types";
2
+ export declare const DEFAULT_MAX_DICE_PER_TYPE = 100;
3
+ export declare const DEFAULT_MAX_TOTAL_DICE = 500;
4
+ /**
5
+ * Rolls a dice pool and returns the results.
6
+ *
7
+ * @param pool - The dice pool to roll
8
+ * @param options - Optional roll configuration including dice limits
9
+ * @returns The roll results with detailed die information and summary
10
+ * @throws {Error} If dice counts exceed configured limits
11
+ *
12
+ * Default limits:
13
+ * - Max dice per type: 100 (configurable via options.maxDicePerType)
14
+ * - Max total dice: 500 (configurable via options.maxTotalDice)
15
+ */
16
+ export declare const roll: (pool: DicePool, options?: RollOptions) => RollResult;