Skip to content

Commit

Permalink
Extending PF2e support
Browse files Browse the repository at this point in the history
  • Loading branch information
mclemente committed Jul 31, 2021
1 parent 498c12c commit 3e4a3e1
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 81 deletions.
190 changes: 110 additions & 80 deletions module/SystemProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@ export class SystemProvider {
this.id = id
}

get hasCurrency() {
return false;
}

get hasLanguages() {
return false;
}

get loadTemplates() {
return [];
}
Expand All @@ -23,62 +15,12 @@ export class SystemProvider {
return 500;
}


getUpdate(actors) {
let languages;
let totalCurrency;
if (this.hasLanguages) {
// restructure the languages a bit so rendering gets easier
languages = actors
.reduce((languages, actor) => [...new Set(languages.concat(actor.languages))], [])
.filter(language => language !== undefined)
.sort();
actors = actors.map(actor => {
return {
...actor,
languages: languages.map(language => actor.languages && actor.languages.includes(language)),
};
});
}
if (this.hasCurrency) {
totalCurrency = actors.reduce(
(currency, actor) => {
for (let prop in actor.currency) {
currency[prop] += actor.currency[prop];
}
return currency;
},
this.hasCurrency
);
// summing up the total
}
let totalPartyGP = actors.reduce((totalGP, actor) => totalGP + parseFloat(actor.totalGP), 0).toFixed(2);
return [
actors,
{
languages: languages,
totalCurrency: totalCurrency,
totalPartyGP: totalPartyGP
}
]
return [actors, {}]
}
}

export class dnd5eProvider extends SystemProvider {
get hasCurrency() {
return {
cp: 0,
sp: 0,
ep: 0,
gp: 0,
pp: 0,
}
}

get hasLanguages() {
return true;
}

get template() {
return "/modules/party-overview/templates/dnd5e.hbs"
}
Expand Down Expand Up @@ -159,24 +101,51 @@ export class dnd5eProvider extends SystemProvider {
totalGP: this.getTotalGP(data).toFixed(2)
}
}
}

