bol/module/system/bol-utility.js

498 lines
17 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
2022-02-23 20:39:58 +01:00
const __circle2minpp = { 0: 0, 1: 2, 2: 6, 3: 11 }
2022-01-23 09:25:09 +01:00
2021-12-29 19:15:06 +01:00
export class BoLUtility {
2021-11-01 00:28:42 +01:00
/* -------------------------------------------- */
2022-02-23 20:39:58 +01:00
static init() {
2022-01-16 22:06:49 +01:00
this.attackStore = {}
2022-02-23 20:39:58 +01:00
game.settings.register("bol", "rollArmor", {
name: "Effectuer des jets pour les armures",
hint: "Effectue un jet de dés pour les armures (valeur fixe si désactivé)",
scope: "world",
config: true,
default: true,
type: Boolean,
onChange: lang => window.location.reload()
});
game.settings.register("bol", "useBougette", {
name: "Utiliser la Bougette (règle fan-made)",
hint: "Utilise un indicateur de Bougette, comme décrit dans l'aide de jeu Gold&Glory du RatierBretonnien (https://www.lahiette.com/leratierbretonnien/)",
scope: "world",
config: true,
default: false,
type: Boolean,
onChange: lang => window.location.reload()
});
this.rollArmor = game.settings.get("bol", "rollArmor") // Roll armor or not
this.useBougette = game.settings.get("bol", "useBougette") // Use optionnal bougette rules
}
/* -------------------------------------------- */
static getRollArmor() {
return this.rollArmor
}
/* -------------------------------------------- */
static getUseBougette() {
return this.useBougette
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;
}
2022-03-27 22:56:43 +02:00
/* -------------------------------------------- */
static getOtherWhisperRecipients( name) {
let users = []
for( let user of game.users) {
if ( !user.isGM && user.name != name) {
users.push( user.data._id)
}
}
return users
}
2021-12-29 19:15:06 +01:00
/* -------------------------------------------- */
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);
2022-04-08 23:42:01 +02:00
game.socket.emit("system.bol", { name: "msg_gm_chat_message", data: chatGM });
2021-12-29 19:15:06 +01:00
}
2022-01-01 23:32:48 +01:00
/* -------------------------------------------- */
static sendAttackSuccess(attackDef) {
2022-04-08 23:42:01 +02:00
if (attackDef.targetId) {
2022-01-01 23:32:48 +01:00
// Broadcast to GM or process it directly in case of GM defense
if (!game.user.isGM) {
2022-04-08 23:42:01 +02:00
game.socket.emit("system.bol", { name: "msg_attack_success", data: duplicate(attackDef) })
2022-01-01 23:32:48 +01:00
} else {
2022-04-08 23:42:01 +02:00
BoLUtility.processAttackSuccess(attackDef)
2022-01-01 23:32:48 +01:00
}
2022-02-23 20:39:58 +01:00
}
2022-01-01 23:32:48 +01:00
}
2022-02-23 20:39:58 +01:00
2022-03-27 22:56:43 +02:00
/* -------------------------------------------- */
static async chatMessageHandler(message, html, data) {
const chatCard = html.find('.flavor-text')
if (chatCard.length > 0) {
// If the user is the message author or the actor owner, proceed
const actor = game.actors.get(data.message.speaker.actor)
2022-04-08 23:42:01 +02:00
//console.log("FOUND 1!!! ", actor)
2022-03-27 22:56:43 +02:00
if (actor && actor.isOwner) return
else if (game.user.isGM || data.author.id === game.user.id) return
const divButtons = chatCard.find('.actions-section')
console.log("FOUND 2!! ", divButtons)
divButtons.hide()
}
}
2021-12-29 19:15:06 +01:00
/* -------------------------------------------- */
static async chatListeners(html) {
2022-03-27 22:56:43 +02:00
2021-12-29 19:15:06 +01:00
// Damage handling
2022-02-23 20:39:58 +01:00
html.on("click", '.chat-damage-apply', event => {
2022-01-17 23:50:57 +01:00
let rollData = BoLUtility.getLastRoll()
$(`#${rollData.applyId}`).hide()
BoLUtility.sendAttackSuccess(rollData)
2022-01-01 23:32:48 +01:00
});
2022-02-23 20:39:58 +01:00
html.on("click", '.chat-damage-roll', event => {
2022-01-17 23:50:57 +01:00
event.preventDefault();
let rollData = BoLUtility.getLastRoll()
rollData.damageMode = event.currentTarget.attributes['data-damage-mode'].value;
let bolRoll = new BoLDefaultRoll(rollData)
bolRoll.rollDamage()
});
2022-02-23 20:39:58 +01:00
2022-03-27 22:56:43 +02:00
html.on("click", '.transform-legendary-roll', event => {
event.preventDefault();
let rollData = BoLUtility.getLastRoll()
2022-04-08 23:42:01 +02:00
let actor = game.actors.get( rollData.actorId)
actor.subHeroPoints(1)
2022-03-27 22:56:43 +02:00
let r = new BoLDefaultRoll(rollData)
r.upgradeToLegendary()
})
2022-01-17 23:50:57 +01:00
html.on("click", '.transform-heroic-roll', event => {
event.preventDefault();
let rollData = BoLUtility.getLastRoll()
2022-04-08 23:42:01 +02:00
let actor = game.actors.get( rollData.actorId)
actor.subHeroPoints(1)
2022-02-23 20:39:58 +01:00
let r = new BoLDefaultRoll(rollData)
2022-03-27 22:56:43 +02:00
r.upgradeToHeroic()
})
2022-02-23 20:39:58 +01:00
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()
2022-04-08 23:42:01 +02:00
let actor = game.actors.get( rollData.actorId)
actor.subHeroPoints(1)
2022-01-17 23:50:57 +01:00
rollData.reroll = false // Disable reroll option for second roll
2022-02-23 20:39:58 +01:00
let r = new BoLDefaultRoll(rollData)
2022-01-16 22:06:49 +01:00
r.roll();
2022-02-23 20:39:58 +01:00
});
2022-01-09 13:23:20 +01:00
2022-02-23 20:39:58 +01:00
html.on("click", '.damage-handling', event => {
event.preventDefault()
let attackId = event.currentTarget.attributes['data-attack-id'].value
let defenseMode = event.currentTarget.attributes['data-defense-mode'].value
2021-12-29 19:15:06 +01:00
let weaponId = (event.currentTarget.attributes['data-weapon-id']) ? event.currentTarget.attributes['data-weapon-id'].value : -1
2022-02-23 20:39:58 +01:00
if (game.user.isGM) {
2022-04-08 23:42:01 +02:00
console.log("Process handling !!! -> GM direct damage handling")
2021-12-29 19:15:06 +01:00
BoLUtility.processDamageHandling(event, attackId, defenseMode, weaponId)
} else {
2022-04-08 23:42:01 +02:00
console.log("Process handling !!! -> socket emit")
game.socket.emit("system.bol", { name: "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
});
}
/* -------------------------------------------- */
2022-02-23 20:39:58 +01:00
static async processDamageHandling(event, attackId, defenseMode, weaponId = -1) {
if (!game.user.isGM) {
2021-12-29 19:15:06 +01:00
return;
2021-11-01 00:28:42 +01:00
}
2021-12-29 19:15:06 +01:00
BoLUtility.removeChatMessageId(BoLUtility.findChatMessageId(event.currentTarget));
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]
2022-04-08 23:42:01 +02:00
if (attackDef && attackDef.defenderId) {
if (attackDef.defenseDone) {
return
} // ?? Why ???
2022-01-08 23:47:48 +01:00
attackDef.defenseDone = true
2022-05-23 18:38:51 +02:00
attackDef.defenseMode = defenseMode
let token = game.scenes.current.tokens.get(attackDef.targetId)
let defender = token.actor
2022-01-09 13:23:20 +01:00
2021-12-29 19:15:06 +01:00
if (defenseMode == 'damage-with-armor') {
2022-04-08 23:42:01 +02:00
let armorFormula = defender.getArmorFormula()
2021-12-29 19:15:06 +01:00
attackDef.rollArmor = new Roll(armorFormula)
2022-03-21 13:28:27 +01:00
attackDef.rollArmor.roll( { async: false } )
console.log("Armor roll ", attackDef.rollArmor)
2022-02-23 20:39:58 +01:00
attackDef.armorProtect = (attackDef.rollArmor.total < 0) ? 0 : attackDef.rollArmor.total;
2022-01-01 23:32:48 +01:00
attackDef.finalDamage = attackDef.damageRoll.total - attackDef.armorProtect;
2022-02-23 20:39:58 +01:00
attackDef.finalDamage = (attackDef.finalDamage < 0) ? 0 : attackDef.finalDamage;
2022-04-08 23:42:01 +02:00
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;
2022-04-08 23:42:01 +02:00
defender.sufferDamage(attackDef.finalDamage);
2021-12-29 19:15:06 +01:00
}
if (defenseMode == 'hero-reduce-damage') {
2022-05-10 23:04:04 +02:00
let armorFormula = defender.getArmorFormula()
2022-01-01 23:32:48 +01:00
attackDef.rollArmor = new Roll(armorFormula)
2022-05-10 23:04:04 +02:00
attackDef.rollArmor.roll({ async: false })
attackDef.armorProtect = (attackDef.rollArmor.total < 0) ? 0 : attackDef.rollArmor.total
attackDef.rollHero = new Roll("1d6")
attackDef.rollHero.roll({ async: false })
attackDef.finalDamage = attackDef.damageRoll.total - attackDef.rollHero.total - attackDef.armorProtect
attackDef.finalDamage = (attackDef.finalDamage < 0) ? 0 : attackDef.finalDamage
defender.sufferDamage(attackDef.finalDamage)
defender.subHeroPoints(1)
2021-12-29 19:15:06 +01:00
}
if (defenseMode == 'hero-in-extremis') {
attackDef.finalDamage = 0;
2022-04-08 23:42:01 +02:00
attackDef.weaponHero = defender.weapons.find(item => item._id == weaponId);
defender.deleteEmbeddedDocuments("Item", [weaponId]);
2021-12-29 19:15:06 +01:00
}
ChatMessage.create({
2022-04-08 23:42:01 +02:00
alias: defender.name,
whisper: BoLUtility.getWhisperRecipientsAndGMs(defender.name),
2021-12-29 19:15:06 +01:00
content: await renderTemplate('systems/bol/templates/chat/rolls/defense-result-card.hbs', {
attackId: attackDef.id,
attacker: attackDef.attacker,
rollArmor: attackDef.rollArmor,
rollHero: attackDef.rollHero,
2022-02-23 20:39:58 +01:00
weaponHero: attackDef.weaponHero,
2022-01-01 23:32:48 +01:00
armorProtect: attackDef.armorProtect,
2022-05-23 18:38:51 +02:00
name: defender.name,
2022-04-08 23:42:01 +02:00
defender: defender,
2021-12-29 19:15:06 +01:00
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) {
2022-02-23 20:39:58 +01:00
if (messageId) {
2021-12-29 19:15:06 +01:00
game.messages.get(messageId)?.delete();
}
}
2022-02-23 20:39:58 +01:00
2021-12-29 19:15:06 +01:00
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) {
2022-04-08 23:42:01 +02:00
return target
2021-11-01 22:23:43 +01:00
}
}
return undefined;
}
2021-12-29 19:15:06 +01:00
2021-12-25 23:26:27 +01:00
/* -------------------------------------------- */
2021-12-29 19:15:06 +01:00
static async processAttackSuccess(attackDef) {
2022-04-08 23:42:01 +02:00
console.log("Attack success processing", attackDef)
if (!game.user.isGM || !attackDef.defenderId) { // Only GM process this
return
2021-12-29 19:15:06 +01:00
}
// Build and send the defense message to the relevant people (ie GM + defender)
2022-04-08 23:42:01 +02:00
let defender = game.actors.get(attackDef.defenderId)
let defenderWeapons = defender.weapons
2021-12-29 19:15:06 +01:00
console.log("DEF WEP", attackDef)
2022-04-08 23:42:01 +02:00
this.attackStore[attackDef.id] = attackDef // Store !
2021-12-29 19:15:06 +01:00
ChatMessage.create({
2022-04-08 23:42:01 +02:00
alias: defender.name,
whisper: BoLUtility.getWhisperRecipientsAndGMs(defender.name),
2021-12-29 19:15:06 +01:00
content: await renderTemplate('systems/bol/templates/chat/rolls/defense-request-card.hbs', {
attackId: attackDef.id,
attacker: attackDef.attacker,
2022-04-08 23:42:01 +02:00
defender: defender,
2021-12-29 19:15:06 +01:00
defenderWeapons: defenderWeapons,
2022-03-10 21:05:53 +01:00
damageTotal: attackDef.damageRoll.total,
damagesIgnoresArmor: attackDef.damagesIgnoresArmor,
2021-12-29 19:15:06 +01:00
})
2022-03-10 21:05:53 +01:00
})
2021-12-29 19:15:06 +01:00
}
/* -------------------------------------------- */
static onSocketMessage(sockmsg) {
if (sockmsg.name == "msg_attack_success") {
2022-04-08 23:42:01 +02:00
BoLUtility.processAttackSuccess(sockmsg.data)
2021-12-29 19:15:06 +01:00
}
if (sockmsg.name == "msg_damage_handling") {
BoLUtility.processDamageHandling(sockmsg.data.event, sockmsg.data.attackId, sockmsg.data.defenseMode)
}
2022-02-23 20:39:58 +01:00
}
2022-01-23 09:25:09 +01:00
/* -------------------------------------------- */
2022-02-23 20:39:58 +01:00
static computeSpellCost(spell, nbOptCond = 0) {
2022-01-23 09:25:09 +01:00
let pp = spell.data.data.properties.ppcost
let minpp = __circle2minpp[spell.data.data.properties.circle]
2022-02-23 20:39:58 +01:00
pp = (pp - nbOptCond < minpp) ? minpp : pp - nbOptCond
2022-01-23 09:25:09 +01:00
return pp
2021-12-29 19:15:06 +01:00
}
/* -------------------------------------------- */
2022-03-10 21:05:53 +01:00
static getDamageFormula(weaponData, fightOption) {
let upgradeDamage = (fightOption && fightOption.data.properties.fightoptiontype == "twoweaponsatt")
2022-02-23 20:39:58 +01:00
let damageString = weaponData.properties.damage
let modifier = weaponData.properties.damageModifiers ?? 0
let multiplier = weaponData.properties.damageMultiplier ?? 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-02-23 20:39:58 +01:00
let reroll = (weaponData.properties.damageReroll1) ? "r1" : "" // Reroll 1 option
2022-01-19 21:57:34 +01:00
let formula = damageString
2022-02-23 20:39:58 +01:00
if (damageString.includes("d") || damageString.includes("D")) {
2022-03-10 21:05:53 +01:00
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
// Upgrade damage if needed
if ( upgradeDamage && ( !res[3] || res[3]=="") ) {
res[3] = "B" // Upgrade to bonus
}
2022-01-19 21:57:34 +01:00
if (res[3]) {
2022-03-10 21:05:53 +01:00
if ( upgradeDamage && res[3] == 'M') {
res[3] = "" // Disable lamlus for upgradeDamage
}
2022-01-19 21:57:34 +01:00
if (res[3] == 'M') {
2022-03-10 21:05:53 +01:00
postForm = 'kl' + nbDice
nbDice++
modIndex = 4
2022-01-19 21:57:34 +01:00
}
if (res[3] == 'B') {
2022-03-10 21:05:53 +01:00
postForm = 'kh' + nbDice
nbDice++
modIndex = 4
2022-01-19 21:57:34 +01:00
}
2021-12-25 23:26:27 +01:00
}
2022-03-10 21:05:53 +01:00
formula = "(" + nbDice + "d" + res[2] + reroll + postForm + "+" + modifier + ") *" + multiplier
2021-12-25 23:26:27 +01:00
}
2022-03-10 21:05:53 +01:00
return formula
2021-12-25 23:26:27 +01:00
}
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
}
}