Skip to content

Commit

Permalink
Merge pull request #146 from freohr/feature/add-party-sheet-hooks
Browse files Browse the repository at this point in the history
Add hook to show party sheet from macros
  • Loading branch information
anthonyronda authored Mar 17, 2022
2 parents 0dfce74 + b98aa78 commit 429ebdb
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 49 deletions.
23 changes: 6 additions & 17 deletions src/module/party.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import { OsePartySheet } from "./dialog/party-sheet.js";
import { OsePartySheet } from "./party/party-sheet.js";

export const addControl = (object, html) => {
let control = `<button class='ose-party-sheet' type="button" title='${game.i18n.localize('OSE.dialog.partysheet')}'><i class='fas fa-users'></i></button>`;
html.find(".fas.fa-search").replaceWith($(control))
html.find(".fas.fa-search").replaceWith($(control));
html.find('.ose-party-sheet').click(ev => {
showPartySheet(ev, object);
})
}

export const showPartySheet = (event, object) => {
event.preventDefault();
new OsePartySheet(object, {
top: window.screen.height / 2 - 180,
left: window.screen.width / 2 - 140,
}).render(true);
ev.preventDefault();
Hooks.call("OSE.Party.showSheet");
});
}

export const update = (actor, data) => {
Expand All @@ -23,9 +16,5 @@ export const update = (actor, data) => {
return;
}

const partySheetUI = Object.values(ui.windows).find(win => { return win instanceof OsePartySheet });

if (partySheetUI) {
partySheetUI.render();
}
OsePartySheet.partySheet.render();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { OsePartyXP } from "./party-xp.js";
import { OseParty } from "./party.js";

const Party = {
partySheet: void 0
};

export class OsePartySheet extends FormApplication {

static get defaultOptions() {
return foundry.utils.mergeObject(super.defaultOptions, {
classes: ["ose", "dialog", "party-sheet"],
Expand All @@ -13,6 +19,18 @@ export class OsePartySheet extends FormApplication {
});
}

static init() {
Party.partySheet = new OsePartySheet();
}

static showPartySheet(options = {}) {
OsePartySheet.partySheet.render(true, { focus: true, ...options });
}

static get partySheet() {
return Party.partySheet;
}

/* -------------------------------------------- */

/**
Expand All @@ -30,19 +48,13 @@ export class OsePartySheet extends FormApplication {
* @return {Object}
*/
getData() {
const actors = this.object.documents.filter(
(e) =>
e.data.type === "character" &&
e.data.flags.ose &&
e.data.flags.ose.party === true
);
const settings = {
ascending: game.settings.get("ose", "ascendingAC"),
};

let data = {
partyActors: actors,
data: this.object,
partyActors: OseParty.currentParty,
// data: this.object,
config: CONFIG.OSE,
user: game.user,
settings: settings,
Expand Down Expand Up @@ -91,7 +103,7 @@ export class OsePartySheet extends FormApplication {
return;
}

const actors = this.object.documents;
const actors = game.actors;
let droppedActor = actors.find(actor => actor.id === data.id);

this._addActorToParty(droppedActor);
Expand Down
34 changes: 12 additions & 22 deletions src/module/dialog/party-xp.js → src/module/party/party-xp.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { OseParty } from "./party.js";

export class OsePartyXP extends FormApplication {
static get defaultOptions() {
return foundry.utils.mergeObject(super.defaultOptions, {
Expand Down Expand Up @@ -27,14 +29,8 @@ export class OsePartyXP extends FormApplication {
* @return {Object}
*/
getData() {
const actors = this.object.documents.filter(
(e) =>
e.data.type === "character" &&
e.data.flags.ose &&
e.data.flags.ose.party === true
);
let data = {
actors: actors,
actors: OseParty.currentParty,
data: this.object,
config: CONFIG.OSE,
user: game.user,
Expand All @@ -61,21 +57,15 @@ export class OsePartyXP extends FormApplication {
}

_calculateShare(ev) {
const actors = this.object.documents.filter(
(e) =>
e.data.type === "character" &&
e.data.flags.ose &&
e.data.flags.ose.party === true
);
const toDeal = $(ev.currentTarget.parentElement)
.find('input[name="total"]')
.val();
const currentParty = OseParty.currentParty;

const html = $(this.form);
const value = parseFloat(toDeal) / actors.length;
actors.forEach((a) => {
html
.find(`li[data-actor-id='${a.id}'] input`)
.val(Math.floor((a.data.data.details.xp.share / 100) * value));
const totalXP = html.find('input[name="total"]').val();
const baseXpShare = parseFloat(totalXP) / currentParty.length;

currentParty.forEach((a) => {
const xpShare = Math.floor((a.data.data.details.xp.share / 100) * baseXpShare)
html.find(`li[data-actor-id='${a.id}'] input`).val(xpShare);
});
}

Expand All @@ -86,7 +76,7 @@ export class OsePartyXP extends FormApplication {
const qRow = $(row);
const value = qRow.find("input").val();
const id = qRow.data("actorId");
const actor = this.object.documents.find((e) => e.id === id);
const actor = OseParty.currentParty.find((e) => e.id === id);
if (value) {
actor.getExperience(Math.floor(parseInt(value)));
}
Expand Down
11 changes: 11 additions & 0 deletions src/module/party/party.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export class OseParty {
static get currentParty() {
const characters = game.actors.filter(
(act) =>
act.data.type === "character" &&
act.data.flags.ose &&
act.data.flags.ose.party === true);

return characters;
}
}
6 changes: 6 additions & 0 deletions src/ose.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import * as macros from "./module/macros.js";
import * as party from "./module/party.js";
import { OseCombat } from "./module/combat.js";
import * as renderList from "./module/renderList.js";
import { OsePartySheet } from "./module/party/party-sheet.js";

/* -------------------------------------------- */
/* Foundry VTT Initialization */
Expand All @@ -37,6 +38,9 @@ Hooks.once("init", async function () {
oseCombat: OseCombat,
};

// Init Party Sheet handler
OsePartySheet.init();

// Custom Handlebars helpers
registerHelpers();

Expand Down Expand Up @@ -162,3 +166,5 @@ Hooks.on("updateActor", party.update);

Hooks.on("renderCompendium", renderList.RenderCompendium);
Hooks.on("renderSidebarDirectory", renderList.RenderDirectory);

Hooks.on("OSE.Party.showSheet", OsePartySheet.showPartySheet);
2 changes: 1 addition & 1 deletion src/templates/apps/party-xp.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<form autocomplete="off">
<div class="form-group">
<label>Amount</label>
<input name="total" placeholder="0" type="text" data-dtype="Number" />
<input name="total" placeholder="0" type="number" data-dtype="Number" />
</div>
<hr />
<ul class="actors">
Expand Down

0 comments on commit 429ebdb

Please sign in to comment.