Skip to content

Instantly share code, notes, and snippets.

@baronfel
Last active April 6, 2024 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baronfel/22051b3e0bc2d202bbd12659026ed068 to your computer and use it in GitHub Desktop.
Save baronfel/22051b3e0bc2d202bbd12659026ed068 to your computer and use it in GitHub Desktop.
Pf2e Study action replacement
// built off of the data put in place by the magaambya-study-helper module
function getFlags(actor) {
return actor.getFlag('magaambya-study-helper', 'branch-data');
}
function updateFlags(actor, data) {
actor.setFlag('magaambya-study-helper', 'branch-data', data);
}
function slugify(str) {
return game.pf2e.system.sluggify(str);
}
function updateFirstBranchValue(actor, value) {
var currentFlagData = getFlags(actor);
currentFlagData.firstBranchLevel = value;
updateFlags(actor, currentFlagData);
}
function updateSecondBranchValue(actor, value) {
var currentFlagData = getFlags(actor);
currentFlagData.secondBranchLevel = value;
updateFlags(actor, currentFlagData);
}
var { firstBranch, firstBranchLevel, secondBranch, secondBranchLevel } = getFlags(actor);
var firstBranchSlug = slugify(firstBranch);
var secondBranchSlug = slugify(secondBranch);
// data entry since we don't have a module to store this in
var branches = {
"cascade-bearers": { name: "Cascade Bearers", skills: ["Arcana", "Occultism", "Religion"] },
"emerald-boughs": { name: "Emerald Boughs", skills: ["Deception", "Diplomacy", "Society"] },
"rain-scribes": { name: "Rain Scribes", skills: ["Medicine", "Nature", "Survival"] },
"tempest-sun-mages": { name: "Tempest-Sun Mages", skills: ["Intimidation", "Performance"] },
"uzunjati": { name: "Uzunjati", skills: ["Crafting"] },
};
const dcByLevel = new Map();
dcByLevel.set(-1, 13);
dcByLevel.set(0, 14);
dcByLevel.set(1, 15);
dcByLevel.set(2, 16);
dcByLevel.set(3, 18);
dcByLevel.set(4, 19);
dcByLevel.set(5, 20);
dcByLevel.set(6, 22);
dcByLevel.set(7, 23);
dcByLevel.set(8, 24);
dcByLevel.set(9, 26);
dcByLevel.set(10, 27);
dcByLevel.set(11, 28);
dcByLevel.set(12, 30);
dcByLevel.set(13, 31);
dcByLevel.set(14, 32);
dcByLevel.set(15, 34);
dcByLevel.set(16, 35);
dcByLevel.set(17, 36);
dcByLevel.set(18, 38);
dcByLevel.set(19, 39);
dcByLevel.set(20, 40);
dcByLevel.set(21, 42);
dcByLevel.set(22, 44);
dcByLevel.set(23, 46);
dcByLevel.set(24, 48);
dcByLevel.set(25, 50);
async function promptForSkillsAndRoll(branchSlug, level, actor, updateState) {
const branchData = branches[branchSlug];
const skill_list = branchData.skills.slice();
if (branchData.name == "uzunjati") {
const lores = actor.items
.filter((item) => item.type == "lore")
.map((lore) => lore.name);
skill_list.push(...lores);
}
const options = Object.entries(skill_list)
.map(([arrayPos, skill]) => [
`<option value="${slugify(skill)}">${skill}</option>`,
])
.join();
const content = `<form>
<div class="form-group">
<label>Skill:</label>
<select name="skill-selector">${options}</select>
</div>
</form>`;
const dc = dcByLevel.get(level);
await new Dialog({
title: "Choose which skill to roll",
content,
buttons: {
ok: {
label: "<span class='pf2-icon'>1</span> Roll selected skill",
callback: (html) => {
const skillslug = html.find("[name=skill-selector]")[0].value;
actor.skills[skillslug].check.roll({
label: `Study for the ${branchData.name} branch`,
dc: { value: dc, adjustments: [] },
extraRollOptions: [
`branch:${branchSlug}`,
"action:study"
],
skipDialog: true,
callback: async (roll, outcome, chatMessage) => {
var message = "";
var adjustment = 0;
switch (outcome) {
case 'criticalSuccess':
message = "Incredible results! You ace every exam and cause a stir that all the instructors notice. Increase the level of the branch you chose by 2.";
adjustment = 2;
break;
case 'success':
message = "You succeed in your studies admirably. Increase the level of the branch you chose by 1.";
adjustment = 1;
break;
case 'failure':
message = "You need to work harder and try again. Your branch level remains the same.";
break;
case 'criticalFailure':
message = "You make a major mistake that requires you to perform remedial studies. The next time you would get an opportunity to attempt this activity, you must skip that opportunity to catch up.";
break;
}
await ChatMessage.create({
speaker: ChatMessage.getSpeaker({ actor: actor }),
content: message
});
if (adjustment != 0) {
updateState(actor, level + adjustment);
}
},
});
},
},
cancel: {
label: "<span class='pf2-icon'>R</span> Cancel",
},
},
default: "cancel",
}).render(true);
}
var buttons = {};
buttons[firstBranchSlug] = {
label: firstBranch,
callback: () => promptForSkillsAndRoll(firstBranchSlug, firstBranchLevel || 0, actor, updateFirstBranchValue)
};
buttons[secondBranchSlug] = {
label: secondBranch,
callback: () => promptForSkillsAndRoll(secondBranchSlug, secondBranchLevel || 0, actor, updateSecondBranchValue)
};
await new Dialog({
title: 'Study for your exams',
buttons: buttons
}).render(true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment