395 lines
12 KiB
JavaScript
395 lines
12 KiB
JavaScript
/* -------------------------------------------- */
|
|
import { MournbladeCombat } from "./mournblade-combat.js";
|
|
import { MournbladeCommands } from "./mournblade-commands.js";
|
|
import { MournbladeActorCreate } from "./mournblade-create-char.js";
|
|
|
|
/* -------------------------------------------- */
|
|
export class MournbladeUtility {
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
static async init() {
|
|
Hooks.on('renderChatLog', (log, html, data) => MournbladeUtility.chatListeners(html));
|
|
Hooks.on("getCombatTrackerEntryContext", (html, options) => {
|
|
MournbladeUtility.pushInitiativeOptions(html, options);
|
|
});
|
|
Hooks.on("dropCanvasData", (canvas, data) => {
|
|
MournbladeUtility.dropItemOnToken(canvas, data)
|
|
});
|
|
|
|
this.rollDataStore = {}
|
|
this.defenderStore = {}
|
|
MournbladeCommands.init();
|
|
|
|
Handlebars.registerHelper('count', function (list) {
|
|
return list.length;
|
|
});
|
|
Handlebars.registerHelper('includes', function (array, val) {
|
|
return array.includes(val);
|
|
});
|
|
Handlebars.registerHelper('upper', function (text) {
|
|
return text.toUpperCase();
|
|
});
|
|
Handlebars.registerHelper('upperFirst', function (text) {
|
|
if (typeof text !== 'string') return text
|
|
return text.charAt(0).toUpperCase() + text.slice(1)
|
|
});
|
|
Handlebars.registerHelper('notEmpty', function (list) {
|
|
return list.length > 0;
|
|
});
|
|
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
static pushInitiativeOptions(html, options) {
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
static getSkills() {
|
|
return this.skills
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
static async ready() {
|
|
const skills = await MournbladeUtility.loadCompendium("fvtt-mournblade.skills")
|
|
this.skills = specs.map(i => i.toObject())
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
static async loadCompendiumData(compendium) {
|
|
const pack = game.packs.get(compendium);
|
|
return await pack?.getDocuments() ?? [];
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
static async loadCompendium(compendium, filter = item => true) {
|
|
let compendiumData = await MournbladeUtility.loadCompendiumData(compendium);
|
|
return compendiumData.filter(filter);
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
static getOptionsStatusList() {
|
|
return this.optionsStatusList;
|
|
}
|
|
/* -------------------------------------------- */
|
|
static async chatListeners(html) {
|
|
|
|
html.on("click", '.view-item-from-chat', event => {
|
|
game.system.Mournblade.creator.openItemView(event)
|
|
});
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
static async preloadHandlebarsTemplates() {
|
|
|
|
const templatePaths = [
|
|
'systems/fvtt-mournblade/templates/editor-notes-gm.html',
|
|
'systems/fvtt-mournblade/templates/partial-roll-select-effects.html',
|
|
'systems/fvtt-mournblade/templates/partial-options-statistics.html',
|
|
'systems/fvtt-mournblade/templates/partial-options-level.html',
|
|
'systems/fvtt-mournblade/templates/partial-options-range.html',
|
|
'systems/fvtt-mournblade/templates/partial-options-equipment-types.html',
|
|
'systems/fvtt-mournblade/templates/partial-equipment-effects.html'
|
|
]
|
|
return loadTemplates(templatePaths);
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
static removeChatMessageId(messageId) {
|
|
if (messageId) {
|
|
game.messages.get(messageId)?.delete();
|
|
}
|
|
}
|
|
|
|
static findChatMessageId(current) {
|
|
return MournbladeUtility.getChatMessageId(MournbladeUtility.findChatMessage(current));
|
|
}
|
|
|
|
static getChatMessageId(node) {
|
|
return node?.attributes.getNamedItem('data-message-id')?.value;
|
|
}
|
|
|
|
static findChatMessage(current) {
|
|
return MournbladeUtility.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 MournbladeUtility.findNodeMatching(current.parentElement, predicate);
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
static templateData(it) {
|
|
return MournbladeUtility.data(it)?.data ?? {}
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
static data(it) {
|
|
if (it instanceof Actor || it instanceof Item || it instanceof Combatant) {
|
|
return it.data;
|
|
}
|
|
return it;
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
static createDirectOptionList(min, max) {
|
|
let options = {};
|
|
for (let i = min; i <= max; i++) {
|
|
options[`${i}`] = `${i}`;
|
|
}
|
|
return options;
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
static buildListOptions(min, max) {
|
|
let options = ""
|
|
for (let i = min; i <= max; i++) {
|
|
options += `<option value="${i}">${i}</option>`
|
|
}
|
|
return options;
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
static getTarget() {
|
|
if (game.user.targets && game.user.targets.size == 1) {
|
|
for (let target of game.user.targets) {
|
|
return target;
|
|
}
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
static getDefenseState(actorId) {
|
|
return this.defenderStore[actorId];
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
static updateRollData(rollData) {
|
|
|
|
let id = rollData.rollId;
|
|
let oldRollData = this.rollDataStore[id] || {};
|
|
let newRollData = mergeObject(oldRollData, rollData);
|
|
this.rollDataStore[id] = newRollData;
|
|
}
|
|
/* -------------------------------------------- */
|
|
static saveRollData(rollData) {
|
|
game.socket.emit("system.Mournblade-rpg", {
|
|
name: "msg_update_roll", data: rollData
|
|
}); // Notify all other clients of the roll
|
|
this.updateRollData(rollData);
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
static getRollData(id) {
|
|
return this.rollDataStore[id];
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
static onSocketMesssage(msg) {
|
|
//console.log("SOCKET MESSAGE", msg.name, game.user.character.id, msg.data.defenderId);
|
|
if (msg.name == "msg_update_defense_state") {
|
|
this.updateDefenseState(msg.data.defenderId, msg.data.rollId);
|
|
}
|
|
if (msg.name == "msg_update_roll") {
|
|
this.updateRollData(msg.data);
|
|
}
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
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 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);
|
|
}
|
|
}
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
static async rollMournblade(rollData) {
|
|
|
|
this.createChatWithRollMode(rollData.alias, {
|
|
content: await renderTemplate(`systems/fvtt-Mournblade-rpg/templates/chat-generic-result.html`, rollData)
|
|
});
|
|
|
|
// And save the roll
|
|
this.saveRollData(rollData);
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
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];
|
|
}
|
|
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 = "Blinde message of " + game.user.name + "<br>" + chatOptions.content;
|
|
console.log("blindMessageToGM", chatGM);
|
|
game.socket.emit("system.fvtt-weapons-of-the-gods", { msg: "msg_gm_chat_message", data: chatGM });
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
static async searchItem(dataItem) {
|
|
let item;
|
|
if (dataItem.pack) {
|
|
item = await fromUuid("Compendium." + dataItem.pack + "." + dataItem.id);
|
|
} else {
|
|
item = game.items.get(dataItem.id)
|
|
}
|
|
return item;
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
static split3Columns(data) {
|
|
|
|
let array = [[], [], []];
|
|
if (data == undefined) return array;
|
|
|
|
let col = 0;
|
|
for (let key in data) {
|
|
let keyword = data[key];
|
|
keyword.key = key; // Self-reference
|
|
array[col].push(keyword);
|
|
col++;
|
|
if (col == 3) col = 0;
|
|
}
|
|
return array;
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
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;
|
|
}
|
|
chatOptions.alias = chatOptions.alias || name;
|
|
ChatMessage.create(chatOptions);
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
static getBasicRollData() {
|
|
let rollData = {
|
|
rollId: randomID(16),
|
|
rollMode: game.settings.get("core", "rollMode"),
|
|
}
|
|
MournbladeUtility.updateWithTarget(rollData)
|
|
return rollData
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
static updateWithTarget(rollData) {
|
|
let objectDefender
|
|
let target = MournbladeUtility.getTarget();
|
|
if (target) {
|
|
let defenderActor = game.actors.get(target.data.actorId)
|
|
objectDefender = MournbladeUtility.data(defenderActor)
|
|
objectDefender = mergeObject(objectDefender, target.data.actorData)
|
|
rollData.defender = objectDefender
|
|
rollData.attackerId = this.id
|
|
rollData.defenderId = objectDefender._id
|
|
}
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
static createChatWithRollMode(name, chatOptions) {
|
|
this.createChatMessage(name, game.settings.get("core", "rollMode"), chatOptions);
|
|
}
|
|
|
|
/* -------------------------------------------- */
|
|
static async confirmDelete(actorSheet, li) {
|
|
let itemId = li.data("item-id");
|
|
let msgTxt = "<p>Are you sure to remove this Item ?";
|
|
let buttons = {
|
|
delete: {
|
|
icon: '<i class="fas fa-check"></i>',
|
|
label: "Yes, remove it",
|
|
callback: () => {
|
|
actorSheet.actor.deleteEmbeddedDocuments("Item", [itemId]);
|
|
li.slideUp(200, () => actorSheet.render(false));
|
|
}
|
|
},
|
|
cancel: {
|
|
icon: '<i class="fas fa-times"></i>',
|
|
label: "Cancel"
|
|
}
|
|
}
|
|
msgTxt += "</p>";
|
|
let d = new Dialog({
|
|
title: "Confirm removal",
|
|
content: msgTxt,
|
|
buttons: buttons,
|
|
default: "cancel"
|
|
});
|
|
d.render(true);
|
|
}
|
|
|
|
} |