First row of tests and fixes

This commit is contained in:
2026-03-05 22:50:53 +01:00
parent 12458925a1
commit f28df2ae76
23 changed files with 442 additions and 126 deletions
+11 -27
View File
@@ -1,3 +1,5 @@
import AwERoll from "./roll.mjs"
export default class AwEActor extends Actor {
/** @override */
prepareData() {
@@ -33,38 +35,20 @@ export default class AwEActor extends Actor {
/**
* Roll an attribute check.
* @param {string} attributeId - The attribute to roll.
* @param {object} options - Roll options.
* @returns {Promise<Roll>} The roll result.
* @param {object} options - Roll options (passed through to AwERoll.prompt).
* @returns {Promise<AwERoll|null>} The evaluated roll, or null if cancelled.
*/
async rollAttribute(attributeId, options = {}) {
const attribute = this.system.attributes[attributeId]
if (!attribute) return null
const mod = attribute.mod ?? 0
const formula = `1d20 + ${mod}`
const roll = new Roll(formula)
await roll.evaluate()
// Determine outcome vs DC if provided
let outcome = null
if (options.dc !== undefined) {
const total = roll.total
const dc = options.dc
if (total >= dc + 10) outcome = "criticalSuccess"
else if (total >= dc) outcome = "success"
else if (total <= dc - 10) outcome = "criticalFailure"
else outcome = "failure"
}
// Send to chat
const attrLabel = attributeId.charAt(0).toUpperCase() + attributeId.slice(1)
const flavor = options.flavor || game.i18n.localize(`AWEMMY.Attribute.${attrLabel}`)
await roll.toMessage({
speaker: ChatMessage.getSpeaker({ actor: this }),
flavor,
rollMode: game.settings.get("core", "rollMode")
return AwERoll.prompt({
attributeKey: attributeId,
modifier: attribute.mod ?? 0,
actorId: this.id,
actorName: this.name,
actorImage: this.img,
...options
})
return { roll, outcome }
}
}
+21 -3
View File
@@ -1,7 +1,25 @@
import AwERoll from "./roll.mjs"
export default class AwEChatMessage extends ChatMessage {
/** @override */
async getHTML(...args) {
const html = await super.getHTML(...args)
return html
async _renderRollContent(messageData) {
if (this.rolls[0] instanceof AwERoll) {
const isPrivate = !this.isContentVisible
const roll = this.rolls[0]
const html = await foundry.applications.handlebars.renderTemplate(
AwERoll.CHAT_TEMPLATE,
{
flavor: this.flavor,
total: roll.total,
outcome: isPrivate ? null : roll.outcome,
dc: roll.dc,
isPrivate
}
)
messageData.message.content = html
return
}
return super._renderRollContent(messageData)
}
}
+133 -2
View File
@@ -1,5 +1,136 @@
import { SYSTEM } from "../config/system.mjs"
export default class AwERoll extends Roll {
constructor(formula, data = {}, options = {}) {
super(formula, data, options)
/** @type {string} */
static CHAT_TEMPLATE = "systems/fvtt-adventures-with-emmy/templates/chat-message.hbs"
// --- Accessors for roll options ---
get attributeKey() { return this.options.attributeKey }
get rollName() { return this.options.rollName }
get modifier() { return this.options.modifier ?? 0 }
get bonus() { return this.options.bonus ?? 0 }
get dc() { return this.options.dc }
get outcome() { return this.options.outcome }
get actorId() { return this.options.actorId }
get actorName() { return this.options.actorName }
get actorImage() { return this.options.actorImage }
// --- Outcome calculation ---
/**
* Compute the degree of success for a d20 check.
* - Critical Success : total ≥ dc + 10, OR natural 20 upgrades result
* - Success : total ≥ dc
* - Failure : total < dc
* - Critical Failure : total ≤ dc 10, OR natural 1 downgrades result
*
* @param {number} total The final roll total (d20 + modifiers)
* @param {number} dc The Difficulty Class to compare against
* @param {number} d20Value The raw d20 result (for nat-20 / nat-1 adjustment)
* @returns {"criticalSuccess"|"success"|"failure"|"criticalFailure"}
*/
static computeOutcome(total, dc, d20Value) {
const DEGREES = ["criticalFailure", "failure", "success", "criticalSuccess"]
let idx
if (total >= dc + 10) idx = 3
else if (total >= dc) idx = 2
else if (total <= dc - 10) idx = 0
else idx = 1
if (d20Value === 20) idx = Math.min(3, idx + 1)
if (d20Value === 1) idx = Math.max(0, idx - 1)
return DEGREES[idx]
}
// --- Prompt dialog ---
/**
* Open a dialog so the player can add a situational bonus and choose visibility,
* then evaluate and post the roll to chat.
*
* @param {object} options
* @param {string} options.attributeKey Attribute id (agility | fitness | awareness | influence)
* @param {number} options.modifier Base attribute modifier
* @param {number} [options.dc] Pre-set DC (skips DC field if provided)
* @param {string} options.actorId
* @param {string} options.actorName
* @param {string} options.actorImage
* @returns {Promise<AwERoll|null>} Resolved roll, or null if dialog was cancelled
*/
static async prompt(options = {}) {
const attrLabel = options.attributeKey
? game.i18n.localize(SYSTEM.ATTRIBUTES[options.attributeKey]?.label ?? options.attributeKey)
: (options.rollName ?? game.i18n.localize("AWEMMY.Roll.Check"))
const rollModes = Object.fromEntries(
Object.entries(CONFIG.Dice.rollModes).map(([k, v]) => [k, game.i18n.localize(v.label ?? v)])
)
const content = await foundry.applications.handlebars.renderTemplate(
"systems/fvtt-adventures-with-emmy/templates/roll-dialog.hbs",
{
attrLabel,
modifier: options.modifier ?? 0,
dc: options.dc ?? "",
rollModes,
visibility: game.settings.get("core", "rollMode")
}
)
const result = await foundry.applications.api.DialogV2.wait({
window: { title: game.i18n.format("AWEMMY.Roll.DialogTitle", { name: attrLabel }) },
classes: ["awemmy"],
content,
buttons: [{
label: game.i18n.localize("AWEMMY.Roll.Roll"),
callback: (_event, button) =>
Array.from(button.form.elements).reduce((obj, input) => {
if (input.name) obj[input.name] = input.value
return obj
}, {})
}],
rejectClose: false
})
if (!result) return null
const mod = options.modifier ?? 0
const bonus = parseInt(result.bonus) || 0
const dc = result.dc !== "" ? parseInt(result.dc) : undefined
const rollMode = result.visibility ?? game.settings.get("core", "rollMode")
// Build formula: 1d20 + mod [± bonus]
let formula = `1d20 + ${mod}`
if (bonus > 0) formula += ` + ${bonus}`
else if (bonus < 0) formula += ` - ${Math.abs(bonus)}`
const roll = new this(formula, {}, {
attributeKey: options.attributeKey,
rollName: attrLabel,
modifier: mod,
bonus,
dc,
actorId: options.actorId,
actorName: options.actorName,
actorImage: options.actorImage
})
await roll.evaluate()
// Compute degree of success when a DC is known
if (dc !== undefined) {
const d20Value = roll.dice[0]?.results[0]?.result ?? 0
roll.options.outcome = AwERoll.computeOutcome(roll.total, dc, d20Value)
}
await roll.toMessage({
speaker: ChatMessage.getSpeaker({ actor: game.actors.get(options.actorId) }),
flavor: attrLabel,
rollMode
})
return roll
}
}