refactor: resturcture files so that lincensing info can be bundled properly

This commit is contained in:
Johannes Loher 2022-01-31 15:13:32 +01:00
parent 699ba74840
commit 1aa284311f
484 changed files with 119 additions and 179 deletions

23
src/macros/helpers.ts Normal file
View file

@ -0,0 +1,23 @@
// SPDX-FileCopyrightText: 2021 Johannes Loher
//
// SPDX-License-Identifier: MIT
import { DS4Actor } from "../actor/actor";
import { getCanvas, getGame } from "../helpers";
/**
* Gets the currently active actor and token based on how {@link ChatMessage}
* determines the current speaker.
* @returns The currently active {@link DS4Actor} and {@link TokenDocument}.
*/
export function getActiveActorAndToken(): { actor?: DS4Actor; token?: TokenDocument } {
const speaker = ChatMessage.getSpeaker();
const speakerToken = speaker.token ? getCanvas().tokens?.get(speaker.token)?.document : undefined;
if (speakerToken) {
return { actor: speakerToken.actor ?? undefined, token: speakerToken };
}
const speakerActor = speaker.actor ? getGame().actors?.get(speaker.actor) : undefined;
return { actor: speakerActor };
}

13
src/macros/macros.ts Normal file
View file

@ -0,0 +1,13 @@
// SPDX-FileCopyrightText: 2021 Johannes Loher
//
// SPDX-License-Identifier: MIT
import { rollCheck } from "./roll-check";
import { rollGenericCheck } from "./roll-generic-check";
import { rollItem } from "./roll-item";
export const macros = {
rollCheck,
rollGenericCheck,
rollItem,
};

54
src/macros/roll-check.ts Normal file
View file

@ -0,0 +1,54 @@
// SPDX-FileCopyrightText: 2021 Johannes Loher
//
// SPDX-License-Identifier: MIT
import { Check } from "../actor/actor-data-properties";
import { DS4 } from "../config";
import { getGame } from "../helpers";
import notifications from "../ui/notifications";
import { getActiveActorAndToken } 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);
getGame().user?.assignHotbarMacro(macro ?? null, slot);
}
async function getOrCreateRollCheckMacro(check: Check): Promise<Macro | undefined> {
const command = `game.ds4.macros.rollCheck("${check}");`;
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",
img: DS4.icons.checks[check],
flags: { "ds4.checkMacro": true },
},
{ renderSheet: false },
);
}
/**
* Executes the roll check macro for the given check.
*/
export async function rollCheck(check: Check): Promise<void> {
const { actor, token } = getActiveActorAndToken();
if (!actor) {
return notifications.warn(getGame().i18n.localize("DS4.WarningMustControlActorToUseRollCheckMacro"));
}
return actor.rollCheck(check, { speaker: { token } }).catch((e) => notifications.error(e, { log: true }));
}

View file

@ -0,0 +1,19 @@
// SPDX-FileCopyrightText: 2021 Johannes Loher
//
// SPDX-License-Identifier: MIT
import { getGame } from "../helpers";
import notifications from "../ui/notifications";
import { getActiveActorAndToken } from "./helpers";
/**
* Executes the roll generic check macro.
*/
export async function rollGenericCheck(): Promise<void> {
const { actor, token } = getActiveActorAndToken();
if (!actor) {
return notifications.warn(getGame().i18n.localize("DS4.WarningMustControlActorToUseRollCheckMacro"));
}
return actor.rollGenericCheck({ speaker: { token } }).catch((e) => notifications.error(e, { log: true }));
}

61
src/macros/roll-item.ts Normal file
View file

@ -0,0 +1,61 @@
// SPDX-FileCopyrightText: 2021 Johannes Loher
//
// SPDX-License-Identifier: MIT
import { getGame } from "../helpers";
import notifications from "../ui/notifications";
import { getActiveActorAndToken } from "./helpers";
/**
* Creates a macro from an item drop.
* Get an existing roll item macro if one exists, otherwise create a new one.
* @param itemData - The item data
* @param slot - The hotbar slot to use
*/
export async function createRollItemMacro(itemData: foundry.data.ItemData["_source"], slot: string): Promise<void> {
const macro = await getOrCreateRollItemMacro(itemData);
getGame().user?.assignHotbarMacro(macro ?? null, slot);
}
async function getOrCreateRollItemMacro(itemData: foundry.data.ItemData["_source"]): Promise<Macro | undefined> {
const command = `game.ds4.macros.rollItem("${itemData._id}");`;
const existingMacro = getGame().macros?.find((m) => m.name === itemData.name && m.data.command === command);
if (existingMacro) {
return existingMacro;
}
return Macro.create(
{
command,
name: itemData.name,
type: "script",
img: itemData.img,
flags: { "ds4.itemMacro": true },
},
{ renderSheet: false },
);
}
/**
* Executes the roll item macro for the item associated to the given `itemId`.
*/
export async function rollItem(itemId: string): Promise<void> {
const { actor, token } = getActiveActorAndToken();
if (!actor) {
return notifications.warn(getGame().i18n.localize("DS4.WarningMustControlActorToUseRollItemMacro"));
}
const item = actor.items.get(itemId);
if (!item) {
return notifications.warn(
getGame().i18n.format("DS4.WarningControlledActorDoesNotHaveItem", {
actorName: actor.name,
actorId: actor.id,
itemId,
}),
);
}
return item.roll({ speaker: { token } }).catch((e) => notifications.error(e, { log: true }));
}