Fixed rerolling of duality dice

This commit is contained in:
WBHarry 2025-07-22 19:54:23 +02:00
parent 480d04fee5
commit dfd10da324
9 changed files with 93 additions and 54 deletions

View file

@ -2,14 +2,14 @@ import { SYSTEM } from './module/config/system.mjs';
import * as applications from './module/applications/_module.mjs';
import * as models from './module/data/_module.mjs';
import * as documents from './module/documents/_module.mjs';
import * as dice from './module/dice/_module.mjs';
import RegisterHandlebarsHelpers from './module/helpers/handlebarsHelper.mjs';
import { enricherConfig, enricherRenderSetup } from './module/enrichers/_module.mjs';
import { getCommandTarget, rollCommandToJSON } from './module/helpers/utils.mjs';
import { NarrativeCountdowns } from './module/applications/ui/countdowns.mjs';
import { DualityRollColor } from './module/data/settings/Appearance.mjs';
import { DHRoll, DualityRoll, D20Roll, DamageRoll, DualityDie } from './module/dice/_module.mjs';
import { enrichedDualityRoll, renderDualityButton } from './module/enrichers/DualityRollEnricher.mjs';
import { renderMeasuredTemplate } from './module/enrichers/TemplateEnricher.mjs';
import { enrichedDualityRoll } from './module/enrichers/DualityRollEnricher.mjs';
import { registerCountdownHooks } from './module/data/countdowns.mjs';
import {
handlebarsRegistration,
@ -20,14 +20,14 @@ import { placeables } from './module/canvas/_module.mjs';
import { registerRollDiceHooks } from './module/dice/dhRoll.mjs';
import { registerDHActorHooks } from './module/documents/actor.mjs';
import './node_modules/@yaireo/tagify/dist/tagify.css';
import { renderDamageButton } from './module/enrichers/DamageEnricher.mjs';
Hooks.once('init', () => {
CONFIG.DH = SYSTEM;
game.system.api = {
applications,
models,
documents
documents,
dice
};
CONFIG.TextEditor.enrichers.push(...enricherConfig);
@ -49,6 +49,11 @@ Hooks.once('init', () => {
DamageRoll: DamageRoll
};
CONFIG.Dice.terms = {
...CONFIG.Dice.terms,
DualityDie
};
CONFIG.Dice.rolls = [...CONFIG.Dice.rolls, ...[DHRoll, DualityRoll, D20Roll, DamageRoll]];
CONFIG.MeasuredTemplate.objectClass = placeables.DhMeasuredTemplate;

View file

@ -1339,6 +1339,7 @@
"quantity": "Quantity",
"range": "Range",
"recovery": "Recovery",
"rerollThing": "Reroll {thing}",
"resource": "Resource",
"roll": "Roll",
"rollAll": "Roll All",

View file

@ -1,3 +1,5 @@
import { getDiceSoNicePresets } from '../../config/generalConfig.mjs';
export default class DhpChatLog extends foundry.applications.sidebar.tabs.ChatLog {
constructor(options) {
super(options);
@ -313,47 +315,50 @@ export default class DhpChatLog extends foundry.applications.sidebar.tabs.ChatLo
action.use(event);
};
//Reroll Functionality
rerollEvent = async (event, message) => {
let DieTerm = foundry.dice.terms.Die;
let dicetype = event.target.value;
const target = event.target.closest('button[data-die-index]');
let originalRoll_parsed = message.rolls.map(roll => JSON.parse(roll))[0];
console.log('Parsed Map:', originalRoll_parsed);
let originalRoll = Roll.fromData(originalRoll_parsed);
let diceIndex;
console.log('Dice to reroll is:', dicetype, ', and the message id is:', message._id, originalRoll_parsed);
console.log('Original Roll Terms:', originalRoll.terms);
switch (dicetype) {
case 'hope': {
diceIndex = 0; //Hope Die
break;
let parsedRoll = game.system.api.dice.DualityRoll.fromData({ ...originalRoll_parsed, evaluated: false });
const term = parsedRoll.terms[target.dataset.dieIndex];
await term.reroll(`/r1=${term.total}`);
if (game.modules.get('dice-so-nice')?.active) {
const diceSoNiceRoll = {
_evaluated: true,
dice: [new Die({ ...term, faces: term._faces, results: term.results.filter(x => !x.rerolled) })],
options: { appearance: {} }
};
const diceSoNicePresets = getDiceSoNicePresets();
switch (target.dataset.type) {
case 'hope':
diceSoNiceRoll.dice[0].options = { appearance: diceSoNicePresets.hope };
break;
case 'fear':
diceSoNiceRoll.dice[0].options = { appearance: diceSoNicePresets.fear };
break;
case 'advantage':
diceSoNiceRoll.dice[0].options = { appearance: diceSoNicePresets.advantage };
break;
case 'disadvantage':
diceSoNiceRoll.dice[0].options = { appearance: diceSoNicePresets.disadvantage };
break;
}
case 'fear': {
diceIndex = 2; //Fear Die
break;
await game.dice3d.showForRoll(diceSoNiceRoll, game.user, true);
}
await parsedRoll.evaluate();
const newRoll = game.system.api.dice.DualityRoll.postEvaluate(parsedRoll, {
targets: message.system.targets,
roll: {
advantage: message.system.roll.advantage?.type,
difficulty: message.system.roll.difficulty ? Number(message.system.roll.difficulty) : null
}
default:
ui.notifications.warn('Invalid Dice type selected.');
break;
}
let rollClone = originalRoll.clone();
let rerolledTerm = originalRoll.terms[diceIndex];
console.log('originalRoll:', originalRoll, 'rerolledTerm', rerolledTerm);
if (!(rerolledTerm instanceof DieTerm)) {
ui.notifications.error('Selected term is not a die.');
return;
}
await rollClone.reroll({ allowStrings: true })[diceIndex];
console.log(rollClone);
await rollClone.evaluate({ allowStrings: true });
console.log(rollClone.result);
/*
const confirm = await foundry.applications.api.DialogV2.confirm({
window: { title: 'Confirm Reroll' },
content: `<p>You have rerolled your <strong>${dicetype}</strong> die to <strong>${rollClone.result}</strong>.</p><p>Apply this new roll?</p>`
});
if (!confirm) return;
rollClone.toMessage({flavor: 'Selective reroll applied for ${dicetype}.'});
console.log("Updated Roll",rollClone);*/
});
newRoll.extra = newRoll.extra.slice(2);
await game.messages.get(message._id).update({
'system.roll': newRoll
});
};
}

View file

@ -6,7 +6,7 @@ export default class DHAdversaryRoll extends foundry.abstract.TypeDataModel {
static defineSchema() {
return {
title: new fields.StringField(),
roll: new fields.DataField(),
roll: new fields.ObjectField(),
targets: new fields.ArrayField(
new fields.SchemaField({
id: new fields.StringField({}),

View file

@ -147,7 +147,10 @@ export default class D20Roll extends DHRoll {
const difficulty = config.roll.difficulty ?? target.difficulty ?? target.evasion;
target.hit = this.isCritical || roll.total >= difficulty;
});
} else if (config.roll.difficulty) data.success = roll.isCritical || roll.total >= config.roll.difficulty;
} else if (config.roll.difficulty) {
data.difficulty = config.roll.difficulty;
data.success = roll.isCritical || roll.total >= config.roll.difficulty;
}
data.advantage = {
type: config.roll.advantage,
dice: roll.dAdvantage?.denomination,

View file

@ -110,6 +110,13 @@ export default class DualityRoll extends D20Roll {
return [...(hooks ?? []), 'Duality'];
}
/** @inheritDoc */
static fromData(data) {
data.terms[0].class = game.system.api.dice.DualityDie.name;
data.terms[2].class = game.system.api.dice.DualityDie.name;
return super.fromData(data);
}
createBaseDice() {
if (
this.dice[0] instanceof CONFIG.Dice.daggerheart.DualityDie &&

View file

@ -48,7 +48,7 @@ function getDualityMessage(roll) {
>
<i class="fa-solid fa-circle-half-stroke"></i>
${label}
${roll.difficulty || advantageLabel ? `(${[roll.difficulty, game.i18n.localize(`DAGGERHEART.GENERAL.${advantageLabel}.short`)].filter(x => x).join(' ')})` : ''}
${roll.difficulty || advantageLabel ? `(${[roll.difficulty, advantageLabel ? game.i18n.localize(`DAGGERHEART.GENERAL.${advantageLabel}.short`) : null].filter(x => x).join(' ')})` : ''}
</button>
`;

View file

@ -44,6 +44,21 @@
&.duality {
display: flex;
gap: 0.25rem;
> .roll {
background-image: none;
.reroll-button {
border: none;
background: initial;
width: 42px;
&:hover {
background: var(--button-background-color);
border: 1px solid var(--button-border-color);
}
}
}
}
// margin: 0;
> .roll {
@ -52,6 +67,7 @@
justify-content: center;
gap: 4px;
margin-bottom: 4px;
.dice-container {
display: flex;
flex-direction: column;

View file

@ -38,11 +38,11 @@
</header>
<div class="flexrow">
<ol class="dice-rolls duality">
<li class="roll die {{roll.hope.dice}}" title="{{localize "DAGGERHEART.GENERAL.hope"}}">
<li class="roll die {{roll.hope.dice}}">
<div class="dice-container">
<div class="dice-title">{{localize "DAGGERHEART.GENERAL.hope"}}</div>
<div class="dice-inner-container hope" title="{{localize "DAGGERHEART.GENERAL.hope"}}">s
<button type="checkbox" class="reroll-button" id="reroll-dice" value="hope">
<div class="dice-inner-container hope" data-tooltip="{{localize "DAGGERHEART.GENERAL.rerollThing" thing=(localize "DAGGERHEART.GENERAL.hope")}}">
<button type="checkbox" class="reroll-button" data-die-index="0" data-type="hope">
<div class="dice-wrapper">
<img class="dice" src="../icons/svg/{{roll.hope.dice}}-grey.svg"/>
</div>
@ -51,14 +51,16 @@
</div>
</div>
</li>
<li class="roll die {{roll.fear.dice}}" title="{{localize "DAGGERHEART.GENERAL.fear"}}">
<li class="roll die {{roll.fear.dice}}">
<div class="dice-container">
<div class="dice-title">{{localize "DAGGERHEART.GENERAL.fear"}}</div>
<div class="dice-inner-container fear" title="{{localize "DAGGERHEART.GENERAL.fear"}}">
<div class="dice-wrapper">
<img class="dice" src="../icons/svg/{{roll.fear.dice}}-grey.svg"/>
</div>
<div class="dice-value">{{roll.fear.value}}</div>
<div class="dice-inner-container fear" data-tooltip="{{localize "DAGGERHEART.GENERAL.rerollThing" thing=(localize "DAGGERHEART.GENERAL.fear")}}">
<button type="checkbox" class="reroll-button" data-die-index="2" data-type="fear">
<div class="dice-wrapper">
<img class="dice" src="../icons/svg/{{roll.fear.dice}}-grey.svg"/>
</div>
<div class="dice-value">{{roll.fear.value}}</div>
</button>
</div>
</div>
</li>