Skip to content

Commit

Permalink
Merge pull request #27 from KageJittai/feature_pricemodifier
Browse files Browse the repository at this point in the history
Feature pricemodifier
  • Loading branch information
jopeek authored May 20, 2020
2 parents 807fced + 04092eb commit 2e3fa0d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 50 deletions.
78 changes: 29 additions & 49 deletions lootsheetnpc5e.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ class LootSheet5eNPC extends ActorSheet5eNPC {
return (arg1 != arg2) ? options.fn(this) : options.inverse(this);
});

Handlebars.registerHelper('lootsheetprice', function (basePrice, modifier) {
return Math.round(basePrice * modifier * 100) / 100;
});

const path = "systems/dnd5e/templates/actors/";
if (!game.user.isGM && this.actor.limited) return path + "limited-sheet.html";
return "modules/lootsheetnpc5e/template/npc-sheet.html";
Expand Down Expand Up @@ -92,8 +96,17 @@ class LootSheet5eNPC extends ActorSheet5eNPC {
let lootsheettype = await this.actor.getFlag("lootsheetnpc5e", "lootsheettype");
if (!lootsheettype) await this.actor.setFlag("lootsheetnpc5e", "lootsheettype", "Loot");
lootsheettype = await this.actor.getFlag("lootsheetnpc5e", "lootsheettype");


let priceModifier = 1.0;
if (lootsheettype === "Merchant") {
priceModifier = await this.actor.getFlag("lootsheetnpc5e", "priceModifier");
if (!priceModifier) await this.actor.setFlag("lootsheetnpc5e", "priceModifier", 1.0);
priceModifier = await this.actor.getFlag("lootsheetnpc5e", "priceModifier");
}

sheetData.lootsheettype = lootsheettype;
sheetData.priceModifier = priceModifier;
sheetData.rolltables = game.tables.entities;

// Return data for rendering
Expand All @@ -118,12 +131,7 @@ class LootSheet5eNPC extends ActorSheet5eNPC {
html.find('.split-coins').click(ev => this._distributeCoins(ev));

// Price Modifier
if (this.actor.isToken) {
html.find('.price-modifier').prop('disabled', true);
html.find('.price-modifier').prop('title', 'Disabled on Token. Use Actor to modify prices.');
} else {
html.find('.price-modifier').click(ev => this._priceModifier(ev));
}
html.find('.price-modifier').click(ev => this._priceModifier(ev));

html.find('.merchant-settings').change(ev => this._merchantSettingChange(ev));
html.find('.update-inventory').click(ev => this._merchantInventoryUpdate(ev));
Expand Down Expand Up @@ -402,17 +410,19 @@ class LootSheet5eNPC extends ActorSheet5eNPC {
* Handle price modifier
* @private
*/
_priceModifier(event) {
async _priceModifier(event) {
event.preventDefault();
//console.log("Loot Sheet | Price Modifier clicked");
//console.log(this.actor.isToken);

var html = "<p>Use this slider to increase or decrease the price of all items in this inventory. <i class='fa fa-question-circle' title='This uses a percentage factor where 100% is the current price, 0% is 0, and 200% is double the price.'></i></p>";

html += '<p><input name="price-modifier-percent" id="price-modifier-percent" type="range" min="0" max="200" value="100" class="slider"></p>';
let priceModifier = await this.actor.getFlag("lootsheetnpc5e", "priceModifier");
if (!priceModifier) priceModifier = 1.0;

html += '<p><label>Percentage:</label> <input type=number min="0" max="200" value="100" id="price-modifier-percent-display"></p>';
priceModifier = Math.round(priceModifier * 100);

var html = "<p>Use this slider to increase or decrease the price of all items in this inventory. <i class='fa fa-question-circle' title='This uses a percentage factor where 100% is the current price, 0% is 0, and 200% is double the price.'></i></p>";
html += '<p><input name="price-modifier-percent" id="price-modifier-percent" type="range" min="0" max="200" value="'+priceModifier+'" class="slider"></p>';
html += '<p><label>Percentage:</label> <input type=number min="0" max="200" value="'+priceModifier+'" id="price-modifier-percent-display"></p>';
html += '<script>var pmSlider = document.getElementById("price-modifier-percent"); var pmDisplay = document.getElementById("price-modifier-percent-display"); pmDisplay.value = pmSlider.value; pmSlider.oninput = function() { pmDisplay.value = this.value; }; pmDisplay.oninput = function() { pmSlider.value = this.value; };</script>';

let d = new Dialog({
Expand All @@ -422,7 +432,7 @@ class LootSheet5eNPC extends ActorSheet5eNPC {
one: {
icon: '<i class="fas fa-check"></i>',
label: "Update",
callback: () => this._updatePrices(document.getElementById("price-modifier-percent").value)
callback: () => this.actor.setFlag("lootsheetnpc5e", "priceModifier", document.getElementById("price-modifier-percent").value / 100)
},
two: {
icon: '<i class="fas fa-times"></i>',
Expand All @@ -432,8 +442,8 @@ class LootSheet5eNPC extends ActorSheet5eNPC {
},
default: "two",
close: () => console.log("Loot Sheet | Price Modifier Closed")
});
d.render(true);
});
d.render(true);
}

/* -------------------------------------------- */
Expand Down Expand Up @@ -584,40 +594,6 @@ class LootSheet5eNPC extends ActorSheet5eNPC {

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

/**
* Organize and classify Items for Loot NPC sheets
* @private
*/
_updatePrices(pm) {
//console.log("Loot Sheet | Price Modifier Updating prices...", pm);

let actorData = duplicate(this.actor.data);

if (pm === undefined || pm === "100") return;

for (let i of actorData.items) {

//console.log("Loot Sheet | item", i);

var currentPrice = i.data.price;

//accomodate small prices so they don't get rounded to 0
if (currentPrice < 1) {
var newPrice = pm === 0 ? 0 : (currentPrice * (pm / 100)).toFixed(2);
} else {
var newPrice = pm === 0 ? 0 : Math.round(currentPrice * (pm / 100));
}

//console.log(newPrice);
i.data.price = newPrice;

this.actor.updateOwnedItem(i);
}

}

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

/**
* Organize and classify Items for Loot NPC sheets
* @private
Expand Down Expand Up @@ -979,7 +955,11 @@ Hooks.once("init", () => {
quantity = sellItem.data.quantity;
}

let itemCost = quantity * sellItem.data.price;
let sellerModifier = seller.getFlag("lootsheetnpc5e", "priceModifier");
if (!sellerModifier) sellerModifier = 1.0;

let itemCost = Math.round(sellItem.data.price * sellerModifier * 100) / 100;
itemCost *= quantity;
let buyerFunds = duplicate(buyer.data.data.currency);
const conversionRate = { "pp": 10, "gp": 1, "ep": 0.5, "sp": 0.1, "cp": 0.01 };
let buyerFundsAsGold = 0;
Expand Down
2 changes: 1 addition & 1 deletion template/npc-sheet.html
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ <h4>
<i class="fas fa-box" title="Quantity"></i> {{item.data.quantity}}
</div>
<div class="item-price">
<i class="fas fa-coins" title="Price (in gp)"></i> {{item.data.price}}
<i class="fas fa-coins" title="Price (in gp)"></i> {{ lootsheetprice item.data.price ../../priceModifier }}
</div>

<div class="item-controls">
Expand Down

0 comments on commit 2e3fa0d

Please sign in to comment.