Skip to content

Commit

Permalink
Split up the graphic dice settings for oracle and action action dice
Browse files Browse the repository at this point in the history
  • Loading branch information
bloodynine committed Jul 17, 2024
1 parent 7e5aea4 commit cc5be03
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 23 deletions.
7 changes: 5 additions & 2 deletions src/datastore/parsers/datasworn/oracles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,13 @@ export class DataswornOracle implements Oracle {
],
this.plugin,
);
const res = await group.roll();
const res = await group.roll(this.plugin.settings.graphicalOracleDice);
return this.evaluate(context, res[0].value, res[1].value, cursed.id);
}
return this.evaluate(context, await this.dice.roll());
return this.evaluate(
context,
await this.dice.roll(this.plugin?.settings.graphicalOracleDice ?? true),
);
}

async evaluate(
Expand Down
2 changes: 1 addition & 1 deletion src/moves/action/action-modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export async function rerollDie(
: dieName === "vs1"
? DieKind.Challenge1
: DieKind.Challenge2,
).roll());
).roll(plugin.settings.graphicalActionDice));
}
const props: { action?: string; vs1?: string; vs2?: string } = {};
props[dieName] = newValue;
Expand Down
4 changes: 2 additions & 2 deletions src/moves/action/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ async function processActionMove(
new Dice(1, 10, plugin, DieKind.Challenge2),
],
plugin,
).roll();
).roll(plugin.settings.graphicalActionDice);
roll = {
action: res[0].value,
challenge1: res[1].value,
Expand Down Expand Up @@ -282,7 +282,7 @@ async function processProgressMove(
new Dice(1, 10, plugin, DieKind.Challenge2),
],
plugin,
).roll();
).roll(plugin.settings.graphicalActionDice);
roll = {
challenge1: res[0].value,
challenge2: res[1].value,
Expand Down
2 changes: 1 addition & 1 deletion src/oracles/modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class OracleRollerModal extends Modal {
this.plugin.settings.cursedDieSides,
this.plugin,
DieKind.Cursed,
).roll();
).roll(this.plugin.settings.graphicalOracleDice);
await setRoll(this.currentRoll.withCursedRoll(newCursedRoll));
await onUpdateCursedRoll();
}),
Expand Down
3 changes: 2 additions & 1 deletion src/settings/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import Emittery from "emittery";

export class IronVaultPluginSettings {
graphicalDice: boolean = true;
graphicalOracleDice: boolean = true;
graphicalActionDice: boolean = true;
actionDieColor: string = "#8f8f8f";
challengeDie1Color: string = "#8b5cf5";
challengeDie2Color: string = "#8b5cf5";
Expand Down
25 changes: 21 additions & 4 deletions src/settings/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,29 @@ export class IronVaultSettingTab extends PluginSettingTab {
);

new Setting(containerEl)
.setName("Graphical dice")
.setDesc("If enabled, dice rolls will use on-screen 3d graphical dice.")
.setName("Graphical oracle dice")
.setDesc(
"If enabled, dice rolls will use on-screen 3d graphical dice when making oracle rolls.",
)
.addToggle((toggle) => {
toggle
.setValue(settings.graphicalDice)
.onChange((value) => this.updateSetting("graphicalDice", value));
.setValue(settings.graphicalOracleDice)
.onChange((value) =>
this.updateSetting("graphicalOracleDice", value),
);
});

new Setting(containerEl)
.setName("Graphical action dice")
.setDesc(
"If enabled, dice rolls will use on-screen 3d graphical dice when making action and progress rolls.",
)
.addToggle((toggle) => {
toggle
.setValue(settings.graphicalActionDice)
.onChange((value) =>
this.updateSetting("graphicalActionDice", value),
);
});

new Setting(containerEl)
Expand Down
4 changes: 2 additions & 2 deletions src/truths/truth-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ async function pickRandomSubOption(
plugin: IronVaultPlugin,
) {
const dice = Dice.fromDiceString(table.dice, plugin, DieKind.Oracle);
const res = await dice.roll();
const res = await dice.roll(plugin.settings.graphicalOracleDice);
return table.rows.findIndex(
(row) => row.roll!.min <= res && res <= row.roll!.max,
);
Expand All @@ -306,7 +306,7 @@ async function pickRandomOption(truth: Truth, plugin: IronVaultPlugin) {
if (options.every((option) => option.roll != null)) {
// Do a dice roll
const die = Dice.fromDiceString(truth.dice, plugin, DieKind.Oracle);
const res = await die.roll();
const res = await die.roll(plugin.settings.graphicalOracleDice);
return options.find((opt) => opt.roll.min <= res && res <= opt.roll.max);
} else {
return options[Math.floor(Math.random() * options.length)];
Expand Down
9 changes: 7 additions & 2 deletions src/utils/dice-group.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import IronVaultPlugin from "index";
import { Dice } from "./dice";
import { Dice, randomInt } from "./dice";
import { RollResult } from "@3d-dice/dice-box";

export class DiceGroup {
Expand All @@ -8,7 +8,12 @@ export class DiceGroup {
private plugin: IronVaultPlugin,
) {}

async roll(): Promise<RollResult[]> {
async roll(displayDice: boolean): Promise<RollResult[]> {
if (!displayDice) {
return this.dice.map((d) => {
return { value: randomInt(d.minRoll(), d.maxRoll()) } as RollResult;
});
}
return await this.plugin.diceOverlay.roll(
this.dice.map((d) => ({
qty: d.count,
Expand Down
5 changes: 1 addition & 4 deletions src/utils/dice-overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ export class DiceOverlay {
container?.classList.toggle("active", true);
const roll = await this.diceBox.roll(dice);
const listener = (ev: KeyboardEvent | MouseEvent) => {
if (
(ev instanceof KeyboardEvent && ev.key === "Escape") ||
ev instanceof MouseEvent
) {
if (ev instanceof KeyboardEvent || ev instanceof MouseEvent) {
container?.classList.toggle("active", false);
this.diceBox.clear();
container?.removeEventListener("click", listener);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/dice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ it("parses and rolls", async () => {
const dice = Dice.fromDiceString("3d4");
expect(dice.count).toBe(3);
expect(dice.sides).toBe(4);
expect(await dice.roll()).toBeGreaterThanOrEqual(3);
expect(await dice.roll(false)).toBeGreaterThanOrEqual(3);
expect(dice.maxRoll()).toBe(12);
});

Expand Down
6 changes: 3 additions & 3 deletions src/utils/dice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ export class Dice {
public readonly kind?: DieKind,
) {}

async roll(): Promise<number> {
if (this.plugin?.settings.graphicalDice) {
const res = await this.plugin.diceOverlay.roll({
async roll(displayDice: boolean): Promise<number> {
if (displayDice) {
const res = await this.plugin!.diceOverlay.roll({
qty: this.count,
sides: this.sides,
themeColor: this.themeColor,
Expand Down

0 comments on commit cc5be03

Please sign in to comment.