Small cleanup

- extrait de méthode pour tirage dans un array
- utilisation des operateur ?: / ??
This commit is contained in:
Vincent Vandemeulebrouck 2021-02-12 01:11:03 +01:00
parent 9f2e17537d
commit 1cb4a7dbf5
7 changed files with 70 additions and 78 deletions

View File

@ -2708,25 +2708,23 @@ export class RdDActor extends Actor {
/* -------------------------------------------- */ /* -------------------------------------------- */
async resetItemUse( ) { async resetItemUse( ) {
await this.setFlag('foundryvtt-reve-de-dragon', 'itemUse', null ); await this.unsetFlag('foundryvtt-reve-de-dragon', 'itemUse');
await this.setFlag('foundryvtt-reve-de-dragon', 'itemUse', {} ); await this.setFlag('foundryvtt-reve-de-dragon', 'itemUse', {} );
} }
/* -------------------------------------------- */ /* -------------------------------------------- */
async incItemUse( itemId ) { async incItemUse( itemId ) {
let itemUse = this.getFlag('foundryvtt-reve-de-dragon', 'itemUse'); let itemUse = duplicate(this.getFlag('foundryvtt-reve-de-dragon', 'itemUse') ?? {});
itemUse = (itemUse) ? duplicate(itemUse) : {}; itemUse[itemId] = (itemUse[itemId] ?? 0) + 1;
itemUse[itemId] = (itemUse[itemId]) ? itemUse[itemId] + 1 : 1;
await this.setFlag( 'foundryvtt-reve-de-dragon', 'itemUse', itemUse); await this.setFlag( 'foundryvtt-reve-de-dragon', 'itemUse', itemUse);
console.log("ITEM USE INC", itemUse); console.log("ITEM USE INC", itemUse);
} }
/* -------------------------------------------- */ /* -------------------------------------------- */
getItemUse( itemId ) { getItemUse( itemId ) {
let itemUse = this.getFlag('foundryvtt-reve-de-dragon', 'itemUse'); let itemUse = this.getFlag('foundryvtt-reve-de-dragon', 'itemUse') ?? {};
itemUse = (itemUse) ? itemUse : {};
console.log("ITEM USE GET", itemUse); console.log("ITEM USE GET", itemUse);
return itemUse[itemId] ? itemUse[itemId] : 0; return itemUse[itemId] ?? 0;
} }
/* -------------------------------------------- */ /* -------------------------------------------- */

View File

@ -59,4 +59,12 @@ export class Misc {
} }
} }
static rollOneOf(array) {
return array[new Roll("1d" + array.length).evaluate().total - 1];
}
static distinct(array) {
return [...new Set(array)];
}
} }

View File

