2021-06-26 22:02:00 +02:00
|
|
|
// SPDX-FileCopyrightText: 2021 Johannes Loher
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2021-03-24 09:19:26 +01:00
|
|
|
import { DS4 } from "../config";
|
2022-11-21 03:00:46 +01:00
|
|
|
import { isCheck } from "../documents/actor/actor-data-properties-base";
|
2022-11-04 21:47:18 +01:00
|
|
|
import { notifications } from "../ui/notifications";
|
|
|
|
import { getGame } from "../utils/utils";
|
2021-09-19 20:12:01 +02:00
|
|
|
import { getActiveActorAndToken } from "./helpers";
|
2021-03-24 09:19:26 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a macro from a check drop.
|
|
|
|
* Get an existing roll check macro if one exists, otherwise create a new one.
|
2022-11-21 03:00:46 +01:00
|
|
|
* @param {object} data The check drop data
|
|
|
|
* @param {string} slot The hotbar slot to use
|
|
|
|
* @returns {Promise<void>} A promise that resolves when the macro has been created.
|
2021-03-24 09:19:26 +01:00
|
|
|
*/
|
2022-11-21 03:00:46 +01:00
|
|
|
export async function createRollCheckMacro(data, slot) {
|
2023-07-10 22:23:13 +02:00
|
|
|
if (!("data" in data) || typeof data.data !== "string" || !isCheck(data.data)) {
|
|
|
|
return notifications.warn(getGame().i18n.localize("DS4.WarningInvalidCheckDropped"));
|
|
|
|
}
|
|
|
|
const macro = await getOrCreateRollCheckMacro(data.data);
|
|
|
|
await getGame().user?.assignHotbarMacro(macro ?? null, slot);
|
2021-03-24 09:19:26 +01:00
|
|
|
}
|
|
|
|
|
2022-11-17 00:12:29 +01:00
|
|
|
/**
|
|
|
|
* @param {import("../documents/actor/actor-data-properties-base").Check} check The name of the check to perform
|
|
|
|
* @returns {Promise<Macro|undefined>} A promise that resolves to the created macro
|
|
|
|
*/
|
|
|
|
async function getOrCreateRollCheckMacro(check) {
|
2023-07-10 22:23:13 +02:00
|
|
|
const command = `game.ds4.macros.rollCheck("${check}");`;
|
2021-03-29 21:20:08 +02:00
|
|
|
|
2023-07-10 22:23:13 +02:00
|
|
|
const existingMacro = getGame().macros?.find((m) => m.name === DS4.i18n.checks[check] && m.command === command);
|
|
|
|
if (existingMacro) {
|
|
|
|
return existingMacro;
|
|
|
|
}
|
2021-03-29 21:20:08 +02:00
|
|
|
|
2023-07-10 22:23:13 +02:00
|
|
|
return Macro.create(
|
|
|
|
{
|
|
|
|
command,
|
|
|
|
name: DS4.i18n.checks[check],
|
|
|
|
type: "script",
|
|
|
|
img: DS4.icons.checks[check],
|
|
|
|
flags: { "ds4.checkMacro": true },
|
|
|
|
},
|
|
|
|
{ renderSheet: false },
|
|
|
|
);
|
2021-03-29 21:20:08 +02:00
|
|
|
}
|
|
|
|
|
2021-03-24 09:19:26 +01:00
|
|
|
/**
|
|
|
|
* Executes the roll check macro for the given check.
|
2022-11-17 00:12:29 +01:00
|
|
|
* @param {import("../documents/actor/actor-data-properties-base").Check} check The name of the check to perform
|
|
|
|
* @returns {Promise<void>} A promise that resolves once the check has been performed.
|
2021-03-24 09:19:26 +01:00
|
|
|
*/
|
2022-11-17 00:12:29 +01:00
|
|
|
export async function rollCheck(check) {
|
2023-07-10 22:23:13 +02:00
|
|
|
const { actor, token } = getActiveActorAndToken();
|
|
|
|
if (!actor) {
|
|
|
|
return notifications.warn(getGame().i18n.localize("DS4.WarningMustControlActorToUseRollCheckMacro"));
|
|
|
|
}
|
2021-03-29 21:20:08 +02:00
|
|
|
|
2023-07-10 22:23:13 +02:00
|
|
|
return actor.rollCheck(check, { speaker: { token } }).catch((e) => notifications.error(e, { log: true }));
|
2021-03-24 09:19:26 +01:00
|
|
|
}
|