foundry-vtt-system-midgard5/source/module/rolls/reroll.ts

108 lines
3.0 KiB
TypeScript

import { M5RollData, M5RollResult } from "../M5Base";
import { M5Roll } from "./M5Roll";
export const reroll = async (roll) => {
const message = (game as Game).messages.get(roll.attr("data-message-id"));
const actor = (game as Game).actors.get(message["speaker"].actor);
const template = "systems/midgard5/templates/chat/reroll-dialog.hbs";
const html = await renderTemplate(template, { sg: actor.system.sg, gp: actor.system.gp });
// Button defining
let buttons = {};
buttons = {
destiny: {
icon: '<i class="fas fa-rotate-left"></i>',
label: (game as Game).i18n.localize("midgard5.chat.destiny"),
callback: () => rerollDie("destiny", "sg"),
},
luckPoints: {
icon: '<i class="fas fa-rotate-left"></i>',
label: (game as Game).i18n.localize("midgard5.chat.luckPoint"),
callback: () => rerollDie("luckPoint", "gp"),
},
modify: {
icon: '<i class="fas fa-plus"></i>',
label: (game as Game).i18n.localize("midgard5.chat.modify"),
callback: () => rerollDie("modify", "gp"),
},
cancel: {
icon: '<i class="fas fa-times"></i>',
label: (game as Game).i18n.localize("midgard5.chat.cancel"),
},
};
// Dialog object
new Dialog(
{
title: (game as Game).i18n.localize("midgard5.chat.reroll"),
content: html,
buttons,
render: function () {},
default: "luckPoints",
},
{
classes: ["midgard5"],
}
).render(true);
async function rerollDie(type, target) {
// Update the "content" field
let rollData = actor.getRollData() as M5RollData;
const flagData = message["flags"].data;
rollData.i = flagData.i;
rollData.b = flagData.b;
rollData.iType = flagData.iType;
rollData.res.label = flagData.res.label + " (" + (game as Game).i18n.localize(`midgard5.chat.${type}`) + ")";
actor.update({
system: {
[target]: actor.system[target] - 1,
},
});
if (type !== "modify") {
for (var key in flagData.rolls) {
if (!!flagData.rolls[key]) {
rollData.rolls[key] = {
formula: flagData.rolls[key]?.formula,
enabled: flagData.rolls[key]?.enabled,
label: flagData.rolls[key]?.label,
result: "",
total: 0,
totalStr: "",
dice: {},
css: "",
} as M5RollResult;
}
}
} else {
for (var key in flagData.rolls) {
if (!!flagData.rolls[key]) {
rollData.rolls[key] = {
formula: key === "0" ? flagData.rolls[key]?.result + " + 2" : flagData.rolls[key]?.result,
enabled: flagData.rolls[key]?.enabled,
label: flagData.rolls[key]?.label,
result: "",
total: 0,
totalStr: "",
dice: {},
css: "",
} as M5RollResult;
}
}
}
const newRoll = new M5Roll(rollData, actor, rollData.res.label);
if (!newRoll._evaluated) await newRoll.evaluate();
const chatData = {
type: CONST.CHAT_MESSAGE_TYPES.ROLL,
content: await newRoll.render(),
speaker: ChatMessage.getSpeaker({ actor: actor }),
sound: CONFIG.sounds.dice,
roll: Roll.fromTerms([newRoll.pool]),
flags: { rerolled: true },
};
message.update(chatData);
}
};