From bec12184dabbd7a29c71426730cb0200b3e65e38 Mon Sep 17 00:00:00 2001 From: Erebus Date: Tue, 1 Dec 2020 04:12:47 +0100 Subject: [PATCH] Added roll dialog and exceptional rolls --- lang/en.json | 8 ++ lang/sv.json | 9 ++ module/actors/actor-sheet.js | 62 ++++++++++++- module/helpers/dice-helper.js | 130 ++++++++++++++++++++++++++- templates/dice/roll.html | 14 ++- templates/parts/actor/gear.html | 2 +- templates/parts/actor/ht-skills.html | 4 +- templates/parts/actor/skills.html | 6 +- templates/roll-dialog.html | 53 +++++++++++ 9 files changed, 275 insertions(+), 13 deletions(-) create mode 100644 templates/roll-dialog.html diff --git a/lang/en.json b/lang/en.json index 443a221..3433f91 100644 --- a/lang/en.json +++ b/lang/en.json @@ -62,6 +62,14 @@ "ROLL.SUCCESS": "Success", "ROLL.FAILURE": "Failure", + "ROLL.TITLE": "Roll dice", + "BUTTON.ROLL": "Roll", + "BUTTON.CANCEL": "Cancel", + "ROLL.CLOSED": "Closed", + "ROLL.OPENED": "Opened", + "MENU.SHOWROLLDIALOG": "Show diceroller dialog", + "ROLL.OPENCLOSE": "Open / Close", + "ROLL.EXCEPTIONAL": "Exceptional", "SKILL.TYPE": "Type", "SKILL.BASE": "Basic", diff --git a/lang/sv.json b/lang/sv.json index fd20903..7216551 100644 --- a/lang/sv.json +++ b/lang/sv.json @@ -64,6 +64,15 @@ "ROLL.SUCCESS": "Lyckat", "ROLL.FAILURE": "Misslyckat", + "ROLL.TITLE": "Slå tärningar", + "BUTTON.ROLL": "Slå", + "BUTTON.CANCEL": "Avbryt", + "ROLL.CLOSED": "Stängd", + "ROLL.OPENED": "Öppnad", + "MENU.SHOWROLLDIALOG": "Visa tärningsdialog", + "ROLL.OPENCLOSE": "Öppna / Stäng", + "ROLL.EXCEPTIONAL": "Exceptionelt", + "SKILL.TYPE": "Typ", "SKILL.BASE": "Grundfärdigheter", "SKILL.ADVENTURE": "Äventyrsfärdigheter", diff --git a/module/actors/actor-sheet.js b/module/actors/actor-sheet.js index 2d94178..b57b145 100644 --- a/module/actors/actor-sheet.js +++ b/module/actors/actor-sheet.js @@ -139,6 +139,64 @@ export class ActorSheetKH extends ActorSheet { }, ]); + new ContextMenu(html, "div.item-weapon", [ + { + name: game.i18n.localize("MENU.SHOWROLLDIALOG"), + icon: '', + callback: (li) => { + let itemId = li.data("itemId"); + let _item = this.actor.items.find((element) => element._id == itemId); + + let skillName = _item.name + let skillValue = _item.data.data.value + let showValue = false + + if(this.actor.data.type === "character") { + showValue = true + } + + this.khRoller.rollSkillDialogInChat(skillName, skillValue, showValue, this.actor) + }, + }, + { + name: game.i18n.localize("MENU.SENTTOCHAT"), + icon: '', + callback: (li) => { + let itemId = li.data("itemId"); + this._itemDetailsToChat(itemId); + }, + }, + ]); + + new ContextMenu(html, "li.item-skill", [ + { + name: game.i18n.localize("MENU.SHOWROLLDIALOG"), + icon: '', + callback: (li) => { + let itemId = li.data("itemId"); + let _item = this.actor.items.find((element) => element._id == itemId); + + let skillName = _item.name + let skillValue = _item.data.data.value + let showValue = false + + if(this.actor.data.type === "character") { + showValue = true + } + + this.khRoller.rollSkillDialogInChat(skillName, skillValue, showValue, this.actor) + }, + }, + { + name: game.i18n.localize("MENU.SENTTOCHAT"), + icon: '', + callback: (li) => { + let itemId = li.data("itemId"); + this._itemDetailsToChat(itemId); + }, + }, + ]); + html.find(".feature").click(async (ev) => { const featureName = $(ev.currentTarget).data("feature"); const featureValue = this.actor.data.data.feature[featureName].value; @@ -209,7 +267,7 @@ export class ActorSheetKH extends ActorSheet { /* Roll skill */ html.find(".roll-skill").click((ev) => { - const li = $(ev.currentTarget).parents(".item"); + const li = $(ev.currentTarget).parents(".item-skill"); let itemId = li.data("itemId"); let _item = this.actor.items.find((element) => element._id == itemId); @@ -226,7 +284,7 @@ export class ActorSheetKH extends ActorSheet { /* Roll weapon skill */ html.find(".roll-weapon-skill").click((ev) => { - const li = $(ev.currentTarget).parents(".item"); + const li = $(ev.currentTarget).parents(".item-weapon"); let itemId = li.data("itemId"); let weapon = this.actor.getOwnedItem(itemId); diff --git a/module/helpers/dice-helper.js b/module/helpers/dice-helper.js index 228791a..3784511 100644 --- a/module/helpers/dice-helper.js +++ b/module/helpers/dice-helper.js @@ -84,7 +84,7 @@ export default class KHDiceRoller { }); } - async rollSkillInChat(skillName, skillValue, showValue, speaker) { + async rollSkillInChat(skillName, skillValue, showValue, speaker, openclosed) { const roll = new Roll(`1d100`); let res = roll.roll(); @@ -95,16 +95,43 @@ export default class KHDiceRoller { computedName += " (" + skillValue + ")" } + if(openclosed === undefined) { + openclosed = 0 + } + let rollData = { name: computedName, res: res }; if(skillValue > 0) { - if(res.total <= skillValue) { + let oneRes = Math.floor((res.total / 1) % 10); + let tenRes = Math.floor((res.total / 10) % 10); + + if(openclosed < 0) { + rollData.closed = Math.abs(openclosed) + } + + if(openclosed > 0) { + rollData.opened = Math.abs(openclosed) + } + + if(openclosed < 0 && oneRes !== 0 && Math.abs(openclosed) >= oneRes) { + // roll is closed + rollData.failure = true + } else if(openclosed > 0 && oneRes !== 0 && Math.abs(openclosed) >= oneRes){ + // roll is opened rollData.success = true } else { - rollData.failure = true + if (res.total <= skillValue) { + rollData.success = true + } else { + rollData.failure = true + } + } + + if(oneRes === tenRes) { + rollData.excetional = true } } @@ -121,4 +148,101 @@ export default class KHDiceRoller { }, }); } + + async rollSkillDialogInChat(skillName, skillValue, showValue, speaker, startopen) { + const id = randomID(); + + if(startopen === undefined) { + startopen = 0 + } + + const content = await renderTemplate("systems/kopparhavet/templates/roll-dialog.html", { + id, + startopen, + skillName, + skillValue, + }); + + await new Dialog({ + title: game.i18n.localize("ROLL.TITLE"), + content, + buttons: { + one: { + icon: '', + label: game.i18n.localize("BUTTON.ROLL"), + callback: async () => { + const container = document.getElementById(id); + let openclosed = container.querySelector('[name="openclosed"]').value + + const roll = new Roll(`1d100`); + + let res = roll.roll(); + + let computedName = skillName + + if(showValue) { + computedName += " (" + skillValue + ")" + } + + let rollData = { + name: computedName, + res: res + }; + + // Evaluate result only if we have a positive skillvalue + if(skillValue > 0) { + let oneRes = Math.floor((res.total / 1) % 10); + let tenRes = Math.floor((res.total / 10) % 10); + + if(openclosed < 0) { + rollData.closed = Math.abs(openclosed) + } + + if(openclosed > 0) { + rollData.opened = Math.abs(openclosed) + } + + if(openclosed < 0 && oneRes !== 0 && Math.abs(openclosed) >= oneRes) { + // roll is closed + rollData.failure = true + } else if(openclosed > 0 && oneRes !== 0 && Math.abs(openclosed) >= oneRes){ + // roll is opened + rollData.success = true + } else { + if (res.total <= skillValue) { + rollData.success = true + } else { + rollData.failure = true + } + } + + if(oneRes === tenRes) { + rollData.excetional = true + } + } + + const html = await renderTemplate("systems/kopparhavet/templates/dice/roll.html", rollData); + + await roll.toMessage({ + create: true, + content: html, + user: game.user._id, + speaker: { + actor: speaker._id, + token: speaker.token, + alias: speaker.name, + }, + }); + }, + }, + two: { + icon: '', + label: game.i18n.localize("BUTTON.CANCEL"), + }, + }, + }, + { + classes: ["dialog", "kopparhavet"], + }).render(true); + } } \ No newline at end of file diff --git a/templates/dice/roll.html b/templates/dice/roll.html index 1b79561..0a492bc 100644 --- a/templates/dice/roll.html +++ b/templates/dice/roll.html @@ -4,6 +4,16 @@
+ {{#if closed}} +
+ {{localize "ROLL.CLOSED"}}: {{closed}} +
+ {{/if}} + {{#if opened}} +
+ {{localize "ROLL.OPENED"}}: {{opened}} +
+ {{/if}}
{{#if showFormula}}
{{res.formula}}
@@ -14,12 +24,12 @@
{{#if success}}
- {{localize "ROLL.SUCCESS"}} + {{#if excetional}}{{localize "ROLL.EXCEPTIONAL"}}{{/if}} {{localize "ROLL.SUCCESS"}}
{{/if}} {{#if failure}}
- {{localize "ROLL.FAILURE"}} + {{#if excetional}}{{localize "ROLL.EXCEPTIONAL"}}{{/if}} {{localize "ROLL.FAILURE"}}
{{/if}}
diff --git a/templates/parts/actor/gear.html b/templates/parts/actor/gear.html index f297b34..63fd95c 100644 --- a/templates/parts/actor/gear.html +++ b/templates/parts/actor/gear.html @@ -20,7 +20,7 @@