export default class KHDiceRoller { async rollSpellCostInChat(cost, speaker) { let formula = cost + "d6" const roll = new Roll(formula); let res = roll.roll(); let rollData = { name: "SPELL.COST", res: res, showFormula: 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, }, }); } async rollDamageInChat(facesNr, speaker) { let formula = facesNr + "d6x6" const roll = new Roll(formula); let res = roll.roll(); let rollData = { name: "ITEM.DAMAGE", res: res, showFormula: 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, }, }); } async rollArmorInChat(armor, hasHelmet, speaker) { let formula = armor + "d6" if(hasHelmet) { formula += "x6" } const roll = new Roll(formula); let res = roll.roll(); let rollData = { name: "ITEM.DEFENCE", res: res, showFormula: 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, }, }); } async rollSkillInChat(skillName, skillValue, showValue, speaker, openclosed) { const roll = new Roll(`1d100`); let res = roll.roll(); let computedName = skillName if(showValue) { computedName += " (" + skillValue + ")" } if(openclosed === undefined) { openclosed = 0 } let rollData = { name: computedName, res: res }; 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, }, }); } 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); } }