Skip to content

Commit

Permalink
Support "no roll" moves in "make a move" (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
cwegrzyn authored May 24, 2024
1 parent 926e091 commit 4f2cc33
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/mechanics/node-builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export function generateMechanicsNode(move: MoveDescription): Document {
}),
);
} else {
throw new Error("what kind of move is this?");
// Nothing to do for a no-roll move
}

const doc: Document = [
Expand Down
6 changes: 6 additions & 0 deletions src/moves/action/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { vaultProcess } from "../../utils/obsidian";
import { CustomSuggestModal } from "../../utils/suggest";
import {
ActionMoveAdd,
NoRollMoveDescription,
type ActionMoveDescription,
type MoveDescription,
type ProgressMoveDescription,
Expand Down Expand Up @@ -235,6 +236,11 @@ export async function runMoveCommand(
break;
}
case "no_roll":
moveDescription = {
id: move._id,
name: move.name,
} satisfies NoRollMoveDescription;
break;
case "special_track":
default:
// TODO: this probably makes sense with new mechanics format?
Expand Down
12 changes: 10 additions & 2 deletions src/moves/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Datastore } from "datastore";
import { MarkdownRenderChild, MarkdownRenderer, type App } from "obsidian";
import ForgedPlugin from "../index";
import {
NoRollMoveDescription,
moveIsAction,
moveIsProgress,
type ActionMoveDescription,
Expand Down Expand Up @@ -88,7 +89,9 @@ class MoveMarkdownRenderChild extends MarkdownRenderChild {
}
}

labelForResult<T extends MoveDescription>(wrap: MoveWrapper<T>): string {
labelForResult<T extends ActionMoveDescription | ProgressMoveDescription>(
wrap: MoveWrapper<T>,
): string {
switch (wrap.result()) {
case RollResult.Miss:
return "Miss" + (wrap.isMatch() ? " with Match" : "");
Expand All @@ -104,8 +107,9 @@ class MoveMarkdownRenderChild extends MarkdownRenderChild {
return this.actionTemplate(move);
} else if (moveIsProgress(move)) {
return this.progressTemplate(move);
} else {
return this.noRollTemplate(move);
}
throw new Error("What kind of bizarre move is this?");
}

actionTemplate(move: ActionMoveDescription): string {
Expand Down Expand Up @@ -139,6 +143,10 @@ class MoveMarkdownRenderChild extends MarkdownRenderChild {
>`;
}

noRollTemplate(move: NoRollMoveDescription): string {
return `> [!challenge-strong] ${move.name}\n> \n`;
}

async onload(): Promise<void> {
await MarkdownRenderer.render(
this.app,
Expand Down
6 changes: 3 additions & 3 deletions src/moves/desc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export type ProgressMoveDescription = z.infer<
>;

export const NoRollMoveDescriptionSchema = z.object({
id: z.string(),
id: z.string().optional(),
name: z.string(),
});

Expand All @@ -65,15 +65,15 @@ export const AllMoveDescriptionSchemas = z.union([
ActionMoveDescriptionSchemaV2,
ProgressMoveDescriptionSchema,
ActionMoveDescriptionSchemaV1,
// NoRollMoveDescriptionSchema,
NoRollMoveDescriptionSchema,
]);

export type AllMoveDescriptions = z.infer<typeof AllMoveDescriptionSchemas>;

export const MoveDescriptionSchema = z.union([
ActionMoveDescriptionSchemaV2,
ProgressMoveDescriptionSchema,
// NoRollMoveDescriptionSchema,
NoRollMoveDescriptionSchema,
]);

export type MoveDescription = z.infer<typeof MoveDescriptionSchema>;
Expand Down
24 changes: 11 additions & 13 deletions src/moves/move-modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,17 @@ export class MoveModal extends Modal {
contentEl.empty();
contentEl.toggleClass("forged-modal-content", true);
(async () => {
if (move.roll_type !== "no_roll") {
new ButtonComponent(contentEl)
.setButtonText("Roll this move")
.onClick(() => {
const { workspace } = this.plugin.app;
const view = workspace.getActiveFileView();
if (view && view instanceof MarkdownView) {
const editor = view.editor;
runMoveCommand(this.plugin, editor, view, move);
this.close();
}
});
}
new ButtonComponent(contentEl)
.setButtonText("Make this move")
.onClick(() => {
const { workspace } = this.plugin.app;
const view = workspace.getActiveFileView();
if (view && view instanceof MarkdownView) {
const editor = view.editor;
runMoveCommand(this.plugin, editor, view, move);
this.close();
}
});
await MarkdownRenderer.render(
this.app,
move.text,
Expand Down
17 changes: 12 additions & 5 deletions src/moves/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
moveIsAction,
moveIsProgress,
type ActionMoveDescription,
type MoveDescription,
type ProgressMoveDescription,
} from "./desc";

Expand Down Expand Up @@ -32,9 +31,15 @@ export enum MoveKind {
Progress,
}

export function wrapMove<T extends MoveDescription>(
move: T,
): MoveWrapper<MoveDescription> {
export function wrapMove<ActionMoveDescription>(
move: ActionMoveDescription,
): ActionMoveWrapper;
export function wrapMove<ProgressMoveDescription>(
move: ProgressMoveDescription,
): ProgressMoveWrapper;
export function wrapMove<
T extends ActionMoveDescription | ProgressMoveDescription,
>(move: T): ActionMoveWrapper | ProgressMoveWrapper {
if (moveIsAction(move)) {
return new ActionMoveWrapper(move);
} else if (moveIsProgress(move)) {
Expand All @@ -44,7 +49,9 @@ export function wrapMove<T extends MoveDescription>(
}
}

export abstract class MoveWrapper<T extends MoveDescription> {
export abstract class MoveWrapper<
T extends ActionMoveDescription | ProgressMoveDescription,
> {
public readonly move: T;

public constructor(move: T) {
Expand Down

0 comments on commit 4f2cc33

Please sign in to comment.