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

614 lines
27 KiB
JavaScript
Raw Normal View History

2020-05-22 22:37:02 +02:00
/* Common useful functions shared between objects */
import { TMRUtility } from "./tmr-utility.js";
import { RdDRollTables } from "./rdd-rolltables.js";
2020-06-11 00:29:32 +02:00
2020-05-24 20:19:57 +02:00
const level_category = {
"generale": "-4",
"particuliere": "-8",
"speciale": "-11",
"connaissance": "-11",
"draconic": "-11",
"melee": "-6",
"tir": "-8",
"lancer": "-8"
}
2020-08-13 22:28:56 +02:00
const competenceTroncs = [ ["Esquive", "Dague", "Corps à corps"],
["Epée à 1 main", "Epée à 2 mains", "Hache à 1 main", "Hache à 2 mains", "Lance", "Masse à 1 main", "Masse à 2 mains"] ];
const competence_xp = {
"-11" : [ 5, 10, 15, 25, 35, 45, 55, 70, 85, 100, 115, 135, 155, 175 ],
"-8" : [ 10, 20, 30, 40, 55, 70, 85, 100, 120, 140,160],
"-6" : [ 10, 20, 35, 50, 65, 80, 100, 120, 140],
"-4" : [ 15, 30, 45, 60, 80, 100, 120]
}
2020-11-12 16:35:51 +01:00
2020-08-13 22:28:56 +02:00
// This table starts at 0 -> niveau -10
const competence_xp_par_niveau = [ 5, 5, 5, 10, 10, 10, 10, 15, 15, 15, 15, 20, 20, 20, 20, 30, 30, 40, 40, 60, 60, 100, 100, 100, 100, 100, 100, 100, 100, 100];
2020-05-24 20:19:57 +02:00
const carac_array = [ "taille", "apparence", "constitution", "force", "agilite", "dexterite", "vue", "ouie", "odoratgout", "volonte", "intellect", "empathie", "reve", "chance", "melee", "tir", "lancer", "derobee"];
const difficultesLibres = [0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10];
const ajustementsConditions = [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, +1, +2, +3, +4, +5, +6, +7, +8, +9, +10];
2020-05-27 23:47:49 +02:00
const fatigueMatrix = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, // Dummy filler for the array.
[2, 3, 3, 2, 3, 3, 2, 3, 3, 2, 3, 3 ],
[2, 3, 3, 3, 3, 3, 2, 3, 3, 3, 3, 3 ],
[3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 ],
[3, 3, 3, 3, 3, 4, 3, 3, 3, 3, 3, 4 ],
[3, 3, 4, 3, 3, 4, 3, 3, 4, 3, 3, 4 ],
[3, 3, 4, 3, 4, 4, 3, 3, 4, 3, 4, 4 ],
[3, 4, 4, 3, 4, 4, 3, 4, 4, 3, 4, 4 ],
[3, 4, 4, 4, 4, 4, 3, 4, 4, 4, 4, 4 ],
[4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 ],
[4, 4, 4, 4, 4, 5, 4, 4, 4, 4, 4, 5 ],
[4, 4, 5, 4, 4, 5, 4, 4, 5, 4, 4, 5 ],
[4, 4, 5, 4, 5, 5, 4, 4, 5, 4, 5, 5 ],
[4, 5, 5, 4, 5, 5, 4, 5, 5, 4, 5, 5 ],
[4, 5, 5, 5, 5, 5, 4, 5, 5, 5, 5, 5 ],
[5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 ] ];
const fatigueMalus = [ 0, 0, 0, -1, -1, -1, -2, -3, -4, -5, -6, -7 ]; // Provides the malus for each segment of fatigue
2020-05-28 23:36:09 +02:00
const fatigueLineSize = [ 3, 6, 7, 8, 9, 10, 11, 12];
const fatigueLineMalus = [ 0, -1, -2, -3, -4, -5, -6, -7 ];
2020-05-31 23:06:25 +02:00
const fatigueMarche = { "aise": { "4":1, "6":2, "8":3, "10":4, "12":6 },
"malaise": { "4":2, "6":3, "8":4, "10":6 },
"difficile": { "4":3, "6":4, "8":6 },
"tresdifficile": { "4":4, "6":6 } }
/* Static tables for commands /table */
const table2func = { "queues": {descr: "queues : Tire une queue de Dragon", func: RdDRollTables.getQueue},
"ombre": { descr: "ombre: Tire une Ombre de Dragon", func: RdDRollTables.getOmbre },
"tetehr": {descr: "tetehr: Tire une Tête de Dragon pour Hauts Revants", fund: RdDRollTables.getTeteHR},
"tete" : { descr: "tete: Tire une Tête de Dragon", func: RdDRollTables.getTete},
"souffle": { descr: "souffle: Tire un Souffle de Dragon", func: RdDRollTables.getSouffle},
"tarot" : { descr: "tarot: Tire une carte de Tarot Dracnique", func: RdDRollTables.getTarot} };
2020-07-14 22:19:29 +02:00
2020-11-11 04:21:25 +01:00
const definitionsEncaissement = {
"mortel": [
{ minimum: undefined, maximum: 0, endurance: "0", vie: "0", legeres: 0, graves: 0, critiques: 0 },
{ minimum: 1, maximum: 10, endurance: "1d4", vie: "0", legeres: 0, graves: 0, critiques: 0 },
{ minimum: 11, maximum: 15, endurance: "1d6", vie: "0", legeres: 1, graves: 0, critiques: 0 },
{ minimum: 16, maximum: 19, endurance: "2d6", vie: "2", legeres: 0, graves: 1, critiques: 0 },
{ minimum: 20, maximum: undefined, endurance: "100", vie: "4 + @over20", legeres: 0, graves: 0, critiques: 1 },
],
"non-mortel": [
{ minimum: undefined, maximum: 0, endurance: "0", vie: "0", legeres: 0, graves: 0, critiques: 0 },
{ minimum: 1, maximum: 10, endurance: "1d4", vie: "0", legeres: 0, graves: 0, critiques: 0 },
{ minimum: 11, maximum: 15, endurance: "1d6", vie: "0", legeres: 0, graves: 0, critiques: 0 },
{ minimum: 16, maximum: 19, endurance: "2d6", vie: "0", legeres: 1, graves: 0, critiques: 0 },
{ minimum: 20, maximum: undefined, endurance: "100", vie: "1", legeres: 1, graves: 0, critiques: 0 },
],
"cauchemar": [
{ minimum: undefined, maximum: 0, endurance: "0", vie: "0", legeres: 0, graves: 0, critiques: 0 },
2020-11-11 04:21:25 +01:00
{ minimum: 1, maximum: 10, endurance: "1d4", vie: "0", legeres: 0, graves: 0, critiques: 0 },
{ minimum: 11, maximum: 15, endurance: "1d6", vie: "0", legeres: 0, graves: 0, critiques: 0 },
{ minimum: 16, maximum: 19, endurance: "2d6", vie: "0", legeres: 0, graves: 0, critiques: 0 },
{ minimum: 20, maximum: undefined, endurance: "3d6 + @over20", vie: "0", legeres: 0, graves: 0, critiques: 0 },
]
};
2020-07-06 09:03:21 +02:00
/* -------------------------------------------- */
2020-05-22 22:37:02 +02:00
export class RdDUtility {
2020-05-24 20:19:57 +02:00
/* -------------------------------------------- */
static async preloadHandlebarsTemplates( ) {
const templatePaths = [
//Character Sheets
'systems/foundryvtt-reve-de-dragon/templates/actor-sheet.html',
2020-11-04 16:29:10 +01:00
'systems/foundryvtt-reve-de-dragon/templates/actor-creature-sheet.html',
'systems/foundryvtt-reve-de-dragon/templates/actor-humanoide-sheet.html',
2020-11-14 23:24:01 +01:00
'systems/foundryvtt-reve-de-dragon/templates/actor-entite-sheet.html',
2020-05-24 20:19:57 +02:00
//Items
'systems/foundryvtt-reve-de-dragon/templates/item-competence-sheet.html',
2020-09-20 16:36:39 +02:00
'systems/foundryvtt-reve-de-dragon/templates/item-competencecreature-sheet.html',
2020-06-07 23:16:29 +02:00
'systems/foundryvtt-reve-de-dragon/templates/item-arme-sheet.html',
2020-06-23 23:34:12 +02:00
'systems/foundryvtt-reve-de-dragon/templates/item-armure-sheet.html',
2020-06-25 23:18:14 +02:00
'systems/foundryvtt-reve-de-dragon/templates/item-objet-sheet.html',
'systems/foundryvtt-reve-de-dragon/templates/item-conteneur-sheet.html',
2020-06-26 15:47:44 +02:00
'systems/foundryvtt-reve-de-dragon/templates/item-sort-sheet.html',
2020-06-29 23:21:15 +02:00
'systems/foundryvtt-reve-de-dragon/templates/item-herbe-sheet.html',
'systems/foundryvtt-reve-de-dragon/templates/item-ingredient-sheet.html',
'systems/foundryvtt-reve-de-dragon/templates/item-livre-sheet.html',
'systems/foundryvtt-reve-de-dragon/templates/item-tache-sheet.html',
'systems/foundryvtt-reve-de-dragon/templates/item-potion-sheet.html',
2020-11-10 13:53:51 +01:00
'systems/foundryvtt-reve-de-dragon/templates/item-rencontresTMR-sheet.html',
2020-07-20 12:02:07 +02:00
'systems/foundryvtt-reve-de-dragon/templates/item-queue-sheet.html',
'systems/foundryvtt-reve-de-dragon/templates/item-souffle-sheet.html',
2020-11-09 23:56:25 +01:00
'systems/foundryvtt-reve-de-dragon/templates/item-tarot-sheet.html',
2020-07-20 12:02:07 +02:00
'systems/foundryvtt-reve-de-dragon/templates/item-tete-sheet.html',
'systems/foundryvtt-reve-de-dragon/templates/item-ombre-sheet.html',
2020-05-24 20:19:57 +02:00
'systems/foundryvtt-reve-de-dragon/templates/competence-categorie.html',
'systems/foundryvtt-reve-de-dragon/templates/competence-carac-defaut.html',
'systems/foundryvtt-reve-de-dragon/templates/competence-base.html',
2020-11-09 23:56:25 +01:00
'systems/foundryvtt-reve-de-dragon/templates/enum-aspect-tarot.html',
'systems/foundryvtt-reve-de-dragon/templates/enum-categorie.html',
'systems/foundryvtt-reve-de-dragon/templates/enum-rarete.html',
2020-06-07 23:16:29 +02:00
'systems/foundryvtt-reve-de-dragon/templates/arme-competence.html',
2020-06-26 15:47:44 +02:00
'systems/foundryvtt-reve-de-dragon/templates/sort-draconic.html',
'systems/foundryvtt-reve-de-dragon/templates/sort-tmr.html',
2020-05-24 20:19:57 +02:00
// Dialogs
2020-06-12 22:46:04 +02:00
'systems/foundryvtt-reve-de-dragon/templates/dialog-competence.html',
2020-07-05 21:45:25 +02:00
'systems/foundryvtt-reve-de-dragon/templates/dialog-roll-carac.html',
2020-07-23 22:09:40 +02:00
'systems/foundryvtt-reve-de-dragon/templates/dialog-roll-sort.html',
2020-11-10 13:53:51 +01:00
'systems/foundryvtt-reve-de-dragon/templates/dialog-roll-encaisser.html',
2020-09-20 16:36:39 +02:00
'systems/foundryvtt-reve-de-dragon/templates/dialog-tmr.html',
// Calendrier
2020-11-14 23:24:01 +01:00
'systems/foundryvtt-reve-de-dragon/templates/calendar-template.html',
2020-11-12 14:43:08 +01:00
// Conteneur/item in Actor sheet
'systems/foundryvtt-reve-de-dragon/templates/actor-inventaire-conteneur.html'
2020-05-24 20:19:57 +02:00
];
return loadTemplates(templatePaths);
}
/* -------------------------------------------- */
/** Construit la structure récursive des conteneurs, avec imbrication potentielle
*
*/
2020-11-12 14:43:08 +01:00
static buildConteneur( objet, niveau ) {
if (!niveau) niveau = 1;
let str = Handlebars.partials['systems/foundryvtt-reve-de-dragon/templates/actor-inventaire-conteneur.html']( { item: objet} );
if (objet.type == 'conteneur') {
str = str + "<ol class='item-list'>";
for (let subItem of objet.subItems) {
str = str + this.buildConteneur(subItem, niveau+1);
}
str = str + "</ol>";
}
return new Handlebars.SafeString(str);
}
2020-05-24 20:19:57 +02:00
/* -------------------------------------------- */
static buildResolutionTable( ) {
let tableRes = []
for (var j=0; j<=21; j++) {
let subtab = [];
for (var i=-10; i<=22; i++) {
var m = (i + 10) * 0.5;
var v;
if (i == -9) {
v = Math.floor(j / 2);
} else if (i == -10) {
v = Math.floor(j / 4);
} else {
if (j % 2 == 0) {
var v = Math.ceil(j * m);
} else {
var v = Math.floor(j * m);
}
}
if (v < 1) v = 1;
let specResults
if ( v > 100 )
2020-07-14 22:19:29 +02:00
specResults = { part: Math.ceil(v / 5), epart: 1000, etotal: 1000 };
2020-05-24 20:19:57 +02:00
else
specResults = specialResults[Math.ceil(v / 5 )];
let tabIndex = i+10;
2020-07-14 22:19:29 +02:00
subtab[tabIndex] = { niveau: i, score: v, part: specResults.part, epart: specResults.epart, etotal: specResults.etotal }
2020-05-24 20:19:57 +02:00
}
tableRes[j] = subtab;
}
return tableRes;
}
/* -------------------------------------------- */
static getLevelCategory( )
{
return level_category;
}
static getCaracArray()
{
return carac_array;
}
static getDifficultesLibres()
2020-05-24 20:19:57 +02:00
{
return difficultesLibres;
}
static getAjustementsConditions()
{
return ajustementsConditions;
2020-05-24 20:19:57 +02:00
}
2020-08-13 22:28:56 +02:00
/* -------------------------------------------- */
static isTronc( compName )
{
for (let troncList of competenceTroncs) {
for (let troncName of troncList) {
if ( troncName == compName)
return troncList;
}
}
return false;
}
/* -------------------------------------------- */
static computeCompetenceXPCost( competence )
{
let minLevel = competence.data.base;
if ( minLevel == competence.data.niveau) return 0;
if ( competence.data.niveau < -10) return 0;
let xp = 0;
for (let i=minLevel+1; i<=competence.data.niveau; i++) {
xp += competence_xp_par_niveau[i+10];
//console.log(i, i+10, competence_xp_par_niveau[i+10]);
}
return xp;
}
2020-05-24 20:19:57 +02:00
/* -------------------------------------------- */
2020-08-14 22:24:35 +02:00
static computeCompetenceTroncXP( competenceList )
{
let xp = 0;
for (let troncList of competenceTroncs) {
let minNiveau = 15;
for (let troncName of troncList) {
let comp = RdDUtility.findCompetence( competenceList, troncName);
minNiveau = (comp.data.niveau < minNiveau) ? comp.data.niveau : minNiveau;
}
if ( minNiveau > 0 ) minNiveau = 0; // Clamp à 0, pour le tronc commun
let minNiveauXP = competence_xp_par_niveau[minNiveau+10];
xp += minNiveauXP;
for (let troncName of troncList) {
let comp = RdDUtility.findCompetence( competenceList, troncName);
xp += competence_xp_par_niveau[comp.data.niveau+10] - minNiveauXP;
}
}
return xp;
}
/* -------------------------------------------- */
2020-05-24 20:19:57 +02:00
static computeCarac( data)
{
let fmax = parseInt(data.carac.taille.value) + 4;
if ( data.carac.force.value > fmax )
data.carac.force.value = fmax;
data.carac.derobee.value = Math.floor(parseInt(((21 - data.carac.taille.value)) + parseInt(data.carac.agilite.value)) / 2);
let bonusDomKey = Math.floor( (parseInt(data.carac.force.value) + parseInt(data.carac.taille.value)) / 2);
data.attributs.plusdom.value = 2
if (bonusDomKey < 8)
data.attributs.plusdom.value = -1;
else if (bonusDomKey < 12)
data.attributs.plusdom.value = 0;
else if (bonusDomKey < 14)
data.attributs.plusdom.value = 1;
2020-05-27 21:57:00 +02:00
2020-05-24 20:19:57 +02:00
data.attributs.encombrement.value = (parseInt(data.carac.force.value) + parseInt(data.carac.taille.value)) / 2;
data.carac.melee.value = Math.floor( (parseInt(data.carac.force.value) + parseInt(data.carac.agilite.value)) / 2);
data.carac.tir.value = Math.floor( (parseInt(data.carac.vue.value) + parseInt(data.carac.dexterite.value)) / 2);
data.carac.lancer.value = Math.floor( (parseInt(data.carac.tir.value) + parseInt(data.carac.force.value)) / 2);
2020-09-05 22:56:33 +02:00
data.sante.vie.max = Math.ceil( (parseInt(data.carac.taille.value) + parseInt(data.carac.constitution.value)) /2 );
2020-06-07 23:16:29 +02:00
if ( data.sante.vie.value > data.sante.vie.max)
data.sante.vie.value = data.sante.vie.max;
2020-05-24 20:19:57 +02:00
let endurance = Math.max( parseInt(data.carac.taille.value) + parseInt(data.carac.constitution.value), parseInt(data.sante.vie.max) + parseInt(data.carac.volonte.value) );
data.sante.endurance.max = endurance;
2020-06-07 23:16:29 +02:00
if ( data.sante.endurance.value > endurance)
data.sante.endurance.value = endurance;
2020-05-29 00:43:16 +02:00
data.sante.fatigue.max = endurance*2;
2020-06-07 23:16:29 +02:00
if ( data.sante.fatigue.value > data.sante.fatigue.max )
data.sante.fatigue.value = data.sante.fatigue.max;
2020-05-24 20:19:57 +02:00
data.attributs.sconst.value = 5; // Max !
if ( data.carac.constitution.value < 9 )
data.attributs.sconst.value = 2;
else if (data.carac.constitution.value < 12 )
data.attributs.sconst.value = 3;
else if (data.carac.constitution.value < 15 )
data.attributs.sconst.value = 4;
data.attributs.sust.value = 4; // Max !
2020-05-27 21:57:00 +02:00
if ( data.carac.taille.value < 10 )
data.attributs.sust.value = 2;
else if (data.carac.taille.value < 14 )
data.attributs.sust.value = 3;
2020-05-29 00:43:16 +02:00
//Compteurs
2020-06-07 23:16:29 +02:00
//data.compteurs.reve.value = data.carac.reve.value;
2020-07-17 22:04:35 +02:00
data.reve.reve.max = data.carac.reve.value;
2020-06-07 23:16:29 +02:00
//data.compteurs.chance.value = data.carac.chance.value;
2020-07-17 22:04:35 +02:00
data.compteurs.chance.max = data.carac.chance.value;
2020-05-24 20:19:57 +02:00
}
2020-05-27 23:47:49 +02:00
/* -------------------------------------------- */
2020-05-29 00:43:16 +02:00
// Build the nice (?) html table used to manage fatigue.
2020-08-29 22:52:41 +02:00
// max should Mbe the endurance max value
2020-05-29 00:43:16 +02:00
static makeHTMLfatigueMatrix( value, max )
2020-05-27 23:47:49 +02:00
{
max = (max < 16) ? 16 : max;
max = (max > 30) ? 30 : max;
2020-05-29 00:43:16 +02:00
value = (value > max*2) ? max*2 : value;
2020-05-28 23:36:09 +02:00
value = (value < 0) ? 0 : value;
2020-05-27 23:47:49 +02:00
let fatigueTab = fatigueMatrix[max];
2020-05-29 00:43:16 +02:00
2020-05-27 23:47:49 +02:00
let table = $("<table/>").addClass('table-fatigue');
let segmentIdx = 0;
2020-05-28 23:36:09 +02:00
let fatigueCount = 0;
2020-05-27 23:47:49 +02:00
for (var line=0; line < fatigueLineSize.length; line++) {
let row = $("<tr/>");
let segmentsPerLine = fatigueLineSize[line];
2020-05-28 23:36:09 +02:00
row.append("<td class='fatigue-malus'>" + fatigueLineMalus[line] + "</td>");
2020-05-27 23:47:49 +02:00
while (segmentIdx < segmentsPerLine) {
let freeSize = fatigueTab[segmentIdx];
for (let col=0; col <5; col++) {
2020-05-28 23:36:09 +02:00
if ( col < freeSize ) {
if (fatigueCount < value )
row.append("<td class='fatigue-used'/>");
else
row.append("<td class='fatigue-free'/>");
fatigueCount++;
} else {
2020-05-27 23:47:49 +02:00
row.append("<td class='fatigue-none'/>");
2020-05-28 23:36:09 +02:00
}
2020-05-27 23:47:49 +02:00
}
row.append("<td class='fatigue-separator'/>");
segmentIdx = segmentIdx + 1;
}
table.append(row);
}
2020-05-28 23:36:09 +02:00
//console.log("fatigue", table);
2020-05-27 23:47:49 +02:00
return table;
}
2020-05-31 23:06:25 +02:00
/* -------------------------------------------- */
static getLocalisation( )
{
2020-07-25 10:29:28 +02:00
let result = new Roll("d20").roll().total;
2020-06-07 23:16:29 +02:00
let txt = ""
if ( result <= 3 ) txt = "Jambe, genou, pied, jarret";
else if ( result <= 7 ) txt = "Hanche, cuisse, fesse";
else if ( result <= 9 ) txt = "Ventre, reins";
else if ( result <= 12 ) txt = "Poitrine, dos";
else if ( result <= 14 ) txt = "Avant-bras, main, coude";
else if ( result <= 18 ) txt = "Epaule, bras, omoplate";
else if ( result == 19) txt = "Tête autre";
else if ( result == 20) txt = "Tête visage";
return { result: result, label: txt };
2020-05-31 23:06:25 +02:00
}
2020-11-11 04:21:25 +01:00
2020-11-11 10:38:27 +01:00
/* -------------------------------------------- */
2020-11-11 04:21:25 +01:00
static computeBlessuresSante( degats, mortalite="mortel" ) {
let encaissement = RdDUtility.selectEncaissement(degats, mortalite)
let over20 = degats > 20 ? degats - 20 : 0
encaissement.endurance = - RdDUtility._evaluatePerte(encaissement.endurance, over20)
encaissement.vie = - RdDUtility._evaluatePerte(encaissement.vie, degats, over20)
return encaissement;
}
2020-11-11 10:38:27 +01:00
/* -------------------------------------------- */
2020-11-11 04:21:25 +01:00
static selectEncaissement( degats, mortalite ) {
const table = definitionsEncaissement[mortalite] === undefined ? definitionsEncaissement["mortel"] : definitionsEncaissement[mortalite];
for (let encaissement of table) {
if ((encaissement.minimum === undefined || encaissement.minimum <= degats)
&& (encaissement.maximum === undefined || degats <= encaissement.maximum)) {
return duplicate(encaissement);
}
2020-06-07 23:16:29 +02:00
}
2020-11-11 04:21:25 +01:00
return duplicate(table[0]);
}
2020-11-11 10:38:27 +01:00
/* -------------------------------------------- */
2020-11-11 04:21:25 +01:00
static _evaluatePerte(formula, over20) {
console.log("_evaluatePerte", formula, over20 )
let perte = new Roll(formula, { over20:over20})
perte.evaluate()
return perte.total
2020-06-07 23:16:29 +02:00
}
2020-06-12 22:46:04 +02:00
/* -------------------------------------------- */
2020-05-29 00:43:16 +02:00
static currentFatigueMalus( value, max)
{
max = (max < 16) ? 16 : max;
max = (max > 30) ? 30 : max;
value = (value > max*2) ? max*2 : value;
value = (value < 0) ? 0 : value;
let fatigueTab = fatigueMatrix[max];
let fatigueRem = value;
for (let idx=0; idx<fatigueTab.length; idx++) {
fatigueRem -= fatigueTab[idx];
if ( fatigueRem <= 0) {
return fatigueMalus[idx];
}
}
return -7; // This is the max !
}
2020-08-29 22:52:41 +02:00
/* -------------------------------------------- */
2020-05-22 22:37:02 +02:00
static findCompetence(compList, compName)
{
2020-11-12 23:50:37 +01:00
compName = compName.toLowerCase();
return compList.find(item => item.name.toLowerCase() == compName && (item.type =="competence" || item.type == "competencecreature"))
2020-05-22 22:37:02 +02:00
}
2020-06-12 11:47:41 +02:00
/* -------------------------------------------- */
static getArmeCategory( arme )
{
let compname = arme.data.competence.toLowerCase();
if ( compname.match("hache") ) return "hache";
if ( compname.match("hast") ) return "hast";
if ( compname.match("lance") ) return "lance";
if ( compname.match("bouclier") ) return "bouclier";
if ( compname.match("masse") ) return "masse";
if ( compname.match("fléau") ) return "fleau";
if ( compname.match("epée") ) {
let armename = arme.name.toLowerCase();
if (armename == "dague" || armename.match("gnome") )
return "epee_courte";
}
return "epee_longue";
}
2020-06-10 08:23:58 +02:00
/* -------------------------------------------- */
static isArmeMelee( compName)
{
let comp = compName.toLowerCase();
2020-11-12 23:50:37 +01:00
if (comp.match("epée") || comp.match("épée") || comp.match("hache") || comp.match("fleau") || comp.match("mass") || comp.match("lance") || comp.match("hast") || comp == "dague" || comp=="bouclier")
2020-06-10 08:23:58 +02:00
return true;
return false;
}
/* -------------------------------------------- */
static buildDefenseChatCard( attacker, target, rollData )
{
2020-11-12 23:50:37 +01:00
console.log("Attacker.defense", attacker, target, attacker.data._id, rollData.competence.data.categorie );
2020-07-27 18:58:10 +02:00
let myTarget = target.actor;
2020-06-10 08:23:58 +02:00
let defenseMsg = { title: "Défense en combat",
2020-07-27 18:58:10 +02:00
content: "<strong>"+myTarget.name+"</strong> doit se défendre : <br><span class='chat-card-button-area'>" +
"<a class='chat-card-button' id='encaisser-button' data-attackerid='"+attacker.data._id + "' data-defenderid='" + myTarget.data._id + "'>Encaisser !</a></span>",
2020-07-28 09:20:01 +02:00
whisper: ChatMessage.getWhisperRecipients( myTarget.name ),
defenderid: myTarget.data._id,
rollMode: true
};
2020-06-10 08:23:58 +02:00
2020-11-12 23:50:37 +01:00
if ( rollData.competence.data.categorie == 'melee' || rollData.competence.data.categorie == 'creature') { // Melee attack or creature
2020-06-10 08:23:58 +02:00
let defenderArmes = [];
2020-07-27 18:58:10 +02:00
for (const arme of myTarget.data.items) {
2020-06-11 00:29:32 +02:00
if (arme.type == "arme" && this.isArmeMelee(arme.data.competence)) {
2020-06-10 08:23:58 +02:00
defenderArmes.push( arme );
2020-07-27 18:58:10 +02:00
defenseMsg.content += "<br><a class='chat-card-button' id='parer-button' data-attackerid='"+attacker.data._id + "' data-defenderid='" + myTarget.data._id + "' data-armeid='"+arme._id+"'>Parer avec " + arme.name + "</a></span>";
2020-06-10 08:23:58 +02:00
}
}
2020-07-27 18:58:10 +02:00
defenseMsg.content += "<br><a class='chat-card-button' id='esquiver-button' data-attackerid='"+attacker.data._id + "' data-defenderid='" + myTarget.data._id + "'>Esquiver</a></span>";
2020-06-10 08:23:58 +02:00
}
2020-06-17 20:31:43 +02:00
if ( rollData.competence.data.categorie == "tir" ) {
2020-07-27 18:58:10 +02:00
for (const arme of myTarget.data.items) { // Bouclier for parry
2020-06-17 20:31:43 +02:00
if ( arme.type == "arme" && arme.name.toLowerCase.match("bouclier") ) {
defenderArmes.push( arme );
2020-07-27 18:58:10 +02:00
defenseMsg.content += "<br><a class='chat-card-button' id='parer-button' data-attackerid='"+attacker.data._id + "' data-defenderid='" + myTarget.data._id + "' data-armeid='"+arme._id+"'>Parer avec " + arme.name + "</a></span>";
2020-06-17 20:31:43 +02:00
}
}
}
if ( rollData.competence.data.categorie == "lancer" ) {
2020-07-27 18:58:10 +02:00
for (const arme of myTarget.data.items) { // Bouclier for parry Dodge/Esquive
2020-06-17 20:31:43 +02:00
if ( arme.type == "arme" && arme.name.toLowerCase.match("bouclier") ) {
defenderArmes.push( arme );
2020-07-27 18:58:10 +02:00
defenseMsg.content += "<br><a class='chat-card-button' id='parer-button' data-attackerid='"+attacker.data._id + "' data-defenderid='" + myTarget.data._id + "' data-armeid='"+arme._id+"'>Parer avec " + arme.name + "</a></span>";
2020-06-17 20:31:43 +02:00
}
}
2020-07-27 18:58:10 +02:00
defenseMsg.content += "<br><a class='chat-card-button' id='esquiver-button' data-attackerid='"+attacker.data._id + "' data-defenderid='" + myTarget.data._id + "'>Esquiver</a></span>";
2020-06-17 20:31:43 +02:00
}
2020-07-28 09:20:01 +02:00
2020-11-12 23:50:37 +01:00
defenseMsg.toSocket = true; // True per default for all players
if (game.user.isGM) { // In GM case, only if target is a player
defenseMsg.toSocket = myTarget.hasPlayerOwner;
}
2020-07-28 09:20:01 +02:00
2020-06-10 08:23:58 +02:00
return defenseMsg;
}
2020-06-17 20:31:43 +02:00
/* -------------------------------------------- */
2020-06-22 10:18:03 +02:00
static performSocketMesssage( sockmsg )
2020-06-17 20:31:43 +02:00
{
2020-06-22 10:18:03 +02:00
console.log(">>>>> MSG RECV", sockmsg);
if ( sockmsg.msg == "msg_encaisser" ) {
2020-11-12 23:50:37 +01:00
if ( game.user.isGM ) { // Seul le GM effectue l'encaissement sur la fiche
let rollData = game.system.rdd.rollDataHandler[sockmsg.data.attackerid]; // Retrieve the rolldata from the store
2020-07-28 09:20:01 +02:00
let defenderActor = game.actors.get( sockmsg.data.defenderid );
2020-11-12 23:50:37 +01:00
defenderActor.encaisserDommages( rollData );
2020-06-22 10:18:03 +02:00
}
2020-07-28 09:20:01 +02:00
} else if (sockmsg.msg == "msg_defense" ) {
let defenderActor = game.actors.get( sockmsg.data.defenderid );
2020-11-12 18:13:25 +01:00
if ( defenderActor ) {
if ( (game.user.isGM && !defenderActor.hasPlayerOwner) || (defenderActor.hasPlayerOwner && (game.user.character.id == defenderActor.id) ) ) {
console.log("User is pushing message...", game.user.name);
2020-11-12 23:50:37 +01:00
defenderActor.currentRollData = duplicate(sockmsg.data.rollData);
2020-11-12 18:13:25 +01:00
sockmsg.data.whisper = [ game.user ];
sockmsg.data.blind = true;
sockmsg.data.rollMode = "blindroll";
ChatMessage.create( sockmsg.data );
}
2020-07-28 09:20:01 +02:00
}
2020-06-22 10:18:03 +02:00
}
2020-06-17 20:31:43 +02:00
}
2020-06-07 23:16:29 +02:00
/* -------------------------------------------- */
static async chatListeners( html )
{
html.on("click", '#encaisser-button', event => {
event.preventDefault();
2020-11-12 23:50:37 +01:00
let attackerid = event.currentTarget.attributes['data-attackerid'].value;
let defenderid = event.currentTarget.attributes['data-defenderid'].value;
2020-06-22 10:18:03 +02:00
if ( game.user.isGM ) { // Current user is the GM -> direct access
2020-11-12 23:50:37 +01:00
let rollData = game.system.rdd.rollDataHandler[attackerid];
rollData.attackerid = attackerid;
rollData.defenderid = defenderid;
let defenderActor = game.actors.get(defenderid );
2020-06-22 10:18:03 +02:00
defenderActor.encaisserDommages( rollData );
} else { // Emit message for GM
game.socket.emit("system.foundryvtt-reve-de-dragon", {
msg: "msg_encaisser",
2020-11-12 23:50:37 +01:00
data: { attackerid: attackerid, defenderid: defenderid }
2020-06-22 10:18:03 +02:00
} );
}
2020-07-27 18:58:10 +02:00
});
2020-06-10 08:23:58 +02:00
html.on("click", '#parer-button', event => {
event.preventDefault();
let defenderActor = game.actors.get(event.currentTarget.attributes['data-defenderid'].value );
let armeId = event.currentTarget.attributes['data-armeid'].value;
2020-11-12 23:50:37 +01:00
let rollData = defenderActor.currentRollData;
2020-07-27 18:58:10 +02:00
defenderActor.parerAttaque( rollData, armeId );
});
html.on("click", '#esquiver-button', event => {
event.preventDefault();
let defenderActor = game.actors.get(event.currentTarget.attributes['data-defenderid'].value );
2020-11-12 23:50:37 +01:00
let rollData = defenderActor.currentRollData;
2020-11-14 21:53:46 +01:00
//console.log("Esquive !", rollData, defenderActor);
2020-07-27 18:58:10 +02:00
defenderActor.esquiverAttaque( rollData );
2020-06-10 08:23:58 +02:00
});
2020-11-14 21:22:29 +01:00
html.on("click", '#particuliere-attaque', event => {
event.preventDefault();
let attackerActor = game.actors.get(event.currentTarget.attributes['data-attackerid'].value );
let rollData = attackerActor.currentRollData;
rollData.particuliereAttaque = game.actors.get(event.currentTarget.attributes['data-mode'].value );
2020-11-14 21:53:46 +01:00
//console.log("Particulère !", rollData);
2020-11-14 21:22:29 +01:00
attackerActor.continueRoll( rollData );
});
2020-06-07 23:16:29 +02:00
}
/* -------------------------------------------- */
/* Display help for /table */
static displayHelpTable( msg )
{
msg.content = "";
for (let [name, tableData] of Object.entries(table2func)) {
msg.content += "<br>" + tableData.descr;
}
ChatMessage.create( msg );
}
/* -------------------------------------------- */
/* Manage chat commands */
static processChatCommand( commands, content, msg ) {
// Setup new message's visibility
let rollMode = game.settings.get("core", "rollMode");
2020-11-03 09:14:14 +01:00
if (["gmroll", "blindroll"].includes(rollMode)) msg["whisper"] = ChatMessage.getWhisperRecipients("GM");
if (rollMode === "blindroll") msg["blind"] = true;
msg["type"] = 0;
let command = commands[0];
// Roll on a table
if (command === "/table") {
if ( commands[1] ) {
let tableName = commands[1].toLowerCase();
2020-11-03 09:14:14 +01:00
table2func[tableName].func();
} else {
this.displayHelpTable( msg );
}
return false
} else if (command === "/tmrr") {
TMRUtility.getRencontre(commands[1], commands[2] )
return false
2020-09-13 23:08:52 +02:00
} else if (command === "/tmra") {
TMRUtility.getTMRAleatoire( )
return false
}
return true;
}
2020-05-22 22:37:02 +02:00
}