Refactor for better readability (addressed CR comments)

This commit is contained in:
Johannes Loher 2021-03-29 21:20:08 +02:00
parent 30aed22c03
commit e545e3f030
5 changed files with 80 additions and 49 deletions

View file

@ -197,8 +197,8 @@
"DS4.ErrorUnexpectedAttackType": "Unerwartete Angriffsart '{actualType}', erwartete Angriffarten: {expectedTypes}",
"DS4.ErrorCanvasIsNotInitialized": "Canvas ist noch nicht initialisiert.",
"DS4.WarningItemMustBeEquippedToBeRolled": "Um für das Item '{name}' ({id}) vom Typ '{type}' zu würfeln, muss es ausgerüstet sein.",
"DS4.WarningMustControlActorToUseRollItemMacro": "Um ein ein Item Würfel Makro zu nutzen muss ein Aktor kontolliert werden.",
"DS4.WarningMustControlActorToUseRollCheckMacro": "Um ein ein Proben Würfel Makro zu nutzen muss ein Aktor kontolliert werden.",
"DS4.WarningMustControlActorToUseRollItemMacro": "Um ein ein Item-Würfel-Makro zu nutzen muss ein Aktor kontrolliert werden.",
"DS4.WarningMustControlActorToUseRollCheckMacro": "Um ein ein Proben-Würfel-Makro zu nutzen muss ein Aktor kontrolliert werden.",
"DS4.WarningControlledActorDoesNotHaveItem": "Der kontrollierte Aktor '{actorName}' ({actorId}) hat kein Item mit der ID '{itemId}'.",
"DS4.WarningItemIsNotRollable": "Für das Item '{name}' ({id}) vom Typ '{type}' kann nicht gewürfelt werden.",
"DS4.WarningMacrosCanOnlyBeCreatedForOwnedItems": "Makros können nur für besessene Items angelegt werden.",

View file

@ -116,13 +116,13 @@ export class DS4Actor extends Actor<DS4ActorData, DS4Item, DS4ActorPreparedData>
* The list of properties that are derived from others, given in dot notation.
*/
get derivedDataProperties(): Array<string> {
return Object.keys(DS4.i18n.combatValues)
.map((combatValue) => `data.combatValues.${combatValue}.total`)
.concat(
Object.keys(DS4.i18n.checks)
.filter((check) => check !== "defend")
.map((check) => `data.checks.${check}`),
);
const combatValueProperties = Object.keys(DS4.i18n.combatValues).map(
(combatValue) => `data.combatValues.${combatValue}.total`,
);
const checkProperties = Object.keys(DS4.i18n.checks)
.filter((check) => check !== "defend")
.map((check) => `data.checks.${check}`);
return combatValueProperties.concat(checkProperties);
}
/**

View file

@ -0,0 +1,21 @@
import { DS4Actor } from "../actor/actor";
import { getCanvas } from "../helpers";
/**
* Gets the currently active actor based on how {@link ChatMessage} determines
* the current speaker.
* @returns The currently active {@link DS4Actor} if any, and `undefined` otherwise.
*/
export function getActiveActor(): DS4Actor | undefined {
const speaker = ChatMessage.getSpeaker();
const speakerToken = speaker.token ? getCanvas().tokens.get(speaker.token) : undefined;
if (speakerToken) {
return speakerToken.actor as DS4Actor;
}
const speakerActor = speaker.actor ? game.actors?.get(speaker.actor) : undefined;
if (speakerActor) {
return speakerActor as DS4Actor;
}
}

View file

@ -1,8 +1,7 @@
import { DS4Actor } from "../actor/actor";
import { Check } from "../actor/actor-prepared-data";
import { DS4 } from "../config";
import { getCanvas } from "../helpers";
import notifications from "../ui/notifications";
import { getActiveActor } from "./helpers";
/**
* Creates a macro from a check drop.
@ -11,33 +10,40 @@ import notifications from "../ui/notifications";
* @param slot - The hotbar slot to use.
*/
export async function createRollCheckMacro(check: Check, slot: string): Promise<void> {
const command = `game.ds4.macros.rollCheck("${check}");`;
const macro =
game.macros?.entities.find((m) => m.name === DS4.i18n.checks[check] && m.data.command === command) ??
(await Macro.create(
{
command,
name: DS4.i18n.checks[check],
type: "script",
// TODO: img, should be addressed in https://git.f3l.de/dungeonslayers/ds4/-/issues/79
flags: { "ds4.checkMacro": true },
},
{ displaySheet: false },
));
const macro = await getOrCreateRollCheckMacro(check);
game.user?.assignHotbarMacro(macro, slot);
}
async function getOrCreateRollCheckMacro(check: Check): Promise<Macro | null> {
const command = `game.ds4.macros.rollCheck("${check}");`;
const existingMacro = game.macros?.entities.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",
// TODO: img, should be addressed in https://git.f3l.de/dungeonslayers/ds4/-/issues/79
flags: { "ds4.checkMacro": true },
},
{ displaySheet: false },
);
}
/**
* Executes the roll check macro for the given check.
*/
export async function rollCheck(check: Check): Promise<void> {
const speaker = ChatMessage.getSpeaker();
const actor = (getCanvas().tokens.get(speaker.token ?? "")?.actor ?? game.actors?.get(speaker.actor ?? "")) as
| DS4Actor
| null
| undefined;
const actor = getActiveActor();
if (!actor) {
return notifications.warn(game.i18n.localize("DS4.WarningMustControlActorToUseRollCheckMacro"));
}
return actor.rollCheck(check);
}

View file

@ -1,7 +1,6 @@
import { DS4Actor } from "../actor/actor";
import { getCanvas } from "../helpers";
import { DS4ItemData } from "../item/item-data";
import notifications from "../ui/notifications";
import { getActiveActor } from "./helpers";
/**
* Creates a macro from an item drop.
@ -10,34 +9,39 @@ import notifications from "../ui/notifications";
* @param slot - The hotbar slot to use
*/
export async function createRollItemMacro(itemData: DS4ItemData, slot: string): Promise<void> {
const command = `game.ds4.macros.rollItem("${itemData._id}");`;
const macro =
game.macros?.entities.find((m) => m.name === itemData.name && m.data.command === command) ??
(await Macro.create(
{
command,
name: itemData.name,
type: "script",
img: itemData.img,
flags: { "ds4.itemMacro": true },
},
{ displaySheet: false },
));
const macro = await getOrCreateRollItemMacro(itemData);
game.user?.assignHotbarMacro(macro, slot);
}
async function getOrCreateRollItemMacro(itemData: DS4ItemData): Promise<Macro | null> {
const command = `game.ds4.macros.rollItem("${itemData._id}");`;
const existingMacro = game.macros?.entities.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 },
},
{ displaySheet: false },
);
}
/**
* Executes the roll item macro for the given itemId.
*/
export async function rollItem(itemId: string): Promise<void> {
const speaker = ChatMessage.getSpeaker();
const actor = (getCanvas().tokens.get(speaker.token ?? "")?.actor ?? game.actors?.get(speaker.actor ?? "")) as
| DS4Actor
| null
| undefined;
const actor = getActiveActor();
if (!actor) {
return notifications.warn(game.i18n.localize("DS4.WarningMustControlActorToUseRollItemMacro"));
}
const item = actor.items?.get(itemId);
if (!item) {
return notifications.warn(