Simuler les lancers de dés

Lorsqu'on force le résultat des dés, on force l'affichage d'un lancer
donnant ce résultat avec DiceSoNice
This commit is contained in:
Vincent Vandemeulebrouck 2022-10-08 17:36:11 +02:00
parent 6dc7272ef6
commit 81ae15a6a2
3 changed files with 82 additions and 34 deletions

View File

@ -132,18 +132,15 @@ export class RdDDice {
} }
} }
static async roll(formula, options = { showDice: SHOW_DICE, rollMode: undefined }) { static async rollTotal(formula, options = { showDice: HIDE_DICE }) {
const roll = new Roll(formula); return (await RdDDice.roll(formula, options)).total;
await roll.evaluate({ async: true });
if (options.showDice != HIDE_DICE) {
await this.showDiceSoNice(roll, options.rollMode ?? game.settings.get("core", "rollMode"));
}
return roll;
} }
static async rollTotal(formula, options = { showDice: HIDE_DICE}) { static async roll(formula, options = { showDice: SHOW_DICE, rollMode: undefined }) {
const roll = await RdDDice.roll(formula, options); const roll = new Roll(RdDDice._formulaOrFake(formula, options));
return roll.total; await roll.evaluate({ async: true });
await this.showDiceSoNice(roll, options);
return roll;
} }
static async rollOneOf(array) { static async rollOneOf(array) {
@ -160,27 +157,79 @@ export class RdDDice {
} }
/* -------------------------------------------- */ /* -------------------------------------------- */
static async showDiceSoNice(roll, rollMode) { static async showDiceSoNice(roll, options) {
if (game.modules.get("dice-so-nice")?.active) { if (options.showDice == HIDE_DICE || !game.modules.get("dice-so-nice")?.active || !game.dice3d) {
if (game.dice3d) { return;
let whisper = null; }
let blind = false;
rollMode = rollMode ?? game.settings.get("core", "rollMode"); let { whisper, blind } = RdDDice._getWhisperBlind(options);
switch (rollMode) { if (options.forceDiceResult?.total) {
case "blindroll": //GM only let terms = await RdDDice._getForcedTerms(options);
blind = true; if (terms) {
case "gmroll": //GM + rolling player await game.dice3d.show({ throws: [{ dice: terms }] })
whisper = ChatUtility.getUsers(user => user.isGM); return;
break;
case "roll": //everybody
whisper = ChatUtility.getUsers(user => user.active);
break;
case "selfroll":
whisper = [game.user.id];
break;
}
await game.dice3d.showForRoll(roll, game.user, true, whisper, blind);
} }
} }
await game.dice3d.showForRoll(roll, game.user, true, whisper, blind);
}
static _formulaOrFake(formula, options) {
if (options?.forceDiceResult?.total) {
options.forceDiceResult.formula = formula;
return options.forceDiceResult.total.toString()
}
return formula;
}
static async _getForcedTerms(options) {
const total = options.forceDiceResult.total;
switch (options.forceDiceResult.formula) {
case '1d100':
return terms1d100(total);
case "2d10":
// TODO
}
return undefined;
function terms1d100(total) {
const unites = total % 10;
const dizaines = Math.floor(total / 10);
return [{
resultLabel: dizaines * 10,
d100Result: total,
result: dizaines,
type: "d100",
vectors: [],
options: {}
},
{
resultLabel: unites,
d100Result: total,
result: unites,
type: "d10",
vectors: [],
options: {}
}];
}
}
static _getWhisperBlind(options) {
let whisper = null;
let blind = false;
let rollMode = options.rollMode ?? game.settings.get("core", "rollMode");
switch (rollMode) {
case "blindroll": //GM only
blind = true;
case "gmroll": //GM + rolling player
whisper = ChatUtility.getUsers(user => user.isGM);
break;
case "roll": //everybody
whisper = ChatUtility.getUsers(user => user.active);
break;
case "selfroll":
whisper = [game.user.id];
break;
}
return { whisper, blind };
} }
} }

View File

@ -142,10 +142,8 @@ export class RdDResolutionTable {
/* -------------------------------------------- */ /* -------------------------------------------- */
static async rollChances(chances, diviseur, forceDiceResult = -1) { static async rollChances(chances, diviseur, forceDiceResult = -1) {
if (forceDiceResult <= 0 || forceDiceResult > 100) { chances.forceDiceResult = forceDiceResult <= 0 || forceDiceResult > 100 ? undefined : {total: forceDiceResult};
forceDiceResult = -1; chances.roll = await RdDDice.rollTotal( "1d100", chances);
}
chances.roll = await RdDDice.rollTotal((forceDiceResult == -1) ? "1d100" : `${forceDiceResult}`, chances);
mergeObject(chances, this.computeReussite(chances, chances.roll, diviseur), { overwrite: true }); mergeObject(chances, this.computeReussite(chances, chances.roll, diviseur), { overwrite: true });
return chances; return chances;
} }

View File

@ -144,6 +144,7 @@ export class RdDRoll extends Dialog {
/* -------------------------------------------- */ /* -------------------------------------------- */
async onAction(action, html) { async onAction(action, html) {
this.rollData.forceDiceResult = Number.parseInt($('#force-dice-result').val()) ?? -1;
await RdDResolutionTable.rollData(this.rollData); await RdDResolutionTable.rollData(this.rollData);
console.log("RdDRoll -=>", this.rollData, this.rollData.rolled); console.log("RdDRoll -=>", this.rollData, this.rollData.rolled);
this.actor.setRollWindowsOpened(false); this.actor.setRollWindowsOpened(false);