Skip to content

Commit 3fd3bb3

Browse files
jboehm77ebullient
authored andcommitted
🐛 Make singular dice roll formats consistent with source material
1 parent fddd621 commit 3fd3bb3

2 files changed

Lines changed: 18 additions & 23 deletions

File tree

src/main/java/dev/ebullient/convert/tools/JsonTextConverter.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ default String replaceWithDiceRoller(String input) {
191191
String[] alternatives = rollString.split(";");
192192
if (displayText == null && alternatives.length > 1) {
193193
for (int i = 0; i < alternatives.length; i++) {
194-
String coded = codeString(alternatives[i], formulaState, true);
194+
String coded = codeString(alternatives[i], formulaState);
195195
alternatives[i] = formatDice(alternatives[i], coded, formulaState, true, false);
196196
}
197197
displayText = String.join(" or ", alternatives);
@@ -264,12 +264,7 @@ default String diceFormula(String diceRoll, String displayText, boolean average)
264264
}
265265

266266
default String codeString(String text, DiceFormulaState formulaState) {
267-
return codeString(text, formulaState, false);
268-
}
269-
270-
default String codeString(String text, DiceFormulaState formulaState, boolean alternative) {
271267
if (text.matches("^1?d\\d+$")) {
272-
text = alternative ? text : text.replace("1d", "d");
273268
return formulaState.plainText() ? text : "`%s`".formatted(text);
274269
}
275270
text = text.replace("1d20", "");
@@ -292,8 +287,8 @@ default String simplifyFormattedDiceText(String text) {
292287
if (text.contains("reach levels")) {
293288
// don't look for averages here. This is spell progression
294289
} else if (text.matches("^`dice:1?d\\d+\\|.*?` \\(`1?d\\d+`\\)")) {
295-
text = text.replaceAll("^`dice:1?d(\\d+)\\|.*",
296-
"`dice:1d$1|noform|noparens|avg|text(d$1)`");
290+
text = text.replaceAll("^`dice:(1)?d(\\d+)\\|.*",
291+
"`dice:1d$2|noform|noparens|avg|text($1d$2)`");
297292
} else {
298293
// otherwise look for average rolls
299294
// 7 (`dice:1d6+4|noform|avg` (`1d6 + 4`)) --> `dice:1d6+4|noform|avg|text(7)` (`1d6 + 4`)

src/test/java/dev/ebullient/convert/tools/dnd5e/TextReplacementTest.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public void testDiceFormla() {
142142
"with extended display text (`+2`) and display text (`+2`)",
143143
"a special 'hit' version which assumes a d20 is to be rolled `+7`",
144144
"There's also `1d12+3` and `-4`",
145-
"scaledamage: `d6`, scaledice: extra amount (`d6`)",
145+
"scaledamage: `1d6`, scaledice: extra amount (`1d6`)",
146146
"<span title='Strength'>`20` (`+5`)</span>, <span title='Strength'>`+5`</span>, and [Animal Handling](rules/skills.md#Animal%20Handling) (`+5`)",
147147
"with an Intelligence of `+3` (`16`), a Wisdom of `+0` (`10`), and a Charisma of `+4` (`18`)",
148148
"`+3 plus PB` to hit; *Hit:* 7 (`1d6 + 4`) piercing damage plus 7 (`2d6`) poison damage.",
@@ -165,7 +165,7 @@ public void testDiceFormla() {
165165
"with extended `dice:1d20+2|noform|noparens|avg|text(display text)` (`+2`) and `dice:1d20+2|noform|noparens|avg|text(display text)` (`+2`)",
166166
"a special 'hit' version which assumes a d20 is to be rolled `dice:1d20+7|noform|noparens|text(+7)`",
167167
"There's also `dice:1d12+3|noform|noparens|avg` (`1d12+3`) and `dice:1d20-4|noform|noparens|text(-4)`",
168-
"scaledamage: `dice:1d6|noform|noparens|avg|text(d6)`, scaledice: `dice:1d6|noform|noparens|avg|text(extra amount)` (`d6`)",
168+
"scaledamage: `dice:1d6|noform|noparens|avg|text(1d6)`, scaledice: `dice:1d6|noform|noparens|avg|text(extra amount)` (`1d6`)",
169169
"<span title='Strength'>`20` (`dice:d20+5|noform|noparens|text(+5)`)</span>, <span title='Strength'>`dice:d20+5|noform|noparens|text(+5)`</span>, and [Animal Handling](rules/skills.md#Animal%20Handling) (`dice:1d20+5|noform|noparens|text(+5)`)",
170170
"with an Intelligence of `dice:1d20+3|noform|noparens|text(+3)` (`16`), a Wisdom of `dice:1d20+0|noform|noparens|text(+0)` (`10`), and a Charisma of `dice:1d20+4|noform|noparens|text(+4)` (`18`)",
171171
"`+3 plus PB` to hit; *Hit:* `dice:1d6+4|noform|noparens|avg|text(7)` (`1d6 + 4`) piercing damage plus `dice:2d6|noform|noparens|avg|text(7)` (`2d6`) poison damage.",
@@ -195,7 +195,7 @@ public void testDiceFormla() {
195195
"with extended display text (+2) and display text (+2)",
196196
"a special 'hit' version which assumes a d20 is to be rolled +7",
197197
"There's also 1d12+3 and -4",
198-
"scaledamage: d6, scaledice: extra amount (d6)",
198+
"scaledamage: 1d6, scaledice: extra amount (1d6)",
199199
"<span title='Strength'>20 (+5)</span>, <span title='Strength'>+5</span>, and [Animal Handling](rules/skills.md#Animal%20Handling) (+5)",
200200
"with an Intelligence of +3 (16), a Wisdom of +0 (10), and a Charisma of +4 (18)",
201201
"+3 plus PB to hit; *Hit:* 7 (1d6 + 4) piercing damage plus 7 (2d6) poison damage.",
@@ -250,25 +250,25 @@ void testPlainD20() {
250250
String tag20 = "{@d20}";
251251

252252
assertThat(this.replaceText(d20)).isEqualTo("`d20`");
253-
assertThat(this.replaceText(oneD20)).isEqualTo("`d20`");
253+
assertThat(this.replaceText(oneD20)).isEqualTo("`1d20`");
254254
assertThat(this.replaceText(tag20)).isEqualTo("`d20`");
255255

256256
configurator.setUseDiceRoller(DiceRoller.enabled);
257257

258258
assertThat(this.replaceText(d20)).isEqualTo("`dice:1d20|noform|noparens|avg|text(d20)`");
259-
assertThat(this.replaceText(oneD20)).isEqualTo("`dice:1d20|noform|noparens|avg|text(d20)`");
259+
assertThat(this.replaceText(oneD20)).isEqualTo("`dice:1d20|noform|noparens|avg|text(1d20)`");
260260
assertThat(this.replaceText(tag20)).isEqualTo("`dice:1d20|noform|noparens|avg|text(d20)`");
261261

262262
configurator.setUseDiceRoller(DiceRoller.enabledUsingFS);
263263

264264
assertThat(this.replaceText(d20)).isEqualTo("`dice:1d20|noform|noparens|avg|text(d20)`");
265-
assertThat(this.replaceText(oneD20)).isEqualTo("`dice:1d20|noform|noparens|avg|text(d20)`");
265+
assertThat(this.replaceText(oneD20)).isEqualTo("`dice:1d20|noform|noparens|avg|text(1d20)`");
266266
assertThat(this.replaceText(tag20)).isEqualTo("`dice:1d20|noform|noparens|avg|text(d20)`");
267267

268268
boolean pushed = parseState().pushMarkdownTable(true);
269269
try {
270270
assertThat(this.replaceText(d20)).isEqualTo("`dice:1d20\\|noform\\|noparens\\|avg\\|text(d20)`");
271-
assertThat(this.replaceText(oneD20)).isEqualTo("`dice:1d20\\|noform\\|noparens\\|avg\\|text(d20)`");
271+
assertThat(this.replaceText(oneD20)).isEqualTo("`dice:1d20\\|noform\\|noparens\\|avg\\|text(1d20)`");
272272
assertThat(this.replaceText(tag20)).isEqualTo("`dice:1d20\\|noform\\|noparens\\|avg\\|text(d20)`");
273273
} finally {
274274
parseState().pop(pushed);
@@ -277,13 +277,13 @@ void testPlainD20() {
277277
pushed = parseState().pushTrait();
278278
try {
279279
assertThat(this.replaceText(d20)).isEqualTo("d20");
280-
assertThat(this.replaceText(oneD20)).isEqualTo("d20");
280+
assertThat(this.replaceText(oneD20)).isEqualTo("1d20");
281281
assertThat(this.replaceText(tag20)).isEqualTo("d20");
282282

283283
configurator.setUseDiceRoller(DiceRoller.disabledUsingFS);
284284

285285
assertThat(this.replaceText(d20)).isEqualTo("d20");
286-
assertThat(this.replaceText(oneD20)).isEqualTo("d20");
286+
assertThat(this.replaceText(oneD20)).isEqualTo("1d20");
287287
assertThat(this.replaceText(tag20)).isEqualTo("d20");
288288
} finally {
289289
parseState().pop(pushed);
@@ -298,35 +298,35 @@ void testPlainD12() {
298298
String oneD12 = "{@dice 1d12}";
299299

300300
assertThat(this.replaceText(d12)).isEqualTo("`d12`");
301-
assertThat(this.replaceText(oneD12)).isEqualTo("`d12`");
301+
assertThat(this.replaceText(oneD12)).isEqualTo("`1d12`");
302302

303303
configurator.setUseDiceRoller(DiceRoller.enabled);
304304

305305
assertThat(this.replaceText(d12)).isEqualTo("`dice:1d12|noform|noparens|avg|text(d12)`");
306-
assertThat(this.replaceText(oneD12)).isEqualTo("`dice:1d12|noform|noparens|avg|text(d12)`");
306+
assertThat(this.replaceText(oneD12)).isEqualTo("`dice:1d12|noform|noparens|avg|text(1d12)`");
307307

308308
configurator.setUseDiceRoller(DiceRoller.enabledUsingFS);
309309

310310
assertThat(this.replaceText(d12)).isEqualTo("`dice:1d12|noform|noparens|avg|text(d12)`");
311-
assertThat(this.replaceText(oneD12)).isEqualTo("`dice:1d12|noform|noparens|avg|text(d12)`");
311+
assertThat(this.replaceText(oneD12)).isEqualTo("`dice:1d12|noform|noparens|avg|text(1d12)`");
312312

313313
boolean pushed = parseState().pushMarkdownTable(true);
314314
try {
315315
assertThat(this.replaceText(d12)).isEqualTo("`dice:1d12\\|noform\\|noparens\\|avg\\|text(d12)`");
316-
assertThat(this.replaceText(oneD12)).isEqualTo("`dice:1d12\\|noform\\|noparens\\|avg\\|text(d12)`");
316+
assertThat(this.replaceText(oneD12)).isEqualTo("`dice:1d12\\|noform\\|noparens\\|avg\\|text(1d12)`");
317317
} finally {
318318
parseState().pop(pushed);
319319
}
320320

321321
pushed = parseState().pushTrait();
322322
try {
323323
assertThat(this.replaceText(d12)).isEqualTo("d12");
324-
assertThat(this.replaceText(oneD12)).isEqualTo("d12");
324+
assertThat(this.replaceText(oneD12)).isEqualTo("1d12");
325325

326326
configurator.setUseDiceRoller(DiceRoller.disabledUsingFS);
327327

328328
assertThat(this.replaceText(d12)).isEqualTo("d12");
329-
assertThat(this.replaceText(oneD12)).isEqualTo("d12");
329+
assertThat(this.replaceText(oneD12)).isEqualTo("1d12");
330330
} finally {
331331
parseState().pop(pushed);
332332
}

0 commit comments

Comments
 (0)