Skip to content

Commit

Permalink
Merge branch 'main' into cwegrzyn/issue46-Make-entity-folders-configu…
Browse files Browse the repository at this point in the history
…rable
  • Loading branch information
cwegrzyn authored May 24, 2024
2 parents 943ba32 + 088836b commit 03f0fde
Show file tree
Hide file tree
Showing 24 changed files with 359 additions and 141 deletions.
4 changes: 2 additions & 2 deletions esbuild.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { typecheckPlugin } from "@jgoz/esbuild-plugin-typecheck";
import builtins from "builtin-modules";
import esbuild from "esbuild";
import process from "process";
Expand All @@ -10,8 +11,6 @@ if you want to view the source, please visit the github repository of this plugi

const prod = process.argv[2] === "production";

const ASSETS = ["styles.css", "manifest.json"];

const context = await esbuild.context({
banner: {
js: banner,
Expand Down Expand Up @@ -40,6 +39,7 @@ const context = await esbuild.context({
sourcemap: prod ? false : "inline",
treeShaking: true,
outfile: prod ? "main.js" : "test-vault/.obsidian/plugins/forged/main.js",
plugins: [typecheckPlugin({ watch: !prod })],
});

const cssCtx = await esbuild.context({
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "forged",
"name": "Forged Plugin",
"version": "1.18.0",
"version": "1.19.0",
"minAppVersion": "0.15.0",
"description": "Ironsworn/Starforged gameplay plugin",
"author": "Chris Wegrzyn",
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "forged",
"version": "1.18.0",
"version": "1.19.0",
"description": "",
"main": "main.js",
"type": "module",
Expand All @@ -24,6 +24,7 @@
"@codemirror/view": "^6.26.3",
"@datasworn/core": "0.0.10",
"@datasworn/starforged": "0.0.10",
"@jgoz/esbuild-plugin-typecheck": "^4.0.0",
"@nrsk/sigma": "^3.8.0",
"@popperjs/core": "^2.11.8",
"@swc/core": "^1.5.7",
Expand Down
18 changes: 18 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion src/characters/lens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,6 @@ describe("Special Tracks", () => {
).toMatchObject({
Quests_Progress: 6,
Quests_XPEarned: 2,
Quests_TrackImage: "[[track-6]]",
});
});

Expand Down
2 changes: 0 additions & 2 deletions src/characters/lens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,10 @@ function camelCase(str: string): string {

function legacyTrack(specialTrackRule: SpecialTrackRule) {
const formattedLabel = camelCase(specialTrackRule.label);
const trackImageKey = `${formattedLabel}_TrackImage`;
const progressKey = `${formattedLabel}_Progress`;
const xpEarnedKey = `${formattedLabel}_XPEarned`;
return {
schema: {
[trackImageKey]: z.string().optional(),
[progressKey]: z.number().int().nonnegative(),
[xpEarnedKey]: z.number().int().nonnegative(),
},
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { OracleModal } from "oracles/oracle-modal";
import { MoveModal } from "moves/move-modal";
import { ViewPlugin, ViewUpdate } from "@codemirror/view";
import registerTrackBlock from "tracks/track-block";
import registerClockBlock from "tracks/clock-block";

export default class ForgedPlugin extends Plugin {
settings!: ForgedPluginSettings;
Expand Down Expand Up @@ -220,6 +221,7 @@ export default class ForgedPlugin extends Plugin {
registerOracleBlock(this, this.datastore);
registerMechanicsBlock(this);
registerTrackBlock(this);
registerClockBlock(this);
}

async activateView() {
Expand Down
65 changes: 65 additions & 0 deletions src/mechanics/node-builders.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as kdl from "kdljs";
import { Document, Node } from "kdljs";
import { RollWrapper } from "model/rolls";
import { MoveDescription, moveIsAction, moveIsProgress } from "moves/desc";
import { oracleNameWithParents } from "oracles/render";
import { Clock } from "tracks/clock";
import { ClockFileAdapter } from "tracks/clock-file";
Expand Down Expand Up @@ -51,3 +53,66 @@ export function createOracleNode(roll: RollWrapper, prompt?: string): kdl.Node {
],
});
}
export function generateMechanicsNode(move: MoveDescription): Document {
const children: Node[] = [];
if (moveIsAction(move)) {
const adds = (move.adds ?? []).reduce((acc, { amount }) => acc + amount, 0);

// Add "add" nodes for each non-zero add
children.push(
...(move.adds ?? [])
.filter(({ amount }) => amount != 0)
.map(({ amount, desc }) =>
node("add", { values: [amount, ...(desc ? [desc] : [])] }),
),
);

// Main roll node
children.push(
node("roll", {
values: [move.stat],
properties: {
action: move.action,
stat: move.statVal,
adds,
vs1: move.challenge1,
vs2: move.challenge2,
},
}),
);

// Momentum burn
if (move.burn) {
children.push(
node("burn", {
properties: { from: move.burn.orig, to: move.burn.reset },
}),
);
}
} else if (moveIsProgress(move)) {
children.push(
node("progress-roll", {
properties: {
// TODO: what about progress track id?
// TODO: use a ticks prop instead... or at least use a helper to get this
score: Math.floor(move.progressTicks / 4),
vs1: move.challenge1,
vs2: move.challenge2,
},
}),
);
} else {
throw new Error("what kind of move is this?");
}

const doc: Document = [
node("move", {
values: [generateMoveLink(move)],
children,
}),
];
return doc;
}
function generateMoveLink(move: MoveDescription): string {
return move.id ? `[${move.name}](move:${move.id})` : move.name;
}
67 changes: 2 additions & 65 deletions src/moves/action/format.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,7 @@
import { Document, Node } from "kdljs";
import { createOrAppendMechanics } from "mechanics/editor";
import { generateMechanicsNode } from "mechanics/node-builders";
import { Editor } from "obsidian";
import { node } from "../../utils/kdl";
import { MoveDescription, moveIsAction, moveIsProgress } from "../desc";

function generateMechanicsNode(move: MoveDescription): Document {
const children: Node[] = [];
if (moveIsAction(move)) {
const adds = (move.adds ?? []).reduce((acc, { amount }) => acc + amount, 0);

// Add "add" nodes for each non-zero add
children.push(
...(move.adds ?? [])
.filter(({ amount }) => amount != 0)
.map(({ amount, desc }) =>
node("add", { values: [amount, ...(desc ? [desc] : [])] }),
),
);

// Main roll node
children.push(
node("roll", {
values: [move.stat],
properties: {
action: move.action,
stat: move.statVal,
adds,
vs1: move.challenge1,
vs2: move.challenge2,
},
}),
);

// Momentum burn
if (move.burn) {
children.push(
node("burn", {
properties: { from: move.burn.orig, to: move.burn.reset },
}),
);
}
} else if (moveIsProgress(move)) {
children.push(
node("progress-roll", {
properties: {
// TODO: what about progress track id?
// TODO: use a ticks prop instead... or at least use a helper to get this
score: Math.floor(move.progressTicks / 4),
vs1: move.challenge1,
vs2: move.challenge2,
},
}),
);
} else {
throw new Error("what kind of move is this?");
}

// TODO: move name vs move id
const doc: Document = [
node("move", {
values: [move.name],
children,
}),
];
return doc;
}
import { MoveDescription } from "../desc";

export function renderMechanics(editor: Editor, move: MoveDescription): void {
createOrAppendMechanics(editor, generateMechanicsNode(move));
Expand Down
24 changes: 13 additions & 11 deletions src/moves/action/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
type MarkdownView,
} from "obsidian";
import { MeterCommon } from "rules/ruleset";
import { makeSafeForId } from "utils/strings";
import {
MeterWithLens,
MeterWithoutLens,
Expand Down Expand Up @@ -66,10 +65,11 @@ const ROLL_TYPES: Record<Datasworn.Move["roll_type"], string> = {
special_track: "Special track roll",
};

const promptForMove = async (
async function promptForMove(
app: App,
moves: Datasworn.Move[],
): Promise<Datasworn.Move> => {
context: ActionContext,
): Promise<Datasworn.Move> {
const moves = [...context.moves].sort((a, b) => a.name.localeCompare(b.name));
const choice = await CustomSuggestModal.selectWithUserEntry(
app,
moves,
Expand Down Expand Up @@ -102,7 +102,7 @@ const promptForMove = async (
const baseMove = {
roll_type,
type: "move",
_id: `adhoc/moves/custom/${makeSafeForId(choice.custom)}`,
_id: "",
name: choice.custom,
_source: {
title: "Adhoc",
Expand Down Expand Up @@ -157,14 +157,16 @@ const promptForMove = async (
},
} satisfies Datasworn.MoveSpecialTrack;
}
};
}

function processActionMove(
move: Datasworn.Move,
stat: string,
statVal: number,
adds: ActionMoveAdd[],
): ActionMoveDescription {
return {
id: move._id,
name: move.name,
action: randomInt(1, 6),
stat,
Expand All @@ -180,6 +182,7 @@ function processProgressMove(
tracker: ProgressTrackWriterContext,
): ProgressMoveDescription {
return {
id: move._id,
name: move.name,
progressTrack: `[[${tracker.location}]]`,
progressTicks: tracker.track.progress,
Expand All @@ -200,6 +203,7 @@ export async function runMoveCommand(
plugin: ForgedPlugin,
editor: Editor,
view: MarkdownView,
chosenMove?: Datasworn.Move,
): Promise<void> {
if (view.file?.path == null) {
console.error("No file for view. Why?");
Expand All @@ -212,11 +216,9 @@ export async function runMoveCommand(
return;
}

const allMoves = [...context.moves].sort((a, b) =>
a.name.localeCompare(b.name),
);

const move = await promptForMove(plugin.app, allMoves);
// Use the provided move, or prompt the user for a move appropriate to the current action context.
const move: Datasworn.Move =
chosenMove ?? (await promptForMove(plugin.app, context));

let moveDescription: MoveDescription;
switch (move.roll_type) {
Expand Down
11 changes: 11 additions & 0 deletions src/moves/desc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export type ActionMoveAdd = z.output<typeof ActionMoveAddSchema>;

export const ActionMoveDescriptionSchemaV2 =
ActionMoveDescriptionSchemaV1.extend({
id: z.string().optional(),
adds: z.array(ActionMoveAddSchema).default([]),
});

Expand All @@ -41,6 +42,7 @@ export type ActionMoveDescription = ActionMoveDescriptionV2;
export type BurnDescriptor = z.infer<typeof BurnDescriptorSchema>;

const ProgressMoveDescriptionSchema = z.object({
id: z.string().optional(),
name: z.string(),
progressTrack: z.string(),
progressTicks: z.number(),
Expand All @@ -52,17 +54,26 @@ export type ProgressMoveDescription = z.infer<
typeof ProgressMoveDescriptionSchema
>;

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

export type NoRollMoveDescription = z.infer<typeof NoRollMoveDescriptionSchema>;

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

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

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

export type MoveDescription = z.infer<typeof MoveDescriptionSchema>;
Expand Down
Loading

0 comments on commit 03f0fde

Please sign in to comment.