ds4/src/module/macros/roll-check.ts

55 lines
1.7 KiB
TypeScript
Raw Normal View History

2021-06-26 22:02:00 +02:00
// SPDX-FileCopyrightText: 2021 Johannes Loher
//
// SPDX-License-Identifier: MIT
2021-06-30 02:17:54 +02:00
import { Check } from "../actor/actor-data-properties";
import { DS4 } from "../config";
2021-07-07 19:22:35 +02:00
import { getGame } from "../helpers";
import notifications from "../ui/notifications";
import { getActiveActor } from "./helpers";
/**
* Creates a macro from a check drop.
* Get an existing roll check macro if one exists, otherwise create a new one.
* @param check - The name of the check to perform.
* @param slot - The hotbar slot to use.
*/
export async function createRollCheckMacro(check: Check, slot: string): Promise<void> {
const macro = await getOrCreateRollCheckMacro(check);
2021-07-07 19:22:35 +02:00
getGame().user?.assignHotbarMacro(macro ?? null, slot);
}
2021-06-30 04:32:10 +02:00
async function getOrCreateRollCheckMacro(check: Check): Promise<Macro | undefined> {
2021-07-08 08:00:58 +02:00
const command = `game.ds4.macros.rollCheck("${check}");`;
2021-07-07 19:22:35 +02:00
const existingMacro = getGame().macros?.find(
(m) => m.name === DS4.i18n.checks[check] && m.data.command === command,
);
if (existingMacro) {
return existingMacro;
}
return Macro.create(
{
command,
name: DS4.i18n.checks[check],
type: "script",
2021-04-15 20:00:37 +02:00
img: DS4.icons.checks[check],
flags: { "ds4.checkMacro": true },
},
2021-06-30 04:32:10 +02:00
{ renderSheet: false },
);
}
/**
* Executes the roll check macro for the given check.
*/
export async function rollCheck(check: Check): Promise<void> {
const actor = getActiveActor();
if (!actor) {
2021-07-07 19:22:35 +02:00
return notifications.warn(getGame().i18n.localize("DS4.WarningMustControlActorToUseRollCheckMacro"));
}
return actor.rollCheck(check).catch((e) => notifications.error(e, { log: true }));
}