foundryvtt-reve-de-dragon/module/rdd-commands.js

186 lines
7.5 KiB
JavaScript
Raw Normal View History

2020-12-29 00:11:58 +01:00
/* -------------------------------------------- */
import { ChatUtility } from "./chat-utility.js";
import { Misc } from "./misc.js";
import { RdDResolutionTable } from "./rdd-resolution-table.js";
import { RdDRollResolution } from "./rdd-roll-resolution.js";
import { RdDRollTables } from "./rdd-rolltables.js";
import { RdDUtility } from "./rdd-utility.js";
import { TMRUtility } from "./tmr-utility.js";
const rddRollNumeric = /(\d+)\s*([\+\-]?\d+)?\s*(s)?/;
/* -------------------------------------------- */
export class RdDCommands {
static init() {
if (!game.system.rdd.commands) {
const rddCommands = new RdDCommands();
rddCommands.registerCommand({ path: ["/aide"], descr: "Affiche l'aide pour toutes les commandes", func: (content, msg, params) => rddCommands.help(msg) });
rddCommands.registerCommand({ path: ["/help"], descr: "Affiche l'aide pour toutes les commandes", func: (content, msg, params) => rddCommands.help(msg) });
rddCommands.registerCommand({ path: ["/table", "queues"], descr: "Tire une Queue de Dragon", func: (content, msg, params) => RdDRollTables.getQueue() });
rddCommands.registerCommand({ path: ["/table", "ombre"], descr: "Tire une Ombre de Dragon", func: (content, msg, params) => RdDRollTables.getOmbre() });
rddCommands.registerCommand({ path: ["/table", "tetehr"], descr: "Tire une Tête de Dragon pour Hauts Revants", func: (content, msg, params) => RdDRollTables.getTeteHR() });
rddCommands.registerCommand({ path: ["/table", "tete"], descr: "Tire une Tête de Dragon", func: (content, msg, params) => RdDRollTables.getTete() });
rddCommands.registerCommand({ path: ["/table", "souffle"], descr: " Tire un Souffle de Dragon", func: (content, msg, params) => RdDRollTables.getSouffle() });
rddCommands.registerCommand({ path: ["/table", "tarot"], descr: "Tire une carte du Tarot Draconique", func: (content, msg, params) => RdDRollTables.getTarot() });
rddCommands.registerCommand({ path: ["/table", "tmr"], descr: "Tire une case aléatoire des Terre médiane", func: (content, msg, params) => TMRUtility.getTMRAleatoire() });
rddCommands.registerCommand({ path: ["/tmra"], descr: "Tire une case aléatoire des Terre médiane", func: (content, msg, params) => TMRUtility.getTMRAleatoire() });
rddCommands.registerCommand({ path: ["/tmrr"], descr: "Syntaxe: <strong>/tmrr case jet</strong><br>Détermine quelle est la rencontre dans la case pour le jet<br>Example: <strong>/tmrr forêt 50</strong>", func: (content, msg, params) => rddCommands.getRencontreTMR(params) });
rddCommands.registerCommand({
path: ["/rdd"], descr: `Effectue un jet de dés dans la table de résolution. Examples:
<br><strong>/rdd</strong> ouvre la table de résolution
<br><strong>/rdd 10 3</strong> effectue un jet 10 à +3
<br><strong>/rdd 10 +2</strong> effectue un jet 10 à +2
<br><strong>/rdd 15 -2</strong> effectue un jet 15 à -2
<br><strong>/rdd 15 0 s</strong> effectue un jet 15 à 0, avec significative requise
`, func: (content, msg, params) => rddCommands.rollRdd(msg, params)
});
rddCommands.registerCommand({ path: ["/payer"], descr: `Permet de payer un montant. Exemples:
<br><strong>/payer 5s 10d</strong> permet d'envoyer un message pour payer 5 sols et 10 deniers
<br><strong>/payer 10d</strong> permet d'envoyer un message pour payer 10 deniers
`, func: (content, msg, params) => RdDUtility.afficherDemandePayer(params[0], params[1])});
game.system.rdd.commands = rddCommands;
}
}
constructor() {
this.commandsTable = {};
}
registerCommand(command) {
this._addCommand(this.commandsTable, command.path, command);
}
_addCommand(targetTable, path, command) {
if (!this._validateCommand(targetTable, path, command)) {
return;
}
const term = path[0];
if (path.length == 1) {
targetTable[term] = command;
}
else {
if (!targetTable[term]) {
targetTable[term] = { subTable: {} };
}
this._addCommand(targetTable[term].subTable, path.slice(1), command)
}
}
_validateCommand(targetTable, path, command) {
if (path.length > 0 && path[0] && command.descr && (path.length != 1 || targetTable[path[0]] == undefined)) {
return true;
}
console.warn("RdDCommands._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];
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 = "") {
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) {
if (command.func(content, msg, params) === false) {
this._displayHelp(msg, `${path}: ${command.descr}`);
}
return true;
}
return false;
}
/* -------------------------------------------- */
help(msg, table = undefined) {
let list = []
this._buildSubTableHelp(list, '', table || this.commandsTable);
msg.whisper = [game.user._id];
msg.content = 'Commandes disponibles<ul class="alterne-list"><li class="list-item">' + list.reduce((a, b) => a + '</li><li class="list-item">' + b) + '</li></ul>';
ChatMessage.create(msg);
}
/* -------------------------------------------- */
_buildSubTableHelp(list, path, table) {
for (let [name, command] of Object.entries(table)) {
if (command) {
if (command.subTable) {
this._buildSubTableHelp(list, path + name + " ", command.subTable);
} else {
list.push(`<strong>${path}${name}</strong>: ${command.descr}`);
}
}
}
return list;
}
getRencontreTMR(params) {
if (params.length == 2) {
return TMRUtility.getRencontre(params[0], params[1])
}
else {
return false;
}
}
async rollRdd(msg, params) {
if (params.length == 0) {
RdDRollResolution.open();
}
else {
let flatParams = params.reduce((a, b) => `${a} ${b}`);
const numericParams = flatParams.match(rddRollNumeric);
if (numericParams) {
const carac = Misc.toInt(numericParams[1]);
const diff = Misc.toInt(numericParams[2] || 0);
const significative = numericParams[3] == 's'
await this.rollRdDNumeric(msg, carac, diff, significative);
return;
}
}
}
async rollRdDNumeric(msg, carac, diff, significative = false) {
let rollData = {
caracValue: carac,
finalLevel: diff,
showDice: true,
needSignificative: significative,
show: { title: "Table de résolution", points: true }
};
await RdDResolutionTable.rollData(rollData);
msg.content = await RdDResolutionTable.explainRollDataV2(rollData);
ChatUtility.chatWithRollMode(msg, game.user.name);
}
}