bol/module/system/bol-utility.js

756 lines
26 KiB
JavaScript
Raw Normal View History

2023-04-06 20:15:04 +02:00
import { BoLRoll, 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 }
2023-06-23 08:37:50 +02:00
const __validDices = { "6": 1, "8": 1, "10": 1, "12": 1 }
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() {
game.settings.register("bol", "rollArmor", {
2023-12-04 21:30:12 +01:00
name: game.i18n.localize("BOL.settings.rollArmor"),
hint: game.i18n.localize("BOL.settings.rollArmorTooltip"),
2022-02-23 20:39:58 +01:00
scope: "world",
config: true,
default: true,
type: Boolean,
onChange: lang => window.location.reload()
2022-07-17 18:20:05 +02:00
})
2022-02-23 20:39:58 +01:00
game.settings.register("bol", "useBougette", {
2023-12-04 21:30:12 +01:00
name: game.i18n.localize("BOL.settings.useBougette"),
hint: game.i18n.localize("BOL.settings.useBougetteTooltip"),
2022-02-23 20:39:58 +01:00
scope: "world",
config: true,
default: false,
type: Boolean,
onChange: lang => window.location.reload()
2022-07-17 18:20:05 +02:00
})
2023-04-06 20:15:04 +02:00
game.settings.register("bol", "auto-remove-dead", {
2023-12-04 21:30:12 +01:00
name: game.i18n.localize("BOL.settings.removeDead"),
hint: game.i18n.localize("BOL.settings.removeDeadTooltip"),
2023-04-06 20:15:04 +02:00
scope: "world",
config: true,
default: false,
type: Boolean
})
2023-01-26 20:50:56 +01:00
game.settings.register("bol", "dice-formula", {
2023-12-04 21:30:12 +01:00
name: game.i18n.localize("BOL.settings.diceFormula"),
hint: game.i18n.localize("BOL.settings.diceFormulaTooltip"),
2023-01-26 20:50:56 +01:00
scope: "world",
config: true,
2023-03-29 23:04:02 +02:00
default: "6",
2023-01-26 20:50:56 +01:00
type: String,
2023-06-23 08:37:50 +02:00
choices: { "6": "2d6", "8": "2d8", "10": "2d10", "12": "2d12", "20": "2d20" },
onChange: value => {
2023-01-26 20:50:56 +01:00
BoLUtility.setDiceFormula(value)
}
})
game.settings.register("bol", "dice-success-value", {
2023-12-04 21:30:12 +01:00
name: game.i18n.localize("BOL.settings.diceSuccessValue"),
hint: game.i18n.localize("BOL.settings.diceSuccessValueTooltip"),
2023-01-26 20:50:56 +01:00
scope: "world",
config: true,
default: 9,
range: {
min: 2,
max: 40,
step: 1
},
type: Number,
2023-06-23 08:37:50 +02:00
onChange: value => {
2023-01-26 20:50:56 +01:00
BoLUtility.setSuccessValue(value)
}
})
game.settings.register("bol", "dice-critical-success-value", {
2023-12-04 21:30:12 +01:00
name: game.i18n.localize("BOL.settings.diceCriticalValue"),
hint: game.i18n.localize("BOL.settings.diceCriticalValueTooltip"),
2023-01-26 20:50:56 +01:00
scope: "world",
config: true,
default: 12,
range: {
min: 2,
max: 40,
step: 1
},
type: Number,
2023-06-23 08:37:50 +02:00
onChange: value => {
2023-01-26 20:50:56 +01:00
BoLUtility.setCriticalSuccessValue(value)
}
})
game.settings.register("bol", "dice-critical-failure-value", {
2023-12-04 21:30:12 +01:00
name: game.i18n.localize("BOL.settings.diceCriticalFailure"),
hint: game.i18n.localize("BOL.settings.diceCriticalFailureTooltip"),
2023-01-26 20:50:56 +01:00
scope: "world",
config: true,
default: 2,
range: {
min: 2,
max: 40,
step: 1
},
type: Number,
2023-06-23 08:37:50 +02:00
onChange: value => {
2023-01-26 20:50:56 +01:00
BoLUtility.setCriticalFailureValue(value)
}
})
2022-11-25 20:47:28 +01:00
game.settings.register("world", "character-summary-data", {
name: "character-summary-data",
scope: "world",
config: false,
2022-12-25 18:00:42 +01:00
default: { npcList: [], x: 200, y: 200 },
2022-11-25 20:47:28 +01:00
type: Object
})
2022-07-17 18:20:05 +02:00
game.settings.register("bol", "logoActorSheet", {
2023-12-04 21:30:12 +01:00
name: game.i18n.localize("BOL.settings.defaultLogoActorSheetPath"),
hint: game.i18n.localize("BOL.settings.defaultLogoPathActorSheetTooltip"),
2022-07-17 18:20:05 +02:00
scope: "world",
config: true,
default: "/systems/bol/ui/logo.webp",
type: String,
onChange: lang => window.location.reload()
})
game.settings.register("bol", "logoTopLeft", {
2023-12-04 21:30:12 +01:00
name: game.i18n.localize("BOL.settings.defaultLogoTopLeftPath"),
hint: game.i18n.localize("BOL.settings.defaultLogoTopLeftPathTooltip"),
2022-07-17 18:20:05 +02:00
scope: "world",
config: true,
default: "/systems/bol/ui/logo2.webp",
type: String,
onChange: lang => window.location.reload()
})
2022-12-23 23:24:09 +01:00
game.settings.register("bol", "horoscope-group", {
name: "horoscope-group",
scope: "world",
config: false,
default: {},
type: Object
})
2022-02-23 20:39:58 +01:00
this.rollArmor = game.settings.get("bol", "rollArmor") // Roll armor or not
this.useBougette = game.settings.get("bol", "useBougette") // Use optionnal bougette rules
2022-07-17 18:20:05 +02:00
this.actorSheetLogo = game.settings.get("bol", "logoActorSheet") || "/systems/bol/ui/logo.webp"
this.logoTopLeft = game.settings.get("bol", "logoTopLeft") || "/systems/bol/ui/logo2.webp"
2023-01-26 20:50:56 +01:00
this.diceFormula = game.settings.get("bol", "dice-formula")
this.successValue = Number(game.settings.get("bol", "dice-success-value"))
this.criticalSuccessValue = Number(game.settings.get("bol", "dice-critical-success-value"))
this.criticalFailureValue = Number(game.settings.get("bol", "dice-critical-failure-value"))
// Update the effect modifiers
game.bol.config.effectIdentifiers = foundry.utils.mergeObject(game.bol.config.effectIdentifiers, game.bol.config.attackAttributes)
game.bol.config.effectIdentifiers = foundry.utils.mergeObject(game.bol.config.effectIdentifiers, game.bol.config.aptitudes)
2022-02-23 20:39:58 +01:00
}
2023-01-26 20:50:56 +01:00
/* -------------------------------------------- */
static setDiceFormula(value) {
this.diceFormula = value
}
static setSuccessValue(value) {
this.successValue = Number(value)
}
static setCriticalSuccessValue(value) {
this.criticalSuccessValue = Number(value)
}
static setCriticalFailureValue(value) {
this.criticalFailureValue = Number(value)
}
static getDiceData() {
2023-02-19 19:35:03 +01:00
let df = this.diceFormula
2023-06-23 08:37:50 +02:00
if (!__validDices[String(this.diceFormula)]) {
2023-02-19 19:35:03 +01:00
df = "6"
}
2023-01-26 20:50:56 +01:00
return {
2023-03-29 23:04:02 +02:00
diceFormula: df,
2023-06-23 08:37:50 +02:00
successValue: this.successValue,
2023-01-26 20:50:56 +01:00
criticalSuccessValue: this.criticalSuccessValue,
criticalFailureValue: this.criticalFailureValue
}
}
2022-02-23 20:39:58 +01:00
/* -------------------------------------------- */
static getRollArmor() {
return this.rollArmor
}
/* -------------------------------------------- */
static getUseBougette() {
return this.useBougette
2021-11-01 00:28:42 +01:00
}
2022-07-17 18:20:05 +02:00
/* -------------------------------------------- */
static getLogoActorSheet() {
return this.actorSheetLogo
}
/* -------------------------------------------- */
static getLogoTopLeft() {
return this.logoTopLeft
}
/* -------------------------------------------- */
2022-12-25 18:00:42 +01:00
static getActorFromRollData(rollData) {
let actor = game.actors.get(rollData.actorId)
if (rollData.tokenId) {
let token = canvas.tokens.placeables.find(t => t.id == rollData.tokenId)
if (token) {
actor = token.actor
}
}
return actor
}
2022-11-25 20:47:28 +01:00
2021-11-01 00:28:42 +01:00
/* -------------------------------------------- */
static async ready() {
//$("#logo").attr("src", this.getLogoTopLeft() )
2022-11-25 20:47:28 +01:00
$("#logo").css("content", `url(${this.getLogoTopLeft()})`)
CONFIG.statusEffects = foundry.utils.duplicate(game.bol.config.statusEffects)
2021-11-01 00:28:42 +01:00
}
2021-12-29 19:15:06 +01:00
2023-06-23 08:37:50 +02:00
/* -------------------------------------------- */
static chatDataSetup(content, modeOverride, isRoll = false, forceWhisper) {
let chatData = {
user: game.user.id,
rollMode: modeOverride || game.settings.get("core", "rollMode"),
content: content
};
if (["gmroll", "blindroll"].includes(chatData.rollMode)) chatData["whisper"] = ChatMessage.getWhisperRecipients("GM").map(u => u.id);
if (chatData.rollMode === "blindroll") chatData["blind"] = true;
else if (chatData.rollMode === "selfroll") chatData["whisper"] = [game.user];
if (forceWhisper) { // Final force !
chatData["speaker"] = ChatMessage.getSpeaker();
chatData["whisper"] = ChatMessage.getWhisperRecipients(forceWhisper);
}
return chatData;
}
/* -------------------------------------------- */
static postItem(chatData) {
// Don't post any image for the item (which would leave a large gap) if the default image is used
if (chatData.img.includes("/blank.png")) {
chatData.img = null;
}
// JSON object for easy creation
chatData.jsondata = JSON.stringify(
{
compendium: "postedItem",
payload: chatData,
});
renderTemplate('systems/bol/templates/item/post-item.hbs', chatData).then(html => {
let chatOptions = BoLUtility.chatDataSetup(html);
ChatMessage.create(chatOptions)
2023-06-23 08:37:50 +02:00
});
}
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) {
2023-04-04 13:41:22 +02:00
return game.users.filter(filter).map(user => user.id);
2021-12-29 19:15:06 +01:00
}
/* -------------------------------------------- */
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
/* -------------------------------------------- */
2022-06-11 20:56:35 +02:00
static getOtherWhisperRecipients(name) {
2022-03-27 22:56:43 +02:00
let users = []
2022-06-11 20:56:35 +02:00
for (let user of game.users) {
2022-12-25 18:00:42 +01:00
if (!user.isGM && user.name != name) {
2022-08-31 22:24:56 +02:00
users.push(user.id)
2022-03-27 22:56:43 +02:00
}
}
return users
}
2022-06-11 20:56:35 +02:00
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 = foundry.utils.duplicate(chatOptions);
2021-12-29 19:15:06 +01:00
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
/* -------------------------------------------- */
2023-03-18 10:24:30 +01:00
static sendAttackSuccess(rollData) {
if (rollData.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) {
game.socket.emit("system.bol", { name: "msg_attack_success", data: foundry.utils.duplicate(rollData) })
2022-01-01 23:32:48 +01:00
} else {
2023-03-18 10:24:30 +01:00
BoLUtility.processAttackSuccess(rollData)
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)
2024-02-22 18:29:04 +01:00
if (actor?.isOwner) return
2022-03-27 22:56:43 +02:00
else if (game.user.isGM || data.author.id === game.user.id) return
2022-06-11 20:56:35 +02:00
2022-03-27 22:56:43 +02:00
const divButtons = chatCard.find('.actions-section')
divButtons.hide()
}
}
2022-06-11 20:56:35 +02:00
2022-06-11 10:21:18 +02:00
/* -------------------------------------------- */
2022-06-11 20:56:35 +02:00
static getRollDataFromMessage(event) {
2022-06-11 10:21:18 +02:00
let messageId = BoLUtility.findChatMessageId(event.currentTarget)
let message = game.messages.get(messageId)
2022-06-11 20:56:35 +02:00
return message.getFlag("world", "bol-roll-data")
}
2023-04-06 20:15:04 +02:00
/* -------------------------------------------- */
2023-06-23 08:37:50 +02:00
static requestInitRoll(actorId, combatData) {
let actor = game.actors.get(actorId)
2024-02-22 18:29:04 +01:00
if (actor?.isOwner) {
2023-04-06 20:15:04 +02:00
ui.notifications.info(game.i18n.localize("BOL.ui.warninitiative"))
2023-06-23 08:37:50 +02:00
BoLRoll.aptitudeCheck(actor, "init", undefined, combatData)
2023-04-06 20:15:04 +02:00
}
}
2022-06-11 20:56:35 +02:00
/* -------------------------------------------- */
static cleanupButtons(id) {
$(`#${id}`).hide() // Hide the options roll buttons
game.socket.emit("system.bol", { name: "msg_cleanup_buttons", data: { id: id } })
2022-06-11 10:21:18 +02:00
}
2021-12-29 19:15:06 +01:00
/* -------------------------------------------- */
static async chatListeners(html) {
2022-06-11 20:56:35 +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-06-11 10:21:18 +02:00
let rollData = BoLUtility.getRollDataFromMessage(event)
2022-06-11 20:56:35 +02:00
BoLUtility.cleanupButtons(rollData.applyId)
2022-01-17 23:50:57 +01:00
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-06-11 20:56:35 +02:00
event.preventDefault()
2022-06-11 10:21:18 +02:00
let rollData = BoLUtility.getRollDataFromMessage(event)
2022-06-11 20:56:35 +02:00
rollData.damageMode = event.currentTarget.attributes['data-damage-mode'].value
2022-01-17 23:50:57 +01:00
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();
2022-06-11 10:21:18 +02:00
let rollData = BoLUtility.getRollDataFromMessage(event)
2022-06-11 20:56:35 +02:00
let actor = game.actors.get(rollData.actorId)
2022-04-08 23:42:01 +02:00
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();
2022-06-11 10:21:18 +02:00
let rollData = BoLUtility.getRollDataFromMessage(event)
2022-06-11 20:56:35 +02:00
let actor = game.actors.get(rollData.actorId)
2022-04-08 23:42:01 +02:00
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-06-11 10:21:18 +02:00
let rollData = BoLUtility.getRollDataFromMessage(event)
2022-06-11 20:56:35 +02:00
let actor = game.actors.get(rollData.actorId)
2022-04-08 23:42:01 +02:00
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()
2023-10-15 17:51:07 +02:00
let attr = event.currentTarget.attributes['data-attack-id']
if ( !attr) {
ui.notifications.warn("Impossible de trouver l'attaque correspondante, erreur de suivi de combat.")
return
}
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-11-25 20:47:28 +01:00
2022-06-11 20:56:35 +02:00
// Remove message for all
let msgId = BoLUtility.findChatMessageId(event.currentTarget)
2022-02-23 20:39:58 +01:00
if (game.user.isGM) {
2022-06-11 20:56:35 +02:00
BoLUtility.processDamageHandling(attackId, defenseMode, weaponId, msgId)
2021-12-29 19:15:06 +01:00
} else {
2022-06-11 20:56:35 +02:00
game.socket.emit("system.bol", { name: "msg_damage_handling", data: { msgId: msgId, attackId: attackId, defenseMode: defenseMode, weaponId: weaponId } })
2021-11-01 00:28:42 +01:00
}
2022-06-11 20:56:35 +02:00
})
2023-03-18 10:24:30 +01:00
html.on("click", '.recup-vitalite', event => {
2023-06-23 08:37:50 +02:00
event.preventDefault()
2023-03-18 10:24:30 +01:00
let actorId = event.currentTarget.attributes['data-actor-id'].value
let recupHP = event.currentTarget.attributes['data-recup-hp'].value
let actor = game.actors.get(actorId)
let messageId = BoLUtility.findChatMessageId(event.currentTarget)
BoLUtility.removeChatMessageId(messageId)
actor.applyRecuperation(recupHP)
})
2021-12-29 19:15:06 +01:00
}
/* -------------------------------------------- */
2024-02-22 18:29:04 +01:00
static async processDamageHandling(attackId, defenseMode, weaponId, msgId) {
2022-02-23 20:39:58 +01:00
if (!game.user.isGM) {
2022-06-11 20:56:35 +02:00
return
2021-11-01 00:28:42 +01:00
}
2023-03-18 10:24:30 +01:00
let message = game.messages.get(msgId)
let rollData = message.getFlag("world", "bol-roll-data")
2022-11-25 20:47:28 +01:00
BoLUtility.removeChatMessageId(msgId)
2023-03-18 10:24:30 +01:00
2022-06-11 20:56:35 +02:00
console.log("Damage Handling", attackId, defenseMode, weaponId)
// Only GM process this
2024-02-22 18:29:04 +01:00
if (rollData?.defenderId) {
2023-03-25 08:42:14 +01:00
if (rollData.defenseDone || defenseMode == 'damage-not-applied') {
2022-04-08 23:42:01 +02:00
return
} // ?? Why ???
2023-03-18 10:24:30 +01:00
rollData.defenseDone = true
rollData.defenseMode = defenseMode
let token = game.scenes.current.tokens.get(rollData.targetId)
2022-05-23 18:38:51 +02:00
let defender = token.actor
2023-06-23 08:37:50 +02: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()
2023-03-18 10:24:30 +01:00
rollData.rollArmor = new Roll(armorFormula)
2024-08-13 10:37:56 +02:00
await rollData.rollArmor.roll()
2023-03-18 10:24:30 +01:00
rollData.armorProtect = (rollData.rollArmor.total < 0) ? 0 : rollData.rollArmor.total
rollData.finalDamage = rollData.damageTotal - rollData.armorProtect
rollData.finalDamage = (rollData.finalDamage < 0) ? 0 : rollData.finalDamage
defender.sufferDamage(rollData.finalDamage)
console.log("Armor roll -> result ", rollData)
2021-11-01 00:28:42 +01:00
}
2021-12-29 19:15:06 +01:00
if (defenseMode == 'damage-without-armor') {
rollData.finalDamage = rollData.damageTotal
2023-03-18 10:24:30 +01:00
defender.sufferDamage(rollData.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()
2023-03-18 10:24:30 +01:00
rollData.rollArmor = new Roll(armorFormula)
2024-08-13 10:37:56 +02:00
await rollData.rollArmor.roll()
2023-03-18 10:24:30 +01:00
rollData.armorProtect = (rollData.rollArmor.total < 0) ? 0 : rollData.rollArmor.total
rollData.rollHero = new Roll("1d6")
2024-08-13 10:37:56 +02:00
await rollData.rollHero.roll()
2023-03-18 10:24:30 +01:00
rollData.finalDamage = rollData.damageTotal - rollData.rollHero.total - rollData.armorProtect
rollData.finalDamage = (rollData.finalDamage < 0) ? 0 : rollData.finalDamage
defender.sufferDamage(rollData.finalDamage)
2022-05-10 23:04:04 +02:00
defender.subHeroPoints(1)
2021-12-29 19:15:06 +01:00
}
if (defenseMode == 'hero-in-extremis') {
2023-03-18 10:24:30 +01:00
rollData.finalDamage = 0;
rollData.weaponHero = defender.weapons.find(item => item._id == weaponId);
2022-04-08 23:42:01 +02:00
defender.deleteEmbeddedDocuments("Item", [weaponId]);
2021-12-29 19:15:06 +01:00
}
2022-06-11 20:56:35 +02:00
let defenderUser
for (let user of game.users) {
2022-11-25 20:47:28 +01:00
if (user.character && user.character.id == defender.id) {
2022-06-11 20:56:35 +02:00
defenderUser = user
}
2022-11-25 20:47:28 +01:00
}
2022-06-11 20:56:35 +02:00
let damageResults = {
2023-03-18 10:24:30 +01:00
attackId: rollData.id,
attacker: rollData.attacker,
rollArmor: rollData.rollArmor,
rollHero: rollData.rollHero,
weaponHero: rollData.weaponHero,
armorProtect: rollData.armorProtect,
2022-06-11 20:56:35 +02:00
name: defender.name,
defender: defender,
2023-03-18 10:24:30 +01:00
defenseMode: rollData.defenseMode,
finalDamage: rollData.finalDamage
2022-06-11 20:56:35 +02:00
}
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),
2022-06-11 20:56:35 +02:00
content: await renderTemplate('systems/bol/templates/chat/rolls/defense-result-card.hbs', damageResults)
})
console.log("Defender data : ", defenderUser)
ChatMessage.create({
alias: defender.name,
whisper: BoLUtility.getOtherWhisperRecipients(defenderUser?.name),
content: await renderTemplate('systems/bol/templates/chat/rolls/defense-summary-card.hbs', damageResults)
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 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) {
2023-04-04 13:41:22 +02:00
return weapon.system.type == 'ranged' || weapon.system.thrown;
2021-11-01 23:06:34 +01:00
}
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
/* -------------------------------------------- */
2023-03-18 10:24:30 +01:00
static async processAttackSuccess(rollData) {
console.log("Attack success processing", rollData)
if (!game.user.isGM || !rollData.defenderId) { // Only GM process this
2022-04-08 23:42:01 +02:00
return
2021-12-29 19:15:06 +01:00
}
// Build and send the defense message to the relevant people (ie GM + defender)
2023-03-18 10:24:30 +01:00
let defender = game.actors.get(rollData.defenderId)
2022-07-01 16:30:21 +02:00
let defenderWeapons = defender.weapons || []
2023-03-18 10:24:30 +01:00
let msg = await 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', {
2023-03-18 10:24:30 +01:00
attackId: rollData.id,
attacker: rollData.attacker,
2022-04-08 23:42:01 +02:00
defender: defender,
defenderHeroPoints:defender.getHeroPoints(),
2021-12-29 19:15:06 +01:00
defenderWeapons: defenderWeapons,
2023-03-18 10:24:30 +01:00
damageTotal: rollData.damageTotal,
damagesIgnoresArmor: rollData.damagesIgnoresArmor,
2021-12-29 19:15:06 +01:00
})
2022-03-10 21:05:53 +01:00
})
2023-03-18 10:24:30 +01:00
msg.setFlag("world", "bol-roll-data", rollData)
console.log("DEF WEP", rollData, defender)
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
}
2022-06-11 20:56:35 +02:00
if (sockmsg.name == "msg_cleanup_buttons") {
$(`#${sockmsg.data.id}`).hide() // Hide the options roll buttons
}
2023-04-06 20:15:04 +02:00
if (sockmsg.name == "msg_request_init_roll") {
2023-06-23 08:37:50 +02:00
this.requestInitRoll(sockmsg.data.actorId, sockmsg.data.combatData)
2023-04-06 20:15:04 +02:00
}
2021-12-29 19:15:06 +01:00
if (sockmsg.name == "msg_damage_handling") {
2022-06-11 20:56:35 +02:00
BoLUtility.processDamageHandling(sockmsg.data.attackId, sockmsg.data.defenseMode, sockmsg.data.weaponId, sockmsg.data.msgId)
2021-12-29 19:15:06 +01:00
}
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) {
2023-04-04 13:41:22 +02:00
let pp = spell.system.properties.ppcost
let minpp = __circle2minpp[spell.system.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) {
2023-04-04 13:41:22 +02:00
let upgradeDamage = (fightOption && fightOption.system.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")) {
2023-08-26 18:03:08 +02:00
let myReg = new RegExp('(\\d+)[dD]([\\d]+)([MB]*)?([\\+\\d]*)?', 'g')
2022-03-10 21:05:53 +01:00
let res = myReg.exec(damageString)
let nbDice = parseInt(res[1])
let postForm = 'kh' + nbDice
// Upgrade damage if needed
2022-06-11 20:56:35 +02:00
if (upgradeDamage && (!res[3] || res[3] == "")) {
2022-03-10 21:05:53 +01:00
res[3] = "B" // Upgrade to bonus
}
2022-01-19 21:57:34 +01:00
if (res[3]) {
2022-06-11 20:56:35 +02:00
if (upgradeDamage && res[3] == 'M') {
2022-03-10 21:05:53 +01:00
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++
2022-01-19 21:57:34 +01:00
}
2022-07-16 11:03:33 +02:00
if (res[3] == 'MM') {
postForm = 'kl' + nbDice
nbDice += 2
}
2022-01-19 21:57:34 +01:00
if (res[3] == 'B') {
2022-03-10 21:05:53 +01:00
postForm = 'kh' + nbDice
nbDice++
2022-01-19 21:57:34 +01:00
}
2022-07-16 11:03:33 +02:00
if (res[3] == 'BB') {
postForm = 'kh' + nbDice
nbDice += 2
}
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
/* -------------------------------------------- */
2022-11-25 20:47:28 +01:00
static async loadCompendiumData(compendium) {
const pack = game.packs.get(compendium);
return await pack?.getDocuments() ?? [];
}
/* -------------------------------------------- */
static async loadCompendium(compendium, filter = item => true) {
let compendiumData = await this.loadCompendiumData(compendium);
return compendiumData.filter(filter);
}
/* -------------------------------------------- */
static async searchItem(dataItem) {
let item
if (dataItem.pack) {
let id = dataItem.id || dataItem._id
let items = await this.loadCompendium(dataItem.pack, item => item.id == id)
item = items[0] || undefined
} else {
item = game.items.get(dataItem.id)
2021-12-29 19:15:06 +01:00
}
2022-11-25 20:47:28 +01:00
return item
2021-11-01 00:28:42 +01:00
}
2022-12-25 18:00:42 +01:00
/* -------------------------------------------- */
static updateSheets() {
// Then force opened actor refresh if needed
for (let actor of game.actors) {
if (actor.sheet.rendered) {
actor.sheet.render()
}
}
game.bol.charSummary.updatePCSummary() // Refresh if needed
}
2022-11-25 20:47:28 +01:00
2022-12-25 18:00:42 +01:00
/* -------------------------------------------- */
static removeGroupHoroscope(rollData) {
let horo = rollData.horoscopeGroupList[rollData.selectedGroupHoroscopeIndex]
let horoscopes = foundry.utils.duplicate(game.settings.get("bol", "horoscope-group"))
let toChange = foundry.utils.duplicate(horoscopes[horo.id])
2022-12-25 18:00:42 +01:00
toChange.availableDice -= horo.nbDice // Remove the dice
if (toChange.availableDice <= 0) {
horoscopes[horo.id] = undefined
} else {
horoscopes[horo.id] = toChange
}
game.settings.set("bol", "horoscope-group", horoscopes)
this.updateSheets()
}
2021-11-01 00:28:42 +01:00
}