Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include asset meters in the take/suffer meter lists #7

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/characters/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
AssetConditionMeterControlField,
AssetControlField,
AssetOptionField,
ConditionMeterField,
} from "@datasworn/core";
import { produce } from "immer";
import { ConditionMeterDefinition } from "rules/ruleset";
import { DataIndex } from "../datastore/data-index";
import { Either, Left, Right } from "../utils/either";
import { reader, writer } from "../utils/lens";
import {
CharLens,
CharReader,
CharWriter,
CharacterLens,
Expand Down Expand Up @@ -111,7 +114,7 @@
return [...baseControls, ...abilityOptions];
}

export function samePath(left: Pathed<any>, right: Pathed<any>): boolean {

Check failure on line 117 in src/characters/assets.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check failure on line 117 in src/characters/assets.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
if (left.path == right.path) return true;
if (left.path.length != right.path.length) return false;

Expand All @@ -122,7 +125,7 @@
return true;
}

export function getPathLabel(pathed: Pathed<any>): string {

Check failure on line 128 in src/characters/assets.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
// Skip first element (which is asset/ability id)
return pathed.path.slice(1).join("/");
}
Expand All @@ -136,7 +139,7 @@

export function updateAssetWithOptions(
asset: Asset,
options: Record<string, any>,

Check failure on line 142 in src/characters/assets.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
): Asset {
return produce(asset, (draft) => {
for (const pathed of traverseAssetOptions(
Expand Down Expand Up @@ -179,3 +182,64 @@
]);
});
}

export class MissingAssetError extends Error {}

export function assetMeters(
charLens: CharacterLens,
asset: Asset,
markedAbilities: number[],
): {
key: string;
definition: ConditionMeterDefinition;
lens: CharLens<number>;
}[] {
const meters = traverseAssetControls(
asset,
markedAbilities ?? defaultMarkedAbilitiesForAsset(asset),
).filter(
(pathed): pathed is Pathed<ConditionMeterField> =>
pathed.value.field_type == "condition_meter",
);

return meters.map((pathed) => {
const { value: control } = pathed;
const key = getPathLabel(pathed);

return {
key,
definition: {
kind: "condition_meter",
label: control.label,
min: control.min,
max: control.max,
rollable: control.rollable,
},
lens: {
get(source) {
const assets = charLens.assets.get(source);
const thisAsset = assets.find(({ id }) => id === asset.id);
if (!thisAsset) {
// should probably use a lens type that has concept of errors
throw new MissingAssetError(`expected asset with id ${asset.id}`);
}
// TODO: should this raise an error if not a number?
return typeof thisAsset.controls[key] === "number"
? (thisAsset.controls[key] as number)
: control.value;
},
update(source, newval) {
const assets = charLens.assets.get(source);
const thisAsset = assets.find(({ id }) => id === asset.id);
if (!thisAsset) {
// should probably use a lens type that has concept of errors
throw new MissingAssetError(`expected asset with id ${asset.id}`);
}
if (thisAsset.controls[key] === newval) return source;
thisAsset.controls[key] = newval;
return charLens.assets.update(source, assets);
},
},
};
});
}
50 changes: 38 additions & 12 deletions src/characters/lens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ import {
reader,
updating,
} from "../utils/lens";
import { AssetError, assetWithDefnReader } from "./assets";
import {
AssetError,
assetMeters,
assetWithDefnReader,
defaultMarkedAbilitiesForAsset,
} from "./assets";

const ValidationTag: unique symbol = Symbol("validated ruleset");

Expand Down Expand Up @@ -306,7 +311,7 @@ export function movesReader(
return reader((source) => {
return collectEither(assetReader.get(source)).map((assets) =>
assets.flatMap(({ asset, defn }) => {
const moveList = [];
const moveList: Move[] = [];
const marked_abilities = asset.marked_abilities ?? [];
for (const [idx, ability] of defn.abilities.entries()) {
if (marked_abilities.includes(idx + 1)) {
Expand Down Expand Up @@ -337,21 +342,41 @@ export function conditionMetersReader(

export function meterLenses(
charLens: CharacterLens,
character: ValidatedCharacter,
dataIndex: DataIndex,
): Record<
string,
{ key: string; definition: ConditionMeterDefinition; lens: CharLens<number> }
> {
return {
...Object.fromEntries(
Object.entries(charLens.condition_meters).map(([key, lens]) => [
const baseMeters = Object.fromEntries(
Object.entries(charLens.condition_meters).map(([key, lens]) => [
key,
{
key,
{
key,
lens,
definition: charLens.ruleset.condition_meters[key],
},
]),
),
lens,
definition: charLens.ruleset.condition_meters[key],
},
]),
);
const allAssetMeters = assetWithDefnReader(charLens, dataIndex)
.get(character)
.flatMap((assetResult) => {
if (assetResult.isLeft()) {
// TODO: should we handle this error differently? pass it up?
console.warn("Missing asset: %o", assetResult.error);
return [];
} else {
const { asset, defn } = assetResult.value;
return assetMeters(
charLens,
defn,
asset.marked_abilities ?? defaultMarkedAbilitiesForAsset(defn),
);
}
})
.map((val) => [val.key, val]);
return {
...baseMeters,
momentum: {
key: "momentum",
lens: charLens.momentum,
Expand All @@ -362,6 +387,7 @@ export function meterLenses(
rollable: true,
}),
},
...Object.fromEntries(allAssetMeters),
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/characters/meter-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export const modifyMeterCommand = async (
const { character, lens } = context;
const measure = await CustomSuggestModal.select(
plugin.app,
Object.values(meterLenses(lens))
Object.values(meterLenses(lens, character, plugin.datastore.index))
.map(({ key, definition, lens }) => ({
key,
definition,
Expand Down
Loading