Skip to content

Commit

Permalink
Move action context to own file
Browse files Browse the repository at this point in the history
  • Loading branch information
cwegrzyn committed May 16, 2024
1 parent ce894b8 commit 1819b85
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 99 deletions.
96 changes: 96 additions & 0 deletions src/characters/action-context.ts
Original file line number Diff line number Diff line change
@@ -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<ActionContext | undefined> {
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);
}
}
104 changes: 6 additions & 98 deletions src/moves/action/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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";
Expand Down Expand Up @@ -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<ActionContext | undefined> {
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,
Expand Down
2 changes: 1 addition & 1 deletion src/rules/ruleset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 1819b85

Please sign in to comment.