@ -1,3 +1,4 @@
import { Misc } from "./misc.js"
const poesieHautReve = [ const poesieHautReve = [
{ {
@ -64,7 +65,7 @@ const poesieHautReve = [
export class Poetique { export class Poetique {
static getExtrait(){ static getExtrait(){
return poesieHautReve[new Roll("1d" + poesieHautReve.length).evaluate().total - 1] return Misc.rollOneOf(poesieHautReve);
} }
} }

View File

@ -1,3 +1,4 @@
import { Grammar } from "./grammar.js";
import { Misc } from "./misc.js"; import { Misc } from "./misc.js";
const words = [ 'pore', 'pre', 'flor', 'lane', 'turlu', 'pin', 'a', 'alph', 'i', 'onse', 'iane', 'ane', 'zach', 'arri', 'ba', 'bo', 'bi', const words = [ 'pore', 'pre', 'flor', 'lane', 'turlu', 'pin', 'a', 'alph', 'i', 'onse', 'iane', 'ane', 'zach', 'arri', 'ba', 'bo', 'bi',
@ -7,9 +8,7 @@ const words = [ 'pore', 'pre', 'flor', 'lane', 'turlu', 'pin', 'a', 'alph', 'i',
export class RdDNameGen { export class RdDNameGen {
static getName( msg, params ) { static getName( msg, params ) {
let max = words.length; let name = Misc.upperFirst( Misc.rollOneOf(words) + Misc.rollOneOf(words) )
let name = words[new Roll("1d"+max+" -1").roll().total];
name += words[new Roll("1d"+max+" -1").roll().total];
//console.log(name); //console.log(name);
ChatMessage.create( { content: `Nom : ${name}`, whisper: ChatMessage.getWhisperRecipients("GM") } ); ChatMessage.create( { content: `Nom : ${name}`, whisper: ChatMessage.getWhisperRecipients("GM") } );
} }

View File

@ -449,8 +449,8 @@ export class RdDTMRDialog extends Dialog {
forceCarac: { 'reve-actuel': { label: "Rêve Actuel", value: this.actor.getReveActuel() } }, forceCarac: { 'reve-actuel': { label: "Rêve Actuel", value: this.actor.getReveActuel() } },
maitrise: { verbe: 'maîtriser', action: 'Maîtriser le fleuve' } maitrise: { verbe: 'maîtriser', action: 'Maîtriser le fleuve' }
} }
rollData.double = EffetsDraconiques.isDoubleResistanceFleuve(this.actor) ? true: undefined,
rollData.competence.data.defaut_carac = 'reve-actuel'; rollData.competence.data.defaut_carac = 'reve-actuel';
await this._rollMaitriseCaseHumide(rollData); await this._rollMaitriseCaseHumide(rollData);
} }
} }
@ -465,8 +465,9 @@ export class RdDTMRDialog extends Dialog {
rollData.souffle = await this.actor.ajouterSouffle({ chat: false }); rollData.souffle = await this.actor.ajouterSouffle({ chat: false });
} }
this.toclose = rollData.rolled.isEchec; this.toclose = rollData.rolled.isEchec;
if (rollData.rolled.isSuccess && !rollData.previous && EffetsDraconiques.isDoubleResistanceFleuve(this.actor)) { if (rollData.rolled.isSuccess && rollData.double) {
rollData.previous = { rolled: rollData.rolled, ajustements: rollData.ajustements }; rollData.previous = { rolled: rollData.rolled, ajustements: rollData.ajustements };
rollData.double = undefined;
await this._rollMaitriseCaseHumide(rollData); await this._rollMaitriseCaseHumide(rollData);
return; return;
} }
@ -601,8 +602,8 @@ export class RdDTMRDialog extends Dialog {
} }
/* -------------------------------------------- */ /* -------------------------------------------- */
lancerSortEnReserve(coordTMR, sortId) { lancerSortEnReserve(coord, sortId) {
let sortReserveList = TMRUtility.getSortReserveList(this.sortsReserves, coordTMR); let sortReserveList = TMRUtility.getSortReserveList(this.sortsReserves, coord);
let sortReserve = sortReserveList.find(sortReserve => sortReserve.sort._id == sortId); let sortReserve = sortReserveList.find(sortReserve => sortReserve.sort._id == sortId);
//console.log("SORT RESA", sortReserveList, coordTMR, sortId, sortReserve); //console.log("SORT RESA", sortReserveList, coordTMR, sortId, sortReserve);
if (sortReserve) { if (sortReserve) {
@ -620,10 +621,10 @@ export class RdDTMRDialog extends Dialog {
await this.actor.deleteSortReserve(sortReserve); await this.actor.deleteSortReserve(sortReserve);
this.updateSortReserve(); this.updateSortReserve();
console.log("declencheSortEnReserve", sortReserve) console.log("declencheSortEnReserve", sortReserve)
const declenchementSort = "Vous avez déclenché le sort <strong>" + sortReserve.sort.name this._tellToGM(`Vous avez déclenché le sort en réserve <strong> ${sortReserve.sort.name}</strong>
+ "</strong> en réserve en " + sortReserve.coord + " (" + TMRUtility.getTMR(sortReserve.coord).label avec ${sortReserve.sort.data.ptreve_reel} points de Rêve
+ ") avec " + sortReserve.sort.data.ptreve_reel + " points de Rêve"; en ${sortReserve.coord} (${TMRUtility.getTMRLabel(sortReserve.coord)})
this._tellToGM(declenchementSort); `);
this.close(); this.close();
} }
@ -700,35 +701,21 @@ export class RdDTMRDialog extends Dialog {
console.log("deplacerDemiReve >>>>", currentPos, eventPos); console.log("deplacerDemiReve >>>>", currentPos, eventPos);
let targetCoordTMR = TMRUtility.convertToTMRCoord(eventPos); let targetCoord = TMRUtility.convertToTMRCoord(eventPos);
let currentCoordTMR = TMRUtility.convertToTMRCoord(currentPos); let currentCoord = TMRUtility.convertToTMRCoord(currentPos);
// Validation de la case de destination (gestion du cas des rencontres qui peuvent téléporter) // Validation de la case de destination (gestion du cas des rencontres qui peuvent téléporter)
let deplacementType = 'erreur'; let deplacementType = this._calculDeplacement(targetCoord, currentCoord, currentPos, eventPos);
if (this.rencontreState == 'aucune') { // Pas de recontre en post-processing, donc deplacement normal
if (this.isTerreAttache(targetCoordTMR) || this.isConnaissanceFleuve(currentCoordTMR, targetCoordTMR) || !RdDTMRDialog._horsDePortee(currentPos, eventPos)) {
deplacementType = 'normal';
}
} else {
deplacementType = this.processClickPostRencontre(targetCoordTMR);
}
// Si le deplacement est valide // Si le deplacement est valide
if (deplacementType == 'normal' || deplacementType == 'saut') { if (deplacementType == 'normal' || deplacementType == 'saut') {
await this._deplacerDemiReve(targetCoordTMR, deplacementType); await this._deplacerDemiReve(targetCoord, deplacementType);
} else if (deplacementType == 'messager') { // Dans ce cas, ouverture du lancement de sort sur la case visée } else if (deplacementType == 'messager') { // Dans ce cas, ouverture du lancement de sort sur la case visée
/* await this._messagerDemiReve(targetCoord);
TODO: si la case a un sort en réserve, lancer ce sort.
Si la case est le demi-rêve, ne pas lancer de sort.
Si un lancement de sort est en cours, trouver un moyen de réafficher cette fenêtre si on essaie de lancer un sort (ou bloquer le lancer de sort)
*/
await this._messagerDemiReve(targetCoordTMR);
} else { } else {
ui.notifications.error("Vous ne pouvez vous déplacer que sur des cases adjacentes à votre position ou valides dans le cas d'une rencontre"); ui.notifications.error("Vous ne pouvez vous déplacer que sur des cases adjacentes à votre position ou valides dans le cas d'une rencontre");
console.log("STATUS :", this.rencontreState, this.currentRencontre); console.log("STATUS :", this.rencontreState, this.currentRencontre);
} }
this.checkQuitterTMR(); this.checkQuitterTMR();
} }
@ -757,7 +744,7 @@ export class RdDTMRDialog extends Dialog {
game.socket.emit("system.foundryvtt-reve-de-dragon", { game.socket.emit("system.foundryvtt-reve-de-dragon", {
msg: "msg_tmr_move", data: { msg: "msg_tmr_move", data: {
actorId: this.actor.data._id, actorId: this.actor.data._id,
tmrPos: tmrPos tmrPos: this.actor.data.data.reve.tmrpos
} }
}); });

View File

@ -1,5 +1,6 @@
import { DeDraconique } from "./de-draconique.js"; import { DeDraconique } from "./de-draconique.js";
import { Grammar } from "./grammar.js"; import { Grammar } from "./grammar.js";
import { Misc } from "./misc.js";
import { TMRUtility } from "./tmr-utility.js"; import { TMRUtility } from "./tmr-utility.js";
import { TMRType } from "./tmr-utility.js"; import { TMRType } from "./tmr-utility.js";
@ -367,10 +368,10 @@ export class TMRRencontres {
/* -------------------------------------------- */ /* -------------------------------------------- */
static async getMauvaiseRencontre(index = undefined) { static async getMauvaiseRencontre(index = undefined) {
if (index == undefined || index >= mauvaisesRencontres.length) { const rencontre = duplicate(
index = new Roll("1d" + mauvaisesRencontres.length).roll().total - 1; (index && index>=0 && index < mauvaisesRencontres.length)
} ? mauvaisesRencontres[index]
const rencontre = duplicate(mauvaisesRencontres[index]); : Misc.rollOneOf(mauvaisesRencontres));
await TMRRencontres.evaluerForceRencontre(rencontre); await TMRRencontres.evaluerForceRencontre(rencontre);
return rencontre; return rencontre;
} }
@ -428,8 +429,7 @@ export class TMRRencontres {
data.newTMR = TMRUtility.getTMR(data.sortReserve.coord); data.newTMR = TMRUtility.getTMR(data.sortReserve.coord);
} else { } else {
// Déplacement aléatoire de la force du Passeur Fou // Déplacement aléatoire de la force du Passeur Fou
const locList = TMRUtility.getTMRPortee(data.tmr.coord, data.rencontre.force); const newCoord = Misc.rollOneOf(TMRUtility.getTMRPortee(data.tmr.coord, data.rencontre.force));
const newCoord = locList[new Roll("1d" + locList.length).evaluate().total - 1];
data.newTMR = TMRUtility.getTMR(newCoord); data.newTMR = TMRUtility.getTMR(newCoord);
} }
if (data.sortReserve) { if (data.sortReserve) {

View File

@ -1,6 +1,4 @@
import { DeDraconique } from "./de-draconique.js";
import { TMRRencontres } from "./tmr-rencontres.js"; import { TMRRencontres } from "./tmr-rencontres.js";
import { Grammar } from "./grammar.js";
import { Misc } from "./misc.js"; import { Misc } from "./misc.js";
/* -------------------------------------------- */ /* -------------------------------------------- */
@ -270,6 +268,7 @@ export const tmrColors = {
sort: 0xFF8800, sort: 0xFF8800,
tetes: 0xA000FF, tetes: 0xA000FF,
souffle: 0x804040, souffle: 0x804040,
queues: 0xAA4040,
trounoir: 0x401060, trounoir: 0x401060,
demireve: 0x00FFEE, demireve: 0x00FFEE,
rencontre: 0xFF0000, rencontre: 0xFF0000,
@ -283,7 +282,9 @@ export const tmrColors = {
export class TMRUtility { export class TMRUtility {
static init() { static init() {
for (let coord in TMRMapping) { for (let coord in TMRMapping) {
TMRMapping[coord].coord = coord; const tmr = TMRMapping[coord];
tmr.coord = coord;
tmr.genre = TMRType[tmr.type].genre;
} }
let tmrByType = Misc.classify(Object.values(TMRMapping)); let tmrByType = Misc.classify(Object.values(TMRMapping));
for (const [type, list] of Object.entries(tmrByType)) { for (const [type, list] of Object.entries(tmrByType)) {
@ -317,9 +318,14 @@ export class TMRUtility {
} }
/* -------------------------------------------- */ /* -------------------------------------------- */
static getTMR(coordTMR) { static getTMR(coord) {
return TMRMapping[coordTMR]; return TMRMapping[coord];
} }
static getTMRLabel(coord) {
return TMRMapping[coord]?.label ?? (coord+": case inconnue");
}
static isCaseHumide(tmr) { static isCaseHumide(tmr) {
return tmr.type == 'fleuve' || tmr.type == 'lac' || tmr.type == 'marais'; return tmr.type == 'fleuve' || tmr.type == 'lac' || tmr.type == 'marais';
} }
@ -355,22 +361,21 @@ export class TMRUtility {
/* -------------------------------------------- */ /* -------------------------------------------- */
static getDirectionPattern() { static getDirectionPattern() {
let roll = new Roll("1d" + tmrRandomMovePatten.length).evaluate().total; return Misc.rollOneOf(tmrRandomMovePatten);
return tmrRandomMovePatten[roll - 1];
} }
static deplaceTMRAleatoire(coord) { static deplaceTMRAleatoire(actor, coord) {
return TMRUtility.deplaceTMRSelonPattern(coord, TMRUtility.getDirectionPattern(), 1); return TMRUtility.deplaceTMRSelonPattern(actor, coord, TMRUtility.getDirectionPattern(), 1);
} }
/* -------------------------------------------- */ /* -------------------------------------------- */
static deplaceTMRSelonPattern(coord, direction, nTime) { static async deplaceTMRSelonPattern(actor, coord, direction, nTime) {
for (let i = 0; i < nTime; i++) { for (let i = 0; i < nTime; i++) {
let currentPosXY = TMRUtility.convertToCellPos(coord); let currentPos = TMRUtility.convertToCellPos(coord);
currentPosXY.x = currentPosXY.x + direction.x; currentPos.x = currentPos.x + direction.x;
currentPosXY.y = currentPosXY.y + direction.y; currentPos.y = currentPos.y + direction.y;
if (this._checkTMRCoord(currentPosXY.x, currentPosXY.y)) { // Sortie de carte ! Ré-insertion aléatoire if (this._checkTMRCoord(currentPos.x, currentPos.y)) { // Sortie de carte ! Ré-insertion aléatoire
coord = TMRUtility.convertToTMRCoord(currentPosXY); coord = TMRUtility.getTMR(TMRUtility.convertToTMRCoord(currentPos));
} else { } else {
coord = this.getTMRAleatoire().coord; coord = this.getTMRAleatoire().coord;
} }
@ -392,10 +397,8 @@ export class TMRUtility {
return TMRUtility.filterTMR(filter).map(it => it.coord); return TMRUtility.filterTMR(filter).map(it => it.coord);
} }
static getTMRAleatoire(filter = undefined) { static getTMRAleatoire(filter = it => true) {
let list = TMRUtility.filterTMR(filter); return Misc.rollOneOf(TMRUtility.filterTMR(filter))
let index = new Roll("1d" + list.length).evaluate().total - 1;
return list[index];
} }
/* -------------------------------------------- */ /* -------------------------------------------- */
@ -433,21 +436,17 @@ export class TMRUtility {
/** Returns a list of case inside a given distance /** Returns a list of case inside a given distance
* *
*/ */
static getTMRPortee(centerCoord, portee) { static getTMRPortee(coord, portee) {
return TMRUtility.getTMRArea(centerCoord, portee, tmrConstants); let centerPos = this.convertToCellPos(coord);
}
static getTMRArea(centerCoord, distance, tmrConstants) {
let centerPos = this.convertToCellPos(centerCoord);
let posPic = this.computeRealPictureCoordinates(centerPos, tmrConstants); let posPic = this.computeRealPictureCoordinates(centerPos, tmrConstants);
let caseList = []; let caseList = [];
for (let dx = -distance; dx <= distance; dx++) { // Loop thru lines for (let dx = -portee; dx <= portee; dx++) { // Loop thru lines
for (let dy = -distance; dy <= distance; dy++) { // Loop thru lines for (let dy = -portee; dy <= portee; dy++) { // Loop thru lines
const currentPos = { x: centerPos.x + dx, y: centerPos.y + dy }; const currentPos = { x: centerPos.x + dx, y: centerPos.y + dy };
if (this._checkTMRCoord(currentPos.x, currentPos.y)) { // Coordinate is valie if (this._checkTMRCoord(currentPos.x, currentPos.y)) { // Coordinate is valie
let posPicNow = this.computeRealPictureCoordinates(currentPos, tmrConstants); let posPicNow = this.computeRealPictureCoordinates(currentPos, tmrConstants);
let dist = Math.sqrt(Math.pow(posPicNow.x - posPic.x, 2) + Math.pow(posPicNow.y - posPic.y, 2)) / tmrConstants.cellw; let dist = Math.sqrt(Math.pow(posPicNow.x - posPic.x, 2) + Math.pow(posPicNow.y - posPic.y, 2)) / tmrConstants.cellw;
if (dist < distance + 0.5) { if (dist < portee + 0.5) {
caseList.push(this.convertToTMRCoord(currentPos)); // Inside the area caseList.push(this.convertToTMRCoord(currentPos)); // Inside the area
} }
} }