diff --git a/src/characters/action-context.ts b/src/characters/action-context.ts new file mode 100644 index 00000000..25c62730 --- /dev/null +++ b/src/characters/action-context.ts @@ -0,0 +1,96 @@ +import { Move } from "@datasworn/core"; +import { CharacterContext } from "../character-tracker"; +import { movesReader, rollablesReader } from "../characters/lens"; +import { type Datastore } from "../datastore"; +import ForgedPlugin from "../index"; +import { MeterCommon } from "../rules/ruleset"; +import { InfoModal } from "../utils/ui/info"; + +export interface ActionContext { + readonly moves: Move[]; + readonly rollables: { + key: string; + value?: number | undefined; + definition: MeterCommon; + }[]; + readonly momentum?: number; +} + +export class NoCharacterActionConext implements ActionContext { + constructor(public readonly datastore: Datastore) {} + + get moves(): Move[] { + return this.datastore.moves; + } + + get rollables(): { + key: string; + value?: number | undefined; + definition: MeterCommon; + }[] { + return Object.entries(this.datastore.ruleset.stats).map(([key, stat]) => ({ + key, + definition: stat, + })); + } + + get momentum() { + return undefined; + } +} + +export class CharacterActionContext implements ActionContext { + constructor( + public readonly datastore: Datastore, + public readonly characterPath: string, + public readonly characterContext: CharacterContext, + ) {} + + get moves() { + const characterMoves = movesReader( + this.characterContext.lens, + this.datastore.index, + ) + .get(this.characterContext.character) + .expect("unexpected failure finding assets for moves"); + + return this.datastore.moves.concat(characterMoves); + } + + get rollables(): { key: string; value?: number; definition: MeterCommon }[] { + return rollablesReader( + this.characterContext.lens, + this.datastore.index, + ).get(this.characterContext.character); + } + + get momentum() { + return this.characterContext.lens.momentum.get( + this.characterContext.character, + ); + } +} +export async function determineCharacterActionContext( + plugin: ForgedPlugin, +): Promise { + if (plugin.settings.useCharacterSystem) { + try { + const [characterPath, characterContext] = + plugin.characters.activeCharacter(); + return new CharacterActionContext( + plugin.datastore, + characterPath, + characterContext, + ); + } catch (e) { + // TODO: probably want to show character parse errors in full glory + await InfoModal.show( + plugin.app, + `An error occurred while finding your active character.\n\n${e}`, + ); + return undefined; + } + } else { + return new NoCharacterActionConext(plugin.datastore); + } +} diff --git a/src/moves/action/index.ts b/src/moves/action/index.ts index 6b9fb3a1..b56e84e3 100644 --- a/src/moves/action/index.ts +++ b/src/moves/action/index.ts @@ -4,6 +4,11 @@ import { MoveProgressRoll, TriggerActionRollCondition, } from "@datasworn/core"; +import { + ActionContext, + CharacterActionContext, + determineCharacterActionContext, +} from "characters/action-context"; import ForgedPlugin from "index"; import { type App, @@ -12,14 +17,7 @@ import { type MarkdownView, } from "obsidian"; import { MeterCommon } from "rules/ruleset"; -import { InfoModal } from "utils/ui/info"; -import { CharacterContext } from "../../character-tracker"; -import { - momentumOps, - movesReader, - rollablesReader, -} from "../../characters/lens"; -import { type Datastore } from "../../datastore"; +import { momentumOps } from "../../characters/lens"; import { ProgressContext } from "../../tracks/context"; import { selectProgressTrack } from "../../tracks/select"; import { ProgressTrackWriterContext } from "../../tracks/writer"; @@ -112,96 +110,6 @@ export function validAdds(baseStat: number): number[] { return adds; } -interface ActionContext { - readonly moves: Move[]; - readonly rollables: { - key: string; - value?: number | undefined; - definition: MeterCommon; - }[]; - readonly momentum?: number; -} - -class NoCharacterActionConext implements ActionContext { - constructor(public readonly datastore: Datastore) {} - - get moves(): Move[] { - return this.datastore.moves; - } - - get rollables(): { - key: string; - value?: number | undefined; - definition: MeterCommon; - }[] { - return Object.entries(this.datastore.ruleset.stats).map(([key, stat]) => ({ - key, - definition: stat, - })); - } - - get momentum() { - return undefined; - } -} - -class CharacterActionContext implements ActionContext { - constructor( - public readonly datastore: Datastore, - public readonly characterPath: string, - public readonly characterContext: CharacterContext, - ) {} - - get moves() { - const characterMoves = movesReader( - this.characterContext.lens, - this.datastore.index, - ) - .get(this.characterContext.character) - .expect("unexpected failure finding assets for moves"); - - return this.datastore.moves.concat(characterMoves); - } - - get rollables(): { key: string; value?: number; definition: MeterCommon }[] { - return rollablesReader( - this.characterContext.lens, - this.datastore.index, - ).get(this.characterContext.character); - } - - get momentum() { - return this.characterContext.lens.momentum.get( - this.characterContext.character, - ); - } -} - -async function determineCharacterActionContext( - plugin: ForgedPlugin, -): Promise { - if (plugin.settings.useCharacterSystem) { - try { - const [characterPath, characterContext] = - plugin.characters.activeCharacter(); - return new CharacterActionContext( - plugin.datastore, - characterPath, - characterContext, - ); - } catch (e) { - // TODO: probably want to show character parse errors in full glory - await InfoModal.show( - plugin.app, - `An error occurred while finding your active character.\n\n${e}`, - ); - return undefined; - } - } else { - return new NoCharacterActionConext(plugin.datastore); - } -} - export async function runMoveCommand( plugin: ForgedPlugin, editor: Editor, diff --git a/src/rules/ruleset.ts b/src/rules/ruleset.ts index 8c0f37ae..da0641d7 100644 --- a/src/rules/ruleset.ts +++ b/src/rules/ruleset.ts @@ -105,7 +105,7 @@ export class Ruleset { ]), ); this.impacts = Object.fromEntries( - Object.entries(rules.impacts).flatMap(([categoryKey, source]) => { + Object.entries(rules.impacts).flatMap(([_categoryKey, source]) => { const category: ImpactCategory = { label: source.label, description: source.description,