Utilisation nullish / chaining

Simplifie certaines expressions complexes avec operateur ternaire

- Nullish coalescing operator (??)
- Relationship with the optional chaining operator (?.)

- Math.min / Math.max / Math.ceil
This commit is contained in:
Vincent Vandemeulebrouck 2021-01-04 14:10:21 +01:00
parent 0ef119f59c
commit e3439953f9
6 changed files with 27 additions and 33 deletions

View File

@ -700,13 +700,7 @@ export class RdDActor extends Actor {
/* -------------------------------------------- */
detectSurEncombrement( ) {
let diffEnc = Number(this.encTotal) - Number(this.data.data.attributs.encombrement.value);
if ( diffEnc > 0 ) { // Sur-encombrement
let malus = Math.round( diffEnc);
malus = (malus == 0) ? 1 : malus; // Always 1 at least
//console.log("Sur enc malus", malus);
return malus;
}
return 0;
return Math.min(0, Math.ceil(diffEnc));
}
/* -------------------------------------------- */
@ -770,7 +764,7 @@ export class RdDActor extends Actor {
/* -------------------------------------------- */
getEtatGeneral() {
return (this.data.data.compteurs && this.data.data.compteurs.etat) ? this.data.data.compteurs.etat.value : 0;
return this.data.data.compteurs?.etat?.value ?? 0;
}
/* -------------------------------------------- */
@ -784,11 +778,13 @@ export class RdDActor extends Actor {
}
// Pour les autres
let state = - (data.sante.vie.max - data.sante.vie.value);
if (data.sante.fatigue) // Creatures n'ont pas de fatigue
if (data.sante.fatigue){
// Creatures n'ont pas de fatigue
state += RdDUtility.currentFatigueMalus(data.sante.fatigue.value, data.sante.endurance.max);
if (data.compteurs && data.compteurs.ethylisme && data.compteurs.ethylisme.value < 0 ) { // Ajout de l'éthylisme
state += data.compteurs.ethylisme.value;
}
// Ajout de l'éthylisme
state += Math.min(0, (data.compteurs?.ethylisme?.value ??0));
data.compteurs.etat.value = state;
if ( data.compteurs && data.compteurs.surenc) {
data.compteurs.surenc.value = - this.detectSurEncombrement();

View File

@ -66,11 +66,11 @@ export class RdDBonus {
/* -------------------------------------------- */
static _dmgArme(rollData) {
return rollData.arme ? parseInt(rollData.arme.data.dommages) : 0;
return parseInt(rollData.arme?.data.dommages ?? 0);
}
static _peneration(rollData) {
return rollData.arme ? parseInt(rollData.arme.data.penetration) : 0;
return parseInt(rollData.arme?.data.penetration ?? 0);
}
/* -------------------------------------------- */

View File

@ -18,12 +18,12 @@ export class RdDCombat {
static createUsingTarget(attacker) {
const target = RdDCombat.getTarget();
if (target == undefined) {
ui.notifications.warn((game.user.targets && game.user.targets.size > 1)
ui.notifications.warn( (game.user.targets?.size ??0) > 1
? "Vous devez choisir <strong>une seule</strong> cible à attaquer!"
: "Vous devez choisir une cible à attaquer!");
}
const defender = target ? target.actor : undefined;
const defenderTokenId = target ? target.data._id : undefined;
const defender = target?.actor;
const defenderTokenId = target?.data._id;
return this.create(attacker, defender, defenderTokenId, target)
}
@ -68,8 +68,8 @@ export class RdDCombat {
// envoyer le message au destinataire
if (!game.user.isGM || recipient.hasPlayerOwner) {
let data = {
attackerId: sender ? sender.data._id : undefined,
defenderId: recipient ? recipient.data._id : undefined,
attackerId: sender?.data._id,
defenderId: recipient?.data._id,
defenderTokenId: defenderTokenId,
rollData: duplicate(rollData),
rollMode: true
@ -128,7 +128,7 @@ export class RdDCombat {
case '#particuliere-attaque': return await this.choixParticuliere(rollData, event.currentTarget.attributes['data-mode'].value);
case '#parer-button': {
const armeId = event.currentTarget.attributes['data-armeid'];
return this.parade(rollData, armeId ? armeId.value : undefined);
return this.parade(rollData, armeId?.value);
}
case '#esquiver-button': return this.esquive(rollData);
case '#encaisser-button': return this.encaisser(rollData, event.currentTarget.attributes['data-defenderTokenId'].value);
@ -182,7 +182,7 @@ export class RdDCombat {
options: { height: 540 }
}, {
name: 'jet-attaque',
label: 'Attaque: ' + (arme ? arme.name : competence.name),
label: 'Attaque: ' + (arme?.name ?? competence.name),
callbacks: [
this.attacker.createCallbackExperience(),
{ condition: r => (RdDCombat.isReussite(r) && !RdDCombat.isParticuliere(r)), action: r => this._onAttaqueNormale(r) },

View File

@ -122,7 +122,7 @@ export class RdDResolutionTable {
list.push({ label: surprise.descr, value: surprise.attaque });
}
if (rollData.diffLibre != undefined) {
const label = rollData.selectedSort ? rollData.selectedSort.name : 'Libre';
const label = rollData.selectedSort?.name ?? 'Libre';
list.push({ label: label, value: rollData.diffLibre });
}
if (rollData.diffConditions != undefined) {
@ -131,11 +131,11 @@ export class RdDResolutionTable {
if (rollData.etat != undefined) {
list.push({ label: 'Etat', value: rollData.etat });
}
if (rollData.selectedCarac != undefined && rollData.moral != undefined && rollData.selectedCarac.label == 'Volonté') {
list.push({ label: 'Moral', value: rollData.selectedCarac != undefined && rollData.moral != undefined && rollData.selectedCarac.label == 'Volonté' ? rollData.moral : undefined });
if (rollData.selectedCarac?.label == 'Volonté' && rollData.moral != undefined) {
list.push({ label: 'Moral', value: rollData.moral });
}
if (RdDResolutionTable.isAjustementAstrologique(rollData)) {
list.push({ label: 'Astrologique', value: rollData.ajustementAstrologique || 0 });
list.push({ label: 'Astrologique', value: rollData.ajustementAstrologique ?? 0 });
}
if (rollData.rolled.bonus && rollData.selectedSort) {
list.push({ descr: `Bonus de case: ${rollData.rolled.bonus}%` });

View File

@ -38,12 +38,12 @@ export class RdDRoll extends Dialog {
moral: actor.isPersonnage() ? actor.data.data.compteurs.moral.value : 0,
carac: actor.data.data.carac,
finalLevel: 0,
diffConditions: rollData.arme ? RdDBonus.bonusAttaque(rollData.surpriseDefenseur) :0,
diffConditions: rollData.arme ? RdDBonus.bonusAttaque(rollData.surpriseDefenseur) : 0,
diffLibre: rollData.competence?.data.default_diffLibre ?? 0,
editLibre: true,
editConditions: true,
forceValue: actor.getForceValue(),
malusArmureValue: (actor.isPersonnage() && actor.data.data.attributs && actor.data.data.attributs.malusarmure) ? actor.data.data.attributs.malusarmure.value : 0,
malusArmureValue: actor.isPersonnage() ? actor.data.data.attributs?.malusarmure?.value ?? 0 : 0,
surencMalusFlag: actor.isPersonnage() ? (actor.data.data.compteurs.surenc.value < 0) : false,
surencMalusValue: actor.isPersonnage() ? actor.data.data.compteurs.surenc.value : 0,
surencMalusApply: false,
@ -111,10 +111,10 @@ export class RdDRoll extends Dialog {
var dialog = this;
function updateRollResult(rollData) {
rollData.dmg = rollData.attackerRoll ? rollData.attackerRoll.dmg : RdDBonus.dmg(rollData, actor.getBonusDegat());
rollData.dmg = rollData.attackerRoll?.dmg ?? RdDBonus.dmg(rollData, actor.getBonusDegat());
rollData.finalLevel = dialog._computeFinalLevel(rollData);
rollData.caracValue = parseInt(rollData.selectedCarac.value);
rollData.coupsNonMortels = (rollData.attackerRoll ? rollData.attackerRoll.dmg.mortalite : rollData.dmg.mortalite) == 'non-mortel';
rollData.coupsNonMortels = (rollData.attackerRoll?.dmg.mortalite ?? rollData.dmg.mortalite) == 'non-mortel';
let dmgText = Misc.toSignedString(rollData.dmg.total);
if (rollData.coupsNonMortels) {
dmgText = '(' + dmgText + ')';

View File

@ -673,10 +673,8 @@ export class RdDUtility {
/* -------------------------------------------- */
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;
max = Math.min(1, Math.max(max, 60));
value = Math.min(max*2, Math.max(0, value));
let fatigueTab = fatigueMatrix[max];
let fatigueRem = value;