bol/module/controllers/bol-rolls.js

309 lines
11 KiB
JavaScript
Raw Normal View History

2021-12-25 23:26:27 +01:00
import { BoLUtility } from "../system/bol-utility.js";
2022-01-17 23:50:57 +01:00
const __adv2dice = { ["1B"]: 3, ["2B"]: 4, ["2"]: 2, ["1M"]: 3, ["2M"]: 4 }
const _apt2attr = { init: "mind", melee: "agility", ranged: "agility", def: "vigor" }
2022-01-16 22:53:41 +01:00
export class BoLRoll {
2022-01-01 23:32:48 +01:00
static options() {
return { classes: ["bol", "dialog"] };
}
2022-01-17 23:50:57 +01:00
static convertToAdv(adv) {
2022-01-16 22:06:49 +01:00
if (adv == 0) return "2"
2022-01-17 23:50:57 +01:00
return Math.abs(adv) + (adv < 0) ? 'M' : 'B';
2022-01-16 22:06:49 +01:00
}
2022-01-17 23:50:57 +01:00
static getDefaultAttribute(key) {
2022-01-16 22:53:41 +01:00
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(
{
2022-01-17 23:50:57 +01:00
mode: "attribute",
2022-01-16 22:06:49 +01:00
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(
{
2022-01-17 23:50:57 +01:00
mode: "aptitude",
2022-01-16 22:06:49 +01:00
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
2022-01-19 21:57:34 +01:00
rollData.weaponModifier = 0
rollData.attackBonusDice = false
2022-01-16 22:06:49 +01:00
if (rollData.mode == "weapon") {
2022-01-19 21:57:34 +01:00
//console.log("WEAPON", rollData.weapon)
rollData.weaponModifier = rollData.weapon.data.data.properties.attackModifiers?? 0;
rollData.attackBonusDice = rollData.weapon.data.data.properties.attackBonusDice
2022-01-16 22:06:49 +01:00
if (rollData.defender) { // If target is selected
rollData.defence = rollData.defender.defenseValue,
2022-01-17 23:50:57 +01:00
rollData.shieldBlock = 'none'
2022-01-16 22:06:49 +01:00
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
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-17 23:50:57 +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-17 23:50:57 +01:00
if (rollData.mode == "weapon") {
2022-01-16 22:06:49 +01:00
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');
2022-01-19 21:57:34 +01:00
let dicePool = __adv2dice[rollData.adv]
dicePool += (rollData.attackBonusDice) ? 1 : 0
2022-01-16 22:06:49 +01:00
//// 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
2022-01-17 23:50:57 +01:00
2022-01-19 21:57:34 +01:00
const modifiers = rollData.weaponModifier + 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;
2022-01-17 23:50:57 +01:00
rollData.modifiers = modifiers
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-19 21:57:34 +01:00
2022-01-16 22:06:49 +01:00
constructor(rollData) {
2022-01-17 23:50:57 +01:00
BoLUtility.storeRoll(rollData)
2022-01-16 22:06:49 +01:00
this.rollData = rollData
2022-01-17 23:50:57 +01:00
if ( this.rollData.isSuccess == undefined ) { // First init
this.rollData.isSuccess = false;
this.rollData.isCritical = false;
this.rollData.isFumble = false;
}
if ( this.rollData.optionsId) {
$(`#${this.rollData.optionsId}`).hide() // Hide the options roll buttons
}
if ( this.rollData.applyId) {
$(`#${this.rollData.applyId}`).hide() // Hide the options roll buttons
}
this.rollData.optionsId = randomID(16)
this.rollData.applyId = randomID(16)
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
const r = new Roll(this.rollData.formula);
2022-01-01 23:32:48 +01:00
await r.roll({ "async": false });
const activeDice = r.terms[0].results.filter(r => r.active);
const diceTotal = activeDice.map(r => r.result).reduce((a, b) => a + b);
2022-01-17 23:50:57 +01:00
this.rollData.roll = r
2022-01-16 22:06:49 +01:00
this.rollData.isSuccess = (r.total >= 9);
2022-01-17 23:50:57 +01:00
this.rollData.isCritical = (diceTotal === 12)
this.rollData.isRealCritical = (diceTotal === 12)
2022-01-16 22:06:49 +01:00
this.rollData.isFumble = (diceTotal === 2);
this.rollData.isFailure = !this.rollData.isSuccess
2022-01-17 23:50:57 +01:00
if (this.rollData.reroll == undefined) {
this.rollData.reroll = this.rollData.actor.heroReroll()
}
2022-01-16 22:06:49 +01:00
2022-01-16 22:53:41 +01:00
if (this.rollData.registerInit) {
2022-01-17 23:50:57 +01:00
this.rollData.actor.registerInit(r.total, this.rollData.isCritical);
2022-01-16 22:53:41 +01:00
}
2022-01-17 23:50:57 +01:00
console.log("ROLL", this.rollData)
await this.sendChatMessage()
}
async sendChatMessage() {
2022-01-16 22:06:49 +01:00
this._buildChatMessage(this.rollData).then(msgFlavor => {
2022-01-17 23:50:57 +01:00
this.rollData.roll.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-17 23:50:57 +01:00
})
2022-01-01 23:32:48 +01:00
});
2022-01-09 13:23:20 +01:00
}
2022-01-17 23:50:57 +01:00
upgradeToCritical() {
// Force to Critical roll
this.rollData.isCritical = true
this.rollData.isRealCritical = false
this.rollData.isSuccess = true
this.rollData.isFailure = false
this.rollData.reroll = false
this.rollData.roll = new Roll("12+" + this.rollData.modifiers)
this.rollData.reroll = false
this.sendChatMessage()
2022-01-09 13:23:20 +01:00
}
2022-01-17 23:50:57 +01:00
setSuccess(flag) {
2022-01-16 22:06:49 +01:00
this.rollData.isSuccess = flag
}
2022-01-17 23:50:57 +01:00
async sendDamageMessage() {
this._buildDamageChatMessage(this.rollData).then(msgFlavor => {
this.rollData.damageRoll.toMessage({
user: game.user.id,
flavor: msgFlavor,
speaker: ChatMessage.getSpeaker({ actor: this.rollData.actor }),
flags: { msgType: "default" }
})
});
}
async rollDamage() {
if (this.rollData.mode != "weapon") { // Only specific process in Weapon mode
2022-01-16 22:06:49 +01:00
return;
}
if (this.rollData.isSuccess) {
2022-01-17 23:50:57 +01:00
if ( !this.rollData.damageRoll) {
let bonusDmg = 0
if ( this.rollData.damageMode == 'damage-plus-6') {
bonusDmg = 6
}
if ( this.rollData.damageMode == 'damage-plus-12') {
bonusDmg = 12
}
console.log("DAMAGE !!!")
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 + "+" + bonusDmg + ((attrDamage) ? "+" + this.rollData.actor.data.data.attributes[attrDamage].value : "+0");
//console.log("Formula", weaponFormula, damageFormula, this.rollData.weapon.data.data.properties.damage)
this.rollData.damageFormula = damageFormula
this.rollData.damageRoll = new Roll(damageFormula)
this.rollData.damageTotal = this.rollData.damageRoll.total
await this.rollData.damageRoll.roll({ "async": false })
}
$(`#${this.rollData.optionsId}`).hide() // Hide the options roll buttons
this.sendDamageMessage()
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
}