Skip to content

Commit

Permalink
1.4.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Haxxer committed Feb 22, 2022
1 parent aaa0825 commit 592e438
Show file tree
Hide file tree
Showing 8 changed files with 204 additions and 159 deletions.
9 changes: 9 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Item Piles Changelog

## Version 1.4.4

- Improved splitting API functions to improve performance when playing on Forge
- Improved documentation to better describe what each API method requires
- Tweaked `Split n ways` button to disable itself instead of becoming hidden
- Tweaked system recognition to allow systems to set the required settings through the API, which suppresses the system incompatibility warning
- Fixed various bugs surrounding splitting item piles
- Fixed issue with the `Split n ways` button not working sometimes

## Version 1.4.3

- Fixed minor issue with creating item piles
Expand Down
220 changes: 123 additions & 97 deletions scripts/api.js

Large diffs are not rendered by default.

48 changes: 5 additions & 43 deletions scripts/chathandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,40 +125,7 @@ const chatHandler = {
_outputSplitItemPileInventory(source, transferData, userId) {
if (!API.isValidItemPile(source)) return;
if (game.user.id !== userId || game.settings.get(CONSTANTS.MODULE_NAME, "outputToChat") === "off") return;

const sharingData = lib.getItemPileSharingData(source);

let itemData = [];
if (sharingData.items) {
itemData = sharingData.items.map(item => {
const totalQuantity = item.actors.reduce((acc, item) => acc + item.quantity, 0)
return {
name: item.name,
img: item.img,
quantity: Math.floor(totalQuantity / item.actors.length)
}
})
}

let currencyData = [];
if (sharingData.currencies) {
const currencyList = lib.getActorCurrencyList(source);
currencyData = sharingData.currencies.map(currencyData => {
const currency = currencyList.find(currency => currency.path === currencyData.path);
const totalQuantity = currencyData.actors.reduce((acc, storedCurrency) => acc + storedCurrency.quantity, 0);
return {
name: game.i18n.has(currency.name) ? game.i18n.localize(currency.name) : currency.name,
img: currency.img ?? "",
quantity: Math.floor(totalQuantity / currencyData.actors.length),
currency: true,
index: currencyList.indexOf(currency)
}
})
}

const num_players = Object.keys(transferData).length;

return itemPileSocket.executeAsGM(SOCKET_HANDLERS.SPLIT_CHAT_MESSAGE, source.uuid, num_players, itemData, currencyData, userId);
return itemPileSocket.executeAsGM(SOCKET_HANDLERS.SPLIT_CHAT_MESSAGE, source.uuid, transferData, userId);
},

async _outputTradeStarted(party_1, party_2, publicTradeId, isPrivate) {
Expand All @@ -167,11 +134,8 @@ const chatHandler = {
},

async _outputTradeComplete(party_1, party_2, publicTradeId, isPrivate) {

if (game.settings.get(CONSTANTS.MODULE_NAME, "outputToChat") === "off") return;

return this._outputTradeCompleteToChat(party_1, party_2, publicTradeId, isPrivate);

},

/**
Expand Down Expand Up @@ -207,7 +171,6 @@ const chatHandler = {
name: game.i18n.has(currency.name) ? game.i18n.localize(currency.name) : currency.name,
img: currency.img ?? "",
quantity: entry[1],
currency: true,
index: currencyList.indexOf(currency)
}
});
Expand Down Expand Up @@ -316,18 +279,17 @@ const chatHandler = {

},


async _outputSplitToChat(sourceUuid, num_players, items, currencies, userId) {
async _outputSplitToChat(sourceUuid, transferData, userId) {

const source = await fromUuid(sourceUuid);

const sourceActor = source?.actor ?? source;

const chatCardHtml = await renderTemplate(CONSTANTS.PATH + "templates/loot-chat-message.html", {
message: game.i18n.format("ITEM-PILES.Chat.Split", { num_players: num_players }),
message: game.i18n.format("ITEM-PILES.Chat.Split", { num_players: transferData.num_players }),
itemPile: sourceActor,
items: items,
currencies: currencies
items: transferData.items,
currencies: transferData.currencies
});

return this._createNewChatMessage(userId, {
Expand Down
4 changes: 2 additions & 2 deletions scripts/formapplications/item-pile-config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import CONSTANTS from "../constants.js";
import API from "../api.js";
import { ItemPileCurrenciesEditor } from "./item-pile-currencies-editor.js";
import * as lib from "../lib/lib.js";
import { ItemPileCurrenciesEditor } from "./item-pile-currencies-editor.js";
import { ItemPileFiltersEditor } from "./item-pile-filters-editor.js";

export class ItemPileConfig extends FormApplication {
Expand Down Expand Up @@ -205,7 +205,7 @@ export class ItemPileConfig extends FormApplication {
icon: '<i class="fas fa-check"></i>',
label: game.i18n.localize("ITEM-PILES.Dialogs.ResetSharingData.Confirm"),
callback: () => {
lib.updateItemPileSharingData(this.document, {});
lib.clearItemPileSharingData(this.document);
}
},
cancel: {
Expand Down
39 changes: 31 additions & 8 deletions scripts/formapplications/item-pile-inventory.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class ItemPileInventory extends FormApplication {
/** @inheritdoc */
static get defaultOptions() {
return foundry.utils.mergeObject(super.defaultOptions, {
closeOnSubmit: false,
classes: ["sheet"],
template: `${CONSTANTS.PATH}templates/item-pile-inventory.html`,
width: 550,
Expand Down Expand Up @@ -257,13 +258,33 @@ export class ItemPileInventory extends FormApplication {

data.isEmpty = !data?.hasItems && !data?.hasCurrencies;

const num_players = lib.getPlayersForItemPile(this.pile).length;

data.shareItemsEnabled = pileData.shareItemsEnabled;
data.shareCurrenciesEnabled = pileData.shareCurrenciesEnabled;

const hasSplittableQuantities = (pileData.shareItemsEnabled && data.items.find((item) => (item.quantity / num_players) > 1))
|| (pileData.shareCurrenciesEnabled && data.currencies.find((attribute) => (attribute.quantity / num_players) > 1))
const sharingData = lib.getItemPileSharingData(this.pile);
const num_players = lib.getPlayersForItemPile(this.pile).length;

const hasSplittableItems = pileData.shareItemsEnabled && data.items && !!data.items?.find(item => {
let quantity = item.quantity;
if(sharingData.currencies){
const itemSharingData = sharingData.currencies.find(sharingCurrency => sharingCurrency.path === item.path);
if(itemSharingData){
quantity += itemSharingData.actors.reduce((acc, data) => acc + data.quantity, 0);
}
}
return (quantity / num_players) >= 1;
});

const hasSplittableCurrencies = pileData.shareCurrenciesEnabled && data.currencies && !!data.currencies?.find(currency => {
let quantity = currency.quantity;
if(sharingData.currencies) {
const currencySharingData = sharingData.currencies.find(sharingCurrency => sharingCurrency.path === currency.path);
if (currencySharingData) {
quantity += currencySharingData.actors.reduce((acc, data) => acc + data.quantity, 0);
}
}
return (quantity / num_players) >= 1;
});

data.buttons = [];

Expand All @@ -275,7 +296,7 @@ export class ItemPileInventory extends FormApplication {
});
}

if ((data.hasRecipient || game.user.isGM) && pileData.splitAllEnabled && hasSplittableQuantities && (pileData.shareItemsEnabled || pileData.shareCurrenciesEnabled)) {
if (pileData.splitAllEnabled && (data.hasRecipient || game.user.isGM)) {

let buttonText;
if (pileData.shareItemsEnabled && pileData.shareCurrenciesEnabled) {
Expand All @@ -290,7 +311,7 @@ export class ItemPileInventory extends FormApplication {
value: "splitAll",
icon: "far fa-handshake",
text: buttonText,
disabled: !num_players,
disabled: !num_players || !(hasSplittableItems || hasSplittableCurrencies),
type: "button"
});

Expand Down Expand Up @@ -487,12 +508,12 @@ export class ItemPileInventory extends FormApplication {
async _updateObject(event, formData) {

if (event.submitter.value === "update") {
return this.updatePile(formData);
await this.updatePile(formData);
return this.render(true);
}

if (event.submitter.value === "takeAll") {
API.transferEverything(this.pile, this.recipient, { interactionId: this.interactionId });
return;
}

if (event.submitter.value === "close") {
Expand All @@ -501,6 +522,8 @@ export class ItemPileInventory extends FormApplication {
});
}

return this.close();

}

updatePile(data) {
Expand Down
23 changes: 15 additions & 8 deletions scripts/lib/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,7 @@ export function getItemPileItemsForActor(pile, recipient, floor = false) {
currentQuantity: 1,
quantity: quantity,
shareLeft: quantity,
previouslyTaken: 0,
toShare: pileData.shareItemsEnabled && recipientUuid
};

Expand All @@ -685,17 +686,20 @@ export function getItemPileItemsForActor(pile, recipient, floor = false) {

let totalShares = quantity;
if (foundItem) {
totalShares += foundItem.actors.reduce((acc, actor) => acc + actor.quantity, 0);
totalShares += foundItem.actors.reduce((acc, actor) => {
return acc + actor.quantity;
}, 0);
}

let totalActorShare = totalShares / players.length;
if (!Number.isInteger(totalActorShare) && !floor) {
totalActorShare += 1;
}

let actorQuantity = foundItem?.actors ? foundItem?.actors?.find(actor => actor.uuid === recipientUuid)?.quantity ?? 0 : 0;
const takenBefore = foundItem?.actors?.find(actor => actor.uuid === recipientUuid);
data.previouslyTaken = takenBefore ? takenBefore.quantity : 0;

data.shareLeft = Math.max(0, Math.min(quantity, Math.floor(totalActorShare - actorQuantity)));
data.shareLeft = Math.max(0, Math.min(quantity, Math.floor(totalActorShare - data.previouslyTaken)));

}

Expand All @@ -718,11 +722,13 @@ export function getItemPileCurrenciesForActor(pile, recipient, floor) {

return pileCurrencies.filter(currency => {
return !recipient || hasProperty(recipient?.data ?? {}, currency.path);
}).map(currency => {
}).map((currency, index) => {

currency.currentQuantity = 1;
currency.shareLeft = currency.quantity;
currency.toShare = pileData.shareCurrenciesEnabled && recipientUuid;
currency.toShare = pileData.shareCurrenciesEnabled && !!recipientUuid;
currency.previouslyTaken = 0;
currency.index = index;

if (currency.toShare) {

Expand All @@ -738,14 +744,15 @@ export function getItemPileCurrenciesForActor(pile, recipient, floor) {
totalActorShare += 1;
}

let actorQuantity = foundCurrency?.actors ? foundCurrency?.actors?.find(actor => actor.uuid === recipientUuid)?.quantity ?? 0 : 0;
const takenBefore = foundCurrency?.actors?.find(actor => actor.uuid === recipientUuid);
currency.previouslyTaken = takenBefore ? takenBefore.quantity : 0;

currency.shareLeft = Math.max(0, Math.min(currency.quantity, Math.floor(totalActorShare - actorQuantity)));
currency.shareLeft = Math.max(0, Math.min(currency.quantity, Math.floor(totalActorShare - currency.previouslyTaken)));

}

return currency;

});

}
}
18 changes: 18 additions & 0 deletions scripts/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ const otherSettings = {
config: false,
default: false,
type: Boolean
},

"preconfiguredSystem": {
scope: "world",
config: false,
default: false,
type: Boolean
}
}

Expand Down Expand Up @@ -291,10 +298,21 @@ async function applyDefaultSettings() {

export async function checkSystem() {

await lib.wait(1000);

if(game.settings.get(CONSTANTS.MODULE_NAME, "preconfiguredSystem")) return;

if (!SYSTEMS.DATA) {

if (game.settings.get(CONSTANTS.MODULE_NAME, "systemNotFoundWarningShown")) return;

let settingsValid = true;
for (const [name, data] of Object.entries(defaultSettings())) {
settingsValid = settingsValid && game.settings.get(CONSTANTS.MODULE_NAME, name).length !== (new data.type).length
}

if(settingsValid) return;

await game.settings.set(CONSTANTS.MODULE_NAME, "systemNotFoundWarningShown", true);

return Dialog.prompt({
Expand Down
2 changes: 1 addition & 1 deletion styles/module.css.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 592e438

Please sign in to comment.