2022-01-06 18:22:05 +01:00
|
|
|
/* -------------------------------------------- */
|
|
|
|
|
|
|
|
import { PegasusActorCreate } from "./pegasus-create-char.js";
|
|
|
|
import { PegasusUtility } from "./pegasus-utility.js";
|
2022-01-13 16:13:00 +01:00
|
|
|
import { PegasusRollDialog } from "./pegasus-roll-dialog.js";
|
2022-01-06 18:22:05 +01:00
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
export class PegasusCommands {
|
|
|
|
|
|
|
|
static init() {
|
|
|
|
if (!game.system.pegasus.commands) {
|
|
|
|
const pegasusCommands = new PegasusCommands();
|
|
|
|
pegasusCommands.registerCommand({ path: ["/char"], func: (content, msg, params) => pegasusCommands.createChar(msg), descr: "Create a new character" });
|
2022-01-13 16:13:00 +01:00
|
|
|
pegasusCommands.registerCommand({ path: ["/pool"], func: (content, msg, params) => pegasusCommands.poolRoll(msg), descr: "Generic Roll Window" });
|
2022-01-06 18:22:05 +01:00
|
|
|
game.system.pegasus.commands = pegasusCommands;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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("pegasusCommands._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);
|
|
|
|
}
|
|
|
|
|
2022-02-10 15:53:42 +01:00
|
|
|
/* -------------------------------------------- */
|
2022-01-06 18:22:05 +01:00
|
|
|
process(command, params, content, msg) {
|
|
|
|
return this._processCommand(this.commandsTable, command, params, content, msg);
|
|
|
|
}
|
|
|
|
|
2022-02-10 15:53:42 +01:00
|
|
|
/* -------------------------------------------- */
|
2022-01-06 18:22:05 +01:00
|
|
|
_processCommand(commandsTable, name, params, content = '', msg = {}, path = "") {
|
2022-02-10 15:53:42 +01:00
|
|
|
console.log("===> Processing command")
|
2022-01-06 18:22:05 +01:00
|
|
|
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) {
|
2022-07-10 10:22:04 +02:00
|
|
|
PegasusCommands._chatAnswer(msg, command.descr);
|
2022-01-06 18:22:05 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
async createChar(msg) {
|
|
|
|
game.system.pegasus.creator = new PegasusActorCreate();
|
|
|
|
game.system.pegasus.creator.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
static _chatAnswer(msg, content) {
|
|
|
|
msg.whisper = [game.user.id];
|
|
|
|
msg.content = content;
|
2022-01-13 16:13:00 +01:00
|
|
|
ChatMessage.create(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
async poolRoll( msg) {
|
2022-01-28 10:05:54 +01:00
|
|
|
let rollData = PegasusUtility.getBasicRollData()
|
|
|
|
rollData.alias = "Dice Pool Roll",
|
2022-01-13 16:13:00 +01:00
|
|
|
rollData.mode = "generic"
|
2022-01-16 16:09:32 +01:00
|
|
|
rollData.title = `Dice Pool Roll`;
|
2022-01-28 10:05:54 +01:00
|
|
|
|
2022-01-13 16:13:00 +01:00
|
|
|
let rollDialog = await PegasusRollDialog.create( this, rollData);
|
|
|
|
rollDialog.render( true );
|
2022-01-06 18:22:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|