2021-01-29 15:13:59 +01:00
|
|
|
import { Grammar } from "./grammar.js";
|
2022-11-07 00:04:43 +01:00
|
|
|
import { Misc } from "./misc.js";
|
2021-05-11 21:45:43 +02:00
|
|
|
import { RdDDice } from "./rdd-dice.js";
|
2022-11-26 03:13:45 +01:00
|
|
|
import { SystemCompendiums, SystemCompendiumTable } from "./settings/system-compendiums.js";
|
2021-01-29 15:13:59 +01:00
|
|
|
import { TMRUtility } from "./tmr-utility.js";
|
|
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
export class TMRRencontres {
|
|
|
|
|
2022-11-26 03:13:45 +01:00
|
|
|
static init() {
|
|
|
|
const tmrRencontre = new TMRRencontres();
|
2022-11-28 16:00:49 +01:00
|
|
|
game.system.rdd.rencontresTMR = tmrRencontre;
|
2022-11-26 03:13:45 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(){
|
|
|
|
this.table = new SystemCompendiumTable('rencontres', 'Item', 'rencontre', Misc.ascending(it => it.system.ordreTri));
|
|
|
|
}
|
|
|
|
|
2021-01-29 15:13:59 +01:00
|
|
|
/* -------------------------------------------- */
|
|
|
|
/**
|
|
|
|
* Retourne une recontre en fonction de la case et du tirage
|
|
|
|
* @param {*} terrain
|
2022-11-07 00:04:43 +01:00
|
|
|
* @param {*} forcedRoll
|
2021-01-29 15:13:59 +01:00
|
|
|
*/
|
2022-11-26 03:13:45 +01:00
|
|
|
async rollRencontre(terrain, forcedRoll) {
|
2022-11-17 01:22:45 +01:00
|
|
|
terrain = TMRUtility.findTMRLike(terrain);
|
|
|
|
if (terrain == undefined) {
|
|
|
|
return undefined;
|
2021-01-29 15:13:59 +01:00
|
|
|
}
|
2022-11-26 03:13:45 +01:00
|
|
|
|
2022-11-07 00:04:43 +01:00
|
|
|
if (forcedRoll && (forcedRoll <= 0 || forcedRoll > 100)) {
|
|
|
|
forcedRoll = undefined;
|
2021-01-29 15:13:59 +01:00
|
|
|
}
|
2022-11-17 01:22:45 +01:00
|
|
|
const codeTerrain = Grammar.toLowerCaseNoAccent(terrain)
|
2022-11-07 00:04:43 +01:00
|
|
|
const filtreMauvaise = codeTerrain == 'mauvaise' ? it => it.system.mauvaiseRencontre : it => !it.system.mauvaiseRencontre;
|
2022-11-26 03:13:45 +01:00
|
|
|
const frequence = it => it.system.frequence[codeTerrain];
|
|
|
|
const random = await this.table.getRandom(true, frequence, filtreMauvaise, forcedRoll);
|
|
|
|
|
|
|
|
return random?.document;
|
2021-01-29 15:13:59 +01:00
|
|
|
}
|
2022-11-26 03:13:45 +01:00
|
|
|
|
|
|
|
async chatTable(terrain) {
|
|
|
|
const codeTerrain = Grammar.toLowerCaseNoAccent(terrain)
|
|
|
|
const isMauvaise = codeTerrain == 'mauvaise';
|
|
|
|
const filtreMauvaise = isMauvaise ? it => it.system.mauvaiseRencontre : it => !it.system.mauvaiseRencontre;
|
|
|
|
const frequence = it => it.system.frequence[codeTerrain];
|
|
|
|
const typeName = isMauvaise ? 'Mauvaises rencontres' : `Rencontres en ${Misc.upperFirst(terrain)}`;
|
|
|
|
await this.table.chatTable(frequence, filtreMauvaise, typeName);
|
|
|
|
return true
|
2021-01-29 15:13:59 +01:00
|
|
|
}
|
2021-02-14 09:31:52 +01:00
|
|
|
/* -------------------------------------------- */
|
2022-11-26 03:13:45 +01:00
|
|
|
async createRencontre(rencontre, tmr = undefined) {
|
2022-11-07 00:04:43 +01:00
|
|
|
return rencontre.clone({
|
|
|
|
'system.force': await RdDDice.rollTotal(rencontre.system.formule),
|
|
|
|
'system.coord': tmr?.coord,
|
|
|
|
'system.date': game.system.rdd.calendrier.getDateFromIndex(),
|
|
|
|
'system.heure': game.system.rdd.calendrier.getCurrentHeure()
|
2022-11-26 03:13:45 +01:00
|
|
|
}, { save: false });
|
2021-01-29 15:13:59 +01:00
|
|
|
}
|
|
|
|
|
2022-11-26 03:13:45 +01:00
|
|
|
async calculRencontre(rencontre, tmr = undefined) {
|
|
|
|
if (rencontre.system.coord == "") {
|
2022-11-07 00:04:43 +01:00
|
|
|
rencontre.system.coord = tmr?.coord;
|
2021-01-29 15:13:59 +01:00
|
|
|
}
|
2022-11-26 03:13:45 +01:00
|
|
|
if (rencontre.system.force == 0) {
|
2022-11-07 00:04:43 +01:00
|
|
|
rencontre.system.force = await RdDDice.rollTotal(rencontre.system.formule);
|
2021-01-29 15:13:59 +01:00
|
|
|
}
|
2022-11-26 03:13:45 +01:00
|
|
|
if (rencontre.system.date == "") {
|
2022-11-07 00:04:43 +01:00
|
|
|
rencontre.system.date = game.system.rdd.calendrier.getDateFromIndex();
|
2021-01-29 15:13:59 +01:00
|
|
|
}
|
2022-11-07 00:04:43 +01:00
|
|
|
if (rencontre.system.heure == "") {
|
|
|
|
rencontre.system.heure = game.system.rdd.calendrier.getCurrentHeure();
|
2021-01-29 15:13:59 +01:00
|
|
|
}
|
2022-11-07 00:04:43 +01:00
|
|
|
return rencontre;
|
2021-01-29 15:13:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-26 03:13:45 +01:00
|
|
|
async getPresentsCite() {
|
2022-11-07 00:04:43 +01:00
|
|
|
const rencontres = await SystemCompendiums.getDefaultItems('rencontres');
|
2022-11-26 03:13:45 +01:00
|
|
|
return rencontres.filter(it => !it.system.mauvaiseRencontre && it.system.presentCite).map(it =>
|
|
|
|
it.clone({ 'system.formule': "2d6" }, { save: false }));
|
2021-01-29 15:13:59 +01:00
|
|
|
}
|
|
|
|
|
2022-11-26 03:13:45 +01:00
|
|
|
async getReveDeDragon(force) {
|
2022-11-07 00:04:43 +01:00
|
|
|
const rencontres = await SystemCompendiums.getDefaultItems('rencontres');
|
|
|
|
const reveDeDragon = rencontres.find(it => Grammar.equalsInsensitive(it.name, 'Rêve de Dragon'));
|
2022-11-26 03:13:45 +01:00
|
|
|
return reveDeDragon?.clone({ 'system.force': force }, { save: false });
|
2021-01-29 15:13:59 +01:00
|
|
|
}
|
|
|
|
|
2021-04-25 10:08:40 +02:00
|
|
|
/* -------------------------------------------- */
|
2022-11-26 03:13:45 +01:00
|
|
|
async getRencontreAleatoire(tmr, mauvaise) {
|
2022-11-07 00:04:43 +01:00
|
|
|
const codeTerrain = mauvaise ? 'mauvaise' : tmr.type;
|
2022-11-26 03:13:45 +01:00
|
|
|
const filtreMauvaise = codeTerrain == 'mauvaise' ? it => it.system.mauvaiseRencontre : it => !it.system.mauvaiseRencontre;
|
|
|
|
const frequence = it => it.system.frequence[codeTerrain];
|
|
|
|
|
|
|
|
const row = await this.table.getRandom(false, frequence, filtreMauvaise);
|
|
|
|
if (row) {
|
|
|
|
row.document = this.createRencontre(row.document, tmr);
|
|
|
|
await this.$chatRolledRencontre(row, tmr);
|
|
|
|
}
|
|
|
|
return row?.document;
|
2021-01-29 15:13:59 +01:00
|
|
|
}
|
2021-02-12 01:44:27 +01:00
|
|
|
|
2022-11-26 03:13:45 +01:00
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
async $chatRolledRencontre(row, tmr) {
|
|
|
|
const flavorContent = await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/chat-compendium-table-roll-rencontre.html',
|
|
|
|
{
|
|
|
|
roll: row.roll,
|
|
|
|
rencontre: row?.document,
|
|
|
|
percentages: (row.total == 100) ? '%' : '',
|
|
|
|
tmr,
|
|
|
|
isGM: game.user.isGM,
|
|
|
|
});
|
|
|
|
const messageData = {
|
|
|
|
user: game.user.id,
|
|
|
|
type: CONST.CHAT_MESSAGE_TYPES.ROLL,
|
|
|
|
roll: row.roll,
|
|
|
|
sound: CONFIG.sounds.dice,
|
|
|
|
content: flavorContent
|
|
|
|
};
|
|
|
|
ChatMessage.create(messageData, { rollMode: "gmroll" });
|
|
|
|
}
|
2021-01-29 15:13:59 +01:00
|
|
|
}
|