fvtt-hero-system-6/modules/hero6-commands.js

142 lines
4.6 KiB
JavaScript
Raw Permalink Normal View History

2022-12-15 21:44:23 +01:00
/* -------------------------------------------- */
import { Hero6Utility } from "./hero6-utility.js";
import { Hero6RollDialog } from "./hero6-roll-dialog.js";
/* -------------------------------------------- */
2023-05-07 14:03:14 +02:00
const __saveFirstToKey = { r: "reflex", f: "fortitude", w: "willpower" }
2022-12-15 21:44:23 +01:00
/* -------------------------------------------- */
export class Hero6Commands {
2023-05-07 14:03:14 +02:00
static ready() {
2022-12-15 22:12:28 +01:00
if (!game.system.hero6.commands) {
const hero6Commands = new Hero6Commands();
2023-05-07 14:03:14 +02:00
hero6Commands.registerCommand({ path: ["/rh"], func: (content, msg, params) => Hero6Commands.rollSpecialHero(msg, params), descr: "Special roll hero roll (1/2d6 like)" });
2022-12-15 22:12:28 +01:00
game.system.hero6.commands = hero6Commands;
2022-12-15 21:44:23 +01:00
}
2023-05-07 14:03:14 +02:00
Hooks.on("chatMessage", (html, content, msg) => {
if (content[0] == '/') {
let regExp = /(\S+)/g;
let commands = content.match(regExp);
if (game.hero6.commands.processChatCommand(commands, content, msg)) {
return false;
}
}
return true
})
2022-12-15 21:44:23 +01:00
}
constructor() {
this.commandsTable = {};
}
/* -------------------------------------------- */
registerCommand(command) {
this._addCommand(this.commandsTable, command.path, '', command);
}
/* -------------------------------------------- */
_addCommand(targetTable, path, fullPath, command) {
if (!this._validateCommand(targetTable, path, command)) {
return;
}
const term = path[0];
fullPath = fullPath + term + ' '
if (path.length == 1) {
command.descr = `<strong>${fullPath}</strong>: ${command.descr}`;
targetTable[term] = command;
}
else {
if (!targetTable[term]) {
targetTable[term] = { subTable: {} };
}
this._addCommand(targetTable[term].subTable, path.slice(1), fullPath, command)
}
}
/* -------------------------------------------- */
_validateCommand(targetTable, path, command) {
if (path.length > 0 && path[0] && command.descr && (path.length != 1 || targetTable[path[0]] == undefined)) {
return true;
}
console.warn("Hero6Commands._validateCommand failed ", targetTable, path, command);
return false;
}
/* -------------------------------------------- */
/* Manage chat commands */
processChatCommand(commandLine, content = '', msg = {}) {
// Setup new message's visibility
let rollMode = game.settings.get("core", "rollMode");
if (["gmroll", "blindroll"].includes(rollMode)) msg["whisper"] = ChatMessage.getWhisperRecipients("GM");
if (rollMode === "blindroll") msg["blind"] = true;
msg["type"] = 0;
let command = commandLine[0].toLowerCase();
let params = commandLine.slice(1);
return this.process(command, params, content, msg);
}
/* -------------------------------------------- */
process(command, params, content, msg) {
return this._processCommand(this.commandsTable, command, params, content, msg);
}
/* -------------------------------------------- */
_processCommand(commandsTable, name, params, content = '', msg = {}, path = "") {
console.log("===> Processing command")
let command = commandsTable[name];
path = path + name + " ";
if (command && command.subTable) {
if (params[0]) {
return this._processCommand(command.subTable, params[0], params.slice(1), content, msg, path)
}
else {
this.help(msg, command.subTable);
return true;
}
}
if (command && command.func) {
const result = command.func(content, msg, params);
if (result == false) {
Hero6Commands._chatAnswer(msg, command.descr);
}
return true;
}
return false;
}
/* -------------------------------------------- */
static _chatAnswer(msg, content) {
msg.whisper = [game.user.id];
msg.content = content;
ChatMessage.create(msg);
}
/* -------------------------------------------- */
2023-05-07 14:03:14 +02:00
static async rollSpecialHero(msg, params) {
console.log("ROLL HERE", msg, params)
let formula = params.join(' ')
if (formula) {
let foundryFormula = Hero6Utility.convertRollHeroSyntax(formula)
2023-05-07 14:03:14 +02:00
let myRoll = new Roll(foundryFormula).roll({ async: false })
await Hero6Utility.showDiceSoNice(myRoll, game.settings.get("core", "rollMode"))
let bodyValue = Hero6Utility.computeBodyValue(myRoll)
let msgFlavor = await renderTemplate('systems/fvtt-hero-system-6/templates/chat/chat-hr-roll.hbs', {myRoll, bodyValue} )
let msg = await myRoll.toMessage({
user: game.user.id,
rollMode: game.settings.get("core", "rollMode"),
flavor: msgFlavor
})
2023-05-07 14:03:14 +02:00
return true
2022-12-15 21:44:23 +01:00
}
2023-05-07 14:03:14 +02:00
return false
2022-12-15 21:44:23 +01:00
}
}