Skip to content

Commit

Permalink
Fixes for point transfer not working right
Browse files Browse the repository at this point in the history
  • Loading branch information
jbhaywood committed Dec 2, 2021
1 parent 1376b6f commit f086c43
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 31 deletions.
49 changes: 29 additions & 20 deletions module/actor-sheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class HoneyHeistActorSheet extends ActorSheet {
activateListeners(html) {
super.activateListeners(html);

html.find(".attribute-roll").click((ev) => {
html.find(".attribute-roll").click(async (ev) => {
const roller = $(ev.currentTarget);
const roll = new Roll(roller.data("roll"), this.actor.getRollData()).evaluate({ async: false }); // avoid deprecation warning, backwards compatible
const parent = roller.parent("div");
Expand All @@ -42,7 +42,7 @@ export class HoneyHeistActorSheet extends ActorSheet {
const attributeName = select.name;
const option = select.options[roll.total - 1];

this.actor.update({ [attributeName]: option.value });
await this.actor.update({ [attributeName]: option.value });

// If you roll an 8 on a hat roll, you get two hats!
if (attributeName === "data.hat" && roll.total === 9) {
Expand All @@ -64,7 +64,7 @@ export class HoneyHeistActorSheet extends ActorSheet {
html.find(".stat-button").click((ev) => {
const isBearRoll = this._isBearRoll(ev.currentTarget);
const updateValue = isBearRoll ? 1 : -1;
const isEnd = this._updateStats(updateValue, null, isBearRoll);
const isEnd = this._updateStatsAsync(updateValue, null, isBearRoll);

if (!isEnd) {
ChatMessage.create({
Expand All @@ -77,11 +77,11 @@ export class HoneyHeistActorSheet extends ActorSheet {
}
});

html.find(".stat-roll-single, .stat-roll-double").click((ev) => {
html.find(".stat-roll-single, .stat-roll-double").click(async (ev) => {
const isBearRoll = this._isBearRoll(ev.currentTarget);
const roller = $(ev.currentTarget);
const input = roller.siblings(".stat-value").get(0);
const currentValue = parseInt(input.value, 10);
const currentValue = parseInt(input.value);
const roll = new Roll(roller.data("roll"), this.actor.getRollData()).evaluate({ async: false }); // avoid deprecation warning, backwards compatible
const isSuccess = roll.total <= currentValue;
const rollSuccess = isSuccess ? game.i18n.localize("HH.Success") : game.i18n.localize("HH.Failed");
Expand All @@ -96,7 +96,7 @@ export class HoneyHeistActorSheet extends ActorSheet {
// difficulty, move one point from Criminal into Bear.
// GREED: When the plan goes off without a hitch, move
// one point from Bear into Criminal.
const isEnd = this._updateStats(isSuccess ? -1 : 1, roll, isBearRoll);
const isEnd = await this._updateStatsAsync(isSuccess ? -1 : 1, roll, isBearRoll);

if (!isEnd) {
roll.toMessage({
Expand All @@ -108,27 +108,36 @@ export class HoneyHeistActorSheet extends ActorSheet {
});
}

_updateStats(bearOffset, roll, isBearRoll) {
const bearStat = $("#stat-bear").find(".stat-value").get(0);
const criminalStat = $("#stat-criminal").find(".stat-value").get(0);
let bearVal = parseInt(bearStat.value, 10);
let criminalVal = parseInt(criminalStat.value, 10);
let endResult = (isBearRoll && bearVal === 6) || (!isBearRoll && criminalVal === 6);
async _updateStatsAsync(offset, roll, isBearRoll) {
let bearStat = this.actor.data.data.stats.bear;
let criminalStat = this.actor.data.data.stats.criminal;

// These stat values should always be numbers, but sometimes
// they get returned as strings and I don't know why.
if (typeof bearStat === "string") {
bearStat = parseInt(bearStat);
}

if (typeof criminalStat === "string") {
criminalStat = parseInt(criminalStat);
}

let endResult = (isBearRoll && bearStat === 6) || (!isBearRoll && criminalStat === 6);

if (!endResult) {
// Adjust the current values based on the given offset.
bearVal += bearOffset;
criminalVal -= bearOffset;
bearStat += offset;
criminalStat -= offset;

// Only need to check one or the other stat value to make sure they're in the 0-6 range.
if (bearVal >= 0 && bearVal <= 6) {
if (bearStat >= 0 && bearStat <= 6) {
// Set the new values in the sheet.
this.actor.update({ "data.stats.bear": bearVal });
this.actor.update({ "data.stats.criminal": criminalVal });
await this.actor.update({ "data.stats.bear": bearStat });
await this.actor.update({ "data.stats.criminal": criminalStat });

// Check to see if either the bear criminal stat has reached 6,
// Check to see if either the bear or criminal stat has reached 6,
// which means it's the end for this bear.
if (bearVal === 6) {
if (bearStat === 6) {
endResult = true;

if (roll) {
Expand All @@ -144,7 +153,7 @@ export class HoneyHeistActorSheet extends ActorSheet {
content: game.i18n.localize("HH.BearEndMessage")
});
}
} else if (criminalVal === 6) {
} else if (criminalStat === 6) {
endResult = true;

if (roll) {
Expand Down
24 changes: 15 additions & 9 deletions module/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,27 @@ Hooks.once("ready", async function () {

Hooks.on("renderHoneyHeistActorSheet", (ev) => {
// Color a stat red if it's value is six.
const bearStat = $("#stat-bear").find(".stat-value").get(0);
const criminalStat = $("#stat-criminal").find(".stat-value").get(0);
let bearVal = parseInt(bearStat.value, 10);
let criminalVal = parseInt(criminalStat.value, 10);
const root = ev.element[0];
const bearStatElement = root.querySelector("#stat-bear .stat-value");
const criminalStatElement = root.querySelector("#stat-criminal .stat-value");
let bearVal = parseInt(bearStatElement.value, 10);
let criminalVal = parseInt(criminalStatElement.value, 10);

if (bearVal === 6) {
$("#stat-bear").children().addClass("error-red");
bearStatElement.classList.add("error-red");
} else if (criminalVal === 6) {
$("#stat-criminal").children().addClass("error-red");
criminalStatElement.classList.add("error-red");
}

// Show the extra hat options if the initial hat stat is 'roll-twice'.
if ($("#hat-roll").val() === "roll-twice") {
$(".hat2").show();
const hatRollElement = root.querySelector("#hat-roll");
if (hatRollElement.value === "roll-twice") {
for (const elem of root.querySelectorAll(".hat2")) {
elem.style.display = "";
}
} else {
$(".hat2").hide();
for (const elem of root.querySelectorAll(".hat2")) {
elem.style.display = "none";
}
}
});
4 changes: 2 additions & 2 deletions system.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "honey-heist",
"title": "Honey Heist",
"description": "It's HoneyCon. You are going to undertake the greatest heist the world has ever seen.",
"version": 1.23,
"version": 1.24,
"templateVersion": 2,
"minimumCoreVersion": "0.6.0",
"compatibleCoreVersion": "0.8.9",
Expand All @@ -19,6 +19,6 @@
],
"url": "https://github.com/jbhaywood/foundryvtt-honeyheist/",
"manifest": "https://raw.githubusercontent.com/jbhaywood/foundryvtt-honeyheist/master/system.json",
"download": "https://github.com/jbhaywood/foundryvtt-honeyheist/archive/v1.23.zip",
"download": "https://github.com/jbhaywood/foundryvtt-honeyheist/archive/v1.24.zip",
"license": "LICENSE.txt"
}

0 comments on commit f086c43

Please sign in to comment.