From fcb3f2b58dc1fa394cbdef8e7272602de2ccb81b Mon Sep 17 00:00:00 2001 From: Vlyan Date: Tue, 25 May 2021 17:08:49 +0200 Subject: [PATCH] Working on 0.8.x - Xp spent curriculum/total for titles --- system/lang/en-en.json | 3 +- system/lang/es-es.json | 3 +- system/lang/fr-fr.json | 3 +- system/scripts/actors/character-sheet.js | 24 ++++++------- system/scripts/helpers.js | 30 ++++++++++++++++ system/scripts/items/title-sheet.js | 36 +++++++++++++++---- system/template.json | 3 +- .../actors/character/advancement-others.html | 4 ++- .../actors/character/advancement-school.html | 2 +- .../actors/character/experience.html | 7 ++-- system/templates/actors/npc/narrative.html | 2 +- system/templates/items/title/title-sheet.html | 17 ++++++--- 12 files changed, 100 insertions(+), 34 deletions(-) diff --git a/system/lang/en-en.json b/system/lang/en-en.json index def5ea5..d801280 100644 --- a/system/lang/en-en.json +++ b/system/lang/en-en.json @@ -329,7 +329,8 @@ "cost": "Cost", "spent": "Used", "saved": "Saved", - "total_xp_rank": "Xp spent", + "total_xp_spent": "Xp spent", + "total_xp_curriculum": "Xp spent on curriculum", "curriculum": "In curriculum", "curriculum_validate": "Complete this rank", "rarity_modifier": "Rarity modifier", diff --git a/system/lang/es-es.json b/system/lang/es-es.json index c7dbcb8..5d38090 100644 --- a/system/lang/es-es.json +++ b/system/lang/es-es.json @@ -329,7 +329,8 @@ "cost": "Coste", "spent": "Gastado", "saved": "Guardado", - "total_xp_rank": "PE gastados", + "total_xp_spent": "PE gastados", + "total_xp_curriculum": "Xp spent on curriculum", "curriculum": "En programa de estudio", "curriculum_validate": "Completar este rango", "rarity_modifier": "Rarity modifier", diff --git a/system/lang/fr-fr.json b/system/lang/fr-fr.json index 1daca5c..04ab8a3 100644 --- a/system/lang/fr-fr.json +++ b/system/lang/fr-fr.json @@ -329,7 +329,8 @@ "cost": "Coût", "spent": "Dépensée", "saved": "Restante", - "total_xp_rank": "Xp dépensée", + "total_xp_spent": "Xp dépensée", + "total_xp_curriculum": "Xp dépensée dans le cursus", "curriculum": "Inclus dans le cursus", "curriculum_validate": "Valider la progression", "rarity_modifier": "Modificateur de rareté", diff --git a/system/scripts/actors/character-sheet.js b/system/scripts/actors/character-sheet.js index e6d1a5f..1410af4 100644 --- a/system/scripts/actors/character-sheet.js +++ b/system/scripts/actors/character-sheet.js @@ -111,25 +111,24 @@ export class CharacterSheetL5r5e extends BaseSheetL5r5e { sheetData.items .filter((item) => ["peculiarity", "technique", "advancement"].includes(item.type)) .forEach((item) => { - let xp = parseInt(item.data.xp_used) || 0; - sheetData.data.data.xp_spent = parseInt(sheetData.data.data.xp_spent) + xp; - - // if not in curriculum, xp spent /2 for this item - if (!item.data.in_curriculum && xp > 0) { - xp = Math.ceil(xp / 2); - } + const { xp_used_total, xp_used } = game.l5r5e.HelpersL5r5e.getItemsXpCost(item); + sheetData.data.data.xp_spent += xp_used_total; const rank = Math.max(0, item.data.bought_at_rank); if (!adv[rank]) { adv[rank] = { rank: rank, - spent: 0, + spent: { + total: 0, + curriculum: 0, + }, goal: CONFIG.l5r5e.xp.costPerRank[rank] || null, list: [], }; } adv[rank].list.push(item); - adv[rank].spent = adv[rank].spent + xp; + adv[rank].spent.total += xp_used_total; + adv[rank].spent.curriculum += xp_used; }); sheetData.data.advancementsListByRank = adv; } @@ -146,15 +145,14 @@ export class CharacterSheetL5r5e extends BaseSheetL5r5e { // Sort by rank desc sheetData.data.advancementsOthers.sort((a, b) => (b.data.rank || 0) - (a.data.rank || 0)); - // Total xp spent + // Total xp spent in curriculum & total sheetData.data.advancementsOthersTotalXp = sheetData.data.advancementsOthers.reduce( - (acc, item) => acc + (item.data.xp_used || 0), + (acc, item) => acc + parseInt(item.data.xp_used_total || item.data.xp_used || 0), 0 ); // Update the total spent - sheetData.data.data.xp_spent = - parseInt(sheetData.data.data.xp_spent) + sheetData.data.advancementsOthersTotalXp; + sheetData.data.data.xp_spent += sheetData.data.advancementsOthersTotalXp; } /** diff --git a/system/scripts/helpers.js b/system/scripts/helpers.js index 8a89cf2..7be6515 100644 --- a/system/scripts/helpers.js +++ b/system/scripts/helpers.js @@ -331,4 +331,34 @@ export class HelpersL5r5e { } }); } + + /** + * Compute the Xp cost for cursus and total + * @param {ItemL5r5e|ItemL5r5e[]} itemsList Item Data + * @return {{xp_used_total: number, xp_used: number}} + */ + static getItemsXpCost(itemsList) { + let xp_used = 0; + let xp_used_total = 0; + + if (!Array.isArray(itemsList)) { + itemsList = [itemsList]; + } + + itemsList.forEach((item) => { + let xp = parseInt(item.data.xp_used_total || item.data.xp_used || 0); + + // Full price + xp_used_total += xp; + + // if not in curriculum, xp spent /2 for this item + if (!item.data.in_curriculum && xp > 0) { + xp = Math.ceil(xp / 2); + } + + // Halved or full + xp_used += xp; + }); + return { xp_used, xp_used_total }; + } } diff --git a/system/scripts/items/title-sheet.js b/system/scripts/items/title-sheet.js index 948849e..a0e39bb 100644 --- a/system/scripts/items/title-sheet.js +++ b/system/scripts/items/title-sheet.js @@ -24,11 +24,10 @@ export class TitleSheetL5r5e extends ItemSheetL5r5e { // Prepare OwnedItems sheetData.data.embedItemsList = this._prepareEmbedItems(sheetData.data.data.items); - // Automatically compute the xp cost - sheetData.data.data.xp_used = sheetData.data.embedItemsList.reduce( - (acc, item) => acc + (+item.data.xp_used || 0), - 0 - ); + // Automatically compute the total xp cost (full price) and XP in title (cursus, some halved prices) + const { xp_used_total, xp_used } = game.l5r5e.HelpersL5r5e.getItemsXpCost(sheetData.data.embedItemsList); + sheetData.data.data.xp_used_total = xp_used_total; + sheetData.data.data.xp_used = xp_used; return sheetData; } @@ -91,10 +90,11 @@ export class TitleSheetL5r5e extends ItemSheetL5r5e { return; } - // *** Items : add, edit, delete *** + // *** Sub-Items management *** html.find(".item-add").on("click", this._addSubItem.bind(this)); html.find(`.item-edit`).on("click", this._editSubItem.bind(this)); html.find(`.item-delete`).on("click", this._deleteSubItem.bind(this)); + html.find(`.item-curriculum`).on("click", this._switchSubItemCurriculum.bind(this)); } /** @@ -104,6 +104,9 @@ export class TitleSheetL5r5e extends ItemSheetL5r5e { * @private */ async _addSubItem(event) { + event.preventDefault(); + event.stopPropagation(); + // Show Dialog const selectedType = await game.l5r5e.HelpersL5r5e.showSubItemDialog(["advancement", "technique"]); if (!selectedType) { @@ -125,4 +128,25 @@ export class TitleSheetL5r5e extends ItemSheetL5r5e { item.sheet.render(true); } } + + /** + * Toogle the curriculum for this embed item + * @param {Event} event + * @return {Promise} + * @private + */ + async _switchSubItemCurriculum(event) { + event.preventDefault(); + event.stopPropagation(); + + const itemId = $(event.currentTarget).data("item-id"); + const item = this.document.getEmbedItem(itemId); + if (!item) { + return; + } + + // Switch the state and update + item.data.data.in_curriculum = !item.data.data.in_curriculum; + return this.document.updateEmbedItem(item); + } } diff --git a/system/template.json b/system/template.json index 1440f54..fc73084 100644 --- a/system/template.json +++ b/system/template.json @@ -216,7 +216,8 @@ }, "title": { "templates": ["basics", "advancement"], - "advancements": [] + "advancements": [], + "xp_used_total": 0 }, "bond": { "templates": ["basics", "advancement"], diff --git a/system/templates/actors/character/advancement-others.html b/system/templates/actors/character/advancement-others.html index 062b24e..d232370 100644 --- a/system/templates/actors/character/advancement-others.html +++ b/system/templates/actors/character/advancement-others.html @@ -1,10 +1,12 @@ + {{#if show_curriculum_toggle}}{{#if advancement.data.in_curriculum}} {{/if}}{{/if}} {{advancement.name}}{{#if advancement.data.bond_type}} ({{advancement.data.bond_type}}){{/if}} - {{advancement.data.xp_used}} + {{#if advancement.data.xp_used_total}}{{advancement.data.xp_used_total}}{{else}}{{advancement.data.xp_used}}{{/if}} {{advancement.data.rank}} {{#if editable}} diff --git a/system/templates/actors/character/advancement-school.html b/system/templates/actors/character/advancement-school.html index d86349d..d26c76c 100644 --- a/system/templates/actors/character/advancement-school.html +++ b/system/templates/actors/character/advancement-school.html @@ -1,6 +1,6 @@ {{#if advancement.data.in_curriculum}} {{/if}} - {{advancement.name}} + {{advancement.name}} {{advancement.data.xp_used}} {{advancement.data.rank}} {{#if editable}} diff --git a/system/templates/actors/character/experience.html b/system/templates/actors/character/experience.html index 1e6a548..92e6145 100644 --- a/system/templates/actors/character/experience.html +++ b/system/templates/actors/character/experience.html @@ -53,7 +53,8 @@ {{/ifCond}} - {{localize 'l5r5e.advancements.total_xp_rank'}} : {{rankObject.spent}}{{#if rankObject.goal}} / {{rankObject.goal}}{{/if}} + {{localize 'l5r5e.advancements.total_xp_curriculum'}} : {{rankObject.spent.curriculum}}{{#if rankObject.goal}} / {{rankObject.goal}}{{/if}} +
{{localize 'l5r5e.advancements.total_xp_spent'}} : {{rankObject.spent.total}} {{/ifCond}} @@ -77,13 +78,13 @@ {{#each data.advancementsOthers as |advancement advancementId|}} - {{> 'systems/l5r5e/templates/actors/character/advancement-others.html' advancement=advancement editable=../options.editable}} + {{> 'systems/l5r5e/templates/actors/character/advancement-others.html' advancement=advancement show_curriculum_toggle=false editable=../options.editable}} {{/each}} - {{localize 'l5r5e.advancements.total_xp_rank'}} : {{data.advancementsOthersTotalXp}} + {{localize 'l5r5e.advancements.total_xp_spent'}} : {{data.advancementsOthersTotalXp}} diff --git a/system/templates/actors/npc/narrative.html b/system/templates/actors/npc/narrative.html index 630e8d2..fa434e0 100644 --- a/system/templates/actors/npc/narrative.html +++ b/system/templates/actors/npc/narrative.html @@ -31,7 +31,7 @@ {{!-- bonds --}}
- {{localize 'l5r5e.social.bonds' }} + {{localize 'l5r5e.social.bonds'}} {{#if options.editable}} {{/if}} diff --git a/system/templates/items/title/title-sheet.html b/system/templates/items/title/title-sheet.html index a0da636..3ef9634 100644 --- a/system/templates/items/title/title-sheet.html +++ b/system/templates/items/title/title-sheet.html @@ -1,4 +1,6 @@
+ +

@@ -16,10 +18,6 @@ {{localize 'l5r5e.advancements.cost'}} -