bol/module/controllers/bol-rolls.js

272 lines
10 KiB
JavaScript
Raw Normal View History

2021-12-25 23:26:27 +01:00
import { BoLUtility } from "../system/bol-utility.js";
2022-01-16 22:06:49 +01:00
const __adv2dice = { ["1B"]: 3, ["2B"]: 4, ["2"]: 2, ["1M"]: 3, ["2M"]: 4}
2022-01-16 22:53:41 +01:00
const _apt2attr = {init: "mind", melee: "agility", ranged: "agility", def: "vigor"}
export class BoLRoll {
2022-01-01 23:32:48 +01:00
static options() {
return { classes: ["bol", "dialog"] };
}
2022-01-16 22:06:49 +01:00
static convertToAdv( adv) {
if (adv == 0) return "2"
return Math.abs(adv) + (adv < 0)?'M':'B';
}
2022-01-16 22:53:41 +01:00
static getDefaultAttribute( key ) {
return _apt2attr[key]
}
2022-01-01 23:32:48 +01:00
static attributeCheck(actor, actorData, dataset, event) {
// const elt = $(event.currentTarget)[0];
// let key = elt.attributes["data-rolling"].value;
const key = dataset.key;
const adv = dataset.adv;
let attribute = eval(`actor.data.data.attributes.${key}`);
let label = (attribute.label) ? game.i18n.localize(attribute.label) : null;
let description = actor.name + " - " + game.i18n.localize('BOL.ui.attributeCheck') + " - " + game.i18n.localize(attribute.label);
2022-01-16 22:06:49 +01:00
return this.displayRollDialog(
{
mode: "attribute",
actor: actor,
actorData: actorData,
attribute: attribute,
label: label,
description: description,
adv: this.convertToAdv(adv),
mod: 0
});
2022-01-01 23:32:48 +01:00
}
2022-01-01 23:32:48 +01:00
static aptitudeCheck(actor, actorData, dataset, event) {
// const elt = $(event.currentTarget)[0];
// let key = elt.attributes["data-rolling"].value;
const key = dataset.key;
const adv = dataset.adv;
2022-01-16 22:53:41 +01:00
2022-01-01 23:32:48 +01:00
let aptitude = eval(`actor.data.data.aptitudes.${key}`);
2022-01-16 22:53:41 +01:00
let attrKey = this.getDefaultAttribute(key)
let attribute = eval(`actor.data.data.attributes.${attrKey}`);
2022-01-01 23:32:48 +01:00
let label = (aptitude.label) ? game.i18n.localize(aptitude.label) : null;
let description = actor.name + " - " + game.i18n.localize('BOL.ui.aptitudeCheck') + " - " + game.i18n.localize(aptitude.label);
2022-01-16 22:06:49 +01:00
return this.displayRollDialog(
{
mode: "aptitude",
actor: actor,
actorData: actorData,
2022-01-16 22:53:41 +01:00
attribute: attribute,
2022-01-16 22:06:49 +01:00
aptitude: aptitude,
label: label,
description: description,
adv: this.convertToAdv(adv),
mod: 0
});
2021-12-25 23:26:27 +01:00
}
2022-01-01 23:32:48 +01:00
static weaponCheck(actor, actorData, dataset, event) {
// const elt = $(event.currentTarget)[0];
// let key = elt.attributes["data-rolling"].value;
let target = BoLUtility.getTarget()
const li = $(event.currentTarget).parents(".item");
const weapon = actor.items.get(li.data("item-id"));
if (!weapon) {
ui.notifications.warn("Unable to find weapon !");
return;
}
2022-01-01 23:32:48 +01:00
let weaponData = weapon.data.data;
let attackDef = {
2022-01-16 22:06:49 +01:00
mode: "weapon",
actor: actor,
actorData: actorData,
2022-01-01 23:32:48 +01:00
weapon: weapon,
target: target,
defender: (target) ? game.actors.get(target.data.actorId) : undefined,
attribute: eval(`actor.data.data.attributes.${weaponData.properties.attackAttribute}`),
aptitude: eval(`actor.data.data.aptitudes.${weaponData.properties.attackAptitude}`),
label: (weapon.name) ? weapon.name : game.i18n.localize('BOL.ui.noWeaponName'),
description: actor.name + " - " + game.i18n.localize('BOL.ui.weaponAttack'),
2022-01-16 22:06:49 +01:00
adv: "2",
2022-01-01 23:32:48 +01:00
}
console.debug("WEAPON!", attackDef, weaponData);
2022-01-16 22:06:49 +01:00
return this.displayRollDialog(attackDef);
2022-01-01 23:32:48 +01:00
}
2022-01-01 23:32:48 +01:00
/* -------------------------------------------- */
/* ROLL DIALOGS */
/* -------------------------------------------- */
2022-01-16 22:06:49 +01:00
static async displayRollDialog(rollData, onEnter = "submit") {
2022-01-01 23:32:48 +01:00
2022-01-16 22:06:49 +01:00
const rollOptionTpl = `systems/bol/templates/dialogs/${rollData.mode}-roll-dialog.hbs`
rollData.careers = rollData.actorData.features.careers
rollData.boons = rollData.actorData.features.boons
rollData.flaws = rollData.actorData.features.flaws
rollData.defence = 0
rollData.mod = 0
rollData.id = randomID(16)
2022-01-01 23:32:48 +01:00
2022-01-16 22:06:49 +01:00
// Weapon mode specific management
if (rollData.mode == "weapon") {
if (rollData.defender) { // If target is selected
rollData.defence = rollData.defender.defenseValue,
rollData.shieldBlock = 'none'
let shields = rollData.defender.shields
for (let shield of shields) {
rollData.shieldBlock = (shield.data.properties.blocking.blockingAll) ? 'blockall' : 'blockone';
rollData.shieldAttackMalus = (shield.data.properties.blocking.malus) ? shield.data.properties.blocking.malus : 1;
rollData.applyShieldMalus = false
}
2021-12-29 20:33:59 +01:00
}
2022-01-01 23:32:48 +01:00
}
2022-01-16 22:06:49 +01:00
console.log("ROL1", rollData)
const rollOptionContent = await renderTemplate(rollOptionTpl, rollData);
2022-01-01 23:32:48 +01:00
let d = new Dialog({
2022-01-16 22:06:49 +01:00
title: rollData.label,
2022-01-01 23:32:48 +01:00
content: rollOptionContent,
2022-01-16 22:06:49 +01:00
rollData: rollData,
2022-01-01 23:32:48 +01:00
buttons: {
cancel: {
icon: '<i class="fas fa-times"></i>',
label: game.i18n.localize("BOL.ui.cancel"),
callback: () => {
}
},
submit: {
icon: '<i class="fas fa-check"></i>',
label: game.i18n.localize("BOL.ui.submit"),
callback: (html) => {
2022-01-16 22:06:49 +01:00
rollData.attrKey = html.find('#attr').val();
rollData.aptKey = html.find('#apt').val();
rollData.adv = $("input[name='adv']:checked").val() || "2";
//rollData.adv = html.find('#adv').val() || 0;
rollData.mod = html.find('#mod').val() || 0;
let careers = html.find('#career').val();
rollData.career = (!careers || careers.length == 0) ? 0 : Math.max(...careers.map(i => parseInt(i)));
2022-01-16 22:53:41 +01:00
rollData.registerInit = (rollData.aptKey == 'init') ? $('#register-init').is(":checked") : false;
2022-01-01 23:32:48 +01:00
let shieldMalus = 0;
2022-01-16 22:06:49 +01:00
if ( rollData.mode == "weapon") {
const applyShieldMalus = html.find('#applyShieldMalus').val() || false;
if (applyShieldMalus || rollData.shieldBlock == 'blockall') {
shieldMalus = rollData.shieldAttackMalus;
}
2022-01-01 23:32:48 +01:00
}
2021-12-29 20:33:59 +01:00
2022-01-16 22:06:49 +01:00
const isMalus = rollData.adv.includes('M');
const dicePool = __adv2dice[rollData.adv]
//// const dicePool = (isMalus) ? 2 - parseInt(rollData.adv) : 2 + parseInt(rollData.adv);
const attrValue = (rollData.attrKey) && eval(`rollData.actor.data.data.attributes.${rollData.attrKey}.value`) || 0;
const aptValue = (rollData.aptKey) && eval(`rollData.actor.data.data.aptitudes.${rollData.aptKey}.value`) || 0
const modifiers = parseInt(attrValue) + parseInt(aptValue) + parseInt(rollData.mod) + parseInt(rollData.career) - rollData.defence - shieldMalus;
2022-01-01 23:32:48 +01:00
const formula = (isMalus) ? dicePool + "d6kl2 + " + modifiers : dicePool + "d6kh2 + " + modifiers;
2022-01-16 22:06:49 +01:00
rollData.formula = formula;
2021-12-25 23:26:27 +01:00
2022-01-16 22:06:49 +01:00
let r = new BoLDefaultRoll(rollData);
r.roll();
2022-01-01 23:32:48 +01:00
}
}
},
default: onEnter,
close: () => { }
}, this.options());
return d.render(true);
}
}
export class BoLDefaultRoll {
2022-01-16 22:06:49 +01:00
constructor(rollData) {
BoLUtility.storeRoll(rollData);
this.rollData = rollData
this.rollData.isSuccess = false;
this.rollData.isCritical = false;
this.rollData.isFumble = false;
2022-01-01 23:32:48 +01:00
}
2021-12-25 23:26:27 +01:00
2022-01-01 23:32:48 +01:00
async roll() {
2022-01-16 22:06:49 +01:00
console.log("ROLL", this.rollData)
const r = new Roll(this.rollData.formula);
2022-01-01 23:32:48 +01:00
await r.roll({ "async": false });
2022-01-16 22:06:49 +01:00
//await BoLUtility.showDiceSoNice(r);
2022-01-01 23:32:48 +01:00
const activeDice = r.terms[0].results.filter(r => r.active);
const diceTotal = activeDice.map(r => r.result).reduce((a, b) => a + b);
2022-01-16 22:06:49 +01:00
this.rollData.isSuccess = (r.total >= 9);
this.rollData.isCritical = (diceTotal === 12);
this.rollData.isFumble = (diceTotal === 2);
this.rollData.isFailure = !this.rollData.isSuccess
this.rollData.reroll = this.rollData.actor.heroReroll()
2022-01-16 22:53:41 +01:00
if (this.rollData.registerInit) {
this.rollData.actor.registerInit( r.total, this.rollData.isCritical);
}
2022-01-16 22:06:49 +01:00
this._buildChatMessage(this.rollData).then(msgFlavor => {
2022-01-01 23:32:48 +01:00
r.toMessage({
user: game.user.id,
flavor: msgFlavor,
2022-01-16 22:06:49 +01:00
speaker: ChatMessage.getSpeaker({ actor: this.rollData.actor }),
2022-01-01 23:32:48 +01:00
flags: { msgType: "default" }
2022-01-16 22:06:49 +01:00
}).then(this.processResult());
2022-01-01 23:32:48 +01:00
});
2022-01-09 13:23:20 +01:00
}
async processDefense() {
2022-01-16 22:06:49 +01:00
if (this.rollData.isCritical) {
2022-01-09 13:23:20 +01:00
ChatMessage.create({
2022-01-16 22:06:49 +01:00
alias: this.rollData.actor.name,
whisper: BoLUtility.getWhisperRecipientsAndGMs(this.rollData.actor.name),
content: await renderTemplate('systems/bol/templates/chat/rolls/attack-heroic-card.hbs', this.rollData )
2022-01-09 13:23:20 +01:00
})
} else {
2022-01-16 22:06:49 +01:00
BoLUtility.sendAttackSuccess(this.rollData);
2022-01-09 13:23:20 +01:00
}
}
2022-01-16 22:06:49 +01:00
setSuccess( flag) {
this.rollData.isSuccess = flag
}
async processResult() {
if ( this.rollData.mode != "weapon") { // Only specific process in Weapon mode
return;
}
if (this.rollData.isSuccess) {
let attrDamage = this.rollData.weapon.data.data.properties.damageAttribute;
let weaponFormula = BoLUtility.getDamageFormula(this.rollData.weapon.data.data.properties.damage,
this.rollData.weapon.data.data.properties.damageModifiers,
this.rollData.weapon.data.data.properties.damageMultiplier)
let damageFormula = weaponFormula + ((attrDamage) ? "+" + this.rollData.actor.data.data.attributes[attrDamage].value : "+0");
2021-12-25 23:26:27 +01:00
2022-01-16 22:06:49 +01:00
//console.log("Formula", weaponFormula, damageFormula, this.rollData.weapon.data.data.properties.damage)
this.rollData.damageRoll = new Roll(damageFormula);
await this.rollData.damageRoll.roll({ "async": false });
await BoLUtility.showDiceSoNice(this.rollData.damageRoll);
// Update rollData object
this.rollData.damageFormula = damageFormula;
2021-12-29 19:15:06 +01:00
2022-01-16 22:06:49 +01:00
this._buildDamageChatMessage( this.rollData ).then(msgFlavor => {
this.rollData.damageRoll.toMessage({
2022-01-01 23:32:48 +01:00
user: game.user.id,
flavor: msgFlavor,
2022-01-16 22:06:49 +01:00
speaker: ChatMessage.getSpeaker({ actor: this.rollData.actor }),
2022-01-01 23:32:48 +01:00
flags: { msgType: "default" }
2022-01-16 22:06:49 +01:00
}).then(this.processDefense());
2021-12-29 19:15:06 +01:00
});
}
2022-01-01 23:32:48 +01:00
}
2021-12-25 23:26:27 +01:00
2022-01-16 22:06:49 +01:00
_buildDamageChatMessage(rollData) {
const rollMessageTpl = 'systems/bol/templates/chat/rolls/damage-roll-card.hbs';
return renderTemplate(rollMessageTpl, rollData);
}
2021-12-25 23:26:27 +01:00
2022-01-16 22:06:49 +01:00
_buildChatMessage(rollData) {
const rollMessageTpl = 'systems/bol/templates/chat/rolls/default-roll-card.hbs';
return renderTemplate(rollMessageTpl, rollData);
}
2021-12-25 23:26:27 +01:00
}