2020-12-29 00:11:58 +01:00
/* -------------------------------------------- */
2020-12-31 11:53:41 +01:00
import { DeDraconique } from "./de-draconique.js" ;
2021-01-21 00:05:22 +01:00
import { RdDItemCompetence } from "./item-competence.js" ;
2020-12-29 00:11:58 +01:00
import { Misc } from "./misc.js" ;
2020-12-31 11:53:41 +01:00
import { RdDDice } from "./rdd-dice.js" ;
2020-12-29 00:11:58 +01:00
import { RdDResolutionTable } from "./rdd-resolution-table.js" ;
2021-01-02 14:10:43 +01:00
import { RdDRollResolutionTable } from "./rdd-roll-resolution-table.js" ;
2020-12-29 00:11:58 +01:00
import { RdDRollTables } from "./rdd-rolltables.js" ;
import { RdDUtility } from "./rdd-utility.js" ;
2021-01-29 15:13:59 +01:00
import { TMRRencontres } from "./tmr-rencontres.js" ;
2020-12-29 00:11:58 +01:00
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 ( ) ;
2020-12-31 11:53:41 +01:00
rddCommands . registerCommand ( { path : [ "/aide" ] , func : ( content , msg , params ) => rddCommands . help ( msg ) , descr : "Affiche l'aide pour toutes les commandes" } ) ;
rddCommands . registerCommand ( { path : [ "/help" ] , func : ( content , msg , params ) => rddCommands . help ( msg ) , descr : "Affiche l'aide pour toutes les commandes" } ) ;
2021-02-05 01:10:54 +01:00
rddCommands . registerCommand ( { path : [ "/table" , "queues" ] , func : ( content , msg , params ) => RdDRollTables . getQueue ( true ) , descr : "Tire une Queue de Dragon" } ) ;
rddCommands . registerCommand ( { path : [ "/table" , "ombre" ] , func : ( content , msg , params ) => RdDRollTables . getOmbre ( true ) , descr : "Tire une Ombre de Dragon" } ) ;
rddCommands . registerCommand ( { path : [ "/table" , "tetehr" ] , func : ( content , msg , params ) => RdDRollTables . getTeteHR ( true ) , descr : "Tire une Tête de Dragon pour Hauts Revants" } ) ;
rddCommands . registerCommand ( { path : [ "/table" , "tete" ] , func : ( content , msg , params ) => RdDRollTables . getTete ( true ) , descr : "Tire une Tête de Dragon" } ) ;
rddCommands . registerCommand ( { path : [ "/table" , "souffle" ] , func : ( content , msg , params ) => RdDRollTables . getSouffle ( true ) , descr : " Tire un Souffle de Dragon" } ) ;
rddCommands . registerCommand ( { path : [ "/table" , "tarot" ] , func : ( content , msg , params ) => RdDRollTables . getTarot ( true ) , descr : "Tire une carte du Tarot Draconique" } ) ;
2020-12-31 11:53:41 +01:00
2021-01-08 03:04:33 +01:00
rddCommands . registerCommand ( { path : [ "/tmra" ] , func : ( content , msg , params ) => TMRUtility . getTMRAleatoire ( ) , descr : "Tire une case aléatoire des Terres médianes" } ) ;
2020-12-29 00:11:58 +01:00
rddCommands . registerCommand ( {
2021-01-23 00:29:03 +01:00
path : [ "/tmrr" ] , func : ( content , msg , params ) => rddCommands . getRencontreTMR ( params ) ,
descr : `
Exemple : < strong > / t m r r f o r e t < / s t r o n g > l a n c e u n d 1 0 0 e t d é t e r m i n e l a r e n c o n t r e c o r r e s p o n d a n t e e n ' f o r ê t '
Exemple : < strong > / t m r r f o r ê t 4 7 < / s t r o n g > d é t e r m i n e l a r e n c o n t r e e n ' f o r ê t ' p o u r u n j e t d e d é d e 4 7
`
2021-01-21 00:05:22 +01:00
} ) ;
rddCommands . registerCommand ( {
path : [ "/xp" , "comp" ] , func : ( content , msg , params ) => rddCommands . getCoutXpComp ( msg , params ) ,
descr : ` Détermine le coût d'expérience pour augmenter une compétence. Exemples:
< br > / x p c o m p - 6 1 : p o u r p a s s e r d e - 6 à + 1
< br > / x p c o m p + 4 : p o u r a t t e i n d r e l e n i v e a u 4 ( d e p u i s + 3 ) `
} ) ;
rddCommands . registerCommand ( {
path : [ "/xp" , "carac" ] , func : ( content , msg , params ) => rddCommands . getCoutXpCarac ( msg , params ) ,
descr : ` Détermine le coût d'expérience pour augmenter une caractéristique. Exemples:
< br > / x p c a r a c 1 5 : c o û t p o u r a t t e i n d r e 1 5 ( d e p u i s 1 4 ) `
2020-12-31 11:53:41 +01:00
} ) ;
rddCommands . registerCommand ( {
path : [ "/rdd" ] , func : ( content , msg , params ) => rddCommands . rollRdd ( msg , params ) ,
2021-01-08 03:04:33 +01:00
descr : ` Effectue un jet de dés dans la table de résolution. Exemples:
2020-12-29 00:11:58 +01:00
< br > < strong > / r d d < / s t r o n g > o u v r e l a t a b l e d e r é s o l u t i o n
< br > < strong > / r d d 1 0 3 < / s t r o n g > e f f e c t u e u n j e t 1 0 à + 3
< br > < strong > / r d d 1 0 + 2 < / s t r o n g > e f f e c t u e u n j e t 1 0 à + 2
< br > < strong > / r d d 1 5 - 2 < / s t r o n g > e f f e c t u e u n j e t 1 5 à - 2
< br > < strong > / r d d 1 5 0 s < / s t r o n g > e f f e c t u e u n j e t 1 5 à 0 , a v e c s i g n i f i c a t i v e r e q u i s e
2020-12-31 11:53:41 +01:00
`
2020-12-29 00:11:58 +01:00
} ) ;
2020-12-31 11:53:41 +01:00
rddCommands . registerCommand ( { path : [ "/ddr" ] , func : ( content , msg , params ) => rddCommands . rollDeDraconique ( msg ) , descr : "Lance un Dé Draconique" } ) ;
2020-12-29 00:11:58 +01:00
2020-12-31 11:53:41 +01:00
rddCommands . registerCommand ( {
path : [ "/payer" ] , func : ( content , msg , params ) => RdDUtility . afficherDemandePayer ( params [ 0 ] , params [ 1 ] ) ,
descr : ` Permet de payer un montant. Exemples:
2020-12-29 00:11:58 +01:00
< br > < strong > / p a y e r 5 s 1 0 d < / s t r o n g > p e r m e t d ' e n v o y e r u n m e s s a g e p o u r p a y e r 5 s o l s e t 1 0 d e n i e r s
< br > < strong > / p a y e r 1 0 d < / s t r o n g > p e r m e t d ' e n v o y e r u n m e s s a g e p o u r p a y e r 1 0 d e n i e r s
2020-12-31 11:53:41 +01:00
`
} ) ;
2021-01-16 18:54:07 +01:00
rddCommands . registerCommand ( {
path : [ "/astro" ] , func : ( content , msg , params ) => RdDUtility . afficherHeuresChanceMalchance ( params [ 0 ] ) ,
descr : ` Affiche les heures de chance et de malchance selon l'heure de naissance donnée en argument. Exemples:
< br > < strong > / a s t r o L y r e < / s t r o n g >
`
} ) ;
2020-12-29 00:11:58 +01:00
game . system . rdd . commands = rddCommands ;
}
}
constructor ( ) {
this . commandsTable = { } ;
}
registerCommand ( command ) {
2020-12-31 11:53:41 +01:00
this . _addCommand ( this . commandsTable , command . path , '' , command ) ;
2020-12-29 00:11:58 +01:00
}
2020-12-31 11:53:41 +01:00
_addCommand ( targetTable , path , fullPath , command ) {
2020-12-29 00:11:58 +01:00
if ( ! this . _validateCommand ( targetTable , path , command ) ) {
return ;
}
const term = path [ 0 ] ;
2021-01-09 19:33:19 +01:00
fullPath = fullPath + term + ' '
2020-12-29 00:11:58 +01:00
if ( path . length == 1 ) {
2020-12-31 11:53:41 +01:00
command . descr = ` <strong> ${ fullPath } </strong>: ${ command . descr } ` ;
2020-12-29 00:11:58 +01:00
targetTable [ term ] = command ;
}
else {
if ( ! targetTable [ term ] ) {
targetTable [ term ] = { subTable : { } } ;
}
2020-12-31 11:53:41 +01:00
this . _addCommand ( targetTable [ term ] . subTable , path . slice ( 1 ) , fullPath , command )
2020-12-29 00:11:58 +01:00
}
}
_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 ) {
2021-01-21 00:05:22 +01:00
const result = command . func ( content , msg , params ) ;
if ( result == false ) {
RdDCommands . _chatAnswer ( msg , command . descr ) ;
2020-12-29 00:11:58 +01:00
}
return true ;
}
return false ;
}
/* -------------------------------------------- */
help ( msg , table = undefined ) {
let list = [ ]
2020-12-31 11:53:41 +01:00
this . _buildSubTableHelp ( list , table || this . commandsTable ) ;
const messageAide = list . reduce ( ( a , b ) => a + '</li><li class="list-item">' + b ) ;
2021-01-21 00:05:22 +01:00
RdDCommands . _chatAnswer ( msg , ` Commandes disponibles<ul class="alterne-list"><li class="list-item"> ${ messageAide } </li></ul> ` ) ;
}
/* -------------------------------------------- */
static _chatAnswer ( msg , content ) {
2020-12-29 00:11:58 +01:00
msg . whisper = [ game . user . _id ] ;
2021-01-21 00:05:22 +01:00
msg . content = content ;
2020-12-29 00:11:58 +01:00
ChatMessage . create ( msg ) ;
}
/* -------------------------------------------- */
2020-12-31 11:53:41 +01:00
_buildSubTableHelp ( list , table ) {
2020-12-29 00:11:58 +01:00
for ( let [ name , command ] of Object . entries ( table ) ) {
if ( command ) {
if ( command . subTable ) {
2020-12-31 11:53:41 +01:00
this . _buildSubTableHelp ( list , command . subTable ) ;
2020-12-29 00:11:58 +01:00
} else {
2020-12-31 11:53:41 +01:00
list . push ( command . descr ) ;
2020-12-29 00:11:58 +01:00
}
}
}
2020-12-31 11:53:41 +01:00
return list . sort ( ) ;
2020-12-29 00:11:58 +01:00
}
2021-01-21 00:05:22 +01:00
/* -------------------------------------------- */
2021-01-23 00:29:03 +01:00
async getRencontreTMR ( params ) {
if ( params . length == 1 || params . length == 2 ) {
2021-01-29 15:13:59 +01:00
return TMRRencontres . rollRencontre ( params [ 0 ] , params [ 1 ] )
2020-12-29 00:11:58 +01:00
}
else {
return false ;
}
}
2021-01-21 00:05:22 +01:00
/* -------------------------------------------- */
2020-12-29 00:11:58 +01:00
async rollRdd ( msg , params ) {
if ( params . length == 0 ) {
2021-01-02 14:10:43 +01:00
RdDRollResolutionTable . open ( ) ;
2020-12-29 00:11:58 +01:00
}
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 ;
}
}
}
2021-01-21 00:05:22 +01:00
/* -------------------------------------------- */
2020-12-29 00:11:58 +01:00
async rollRdDNumeric ( msg , carac , diff , significative = false ) {
let rollData = {
caracValue : carac ,
finalLevel : diff ,
showDice : true ,
2021-01-22 23:18:19 +01:00
diviseurSignificative : significative ? 2 : 1 ,
2021-01-02 04:28:43 +01:00
show : { title : "Table de résolution" }
2020-12-29 00:11:58 +01:00
} ;
await RdDResolutionTable . rollData ( rollData ) ;
2021-01-21 00:05:22 +01:00
RdDCommands . _chatAnswer ( msg , await RdDResolutionTable . buildRollDataHtml ( rollData ) ) ;
2020-12-29 00:11:58 +01:00
}
2020-12-31 11:53:41 +01:00
2021-01-21 00:05:22 +01:00
/* -------------------------------------------- */
2020-12-31 11:53:41 +01:00
async rollDeDraconique ( msg ) {
let ddr = new DeDraconique ( ) . evaluate ( ) ;
2021-01-22 01:59:01 +01:00
ddr . showDice = true ;
2021-01-23 00:29:03 +01:00
await RdDDice . showDiceSoNice ( ddr ) ;
2021-01-21 00:05:22 +01:00
RdDCommands . _chatAnswer ( msg , ` Lancer d'un Dé draconique: ${ ddr . total } ` ) ;
2020-12-31 11:53:41 +01:00
}
2021-01-21 00:05:22 +01:00
/* -------------------------------------------- */
getCoutXpComp ( msg , params ) {
if ( params && ( params . length == 1 || params . length == 2 ) ) {
let to = params . length == 1 ? Number ( params [ 0 ] ) : Number ( params [ 1 ] ) ;
let from = params . length == 1 ? to - 1 : Number ( params [ 0 ] ) ;
RdDCommands . _chatAnswer ( msg , ` Coût pour passer une compétence de ${ from } à ${ to } : ${ RdDItemCompetence . getDeltaXp ( from , to ) } ` ) ;
}
else {
return false ;
}
}
/* -------------------------------------------- */
getCoutXpCarac ( msg , params ) {
if ( params && params . length == 1 ) {
let to = Number ( params [ 0 ] ) ;
2021-01-29 12:08:02 +01:00
RdDCommands . _chatAnswer ( msg , ` Coût pour passer une caractéristique de ${ to - 1 } à ${ to } : ${ RdDUtility . getCaracXp ( to ) } ` ) ;
2021-01-21 00:05:22 +01:00
}
else {
return false ;
}
}
2020-12-29 00:11:58 +01:00
}