-
Notifications
You must be signed in to change notification settings - Fork 389
/
Copy pathsingle-check.ts
240 lines (211 loc) · 9.13 KB
/
single-check.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import { ActorPF2e } from "@actor";
import { ModifierPF2e, RawModifier, StatisticModifier } from "@actor/modifiers.ts";
import { DCSlug } from "@actor/types.ts";
import { SAVE_TYPES } from "@actor/values.ts";
import type { ItemPF2e } from "@item";
import { TokenPF2e } from "@module/canvas/index.ts";
import { RollNotePF2e, RollNoteSource } from "@module/notes.ts";
import { ActionMacroHelpers } from "@system/action-macros/index.ts";
import {
ActionGlyph,
CheckContextData,
CheckContextOptions,
CheckMacroContext,
CheckResultCallback,
} from "@system/action-macros/types.ts";
import { CheckDC } from "@system/degree-of-success.ts";
import { getActionGlyph, isObject, tupleHasValue } from "@util";
import { BaseAction, BaseActionData, BaseActionVariant, BaseActionVariantData } from "./base.ts";
import { ActionUseOptions } from "./types.ts";
import * as R from "remeda";
type SingleCheckActionRollNoteData = Omit<RollNoteSource, "selector"> & { selector?: string };
function toRollNoteSource(data: SingleCheckActionRollNoteData): RollNoteSource {
data.selector ??= "";
return data as RollNoteSource;
}
function isValidDifficultyClass(dc: unknown): dc is CheckDC | DCSlug {
if (isObject<{ value: unknown }>(dc) && typeof dc.value === "number") {
return true;
}
const slug = String(dc);
return (
["ac", "armor", "perception"].includes(slug) || tupleHasValue(SAVE_TYPES, slug) || slug in CONFIG.PF2E.skills
);
}
interface SingleCheckActionVariantData extends BaseActionVariantData {
difficultyClass?: CheckDC | DCSlug;
modifiers?: RawModifier[];
notes?: SingleCheckActionRollNoteData[];
rollOptions?: string[];
statistic?: string | string[];
}
interface SingleCheckActionData extends BaseActionData<SingleCheckActionVariantData> {
difficultyClass?: CheckDC | DCSlug;
modifiers?: RawModifier[];
notes?: SingleCheckActionRollNoteData[];
rollOptions?: string[];
statistic: string | string[];
}
interface ActionVariantCheckPreviewOptions {
actor: ActorPF2e;
}
interface ActionCheckPreviewOptions extends ActionVariantCheckPreviewOptions {
variant: string;
}
interface ActionCheckPreview {
label: string;
modifier?: number;
slug: string;
}
interface SingleCheckActionUseOptions extends ActionUseOptions {
difficultyClass: CheckDC | DCSlug | number;
modifiers: ModifierPF2e[];
multipleAttackPenalty: number;
notes: SingleCheckActionRollNoteData[];
rollOptions: string[];
statistic: string;
}
class SingleCheckActionVariant extends BaseActionVariant {
readonly #action: SingleCheckAction;
readonly #difficultyClass?: CheckDC | DCSlug;
readonly #modifiers?: RawModifier[];
readonly #notes?: RollNoteSource[];
readonly #rollOptions?: string[];
readonly #statistic?: string | string[];
constructor(action: SingleCheckAction, data?: SingleCheckActionVariantData) {
super(action, data);
this.#action = action;
if (data) {
this.#difficultyClass = data.difficultyClass;
this.#modifiers = data?.modifiers;
this.#notes = data.notes ? data.notes.map(toRollNoteSource) : undefined;
this.#rollOptions = data.rollOptions;
this.#statistic = data.statistic;
}
}
get difficultyClass(): CheckDC | DCSlug | undefined {
return this.#difficultyClass ?? this.#action.difficultyClass;
}
get modifiers(): RawModifier[] {
return this.#modifiers ?? this.#action.modifiers;
}
get notes(): RollNoteSource[] {
return this.#notes ?? this.#action.notes;
}
get rollOptions(): string[] {
return this.#rollOptions ?? this.#action.rollOptions;
}
get statistic(): string | string[] {
return this.#statistic ?? this.#action.statistic;
}
preview(options: Partial<ActionVariantCheckPreviewOptions> = {}): ActionCheckPreview[] {
const slugs = this.#statistic || this.#action.statistic;
const candidates = Array.isArray(slugs) ? slugs : [slugs];
// TODO: append relevant statistic replacements from the actor
return candidates
.map((candidate) =>
this.toActionCheckPreview({ actor: options.actor, rollOptions: this.rollOptions, slug: candidate }),
)
.filter((preview): preview is ActionCheckPreview => !!preview);
}
override async use(options: Partial<SingleCheckActionUseOptions> = {}): Promise<CheckResultCallback[]> {
const modifiers = this.modifiers.map((raw) => new ModifierPF2e(raw)).concat(options.modifiers ?? []);
if (options.multipleAttackPenalty) {
const map = options.multipleAttackPenalty;
const modifier = map > 0 ? Math.min(2, map) * -5 : map;
modifiers.push(new ModifierPF2e({ label: "PF2E.MultipleAttackPenalty", modifier }));
}
const notes = (this.notes as SingleCheckActionRollNoteData[])
.concat(options.notes ?? [])
.map(toRollNoteSource)
.map((note) => new RollNotePF2e(note));
const rollOptions = this.rollOptions.concat(options.rollOptions ?? []);
const slug = options.statistic?.trim() || (Array.isArray(this.statistic) ? this.statistic[0] : this.statistic);
const title = this.name
? `${game.i18n.localize(this.#action.name)} - ${game.i18n.localize(this.name)}`
: game.i18n.localize(this.#action.name);
const difficultyClass = Number.isNumeric(options.difficultyClass)
? { value: Number(options.difficultyClass) }
: isValidDifficultyClass(options.difficultyClass)
? options.difficultyClass
: this.difficultyClass;
const results: CheckResultCallback[] = [];
await ActionMacroHelpers.simpleRollActionCheck({
actors: options.actors,
title,
actionGlyph: getActionGlyph(this.cost ?? null) as ActionGlyph,
callback: (result) => results.push(result),
checkContext: (opts) => this.checkContext(opts, { modifiers, rollOptions, slug }),
createMessage: options.message?.create,
difficultyClass,
event: options.event,
extraNotes: (selector) =>
notes.map((note) => {
note.selector ||= selector; // treat empty selectors as always applicable to this check
return note;
}),
target: () => {
if (options.target instanceof ActorPF2e) {
return { token: null, actor: options.target };
} else if (options.target instanceof TokenPF2e) {
return options.target.actor
? { token: options.target.document, actor: options.target.actor }
: null;
}
return null;
},
traits: R.unique(options?.traits ?? this.traits),
});
return results;
}
protected checkContext<ItemType extends ItemPF2e<ActorPF2e>>(
opts: CheckContextOptions<ItemType>,
data: CheckContextData<ItemType>,
): CheckMacroContext<ItemType> | undefined {
return ActionMacroHelpers.defaultCheckContext(opts, data);
}
protected toActionCheckPreview(args: {
actor?: ActorPF2e;
rollOptions: string[];
slug: string;
}): ActionCheckPreview | null {
if (args.actor) {
const statistic = args.actor.getStatistic(args.slug);
if (statistic) {
const modifiers = [
...statistic.modifiers,
...this.modifiers.map((modifier) => new ModifierPF2e(modifier)),
];
const modifier = new StatisticModifier(args.slug, modifiers, args.rollOptions);
return { label: statistic.label, modifier: modifier.totalModifier, slug: args.slug };
}
} else {
const label = ActionMacroHelpers.getSimpleCheckLabel(args.slug) ?? game.i18n.localize(args.slug);
return { label, slug: args.slug };
}
return null;
}
}
class SingleCheckAction extends BaseAction<SingleCheckActionVariantData, SingleCheckActionVariant> {
readonly difficultyClass?: CheckDC | DCSlug;
readonly modifiers: RawModifier[];
readonly notes: RollNoteSource[];
readonly rollOptions: string[];
readonly statistic: string | string[];
constructor(data: SingleCheckActionData) {
super(data);
this.difficultyClass = data.difficultyClass;
this.modifiers = data.modifiers ?? [];
this.notes = (data.notes ?? []).map(toRollNoteSource);
this.rollOptions = data.rollOptions ?? [];
this.statistic = data.statistic;
}
preview(options: Partial<ActionCheckPreviewOptions> = {}): ActionCheckPreview[] {
return this.getDefaultVariant(options).preview(options);
}
protected override toActionVariant(data?: SingleCheckActionVariantData): SingleCheckActionVariant {
return new SingleCheckActionVariant(this, data);
}
}
export { SingleCheckAction, SingleCheckActionVariant };
export type { ActionCheckPreview, SingleCheckActionUseOptions, SingleCheckActionVariantData };