This commit is contained in:
WBHarry 2025-11-24 23:11:18 +01:00
parent 4b76223e45
commit de9a66e1bc
36 changed files with 328 additions and 116 deletions

View file

@ -1,3 +1,4 @@
import { waitForDiceSoNice } from '../../../helpers/utils.mjs';
import { emitAsGM, GMUpdateEvent, RefreshType, socketEvent } from '../../../systemRegistration/socket.mjs';
const fields = foundry.data.fields;
@ -40,40 +41,69 @@ export default class CountdownField extends fields.ArrayField {
}
const data = { countdowns: {} };
const countdownMessages = [];
for (let countdown of config.countdowns) {
const { total: max } = await new Roll(countdown.progress.max).evaluate();
let startFormula = countdown.progress.startFormula ? countdown.progress.startFormula : null;
let countdownStart = startFormula ?? '1';
if (startFormula) {
const roll = await new Roll(startFormula).roll();
if (roll.dice.length > 0) {
countdownStart = roll.total;
const message = await roll.toMessage();
countdownMessages.push(message);
} else {
startFormula = null;
}
}
data.countdowns[foundry.utils.randomID()] = {
...countdown,
progress: {
...countdown.progress,
current: max,
max: max
current: countdownStart,
start: countdownStart,
startFormula
}
};
}
await emitAsGM(
GMUpdateEvent.UpdateCountdowns,
async () => {
const countdownSetting = game.settings.get(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.Countdowns);
await countdownSetting.updateSource(data);
await game.settings.set(
CONFIG.DH.id,
CONFIG.DH.SETTINGS.gameSettings.Countdowns,
countdownSetting.toObject()
),
game.socket.emit(`system.${CONFIG.DH.id}`, {
action: socketEvent.Refresh,
data: { refreshType: RefreshType.Countdown }
});
Hooks.callAll(socketEvent.Refresh, { refreshType: RefreshType.Countdown });
},
data,
null,
{
refreshType: RefreshType.Countdown
}
);
const update = async () => {
await emitAsGM(
GMUpdateEvent.UpdateCountdowns,
async () => {
const countdownSetting = game.settings.get(
CONFIG.DH.id,
CONFIG.DH.SETTINGS.gameSettings.Countdowns
);
await countdownSetting.updateSource(data);
await game.settings.set(
CONFIG.DH.id,
CONFIG.DH.SETTINGS.gameSettings.Countdowns,
countdownSetting.toObject()
),
game.socket.emit(`system.${CONFIG.DH.id}`, {
action: socketEvent.Refresh,
data: { refreshType: RefreshType.Countdown }
});
Hooks.callAll(socketEvent.Refresh, { refreshType: RefreshType.Countdown });
},
data,
null,
{
refreshType: RefreshType.Countdown
}
);
};
if (game.modules.get('dice-so-nice')?.active) {
Promise.all(
countdownMessages.map(message => {
return game.dice3d.waitFor3DAnimationByMessageID(message.id);
})
).then(update);
} else {
update();
}
}
/**