89 lines
2.9 KiB
JavaScript
89 lines
2.9 KiB
JavaScript
|
import "../xregexp-all.js";
|
||
|
import { ACTOR_TYPES } from "../../item.js";
|
||
|
import { RdDCarac } from "../../rdd-carac.js";
|
||
|
import { RdDItemCompetence } from "../../item-competence.js";
|
||
|
import { RdDUtility } from "../../rdd-utility.js";
|
||
|
import { TextRollManager } from "./text-roll-formatter.js";
|
||
|
|
||
|
const REGEXP_ROLL_CARAC_COMP = "(?<carac>[A-Za-zÀ-ÖØ-öø-ÿ\\s\\-]+)(\\/(?<competence>[A-Za-zÀ-ÖØ-öø-ÿ\\s\\-]+))?(/(?<diff>[\\+\\-]?\\d+))?"
|
||
|
const XREGEXP_ROLL_CARAC_COMP = XRegExp("@roll\\[" + REGEXP_ROLL_CARAC_COMP + "\\]", 'giu')
|
||
|
|
||
|
/**
|
||
|
* classe pour gérer les jets de caractéristique/compétence depuis
|
||
|
* les journaux/descriptions
|
||
|
*/
|
||
|
export class TextRollCaracCompetence {
|
||
|
get code() { return 'carac' }
|
||
|
get template() { return `systems/foundryvtt-reve-de-dragon/templates/apps/textroll/link-text-roll-carac-competence.hbs` }
|
||
|
|
||
|
async onReplaceRoll(context) {
|
||
|
const handler = new CaracCompetenceTextBuilder(context)
|
||
|
return await handler.replaceAll()
|
||
|
}
|
||
|
|
||
|
async onRollText(event, actor) {
|
||
|
const node = TextRollManager.getNode(event)
|
||
|
const caracCode = node.data('carac-code')
|
||
|
if (caracCode) {
|
||
|
const competence = node.data('competence')
|
||
|
const diff = node.data('diff')
|
||
|
const actors = this.getSelectedActors(actor)
|
||
|
actors.forEach(async it => await this.doRoll(it, caracCode, competence, diff))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async doRoll(actor, caracCode, competence, diff) {
|
||
|
caracCode = actor.mapCarac(caracCode)
|
||
|
if (competence) {
|
||
|
if (actor.type == ACTOR_TYPES.personnage) {
|
||
|
await actor.rollCaracCompetence(caracCode, competence, diff)
|
||
|
}
|
||
|
else {
|
||
|
await actor.doRollCaracCompetence(caracCode, competence, diff)
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
await actor.rollCarac(caracCode, { diff })
|
||
|
}
|
||
|
}
|
||
|
|
||
|
getSelectedActors(actor) {
|
||
|
const selected = canvas.tokens.controlled.map(it => it.actor).filter(it => it)
|
||
|
if (selected.length > 0) {
|
||
|
return selected
|
||
|
}
|
||
|
actor = actor ?? RdDUtility.getSelectedActor()
|
||
|
if (actor) {
|
||
|
return [actor]
|
||
|
}
|
||
|
return []
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class CaracCompetenceTextBuilder {
|
||
|
constructor(context) {
|
||
|
this.context = context
|
||
|
}
|
||
|
|
||
|
async replaceAll() {
|
||
|
await XRegExp.forEach(this.context.text, XREGEXP_ROLL_CARAC_COMP, async (rollMatch, i) => await this.replaceMatch(rollMatch, i))
|
||
|
return this.context.text
|
||
|
}
|
||
|
|
||
|
async replaceMatch(rollMatch, i) {
|
||
|
const carac = RdDCarac.caracDetails(rollMatch.carac)
|
||
|
if (carac) {
|
||
|
const competence = rollMatch.competence ? RdDItemCompetence.findCompetence(this.context.competences, rollMatch.competence) : undefined
|
||
|
const replacement = await TextRollManager.createRollText(this.context.template,
|
||
|
{
|
||
|
code: this.context.code,
|
||
|
carac: carac,
|
||
|
competence: competence?.name,
|
||
|
diff: rollMatch.diff,
|
||
|
})
|
||
|
this.context.text = this.context.text.replace(rollMatch[0], replacement)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|