bol/module/system/bol-utility.js

449 lines
15 KiB
JavaScript
Raw Normal View History

2022-01-16 22:06:49 +01:00
import { BoLDefaultRoll } from "../controllers/bol-rolls.js";
2021-12-29 19:15:06 +01:00
2022-01-23 09:25:09 +01:00
// Spell circle to min PP cost
const __circle2minpp = { 0: 0, 1: 2,2: 6, 3: 11}
2021-12-29 19:15:06 +01:00
export class BoLUtility {
2021-11-01 00:28:42 +01:00
/* -------------------------------------------- */
static async init() {
2022-01-16 22:06:49 +01:00
this.attackStore = {}
2021-11-01 00:28:42 +01:00
}
2021-12-29 19:15:06 +01:00
2021-11-01 00:28:42 +01:00
/* -------------------------------------------- */
static async ready() {
}
2021-12-29 19:15:06 +01:00
2021-11-01 00:28:42 +01:00
/* -------------------------------------------- */
static templateData(it) {
return BoLUtility.data(it)?.data ?? {}
}
/* -------------------------------------------- */
static data(it) {
if (it instanceof Actor || it instanceof Item || it instanceof Combatant) {
return it.data;
}
return it;
}
2021-12-29 19:15:06 +01:00
2022-01-16 22:06:49 +01:00
/* -------------------------------------------- */
static storeRoll(roll) {
this.lastRoll = roll
}
/* -------------------------------------------- */
static getLastRoll() {
return this.lastRoll
}
2021-11-01 00:28:42 +01:00
/* -------------------------------------------- */
2021-12-29 19:15:06 +01:00
static createDirectOptionList(min, max) {
2021-11-01 00:28:42 +01:00
let options = {};
2021-12-29 19:15:06 +01:00
for (let i = min; i <= max; i++) {
2021-11-01 00:28:42 +01:00
options[`${i}`] = `${i}`;
}
return options;
}
/* -------------------------------------------- */
static buildListOptions(min, max) {
2021-11-08 14:40:29 +01:00
let options = [];
2021-11-01 00:28:42 +01:00
for (let i = min; i <= max; i++) {
2021-11-08 14:40:29 +01:00
options.push(`<option value="${i}">${i}</option>`);
2021-11-01 00:28:42 +01:00
}
2021-11-08 14:40:29 +01:00
return options.join("");
2021-11-01 00:28:42 +01:00
}
2021-11-08 14:40:29 +01:00
2021-11-01 00:28:42 +01:00
/* -------------------------------------------- */
static async showDiceSoNice(roll, rollMode) {
if (game.modules.get("dice-so-nice")?.active) {
if (game.dice3d) {
let whisper = null;
let blind = false;
rollMode = rollMode ?? game.settings.get("core", "rollMode");
switch (rollMode) {
case "blindroll": //GM only
blind = true;
case "gmroll": //GM + rolling player
whisper = this.getUsers(user => user.isGM);
break;
case "roll": //everybody
whisper = this.getUsers(user => user.active);
break;
case "selfroll":
whisper = [game.user.id];
break;
}
await game.dice3d.showForRoll(roll, game.user, true, whisper, blind);
}
}
}
2021-12-29 19:15:06 +01:00
/* -------------------------------------------- */
static getUsers(filter) {
return game.users.filter(filter).map(user => user.data._id);
}
/* -------------------------------------------- */
static getWhisperRecipients(rollMode, name) {
switch (rollMode) {
case "blindroll": return this.getUsers(user => user.isGM);
case "gmroll": return this.getWhisperRecipientsAndGMs(name);
case "selfroll": return [game.user.id];
2021-11-01 00:28:42 +01:00
}
2021-12-29 19:15:06 +01:00
return undefined;
}
/* -------------------------------------------- */
static getWhisperRecipientsAndGMs(name) {
let recep1 = ChatMessage.getWhisperRecipients(name) || [];
return recep1.concat(ChatMessage.getWhisperRecipients('GM'));
}
/* -------------------------------------------- */
static blindMessageToGM(chatOptions) {
let chatGM = duplicate(chatOptions);
chatGM.whisper = this.getUsers(user => user.isGM);
chatGM.content = "Blind message of " + game.user.name + "<br>" + chatOptions.content;
console.log("blindMessageToGM", chatGM);
game.socket.emit("system.fvtt-fragged-kingdom", { msg: "msg_gm_chat_message", data: chatGM });
}
2022-01-01 23:32:48 +01:00
/* -------------------------------------------- */
static sendAttackSuccess(attackDef) {
if (attackDef.target) {
// Broadcast to GM or process it directly in case of GM defense
if (!game.user.isGM) {
game.socket.emit("system.bol", { msg: "msg_attack_success", data: attackDef });
} else {
BoLUtility.processAttackSuccess(attackDef);
}
2022-01-17 23:50:57 +01:00
}
2022-01-01 23:32:48 +01:00
}
2022-01-17 23:50:57 +01:00
2021-12-29 19:15:06 +01:00
/* -------------------------------------------- */
static async chatListeners(html) {
2022-01-17 23:50:57 +01:00
2021-12-29 19:15:06 +01:00
// Damage handling
2022-01-17 23:50:57 +01:00
html.on("click", '.chat-damage-apply', event => {
let rollData = BoLUtility.getLastRoll()
$(`#${rollData.applyId}`).hide()
BoLUtility.sendAttackSuccess(rollData)
2022-01-01 23:32:48 +01:00
});
2022-01-17 23:50:57 +01:00
html.on("click", '.chat-damage-roll', event => {
event.preventDefault();
let rollData = BoLUtility.getLastRoll()
rollData.damageMode = event.currentTarget.attributes['data-damage-mode'].value;
let bolRoll = new BoLDefaultRoll(rollData)
bolRoll.rollDamage()
});
html.on("click", '.transform-heroic-roll', event => {
event.preventDefault();
let rollData = BoLUtility.getLastRoll()
rollData.actor.subHeroPoints(1)
let r = new BoLDefaultRoll( rollData )
r.upgradeToCritical();
} );
2022-01-16 22:06:49 +01:00
html.on("click", '.hero-reroll', event => {
2022-01-09 13:23:20 +01:00
event.preventDefault();
2022-01-16 22:06:49 +01:00
let rollData = BoLUtility.getLastRoll()
rollData.actor.subHeroPoints(1)
2022-01-17 23:50:57 +01:00
rollData.reroll = false // Disable reroll option for second roll
2022-01-16 22:06:49 +01:00
let r = new BoLDefaultRoll( rollData )
r.roll();
2022-01-09 13:23:20 +01:00
} );
2021-12-29 19:15:06 +01:00
html.on("click", '.damage-handling', event => {
2022-01-09 13:23:20 +01:00
event.preventDefault();
2021-12-29 19:15:06 +01:00
let attackId = event.currentTarget.attributes['data-attack-id'].value;
let defenseMode = event.currentTarget.attributes['data-defense-mode'].value;
let weaponId = (event.currentTarget.attributes['data-weapon-id']) ? event.currentTarget.attributes['data-weapon-id'].value : -1
if ( game.user.isGM) {
BoLUtility.processDamageHandling(event, attackId, defenseMode, weaponId)
} else {
game.socket.emit("system.bol", { msg: "msg_damage_handling", data: {event: event, attackId: attackId, defenseMode: defenseMode, weaponId: weaponId} });
2021-11-01 00:28:42 +01:00
}
2021-12-29 19:15:06 +01:00
});
}
/* -------------------------------------------- */
static async processDamageHandling(event, attackId, defenseMode, weaponId=-1) {
if ( !game.user.isGM) {
return;
2021-11-01 00:28:42 +01:00
}
2021-12-29 19:15:06 +01:00
BoLUtility.removeChatMessageId(BoLUtility.findChatMessageId(event.currentTarget));
2022-01-08 23:47:48 +01:00
//console.log("Damage Handling", event, attackId, defenseMode, weaponId)
2021-12-29 19:15:06 +01:00
// Only GM process this
2022-01-17 23:50:57 +01:00
let attackDef = this.attackStore[attackId]
2021-12-29 19:15:06 +01:00
if (attackDef) {
2022-01-08 23:47:48 +01:00
if (attackDef.defenseDone) return; // ?? Why ???
attackDef.defenseDone = true
2021-12-29 19:15:06 +01:00
attackDef.defenseMode = defenseMode;
2022-01-09 13:23:20 +01:00
2021-12-29 19:15:06 +01:00
if (defenseMode == 'damage-with-armor') {
2022-02-15 20:53:13 +01:00
let armorFormula = attackDef.defender.getArmorFormula()
2021-12-29 19:15:06 +01:00
attackDef.rollArmor = new Roll(armorFormula)
2022-02-15 20:53:13 +01:00
attackDef.rollArmor.roll( {async: false} )
2022-01-01 23:32:48 +01:00
attackDef.armorProtect = (attackDef.rollArmor.total<0) ? 0 : attackDef.rollArmor.total;
attackDef.finalDamage = attackDef.damageRoll.total - attackDef.armorProtect;
2021-12-29 19:15:06 +01:00
attackDef.finalDamage = (attackDef.finalDamage<0) ? 0 : attackDef.finalDamage;
attackDef.defender.sufferDamage(attackDef.finalDamage);
2021-11-01 00:28:42 +01:00
}
2021-12-29 19:15:06 +01:00
if (defenseMode == 'damage-without-armor') {
attackDef.finalDamage = attackDef.damageRoll.total;
attackDef.defender.sufferDamage(attackDef.finalDamage);
}
if (defenseMode == 'hero-reduce-damage') {
2022-01-01 23:32:48 +01:00
let armorFormula = attackDef.defender.getArmorFormula();
attackDef.rollArmor = new Roll(armorFormula)
attackDef.rollArmor.roll( {async: false} );
attackDef.armorProtect = (attackDef.rollArmor.total<0) ? 0 : attackDef.rollArmor.total;
2021-12-29 19:15:06 +01:00
attackDef.rollHero = new Roll("1d6");
attackDef.rollHero.roll( {async: false} );
2022-01-01 23:32:48 +01:00
attackDef.finalDamage = attackDef.damageRoll.total - attackDef.rollHero.total - attackDef.armorProtect;
2021-12-29 19:15:06 +01:00
attackDef.finalDamage = (attackDef.finalDamage<0) ? 0 : attackDef.finalDamage;
attackDef.defender.sufferDamage(attackDef.finalDamage);
attackDef.defender.subHeroPoints(1);
}
if (defenseMode == 'hero-in-extremis') {
attackDef.finalDamage = 0;
attackDef.weaponHero = attackDef.defender.weapons.find(item => item._id == weaponId);
attackDef.defender.deleteEmbeddedDocuments("Item", [ weaponId ]);
}
ChatMessage.create({
alias: attackDef.defender.name,
whisper: BoLUtility.getWhisperRecipientsAndGMs(attackDef.defender.name),
content: await renderTemplate('systems/bol/templates/chat/rolls/defense-result-card.hbs', {
attackId: attackDef.id,
attacker: attackDef.attacker,
rollArmor: attackDef.rollArmor,
rollHero: attackDef.rollHero,
weaponHero : attackDef.weaponHero,
2022-01-01 23:32:48 +01:00
armorProtect: attackDef.armorProtect,
2021-12-29 19:15:06 +01:00
defender: attackDef.defender,
defenseMode: attackDef.defenseMode,
finalDamage: attackDef.finalDamage
})
})
2021-11-01 00:28:42 +01:00
}
2021-12-29 19:15:06 +01:00
}
/* -------------------------------------------- */
static createChatMessage(name, rollMode, chatOptions) {
switch (rollMode) {
case "blindroll": // GM only
if (!game.user.isGM) {
this.blindMessageToGM(chatOptions);
chatOptions.whisper = [game.user.id];
chatOptions.content = "Message only to the GM";
}
else {
chatOptions.whisper = this.getUsers(user => user.isGM);
}
break;
default:
chatOptions.whisper = this.getWhisperRecipients(rollMode, name);
break;
2021-11-01 00:28:42 +01:00
}
2021-12-29 19:15:06 +01:00
chatOptions.alias = chatOptions.alias || name;
ChatMessage.create(chatOptions);
}
/* -------------------------------------------- */
static createChatWithRollMode(name, chatOptions) {
this.createChatMessage(name, game.settings.get("core", "rollMode"), chatOptions);
}
2021-11-01 23:06:34 +01:00
/* -------------------------------------------- */
2021-12-29 19:15:06 +01:00
static isRangedWeapon(weapon) {
2021-11-01 23:06:34 +01:00
return weapon.data.type == 'ranged' || weapon.data.thrown;
}
2021-12-29 19:15:06 +01:00
/* -------------------------------------------- */
static removeChatMessageId(messageId) {
if (messageId){
game.messages.get(messageId)?.delete();
}
}
static findChatMessageId(current) {
return BoLUtility.getChatMessageId(BoLUtility.findChatMessage(current));
}
static getChatMessageId(node) {
return node?.attributes.getNamedItem('data-message-id')?.value;
}
static findChatMessage(current) {
return BoLUtility.findNodeMatching(current, it => it.classList.contains('chat-message') && it.attributes.getNamedItem('data-message-id'));
}
static findNodeMatching(current, predicate) {
if (current) {
if (predicate(current)) {
return current;
}
return BoLUtility.findNodeMatching(current.parentElement, predicate);
}
return undefined;
}
2021-11-01 22:23:43 +01:00
/* -------------------------------------------- */
static getTarget() {
if (game.user.targets && game.user.targets.size == 1) {
for (let target of game.user.targets) {
return target;
}
}
return undefined;
}
2021-12-29 19:15:06 +01:00
2021-11-01 00:28:42 +01:00
/* -------------------------------------------- */
2021-12-29 19:15:06 +01:00
static async rollBoL(rollData) {
2021-11-01 00:28:42 +01:00
// Dice bonus/malus selection
let nbDice = 2;
let d6BM = 0;
let mode = "";
2021-12-29 19:15:06 +01:00
if (rollData.d6Malus > rollData.d6Bonus) {
2021-11-01 00:28:42 +01:00
d6BM = rollData.d6Malus - rollData.d6Bonus;
mode = "kl2";
}
2021-12-29 19:15:06 +01:00
if (rollData.d6Bonus > rollData.d6Malus) {
2021-11-01 00:28:42 +01:00
d6BM = rollData.d6Bonus - rollData.d6Malus;
mode = "kh2";
}
2021-12-29 19:15:06 +01:00
nbDice += d6BM;
2021-11-01 00:28:42 +01:00
// Final modifier
2021-11-01 22:23:43 +01:00
let modifier = Number(rollData.bonusMalus);
2021-12-29 19:15:06 +01:00
if (rollData.mode == 'career') {
2021-11-01 22:23:43 +01:00
modifier += Number(rollData.attributes[rollData.rollAttribute].value) + Number(rollData.career.data.rank);
2021-12-29 19:15:06 +01:00
} else if (rollData.mode == 'attribute') {
2021-11-01 22:23:43 +01:00
modifier += rollData.attribute.value;
2021-12-29 19:15:06 +01:00
} else if (rollData.mode == 'weapon') {
2021-11-01 22:23:43 +01:00
modifier += Number(rollData.attributes[rollData.rollAttribute].value) + Number(rollData.aptitude.value) + Number(rollData.rangeModifier);
2021-11-01 23:06:34 +01:00
modifier -= rollData.defender.data.aptitudes.def.value;
2021-11-01 22:23:43 +01:00
}
2021-12-29 19:15:06 +01:00
let formula = nbDice + "d6" + mode + "+" + modifier;
2021-11-01 22:23:43 +01:00
console.log("Going to roll ", formula, rollData.attributes, rollData.rollAttribute);
2021-12-29 19:15:06 +01:00
let myRoll = new Roll(formula).roll({ async: false });
await this.showDiceSoNice(myRoll, game.settings.get("core", "rollMode"));
2021-11-01 00:28:42 +01:00
rollData.roll = myRoll;
rollData.formula = formula;
2021-12-29 19:15:06 +01:00
rollData.modifier = modifier;
2021-11-06 12:04:42 +01:00
rollData.nbDice = nbDice;
2021-11-01 00:28:42 +01:00
rollData.finalScore = myRoll.total;
let actor = game.actors.get(rollData.actorId);
2021-12-29 19:15:06 +01:00
actor.saveRollData(rollData);
this.createChatWithRollMode(rollData.alias, {
content: await renderTemplate(`systems/bol/templates/chat/chat-generic-result.hbs`, rollData)
2021-11-01 00:28:42 +01:00
});
2021-12-29 19:15:06 +01:00
}
2021-11-01 00:28:42 +01:00
2021-12-25 23:26:27 +01:00
/* -------------------------------------------- */
2021-12-29 19:15:06 +01:00
static async processAttackSuccess(attackDef) {
if (!game.user.isGM) { // Only GM process this
return;
}
// Build and send the defense message to the relevant people (ie GM + defender)
let defenderWeapons = attackDef.defender.weapons;
console.log("DEF WEP", attackDef)
this.attackStore[attackDef.id] = attackDef; // Store !
ChatMessage.create({
alias: attackDef.defender.name,
whisper: BoLUtility.getWhisperRecipientsAndGMs(attackDef.defender.name),
content: await renderTemplate('systems/bol/templates/chat/rolls/defense-request-card.hbs', {
attackId: attackDef.id,
attacker: attackDef.attacker,
defender: attackDef.defender,
defenderWeapons: defenderWeapons,
damageTotal: attackDef.damageRoll.total
})
});
}
/* -------------------------------------------- */
static onSocketMessage(sockmsg) {
if (sockmsg.name == "msg_attack_success") {
BoLUtility.processAttackSuccess(sockmsg.data);
}
if (sockmsg.name == "msg_damage_handling") {
BoLUtility.processDamageHandling(sockmsg.data.event, sockmsg.data.attackId, sockmsg.data.defenseMode)
}
2022-01-23 09:25:09 +01:00
}
/* -------------------------------------------- */
static computeSpellCost( spell, nbOptCond= 0) {
let pp = spell.data.data.properties.ppcost
let minpp = __circle2minpp[spell.data.data.properties.circle]
pp = (pp-nbOptCond<minpp) ? minpp : pp-nbOptCond
return pp
2021-12-29 19:15:06 +01:00
}
/* -------------------------------------------- */
2022-01-09 20:00:11 +01:00
static getDamageFormula(damageString, modifier=0, multiplier = 1) {
2021-12-29 19:15:06 +01:00
if (damageString[0] == 'd') { damageString = "1" + damageString } // Help parsing
2022-01-09 20:00:11 +01:00
if (modifier == null) modifier = 0;
2022-01-19 21:57:34 +01:00
let formula = damageString
if ( damageString.includes("d") || damageString.includes("D")) {
var myReg = new RegExp('(\\d+)[dD]([\\d]+)([MB]*)?([\\+\\d]*)?', 'g');
let res = myReg.exec(damageString);
let nbDice = parseInt(res[1]);
let postForm = 'kh' + nbDice;
let modIndex = 3;
if (res[3]) {
if (res[3] == 'M') {
postForm = 'kl' + nbDice;
nbDice++;
modIndex = 4;
}
if (res[3] == 'B') {
postForm = 'kh' + nbDice;
nbDice++;
modIndex = 4;
}
2021-12-25 23:26:27 +01:00
}
2022-01-19 21:57:34 +01:00
formula = "(" + nbDice + "d" + res[2] + postForm + "+" + modifier +") *" + multiplier;
2021-12-25 23:26:27 +01:00
}
return formula;
}
2022-02-16 09:11:49 +01:00
2021-11-01 00:28:42 +01:00
/* -------------------------------------------- */
static async confirmDelete(actorSheet, li) {
let itemId = li.data("item-id");
let msgTxt = "<p>Are you sure to remove this Item ?";
let buttons = {
delete: {
2021-12-29 19:15:06 +01:00
icon: '<i class="fas fa-check"></i>',
label: "Yes, remove it",
callback: () => {
actorSheet.actor.deleteEmbeddedDocuments("Item", [itemId]);
li.slideUp(200, () => actorSheet.render(false));
2021-11-01 00:28:42 +01:00
}
2021-12-29 19:15:06 +01:00
},
cancel: {
icon: '<i class="fas fa-times"></i>',
label: "Cancel"
2021-11-01 00:28:42 +01:00
}
2021-12-29 19:15:06 +01:00
}
msgTxt += "</p>";
let d = new Dialog({
title: "Confirm removal",
content: msgTxt,
buttons: buttons,
default: "cancel"
});
d.render(true);
2021-11-01 00:28:42 +01:00
}
}