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( ) {
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', {} );
}
/* -------------------------------------------- */
async incItemUse( itemId ) {
let itemUse = this.getFlag('foundryvtt-reve-de-dragon', 'itemUse');
itemUse = (itemUse) ? duplicate(itemUse) : {};
itemUse[itemId] = (itemUse[itemId]) ? itemUse[itemId] + 1 : 1;
let itemUse = duplicate(this.getFlag('foundryvtt-reve-de-dragon', 'itemUse') ?? {});
itemUse[itemId] = (itemUse[itemId] ?? 0) + 1;
await this.setFlag( 'foundryvtt-reve-de-dragon', 'itemUse', itemUse);
console.log("ITEM USE INC", itemUse);
}
/* -------------------------------------------- */
getItemUse( itemId ) {
let itemUse = this.getFlag('foundryvtt-reve-de-dragon', 'itemUse');
itemUse = (itemUse) ? itemUse : {};
let itemUse = this.getFlag('foundryvtt-reve-de-dragon', '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 = [
{
@ -64,7 +65,7 @@ const poesieHautReve = [
export class Poetique {
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";
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 {
static getName( msg, params ) {
let max = words.length;
let name = words[new Roll("1d"+max+" -1").roll().total];
name += words[new Roll("1d"+max+" -1").roll().total];
let name = Misc.upperFirst( Misc.rollOneOf(words) + Misc.rollOneOf(words) )
//console.log(name);
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() } },
maitrise: { verbe: 'maîtriser', action: 'Maîtriser le fleuve' }
}
rollData.double = EffetsDraconiques.isDoubleResistanceFleuve(this.actor) ? true: undefined,
rollData.competence.data.defaut_carac = 'reve-actuel';
await this._rollMaitriseCaseHumide(rollData);
}
}
@ -465,8 +465,9 @@ export class RdDTMRDialog extends Dialog {
rollData.souffle = await this.actor.ajouterSouffle({ chat: false });
}
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.double = undefined;
await this._rollMaitriseCaseHumide(rollData);
return;
}
@ -513,7 +514,7 @@ export class RdDTMRDialog extends Dialog {
}
return false;
}
/* -------------------------------------------- */
async conquerirCiteFermee(tmr) {
if (this.viewOnly || this.currentRencontre) {
@ -601,8 +602,8 @@ export class RdDTMRDialog extends Dialog {
}
/* -------------------------------------------- */
lancerSortEnReserve(coordTMR, sortId) {
let sortReserveList = TMRUtility.getSortReserveList(this.sortsReserves, coordTMR);
lancerSortEnReserve(coord, sortId) {
let sortReserveList = TMRUtility.getSortReserveList(this.sortsReserves, coord);
let sortReserve = sortReserveList.find(sortReserve => sortReserve.sort._id == sortId);
//console.log("SORT RESA", sortReserveList, coordTMR, sortId, sortReserve);
if (sortReserve) {
@ -620,10 +621,10 @@ export class RdDTMRDialog extends Dialog {
await this.actor.deleteSortReserve(sortReserve);
this.updateSortReserve();
console.log("declencheSortEnReserve", sortReserve)
const declenchementSort = "Vous avez déclenché le sort <strong>" + sortReserve.sort.name
+ "</strong> en réserve en " + sortReserve.coord + " (" + TMRUtility.getTMR(sortReserve.coord).label
+ ") avec " + sortReserve.sort.data.ptreve_reel + " points de Rêve";
this._tellToGM(declenchementSort);
this._tellToGM(`Vous avez déclenché le sort en réserve <strong> ${sortReserve.sort.name}</strong>
avec ${sortReserve.sort.data.ptreve_reel} points de Rêve
en ${sortReserve.coord} (${TMRUtility.getTMRLabel(sortReserve.coord)})
`);
this.close();
}
@ -700,35 +701,21 @@ export class RdDTMRDialog extends Dialog {
console.log("deplacerDemiReve >>>>", currentPos, eventPos);
let targetCoordTMR = TMRUtility.convertToTMRCoord(eventPos);
let currentCoordTMR = TMRUtility.convertToTMRCoord(currentPos);
let targetCoord = TMRUtility.convertToTMRCoord(eventPos);
let currentCoord = TMRUtility.convertToTMRCoord(currentPos);
// Validation de la case de destination (gestion du cas des rencontres qui peuvent téléporter)
let deplacementType = 'erreur';
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);
}
let deplacementType = this._calculDeplacement(targetCoord, currentCoord, currentPos, eventPos);
// Si le deplacement est valide
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
/*
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);
await this._messagerDemiReve(targetCoord);
} 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");
console.log("STATUS :", this.rencontreState, this.currentRencontre);
}
this.checkQuitterTMR();
}
@ -757,7 +744,7 @@ export class RdDTMRDialog extends Dialog {
game.socket.emit("system.foundryvtt-reve-de-dragon", {
msg: "msg_tmr_move", data: {
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 { Grammar } from "./grammar.js";
import { Misc } from "./misc.js";
import { TMRUtility } from "./tmr-utility.js";
import { TMRType } from "./tmr-utility.js";
@ -367,10 +368,10 @@ export class TMRRencontres {
/* -------------------------------------------- */
static async getMauvaiseRencontre(index = undefined) {
if (index == undefined || index >= mauvaisesRencontres.length) {
index = new Roll("1d" + mauvaisesRencontres.length).roll().total - 1;
}
const rencontre = duplicate(mauvaisesRencontres[index]);
const rencontre = duplicate(
(index && index>=0 && index < mauvaisesRencontres.length)
? mauvaisesRencontres[index]
: Misc.rollOneOf(mauvaisesRencontres));
await TMRRencontres.evaluerForceRencontre(rencontre);
return rencontre;
}
@ -408,15 +409,15 @@ export class TMRRencontres {
data.message = gestion.msgSucces(data);
if (data.nbRounds > 1) {
data.message += ` Au total, vous avez passé ${data.nbRounds} rounds à vous battre!`;
}
}
data.poesie = gestion.poesieSucces;
return gestion.postSucces;
}
data.message = gestion.msgEchec(data);
if (data.nbRounds > 1) {
data.message += ` Vous avez passé ${data.nbRounds} rounds à lutter!`;
}
}
data.poesie = gestion.poesieEchec;
return gestion.postEchec;
}
@ -428,8 +429,7 @@ export class TMRRencontres {
data.newTMR = TMRUtility.getTMR(data.sortReserve.coord);
} else {
// Déplacement aléatoire de la force du Passeur Fou
const locList = TMRUtility.getTMRPortee(data.tmr.coord, data.rencontre.force);
const newCoord = locList[new Roll("1d" + locList.length).evaluate().total - 1];
const newCoord = Misc.rollOneOf(TMRUtility.getTMRPortee(data.tmr.coord, data.rencontre.force));
data.newTMR = TMRUtility.getTMR(newCoord);
}
if (data.sortReserve) {

View File

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