Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Haxxer committed Oct 3, 2023
1 parent 50efad5 commit 6bddb05
Show file tree
Hide file tree
Showing 7 changed files with 434 additions and 432 deletions.
6 changes: 3 additions & 3 deletions languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@
},
"EnableGroupAttacks": {
"Title": "Enable Minion Group Attacks",
"Hint": "When enabled minion type creature attacks prompt how many of them are attacking the target."
"Hint": "When enabled, minion group attacks automatically prompt to ask how many minions are attacking the target."
},
"EnableGroupAttackBonus": {
"Title": "Enable Minion Group Attack Bonus",
"Hint": "When enabled, if minions attack they get a bonus to hit equal to the number of attacking minions."
"Hint": "When enabled, minions get a bonus to their group attack rolls equal to the number of attacking minions."
},
"EnableMinionSuperSave": {
"Title": "Enable Minion Super Saves",
"Hint": "When enabled, if minions are hit by a feature that prompts a save for half damage, if the minions save, they instead take no damage."
"Hint": "When enabled, minions that are forced to make a saving throw, they instead take no damage if they would take half when they succeed on their saving throw."
},
"MinionFeatureName": {
"Title": "Minion Feature Name",
Expand Down
178 changes: 89 additions & 89 deletions scripts/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,66 @@ import * as lib from "./lib.js";
import { getSetting } from "./lib.js";

export function setGroupInitiative(targets, groupNumber = 1) {
if (!Array.isArray(targets)) targets = [targets]
targets = targets.map(target => target?.document ?? target);
if (!targets.some(target => target instanceof TokenDocument)) {
lib.error(`Minion Manager | setGroupInitiative | Targets you provided were not tokens`);
return false;
}
if(groupNumber < 1 || groupNumber > 9) {
lib.error(`Minion Manager | setGroupInitiative | Group number must be between 1 and 9`);
return false;
}
const updates = targets.map(target => ({
_id: target.id,
[CONSTANTS.FLAGS.GROUP_NUMBER]: groupNumber
}));

const promise = canvas.scene.updateEmbeddedDocuments("Token", updates);

promise.then(() => {
ui.combat.render(true);
targets.forEach(refreshInitiativeGroupGraphics);
});

return promise;
if (!Array.isArray(targets)) targets = [targets]
targets = targets.map(target => target?.document ?? target);
if (!targets.some(target => target instanceof TokenDocument)) {
lib.error(`Minion Manager | setGroupInitiative | Targets you provided were not tokens`);
return false;
}
if (groupNumber < 1 || groupNumber > 9) {
lib.error(`Minion Manager | setGroupInitiative | Group number must be between 1 and 9`);
return false;
}
const updates = targets.map(target => ({
_id: target.id,
[CONSTANTS.FLAGS.GROUP_NUMBER]: groupNumber
}));

const promise = canvas.scene.updateEmbeddedDocuments("Token", updates);

promise.then(() => {
ui.combat.render(true);
targets.forEach(refreshInitiativeGroupGraphics);
});

return promise;
}

export function removeGroupInitiative(targets){
if (!Array.isArray(targets)) targets = [targets]
targets = targets.map(target => target?.document ?? target);
if (!targets.some(target => target instanceof TokenDocument)) {
lib.error(`Minion Manager | removeGroupInitiative | Targets you provided were not tokens`);
return false;
}
const updates = targets.map(target => ({
_id: target.id,
[CONSTANTS.FLAGS.DELETE_GROUP_NUMBER]: null
}));

const promise = canvas.scene.updateEmbeddedDocuments("Token", updates);

promise.then(() => {
ui.combat.render(true);
targets.forEach(refreshInitiativeGroupGraphics);
});

return promise;
export function removeGroupInitiative(targets) {
if (!Array.isArray(targets)) targets = [targets]
targets = targets.map(target => target?.document ?? target);
if (!targets.some(target => target instanceof TokenDocument)) {
lib.error(`Minion Manager | removeGroupInitiative | Targets you provided were not tokens`);
return false;
}
const updates = targets.map(target => ({
_id: target.id,
[CONSTANTS.FLAGS.DELETE_GROUP_NUMBER]: null
}));

const promise = canvas.scene.updateEmbeddedDocuments("Token", updates);

promise.then(() => {
ui.combat.render(true);
targets.forEach(refreshInitiativeGroupGraphics);
});

return promise;
}

export function getActors(targets) {
if (!Array.isArray(targets)) targets = [targets]
return targets.map(potentialToken => {
let actor = potentialToken?.actor ?? potentialToken;
if (typeof potentialToken === "string") {
actor = fromUuidSync(potentialToken) ?? game.actors.get(potentialToken) ?? game.actors.getName(potentialToken);
}
return actor;
}).filter(actor => actor instanceof Actor);
if (!Array.isArray(targets)) targets = [targets]
return targets.map(potentialToken => {
let actor = potentialToken?.actor ?? potentialToken;
if (typeof potentialToken === "string") {
actor = fromUuidSync(potentialToken) ?? game.actors.get(potentialToken) ?? game.actors.getName(potentialToken);
}
return actor;
}).filter(actor => actor instanceof Actor);
}

export async function turnIntoMinions(actors) {
actors = getActors(actors);
actors = getActors(actors);
const minionFeatureName = getSetting(CONSTANTS.SETTING_KEYS.MINION_FEATURE_NAME);
const minionFeatureTemplate = foundry.utils.mergeObject(
foundry.utils.deepClone(CONSTANTS.MINION_FEATURE),
Expand All @@ -76,31 +76,31 @@ export async function turnIntoMinions(actors) {
}
}
)
for (const actor of actors) {
const minionFeature = actor.items.find(item => item.name.toLowerCase() === minionFeatureName.toLowerCase());
if (!minionFeature) {
await actor.createEmbeddedDocuments("Item", minionFeatureTemplate)
}
}
if (actors.length) lib.log(`Turned ${actors.length} actors into minions.`);
for (const actor of actors) {
const minionFeature = actor.items.find(item => item.name.toLowerCase() === minionFeatureName.toLowerCase());
if (!minionFeature) {
await actor.createEmbeddedDocuments("Item", minionFeatureTemplate)
}
}
if (actors.length) lib.log(`Turned ${actors.length} actors into minions.`);
}

export async function revertMinions(actors) {
actors = getActors(actors);
actors = getActors(actors);
const minionFeatureName = getSetting(CONSTANTS.SETTING_KEYS.MINION_FEATURE_NAME).toLowerCase();
for (const actor of actors) {
const minionFeature = actor.items.find(item => item.name.toLowerCase() === minionFeatureName);
if (minionFeature) {
await minionFeature.delete();
}
}
if (actors.length) lib.log(`Reverted ${actors.length} actors from being minions.`);
for (const actor of actors) {
const minionFeature = actor.items.find(item => item.name.toLowerCase() === minionFeatureName);
if (minionFeature) {
await minionFeature.delete();
}
}
if (actors.length) lib.log(`Reverted ${actors.length} actors from being minions.`);
}

export function isMinion(target) {
target = target?.actor ?? target;
target = target?.actor ?? target;
const minionFeatureName = getSetting(CONSTANTS.SETTING_KEYS.MINION_FEATURE_NAME).toLowerCase();
return target.items.some(item => item.name.toLowerCase() === minionFeatureName);
return target.items.some(item => item.name.toLowerCase() === minionFeatureName);
}

export function setActorItemToGroupAttack(item, bool = false) {
Expand All @@ -114,27 +114,27 @@ export function isItemGroupAttack(item) {
}

export async function turnActorAttacksIntoGroupActions(actors) {
actors = getActors(actors);
for (const actor of actors) {
await actor.updateEmbeddedDocuments("Item", actor.items.filter(item => CONSTANTS.ATTACK_TYPES.includes(item.system.actionType)).map(item => {
return {
_id: item.id,
[CONSTANTS.FLAGS.MIDI_GROUP_ATTACK]: true
}
}));
}
if (actors.length) lib.log(`Turned ${actors.length} actors' attacks into group actions.`);
actors = getActors(actors);
for (const actor of actors) {
await actor.updateEmbeddedDocuments("Item", actor.items.filter(item => CONSTANTS.ATTACK_TYPES.includes(item.system.actionType)).map(item => {
return {
_id: item.id,
[CONSTANTS.FLAGS.MIDI_GROUP_ATTACK]: true
}
}));
}
if (actors.length) lib.log(`Turned ${actors.length} actors' attacks into group actions.`);
}

export async function revertActorAttacksFromGroupActions(actors) {
actors = getActors(actors);
for (const actor of actors) {
await actor.updateEmbeddedDocuments("Item", actor.items.filter(item => CONSTANTS.ATTACK_TYPES.includes(item.system.actionType)).map(item => {
return {
_id: item.id,
[CONSTANTS.FLAGS.MIDI_GROUP_ATTACK]: false
}
}));
}
if (actors.length) lib.log(`Reverted ${actors.length} actors' attacks from group actions.`);
actors = getActors(actors);
for (const actor of actors) {
await actor.updateEmbeddedDocuments("Item", actor.items.filter(item => CONSTANTS.ATTACK_TYPES.includes(item.system.actionType)).map(item => {
return {
_id: item.id,
[CONSTANTS.FLAGS.MIDI_GROUP_ATTACK]: false
}
}));
}
if (actors.length) lib.log(`Reverted ${actors.length} actors' attacks from group actions.`);
}
Loading

0 comments on commit 6bddb05

Please sign in to comment.