From 642c368f38099090e1f605273c6c1f890f83d3a5 Mon Sep 17 00:00:00 2001 From: U~man Date: Tue, 21 Jul 2020 20:57:16 +0200 Subject: [PATCH] ENH: Creation summary --- src/lang/en.json | 1 - src/module/dialog/character-creation.js | 50 +++++++++++++++++++------ src/templates/chat/roll-creation.html | 29 ++++++++++++++ 3 files changed, 68 insertions(+), 12 deletions(-) create mode 100644 src/templates/chat/roll-creation.html diff --git a/src/lang/en.json b/src/lang/en.json index c7f93ad..608a975 100644 --- a/src/lang/en.json +++ b/src/lang/en.json @@ -252,7 +252,6 @@ "OSE.messages.InflictsDamage": "Inflicts damage!", "OSE.messages.applyDamage": "Apply Damage", "OSE.messages.applyHealing": "Apply Healing", - "OSE.messages.creationFinished": "Character {name} creation finished", "OSE.colors.green": "Green", "OSE.colors.red": "Red", diff --git a/src/module/dialog/character-creation.js b/src/module/dialog/character-creation.js index 893c8a5..c092b7a 100644 --- a/src/module/dialog/character-creation.js +++ b/src/module/dialog/character-creation.js @@ -5,7 +5,7 @@ export class OseCharacterCreator extends FormApplication { static get defaultOptions() { const options = super.defaultOptions; options.classes = ["ose", "dialog", "creator"], - options.id = 'character-creator'; + options.id = 'character-creator'; options.template = 'systems/ose/templates/actors/dialogs/character-creation.html'; options.width = 235; @@ -41,6 +41,11 @@ export class OseCharacterCreator extends FormApplication { con: 0, gold: 0 } + data.stats = { + sum: 0, + avg: 0, + std: 0 + } return data; } @@ -56,20 +61,27 @@ export class OseCharacterCreator extends FormApplication { }) let n = values.length; - let sum = values.reduce((a,b) => a+b); + let sum = values.reduce((a, b) => a + b); let mean = parseFloat(sum) / n; - let std = Math.sqrt(values.map(x => Math.pow(x-mean,2)).reduce((a,b) => a+b)/n); + let std = Math.sqrt(values.map(x => Math.pow(x - mean, 2)).reduce((a, b) => a + b) / n); let stats = list.siblings('.roll-stats'); stats.find('.sum').text(sum); stats.find('.avg').text(Math.round(10 * sum / n) / 10); stats.find('.std').text(Math.round(100 * std) / 100); + if (n >= 6) { $(ev.currentTarget).closest('form').find('button[type="submit"]').removeAttr('disabled'); } + + this.object.data.stats = { + sum: sum, + avg: Math.round(10 * sum / n) / 10, + std: Math.round(100 * std) / 100 + } } - rollScore(score, options={}) { + rollScore(score, options = {}) { // Increase counter this.object.data.counters[score]++; @@ -90,16 +102,32 @@ export class OseCharacterCreator extends FormApplication { data: data, skipDialog: true, speaker: ChatMessage.getSpeaker({ actor: this }), - flavor: game.i18n.format('OSE.dialog.generateScore', {score: label, count: this.object.data.counters[score]}), - title: game.i18n.format('OSE.dialog.generateScore', {score: label, count: this.object.data.counters[score]}), + flavor: game.i18n.format('OSE.dialog.generateScore', { score: label, count: this.object.data.counters[score] }), + title: game.i18n.format('OSE.dialog.generateScore', { score: label, count: this.object.data.counters[score] }), }); } async close() { super.close(); + // Gather scores + let scores = {}; + $(this.form.children).find(".score-roll").each((_, d) => { + let gr = $(d).closest('.form-group'); + let val = gr.find(".score-value").val(); + scores[gr.data("score")] = val; + }) + const gold = $(this.form.children).find('.gold-value').val(); const speaker = ChatMessage.getSpeaker({ actor: this }); + const templateData = { + config: CONFIG.OSE, + scores: scores, + title: game.i18n.localize("OSE.dialog.generator"), + stats: this.object.data.stats, + gold: gold + } + const content = await renderTemplate("/systems/ose/templates/chat/roll-creation.html", templateData) ChatMessage.create({ - content: game.i18n.format("OSE.messages.creationFinished", {name: this.object.name}), + content: content, speaker, }); } @@ -110,14 +138,14 @@ export class OseCharacterCreator extends FormApplication { html.find('a.score-roll').click((ev) => { let el = ev.currentTarget.parentElement.parentElement; let score = el.dataset.score; - this.rollScore(score, {event: ev}).then(r => { + this.rollScore(score, { event: ev }).then(r => { $(el).find('input').val(r.total).trigger('change'); }); }); html.find('a.gold-roll').click((ev) => { let el = ev.currentTarget.parentElement.parentElement.parentElement; - this.rollScore("gold", {event: ev}).then(r => { + this.rollScore("gold", { event: ev }).then(r => { $(el).find('.gold-value').val(r.total * 10); }); }); @@ -127,8 +155,8 @@ export class OseCharacterCreator extends FormApplication { }) } - async _onSubmit(event, {updateData=null, preventClose=false, preventRender=false}={}) { - super._onSubmit(event, {updateData: updateData, preventClose: preventClose, preventRender: preventRender}); + async _onSubmit(event, { updateData = null, preventClose = false, preventRender = false } = {}) { + super._onSubmit(event, { updateData: updateData, preventClose: preventClose, preventRender: preventRender }); // Generate gold let gold = event.target.elements.namedItem('gold').value; const itemData = { diff --git a/src/templates/chat/roll-creation.html b/src/templates/chat/roll-creation.html new file mode 100644 index 0000000..1f6b565 --- /dev/null +++ b/src/templates/chat/roll-creation.html @@ -0,0 +1,29 @@ +
+
+
+

{{title}}

+
+
+
+
    + {{#each scores as |score id|}} +
  1. +
    + {{lookup ../config.scores id}} +
    +
    + {{score}} +
    +
  2. + {{/each}} +
+
+
Sum: {{stats.sum}}
+
Avg: {{stats.avg}}
+
σ: {{stats.std}}
+
+
Gold: {{gold}}
+
+
+
+
\ No newline at end of file