export class pf2eProvider extends SystemProvider {
get hasCurrency() {
return {
cp: 0,
sp: 0,
gp: 0,
pp: 0,
}
}

get hasLanguages() {
return true;
getUpdate(actors) {
let languages = actors
.reduce((languages, actor) => [...new Set(languages.concat(actor.languages))], [])
.filter(language => language !== undefined)
.sort();
actors = actors.map(actor => {
return {
...actor,
languages: languages.map(language => actor.languages && actor.languages.includes(language)),
};
});
let totalCurrency = actors.reduce(
(currency, actor) => {
for (let prop in actor.currency) {
currency[prop] += actor.currency[prop];
}
return currency;
},
{
cp: 0,
sp: 0,
ep: 0,
gp: 0,
pp: 0,
}
);
let totalPartyGP = actors.reduce((totalGP, actor) => totalGP + parseFloat(actor.totalGP), 0).toFixed(2);
return [
actors,
{
languages: languages,
totalCurrency: totalCurrency,
totalPartyGP: totalPartyGP
}
]
}
}

export class pf2eProvider extends SystemProvider {
get loadTemplates() {
return ["modules/party-overview/templates/parts/PF2e-Lore.html"];
return [
"modules/party-overview/templates/parts/PF2e-Lore.html",
"modules/party-overview/templates/parts/PF2e-Bulk.html"
];
}

get template() {
Expand Down Expand Up @@ -205,6 +174,46 @@ export class pf2eProvider extends SystemProvider {
return lore;
}

getEncumbrance(data) {
let sum = 0;
let lightCount = 0;
let containers = {};
let containerItems = [];
for (let item of data.items) {
if (item.data.isPhysical) {
const itemData = item.data.data;
if (item.type == "backpack") containers[item.id] = {equipped: itemData.equipped.value, bulk: 0, negateBulk: -Number(itemData.negateBulk.value), lightCount: 0};
if (itemData.containerId?.value) containerItems.push(item);
else {
if (itemData.weight.value == "L") lightCount += 1;
else sum += (itemData.equipped.value ? Number(itemData.equippedBulk.value || itemData.weight.value) : Number(itemData.weight.value)) || 0;
}
}
}
for (let item of containerItems) {
const itemData = item.data.data;
if (itemData.weight.value == "L") containers[itemData.containerId.value].lightCount += 1;
else containers[itemData.containerId.value].negateBulk += value;
}
for (let container in containers) {
if (containers[container].equipped) {
let bulk = containers[container].bulk + Math.trunc(containers[container].lightCount/10) - containers[container].negateBulk;
sum = (bulk < 0) ? sum : sum + bulk;
lightCount = (bulk < 0) ? lightCount : lightCount + containers[container].lightCount % 10;
}
else {
sum += containers[container].bulk + Math.trunc(containers[container].lightCount/10);
lightCount += containers[container].lightCount % 10;
}
}
sum += Math.trunc(lightCount/10);
lightCount = lightCount % 10;
let encumberedAt = data.data.abilities.str.mod + data.data.attributes.bonusEncumbranceBulk + 5;
let limit = data.data.abilities.str.mod + data.data.attributes.bonusLimitBulk + 10;
// data.items.filter(a => a.data.data.weight).map(a => sum += (a.data.data.equipped.value ? (Number(a.data.data.equippedBulk.value) || 0) : (Number(a.data.data.weight.value) || 0)) - (Number(a.data.data.negateBulk.value) || 0));
return {bulk: sum, encumberedAt, lightItems: lightCount, limit}
}

getTotalGP(currency) {
return currency.cp / 100 + currency.sp / 10 + currency.gp + currency.pp * 10;
}
Expand All @@ -231,30 +240,51 @@ export class pf2eProvider extends SystemProvider {
will: data.saves.will.value,
},
languages: data.traits.languages ? data.traits.languages.value.map(code => game.i18n.localize(CONFIG.PF2E.languages[code])) : [],
lore: this.getLore(actor.data),
currency: currency,

lore: this.getLore(actor.data),
encumbrance: this.getEncumbrance(actor.data),
totalGP: this.getTotalGP(currency).toFixed(2)
}
}

getUpdate(actors) {
let updates;
[actors, updates] = super.getUpdate(actors);
let lores;
lores = actors
let languages = actors
.reduce((languages, actor) => [...new Set(languages.concat(actor.languages))], [])
.filter(language => language !== undefined)
.sort();
let totalCurrency = actors.reduce(
(currency, actor) => {
for (let prop in actor.currency) {
currency[prop] += actor.currency[prop];
}
return currency;
},
{
cp: 0,
sp: 0,
gp: 0,
pp: 0,
}
);
let totalPartyGP = actors.reduce((totalGP, actor) => totalGP + parseFloat(actor.totalGP), 0).toFixed(2);
let lores = actors
.reduce((lore, actor) => [...new Set(lore.concat(actor.lore))], [])
.filter(lore => lore !== undefined)
.sort();
actors = actors.map(actor => {
return {
...actor,
lore: lores.map(lore => actor.lore && actor.lore.includes(lore))
languages: languages.map(language => actor.languages && actor.languages.includes(language)),
lore: lores.map(lore => actor.lore && actor.lore.includes(lore)),
}
})
return [
actors,
{
...updates,
languages: languages,
totalCurrency: totalCurrency,
totalPartyGP: totalPartyGP,
lore: lores
}
]
Expand Down
32 changes: 32 additions & 0 deletions templates/parts/PF2e-Bulk.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<div class="tab" data-tab="bulk" data-group="party">
<div class="table-row header">
{{> "modules/party-overview/templates/parts/FilterButton.html"}}
<div class="text">Name</div>
<div class="text icon" style="flex: 1">Bulk</div>
<div class="text icon" style="flex: 2">Encumbered At</div>
<div class="text icon" style="flex: 1">Limit</div>
</div>

{{#each actors as | actor | }}
<div class="table-row">
<div class="button">
<button class="btn-toggle-visibility" data-actor="{{actor.id }}">
{{#if actor.isHidden }}<i class="value fas fa-eye-slash"></i>{{else}}<i
class="value fas fa-eye"></i>{{/if}}
</button>
</div>
<div class="text">{{ actor.shortestName }}</div>
{{#with actor.encumbrance}}
<div class="text icon" style="flex: 1">
{{bulk}} {{#if lightItems}}({{lightItems}} L){{/if}}
</div>
<div class="text icon" style="flex: 2">
{{encumberedAt}}
</div>
<div class="text icon" style="flex: 1">
{{limit}}
</div>
{{/with}}
</div>
{{/each}}
</div>
6 changes: 5 additions & 1 deletion templates/pf2e.hbs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<div>
<nav class="tabs" data-group="party">
<li class="item" data-tab="general">General</li>
<li class="item" data-tab="bulk">Bulk</li>
<li class="item" data-tab="currencies">Currencies</li>
<li class="item" data-tab="languages">Languages</li>
<li class="item" data-tab="lore">Lore</li>
<li class="item" data-tab="currencies">Currencies</li>
</nav>
<section class="content">
<div class="tab" data-tab="general" data-group="party">
Expand Down Expand Up @@ -82,5 +83,8 @@
<div class="text icon"><b>{{totalPartyGP}}</b></div>
</div>
</div>

<!-- Lore -->
{{> "modules/party-overview/templates/parts/PF2e-Bulk.html"}}
</section>
</div>

0 comments on commit 3e4a3e1

Please sign in to comment.