Skip to content

Commit

Permalink
redid some code for easier reading
Browse files Browse the repository at this point in the history
  • Loading branch information
ChasarooniZ committed Sep 14, 2024
1 parent acd3134 commit ce2532f
Showing 1 changed file with 46 additions and 4 deletions.
50 changes: 46 additions & 4 deletions scripts/hooks.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,63 @@
import { eldenRingNounVerbed } from "./helpers/animation/text/fromSoftwareText.js";
import { getSetting } from "./helpers/misc.js";

/**
* Handles pre-deletion actions for combat encounters.
* @param {Object} encounter - The combat encounter object.
* @param {*} _changed - Unused parameter.
* @param {*} _userid - Unused parameter.
*/
export async function preDeleteCombat(encounter, _changed, _userid) {
// Only proceed if the user is a GM
if (!game.user.isGM) return;

const xpNeeded = getSetting('from-software.noun-verbed.text');

// If xpNeeded is 0, trigger the animation and exit
if (xpNeeded === 0) {
await eldenRingNounVerbed();
return;
}

// Extract combatants from the encounter
const combatants = encounter.combatants.map(c => c?.token);
const enemyLevels = combatants.filter(t => t?.disposition === CONST.TOKEN_DISPOSITIONS.HOSTILE).map(t => t?.actor?.level ?? 0);
const hazardLevels = combatants.filter(t => t?.actor?.type === 'hazard').map(t => t?.actor?.level ?? 0);

// Calculate enemy and hazard levels
const enemyLevels = getActorLevels(combatants, t => t?.disposition === CONST.TOKEN_DISPOSITIONS.HOSTILE);
const hazardLevels = getActorLevels(combatants, t => t?.actor?.type === 'hazard');

// Get party members involved in the combat
const partyMemberIDs = game.actors?.party?.members?.map(a => a.uuid) ?? [];
const partyCombatMembers = combatants.filter(t => partyMemberIDs.includes(t?.actor?.uuid));
const partyCombatLevel = Math.round(partyCombatMembers.map(p => p?.actor?.level ?? 0).reduce((a, b) => a + b) / partyCombatMembers.length);
const xp = game.pf2e.gm.calculateXP(partyCombatLevel, partyCombatMembers.length, enemyLevels, hazardLevels, {})

// Calculate average party level
const partyCombatLevel = calculateAverageLevel(partyCombatMembers);

// Calculate XP
const xp = game.pf2e.gm.calculateXP(partyCombatLevel, partyCombatMembers.length, enemyLevels, hazardLevels, {});

// Trigger animation if conditions are met
if (getSetting('from-software.noun-verbed.enable') && xp >= xpNeeded) {
await eldenRingNounVerbed();
}
}

/**
* Extracts actor levels based on a filter condition.
* @param {Array} combatants - Array of combatants.
* @param {Function} filterCondition - Condition to filter combatants.
* @returns {Array} Array of actor levels.
*/
function getActorLevels(combatants, filterCondition) {
return combatants.filter(filterCondition).map(t => t?.actor?.level ?? 0);
}

/**
* Calculates the average level of party members.
* @param {Array} partyMembers - Array of party members.
* @returns {number} Average level rounded to the nearest integer.
*/
function calculateAverageLevel(partyMembers) {
const totalLevel = partyMembers.reduce((sum, p) => sum + (p?.actor?.level ?? 0), 0);
return Math.round(totalLevel / partyMembers.length);
}

0 comments on commit ce2532f

Please sign in to comment.