Skip to content

Commit

Permalink
Merge pull request #1 from JamzTheMan/feature/add-sockets-to-repair-s…
Browse files Browse the repository at this point in the history
…hield

Update repair-targets-shield macro
  • Loading branch information
JamzTheMan authored Mar 16, 2024
2 parents ed72139 + bf95dbb commit f3ae35b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 27 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
desktop.ini
/Nerps-For-Foundry.lock
/link-dir.bat
/packs/**/*.log
/packs/**/LOG
/packs/**/LOG.*
7 changes: 4 additions & 3 deletions scripts/hooks-for-nerps.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {createGenericTimer} from "./macros/create-generic-timer.js";
import {explorationActivities} from "./macros/exploration-activities.js";
import {exportActorWithImages} from "./macros/export-actor-with-images.js";
import {measureTokenDistance} from "./macros/measure-token-distances.js";
import {repairTargetsShield} from "./macros/repair-targets-shield.js";
import {repairTargetsShield, repairShield} from "./macros/repair-targets-shield.js";
import {rollPerceptionChecks} from "./macros/roll-perception-checks.js";
import {setTokenBarsAndNameplates} from "./macros/set-token-bars-and-nameplates.js";
import {counteractCheck} from "./macros/counteract-check.js";
Expand All @@ -14,7 +14,7 @@ import {getSetting} from "./utils/extensions.js";
import {NerpsForFoundry, log} from "./nerps-for-foundry.js";
import {autoCorrectJournalContent} from "./autocorrect-journal-content.js"

let socket;
export let socket;

/*
__ __ __
Expand Down Expand Up @@ -145,7 +145,8 @@ Hooks.on("pf2e.endTurn", async (combatant, _combat, userId) => {
Hooks.once("socketlib.ready", () => {
socket = socketlib.registerModule(MODULE_NAME);
socket.register("removeReactions", removeReactions);
log.info("SocketLib ready!");
socket.register("repairShield", repairShield);
log.info("SocketLib for Nerps-For_Foundry ready!");
});

async function removeReactions(combatantActorId, expiryText) {
Expand Down
71 changes: 49 additions & 22 deletions scripts/macros/repair-targets-shield.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {log} from "../nerps-for-foundry.js";
import {socket} from "../hooks-for-nerps.js";

export function repairTargetsShield(token) {
ui.notifications.info("Add Reforging shield to function!")

/**
* Set actor to the token's actor if no target is selected, otherwise set it to the first target's actor.
*/
Expand Down Expand Up @@ -63,7 +62,7 @@ export function repairTargetsShield(token) {

const skillName = "Repair";
const skillKey = "crafting";
const skillRank = token.actor.skills[skillKey].rank
// const skillRank = token.actor.skills[skillKey].rank
const actionSlug = "Repair"
const actionName = "Repair"

Expand Down Expand Up @@ -105,7 +104,8 @@ export function repairTargetsShield(token) {
speaker: ChatMessage.getSpeaker(),
});
});
repairShield(damageRepaired, shieldActor.heldShield);
// await repairShield(damageRepaired, shieldActor.heldShield);
await socket.executeAsGM(repairShield, damageRepaired, shieldActor.id);
} else if (roll.degreeOfSuccess === 2) {
// success message
const damageRepaired = successRestored + token.actor.skills.crafting.rank * successRestored;
Expand All @@ -117,7 +117,8 @@ export function repairTargetsShield(token) {
speaker: ChatMessage.getSpeaker(),
});
});
repairShield(damageRepaired, shieldActor.heldShield);
// await repairShield(damageRepaired, shieldActor.heldShield);
await socket.executeAsGM(repairShield, damageRepaired, shieldActor.id);
} else if (roll.degreeOfSuccess === 1) {
// Fail message
dsnHook(() => {
Expand All @@ -134,30 +135,56 @@ export function repairTargetsShield(token) {
new DamageRoll("2d6").toMessage({
flavor: "<strong>Critical Failure</strong><br>You deal 2d6 damage to the item. Apply the item’s Hardness to this damage.",
speaker: ChatMessage.getSpeaker(),
}).then((r) => {
}).then(async (r) => {
let damage = r.rolls.reduce((sum, roll) => sum + roll.total, 0);
repairShield(-damage, shieldActor.heldShield);

// repairShield(-damage, shieldActor.heldShield);
await socket.executeAsGM(repairShield, -damage, shieldActor.id);
});
});
}
},
);
}

/**
* Check if any itemType equipment of the actor matches a slug (and optionally checks in how many hands it is held)
*
* @param {string} hpRestored hp restored to shield
* @param {string} shield shield to be repaired
* @returns {Promise<void>}
*/
async function repairShield(hpRestored, shield) {
if (hpRestored < 0) {
hpRestored = Math.min(0, hpRestored + shield.system.hardness);
}

/**
* Check if any itemType equipment of the actor matches a slug (and optionally checks in how many hands it is held)
*
* @param {number} hpRestored hp restored to shield
* @param {string} shieldActorId actor.id holding the shield to be repaired
* @returns {Promise<void>}
*/
export async function repairShield(hpRestored, shieldActorId) {
let shield = game.actors.get(shieldActorId).heldShield;

log.info(`shield name: ${shield.name}`);

if (hpRestored < 0) {
hpRestored = Math.min(0, hpRestored + shield.system.hardness);
} else {
if (shield.slug === 'reforging-shield') {
hpRestored *= 2;
}
const newShieldHp = Math.max(0, Math.min(shield.system.hp.max, shield.system.hp.value + hpRestored));
const shieldUpdate = {"system.hp.value": newShieldHp};

await shield.update(shieldUpdate);
}
const newShieldHp = Math.max(0, Math.min(shield.system.hp.max, shield.system.hp.value + hpRestored));
const shieldUpdate = {"system.hp.value": newShieldHp};

await shield.update(shieldUpdate);

if (hpRestored > 0) {
ChatMessage.create({
user: game.user.id,
type: CONST.CHAT_MESSAGE_TYPES.OTHER,
flavor: `<strong>${shield.name}</strong> has been repaired for ${hpRestored} Hit Points. It now has ${newShieldHp} Hit Points.`,
speaker: ChatMessage.getSpeaker(),
});
} else {
ChatMessage.create({
user: game.user.id,
type: CONST.CHAT_MESSAGE_TYPES.OTHER,
flavor: `<strong>${shield.name}</strong> has been damaged for ${-hpRestored} Hit Points (after ${shield.system.hardness} hardness). It now has ${newShieldHp} Hit Points.`,
speaker: ChatMessage.getSpeaker(),
});
}
}
2 changes: 0 additions & 2 deletions scripts/nerps-for-foundry.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import {Logger} from "./utils/logger.js";
// CONFIG.debug.hooks = true;
export let log = new Logger();

let socket;

export let i18n = key => {
return game.i18n.localize(key);
};
Expand Down

0 comments on commit f3ae35b

Please sign in to comment.