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-07 00:04:43 +01:00
|
|
|
import { SystemCompendiums } from "./settings/system-compendiums.js";
|
2021-01-29 15:13:59 +01:00
|
|
|
import { TMRUtility } from "./tmr-utility.js";
|
|
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
export class TMRRencontres {
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
/**
|
|
|
|
* 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-07 00:04:43 +01:00
|
|
|
static 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-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 table = await TMRRencontres.$buildTableRencontre(codeTerrain);
|
|
|
|
const [selected, roll] = await TMRRencontres.$selectRencontre(codeTerrain, table, forcedRoll);
|
|
|
|
const rencontre = await TMRRencontres.createRencontre(selected.rencontre);
|
|
|
|
TMRRencontres.$chatRolledRencontre(rencontre, terrain, table, roll, true);
|
2021-01-29 15:13:59 +01:00
|
|
|
return false;
|
|
|
|
}
|
2022-11-07 00:04:43 +01:00
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
static async $buildTableRencontre(codeTerrain) {
|
|
|
|
let max = 0;
|
2022-11-17 01:22:45 +01:00
|
|
|
const items = await SystemCompendiums.getItems('rencontres', 'rencontre');
|
2022-11-07 00:04:43 +01:00
|
|
|
const filtreMauvaise = codeTerrain == 'mauvaise' ? it => it.system.mauvaiseRencontre : it => !it.system.mauvaiseRencontre;
|
|
|
|
const rencontres = items.filter(it => it.type == 'rencontre')
|
|
|
|
.filter(filtreMauvaise)
|
|
|
|
.filter(it => it.system.frequence[codeTerrain] > 0)
|
|
|
|
.sort(Misc.ascending(it => it.system.ordreTri))
|
|
|
|
.map(it => {
|
|
|
|
const frequence = it.system.frequence[codeTerrain];
|
|
|
|
max += frequence;
|
|
|
|
return { rencontre: it, min: max - frequence + 1, max: max,frequence: frequence };
|
|
|
|
});
|
|
|
|
return rencontres;
|
2021-01-29 15:13:59 +01:00
|
|
|
}
|
2022-11-07 00:04:43 +01:00
|
|
|
|
2021-01-29 15:13:59 +01:00
|
|
|
/* -------------------------------------------- */
|
2022-11-07 00:04:43 +01:00
|
|
|
static async $selectRencontre(terrain, table, roll = undefined) {
|
|
|
|
const total = table.map(it => it.frequence).reduce(Misc.sum(), 0);
|
|
|
|
if (total == 0){
|
|
|
|
ui.notifications.warn(`Pas de rencontres définies pour ${terrain}`);
|
|
|
|
return undefined;
|
2021-01-29 15:13:59 +01:00
|
|
|
}
|
2022-11-07 00:04:43 +01:00
|
|
|
if (roll != undefined && (roll > total || roll <= 0)) {
|
|
|
|
ui.notifications.warn(`Jet de rencontre ${roll} en dehors de la table [1..${total}], le jet est relancé`);
|
|
|
|
roll = undefined;
|
|
|
|
}
|
|
|
|
if (!roll) {
|
|
|
|
roll = await RdDDice.rollTotal(`1d${total}`);
|
2021-01-29 15:13:59 +01:00
|
|
|
}
|
2022-11-07 00:04:43 +01:00
|
|
|
return [table.find(it => it.min <= roll && roll <= it.max), roll];
|
2021-01-29 15:13:59 +01:00
|
|
|
}
|
2022-11-07 00:04:43 +01:00
|
|
|
|
2021-02-14 09:31:52 +01:00
|
|
|
/* -------------------------------------------- */
|
2022-11-07 00:04:43 +01:00
|
|
|
static async createRencontre(rencontre, tmr = undefined) {
|
|
|
|
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()
|
|
|
|
}, {save: false});
|
2021-01-29 15:13:59 +01:00
|
|
|
}
|
|
|
|
|
2022-11-07 00:04:43 +01:00
|
|
|
static async calculRencontre(rencontre, tmr = undefined) {
|
|
|
|
if (rencontre.system.coord == ""){
|
|
|
|
rencontre.system.coord = tmr?.coord;
|
2021-01-29 15:13:59 +01:00
|
|
|
}
|
2022-11-07 00:04:43 +01:00
|
|
|
if (rencontre.system.force == 0){
|
|
|
|
rencontre.system.force = await RdDDice.rollTotal(rencontre.system.formule);
|
2021-01-29 15:13:59 +01:00
|
|
|
}
|
2022-11-07 00:04:43 +01:00
|
|
|
if (rencontre.system.date == "" ) {
|
|
|
|
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-07 00:04:43 +01:00
|
|
|
static $chatRolledRencontre(rencontre, terrain, table, roll = 0, displayTable=false){
|
|
|
|
const total = table.map(it => it.frequence).reduce(Misc.sum(), 0);
|
|
|
|
const namesPercent = displayTable ?
|
|
|
|
table.map(it => `<br>${it.rencontre.name} : ${it.frequence}${total == 100 ? '%' : ''} (${it.min} - ${it.max})`).reduce((a, b) => a + b, '<hr>')
|
|
|
|
: '';
|
|
|
|
const chances = game.user.isGM
|
|
|
|
? (roll ? `Jet: ${roll} / ${total}` : `Valeurs: [1..${total}]`)
|
|
|
|
: (roll ? `Jet: ${Math.ceil(roll*100/total)} / 100` : '');
|
|
|
|
ChatMessage.create({
|
|
|
|
user: game.user.id,
|
|
|
|
whisper: [game.user.id],
|
|
|
|
content: `Compendium: ${SystemCompendiums.getCompendium('rencontres')}
|
|
|
|
<br>Rencontre en ${terrain}:
|
|
|
|
${namesPercent}<hr>
|
|
|
|
<br>${chances}
|
|
|
|
<br>Rencontre: ${rencontre.name} ${rencontre.system.force} (${rencontre.system.formule})`
|
|
|
|
});
|
2021-01-29 15:13:59 +01:00
|
|
|
}
|
|
|
|
|
2022-11-07 00:04:43 +01:00
|
|
|
static async getPresentsCite() {
|
|
|
|
const rencontres = await SystemCompendiums.getDefaultItems('rencontres');
|
|
|
|
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-07 00:04:43 +01:00
|
|
|
static async getReveDeDragon(force) {
|
|
|
|
const rencontres = await SystemCompendiums.getDefaultItems('rencontres');
|
|
|
|
const reveDeDragon = rencontres.find(it => Grammar.equalsInsensitive(it.name, 'Rêve de Dragon'));
|
|
|
|
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-07 00:04:43 +01:00
|
|
|
static async getRencontreAleatoire(tmr, mauvaise) {
|
|
|
|
const codeTerrain = mauvaise ? 'mauvaise' : tmr.type;
|
|
|
|
const table = await TMRRencontres.$buildTableRencontre(codeTerrain);
|
|
|
|
const [selected, roll] = await TMRRencontres.$selectRencontre(codeTerrain, table);
|
|
|
|
const rencontre = await TMRRencontres.createRencontre(selected.rencontre, tmr);
|
|
|
|
TMRRencontres.$chatRolledRencontre(rencontre, TMRUtility.getTMRType(tmr.coord), table, roll);
|
|
|
|
return rencontre;
|
2021-01-29 15:13:59 +01:00
|
|
|
}
|
2021-02-12 01:44:27 +01:00
|
|
|
|
2021-01-29 15:13:59 +01:00
|
|
|
}
|