2020-11-15 02:07:41 +01:00
import { Misc } from "./misc.js" ;
2020-11-16 03:54:43 +01:00
import { RdDDice } from "./rdd-dice.js" ;
2020-11-11 22:39:36 +01:00
2020-11-12 16:35:51 +01:00
/ * *
* difficultés au delà de - 10
* /
const levelDown = [
{ level : - 11 , score : 1 , sign : 0 , part : 0 , epart : 2 , etotal : 90 } ,
{ level : - 12 , score : 1 , sign : 0 , part : 0 , epart : 2 , etotal : 70 } ,
{ level : - 13 , score : 1 , sign : 0 , part : 0 , epart : 2 , etotal : 50 } ,
{ level : - 14 , score : 1 , sign : 0 , part : 0 , epart : 2 , etotal : 30 } ,
{ level : - 15 , score : 1 , sign : 0 , part : 0 , epart : 2 , etotal : 10 } ,
{ level : - 16 , score : 1 , sign : 0 , part : 0 , epart : 0 , etotal : 2 }
] ;
const levelImpossible = { score : 0 , sign : 0 , part : 0 , epart : 0 , etotal : 1 } ;
/ * *
* Table des résultats spéciaux - inutilisée , conservée si on veut afficher la table
* /
const specialResults = [
{ part : 0 , epart : 0 , etotal : 0 , min : 0 , max : 0 } ,
{ part : 1 , epart : 81 , etotal : 92 , min : 1 , max : 5 } ,
{ part : 2 , epart : 82 , etotal : 92 , min : 6 , max : 10 } ,
{ part : 3 , epart : 83 , etotal : 93 , min : 11 , max : 15 } ,
{ part : 4 , epart : 84 , etotal : 93 , min : 16 , max : 20 } ,
{ part : 5 , epart : 85 , etotal : 94 , min : 21 , max : 25 } ,
{ part : 6 , epart : 86 , etotal : 94 , min : 26 , max : 30 } ,
{ part : 7 , epart : 87 , etotal : 95 , min : 31 , max : 35 } ,
{ part : 8 , epart : 88 , etotal : 95 , min : 36 , max : 40 } ,
{ part : 9 , epart : 89 , etotal : 96 , min : 41 , max : 45 } ,
{ part : 10 , epart : 90 , etotal : 96 , min : 46 , max : 50 } ,
{ part : 11 , epart : 91 , etotal : 97 , min : 51 , max : 55 } ,
{ part : 12 , epart : 92 , etotal : 97 , min : 56 , max : 60 } ,
{ part : 13 , epart : 93 , etotal : 98 , min : 61 , max : 65 } ,
{ part : 14 , epart : 94 , etotal : 98 , min : 65 , max : 70 } ,
{ part : 15 , epart : 95 , etotal : 99 , min : 71 , max : 75 } ,
{ part : 16 , epart : 96 , etotal : 99 , min : 76 , max : 80 } ,
{ part : 17 , epart : 97 , etotal : 100 , min : 81 , max : 85 } ,
{ part : 18 , epart : 98 , etotal : 100 , min : 86 , max : 90 } ,
{ part : 19 , epart : 99 , etotal : 100 , min : 81 , max : 95 } ,
{ part : 20 , epart : 100 , etotal : 100 , min : 96 , max : 100 }
] ;
const reussites = [
2020-12-12 21:58:44 +01:00
{ code : "etotal" , isPart : false , isSign : false , isSuccess : false , isEchec : true , isEPart : true , isETotal : true , ptTache : - 4 , ptQualite : - 6 , quality : "Echec total" , condition : ( target , roll ) => roll >= target . etotal && roll <= 100 } ,
{ code : "epart" , isPart : false , isSign : false , isSuccess : false , isEchec : true , isEPart : true , isETotal : false , ptTache : - 2 , ptQualite : - 4 , quality : "Echec particulier" , condition : ( target , roll ) => ( roll >= target . epart && roll < target . etotal ) } ,
{ code : "echec" , isPart : false , isSign : false , isSuccess : false , isEchec : true , isEPart : false , isETotal : false , ptTache : 0 , ptQualite : - 2 , quality : "Echec normal" , condition : ( target , roll ) => ( roll > target . score && roll < target . etotal ) } ,
{ code : "norm" , isPart : false , isSign : false , isSuccess : true , isEchec : false , isEPart : false , isETotal : false , ptTache : 1 , ptQualite : 0 , quality : "Réussite normale" , condition : ( target , roll ) => ( roll > target . sign && roll <= target . score ) } ,
{ code : "sign" , isPart : false , isSign : true , isSuccess : true , isEchec : false , isEPart : false , isETotal : false , ptTache : 2 , ptQualite : 1 , quality : "Réussite significative" , condition : ( target , roll ) => ( roll > target . part && roll <= target . sign ) } ,
{ code : "part" , isPart : true , isSign : true , isSuccess : true , isEchec : false , isEPart : false , isETotal : false , ptTache : 3 , ptQualite : 2 , quality : "Réussite Particulière!" , condition : ( target , roll ) => ( roll > 0 && roll <= target . part ) } ,
{ code : "error" , isPart : false , isSign : false , isSuccess : false , isEchec : true , isEPart : true , isETotal : true , ptTache : 0 , ptQualite : 0 , quality : "Jet de dés invalide" , condition : ( target , roll ) => ( roll <= 0 || roll > 100 ) }
2020-11-12 16:35:51 +01:00
] ;
2020-12-05 00:04:40 +01:00
/* -------------------------------------------- */
2020-11-12 16:35:51 +01:00
const reussiteSignificative = reussites . find ( r => r . code == "sign" ) ;
const reussiteNormale = reussites . find ( r => r . code == "norm" ) ;
const echecNormal = reussites . find ( r => r . code == "echec" ) ;
2020-12-06 23:31:23 +01:00
const caracMaximumResolution = 60 ;
2020-12-05 00:04:40 +01:00
/* -------------------------------------------- */
2020-11-11 22:39:36 +01:00
export class RdDResolutionTable {
static resolutionTable = this . build ( )
/* -------------------------------------------- */
static build ( ) {
let table = [ ]
2020-12-06 23:31:23 +01:00
for ( var caracValue = 0 ; caracValue <= caracMaximumResolution ; caracValue ++ ) {
2020-11-25 00:46:11 +01:00
table [ caracValue ] = this . _computeRow ( caracValue ) ;
2020-11-11 22:39:36 +01:00
}
return table ;
}
2020-11-15 16:38:45 +01:00
/* -------------------------------------------- */
2020-11-16 03:54:43 +01:00
static getResultat ( code ) {
2020-11-14 03:16:03 +01:00
let resultat = reussites . filter ( r => code == r . code ) ;
2020-11-16 03:54:43 +01:00
if ( resultat == undefined ) {
2020-11-14 03:16:03 +01:00
resultat = reussites . find ( r => r . code == "error" ) ;
}
return resultat ;
}
2020-11-15 16:38:45 +01:00
2020-12-04 20:52:04 +01:00
/* -------------------------------------------- */
2020-11-16 03:54:43 +01:00
static explain ( rolled ) {
2020-12-06 19:31:11 +01:00
let message = "<br>Jet : <strong>" + rolled . roll + "</strong> sur " + rolled . score + "% " ;
2020-12-12 21:58:44 +01:00
if ( rolled . caracValue != null && rolled . finalLevel != null ) {
message += ( rolled . needSignificative ? "(significative sur " : "(" )
+ rolled . caracValue + " à " + Misc . toSignedString ( rolled . finalLevel ) + ") " ;
2020-11-16 03:54:43 +01:00
}
2020-12-06 23:31:23 +01:00
message += '<strong>' + rolled . quality + '</strong>'
2020-11-16 03:54:43 +01:00
return message ;
2020-12-18 23:48:41 +01:00
}
2020-11-16 03:54:43 +01:00
2020-12-18 23:48:41 +01:00
static explainRollData ( rollData ) {
let message = "<br>Difficultés <strong>libre: " + rollData . diffLibre + "</strong> / conditions: " + Misc . toSignedString ( rollData . diffConditions )
+ " / état: " + rollData . etat ;
message += RdDResolutionTable . explain ( rollData . rolled )
2020-12-18 23:57:28 +01:00
if ( rollData . selectedCarac == rollData . carac . volonte ) {
message += " / moral: " + rollData . moral ;
}
2020-12-18 23:48:41 +01:00
return message ;
2020-11-12 16:35:51 +01:00
}
2020-12-04 20:52:04 +01:00
2020-11-15 16:38:45 +01:00
/* -------------------------------------------- */
2020-12-12 21:58:44 +01:00
static async rollData ( rollData ) {
2020-12-17 00:44:32 +01:00
rollData . rolled = await this . roll ( rollData . caracValue , rollData . finalLevel , rollData . bonus , rollData . needSignificative , rollData . showDice ) ;
return rollData ;
2020-12-06 19:29:10 +01:00
}
2020-12-08 23:07:41 +01:00
2020-12-04 20:52:04 +01:00
/* -------------------------------------------- */
2020-12-12 21:58:44 +01:00
static async roll ( caracValue , finalLevel , bonus = undefined , needSignificative = undefined , showDice = true ) {
2020-11-25 00:46:11 +01:00
let chances = this . computeChances ( caracValue , finalLevel ) ;
2020-12-12 21:58:44 +01:00
this . _updateChancesWithBonus ( chances , bonus ) ;
this . _updateChancesNeedSignificative ( chances , needSignificative ) ;
2020-12-12 23:31:19 +01:00
chances . showDice = showDice ;
2020-12-12 21:58:44 +01:00
2020-11-21 09:10:31 +01:00
let rolled = await this . rollChances ( chances ) ;
2020-11-25 00:46:11 +01:00
rolled . caracValue = caracValue ;
2020-11-16 03:54:43 +01:00
rolled . finalLevel = finalLevel ;
2020-12-12 21:58:44 +01:00
rolled . bonus = bonus ;
rolled . needSignificative = needSignificative ;
2020-11-16 03:54:43 +01:00
return rolled ;
2020-11-15 16:38:45 +01:00
}
2020-11-12 16:35:51 +01:00
2020-12-12 21:58:44 +01:00
/* -------------------------------------------- */
static _updateChancesNeedSignificative ( chances , needSignificative ) {
if ( needSignificative ) {
let newScore = Math . floor ( Number ( chances . score ) / 2 ) ;
mergeObject ( chances , this . _computeCell ( null , newScore ) , { overwrite : true } ) ;
}
}
/* -------------------------------------------- */
static _updateChancesWithBonus ( chances , bonus ) {
if ( bonus ) {
let newScore = Number ( chances . score ) + Number ( bonus ) ;
mergeObject ( chances , this . _computeCell ( null , newScore ) , { overwrite : true } ) ;
}
}
2020-11-15 16:38:45 +01:00
/* -------------------------------------------- */
2020-11-21 09:10:31 +01:00
static async rollChances ( chances ) {
2020-12-04 20:52:04 +01:00
let myRoll = new Roll ( "1d100" ) . roll ( ) ;
2020-11-17 11:35:05 +01:00
myRoll . showDice = chances . showDice ;
2020-11-21 09:10:31 +01:00
await RdDDice . show ( myRoll ) ;
2020-11-15 16:38:45 +01:00
chances . roll = myRoll . total ;
2020-11-12 16:35:51 +01:00
mergeObject ( chances , this . _computeReussite ( chances , chances . roll ) ) ;
return chances ;
}
2020-11-15 16:38:45 +01:00
/* -------------------------------------------- */
2020-11-25 00:46:11 +01:00
static computeChances ( caracValue , difficulte ) {
2020-11-12 16:35:51 +01:00
if ( difficulte < - 16 ) {
return duplicate ( levelImpossible ) ;
}
if ( difficulte < - 10 ) {
return duplicate ( levelDown . find ( levelData => levelData . level == difficulte ) ) ;
}
2020-11-25 00:46:11 +01:00
return duplicate ( this . resolutionTable [ caracValue ] [ difficulte + 10 ] ) ;
2020-11-12 16:35:51 +01:00
}
2020-11-15 16:38:45 +01:00
/* -------------------------------------------- */
2020-12-12 21:58:44 +01:00
static isEchec ( rollData ) {
2020-12-15 02:20:24 +01:00
switch ( rollData . surprise ) {
case 'demi' : return ! rollData . rolled . isSign ;
case 'totale' : return true ;
}
return rollData . rolled . isEchec ;
2020-12-12 21:58:44 +01:00
}
2020-12-15 02:20:24 +01:00
2020-12-15 23:54:09 +01:00
/* -------------------------------------------- */
2020-12-12 21:58:44 +01:00
static isEchecTotal ( rollData ) {
2020-12-15 02:20:24 +01:00
if ( rollData . arme && rollData . surprise == 'demi' ) {
return rollData . rolled . isEchec ;
}
return rollData . rolled . isETotal ;
2020-12-12 21:58:44 +01:00
}
2020-12-18 23:48:41 +01:00
2020-12-15 23:54:09 +01:00
/* -------------------------------------------- */
2020-12-12 21:58:44 +01:00
static isParticuliere ( rollData ) {
2020-12-15 02:20:24 +01:00
if ( rollData . arme && rollData . surprise ) {
return false ;
}
return rollData . rolled . isPart ;
2020-12-12 21:58:44 +01:00
}
2020-12-15 02:20:24 +01:00
2020-12-15 23:54:09 +01:00
/* -------------------------------------------- */
2020-12-12 21:58:44 +01:00
static isReussite ( rollData ) {
2020-12-15 02:20:24 +01:00
switch ( rollData . surprise ) {
case 'demi' : return rollData . rolled . isSign ;
case 'totale' : return false ;
}
return rollData . rolled . isSuccess ;
2020-11-12 16:35:51 +01:00
}
/* -------------------------------------------- */
static _computeReussite ( chances , roll ) {
const reussite = reussites . find ( x => x . condition ( chances , roll ) ) ;
if ( chances . needSignificative ) {
if ( reussite . isSign ) {
return reussiteNormale ;
}
2020-11-13 15:40:53 +01:00
if ( reussite . isSuccess ) {
2020-11-12 16:35:51 +01:00
return echecNormal ;
}
}
return reussite ;
}
2020-11-16 23:28:18 +01:00
/* -------------------------------------------- */
2020-11-25 00:46:11 +01:00
static _computeRow ( caracValue ) {
2020-11-11 22:39:36 +01:00
let dataRow = [
2020-11-25 00:46:11 +01:00
this . _computeCell ( - 10 , Math . max ( Math . floor ( caracValue / 4 ) , 1 ) ) ,
this . _computeCell ( - 9 , Math . max ( Math . floor ( caracValue / 2 ) , 1 ) )
2020-11-11 22:39:36 +01:00
]
for ( var diff = - 8 ; diff <= 22 ; diff ++ ) {
2020-11-25 00:46:11 +01:00
dataRow [ diff + 10 ] = this . _computeCell ( diff , Math . max ( Math . floor ( caracValue * ( diff + 10 ) / 2 ) , 1 ) ) ;
2020-11-11 22:39:36 +01:00
}
return dataRow ;
}
2020-11-16 23:28:18 +01:00
/* -------------------------------------------- */
2020-11-12 16:35:51 +01:00
static _computeCell ( niveau , percentage ) {
2020-11-11 22:39:36 +01:00
return {
2020-11-12 16:35:51 +01:00
niveau : niveau ,
score : percentage ,
sign : this . _reussiteSignificative ( percentage ) ,
part : this . _reussitePart ( percentage ) ,
epart : this . _echecParticulier ( percentage ) ,
etotal : this . _echecTotal ( percentage )
2020-11-11 22:39:36 +01:00
}
}
2020-11-16 23:28:18 +01:00
/* -------------------------------------------- */
2020-11-11 22:39:36 +01:00
static _reussiteSignificative ( score ) {
return Math . floor ( score / 2 ) ;
}
2020-11-16 23:28:18 +01:00
/* -------------------------------------------- */
2020-11-11 22:39:36 +01:00
static _reussitePart ( score ) {
return Math . ceil ( score / 5 ) ;
}
2020-11-16 23:28:18 +01:00
/* -------------------------------------------- */
2020-11-11 22:39:36 +01:00
static _echecParticulier ( score ) {
return Math . ceil ( score / 5 ) + 80 ;
}
2020-11-16 23:28:18 +01:00
/* -------------------------------------------- */
2020-11-11 22:39:36 +01:00
static _echecTotal ( score ) {
return Math . ceil ( score / 10 ) + 91 ;
}
2020-12-15 23:54:09 +01:00
/* -------------------------------------------- */
2020-12-18 23:48:41 +01:00
static buildHTMLResults ( caracValue , levelValue ) {
let cell = this . computeChances ( caracValue , levelValue ) ;
2020-12-15 23:54:09 +01:00
let descr = $ ( '<span class="span-valeur competence-label">' ) ;
2020-12-18 23:48:41 +01:00
descr . append ( "Particulière : " + cell . part + " - Significative : " + cell . sign + " - Réussite : " + cell . score ) ;
descr . append ( "<br>Echec Particulier : " + cell . epart + " - Echec Total : " + cell . etotal ) ;
2020-12-15 23:54:09 +01:00
descr . append ( "</span>" ) ;
return descr ;
}
2020-11-11 22:39:36 +01:00
/* -------------------------------------------- */
2020-11-12 23:37:12 +01:00
static buildHTMLTableExtract ( caracValue , levelValue ) {
return this . buildHTMLTable ( caracValue , levelValue , caracValue - 2 , caracValue + 2 , levelValue - 5 , levelValue + 5 )
}
2020-11-11 22:39:36 +01:00
static buildHTMLTable ( caracValue , levelValue , minCarac = 1 , maxCarac = 21 , minLevel = - 10 , maxLevel = 11 ) {
return this . _buildHTMLTable ( caracValue , levelValue , minCarac , maxCarac , minLevel , maxLevel )
}
/* -------------------------------------------- */
static _buildHTMLTable ( caracValue , levelValue , minCarac , maxCarac , minLevel , maxLevel ) {
2020-11-14 15:57:42 +01:00
let countColonnes = maxLevel - minLevel ;
2020-11-11 22:39:36 +01:00
minCarac = Math . max ( minCarac , 1 ) ;
2020-12-06 23:31:23 +01:00
maxCarac = Math . min ( maxCarac , caracMaximumResolution ) ;
2020-11-11 22:39:36 +01:00
minLevel = Math . max ( minLevel , - 10 ) ;
2020-11-14 15:57:42 +01:00
maxLevel = Math . max ( Math . min ( maxLevel , 22 ) , minLevel + countColonnes ) ;
2020-11-11 22:39:36 +01:00
2020-11-12 23:37:12 +01:00
let table = $ ( "<table class='table-resolution'/>" )
2020-11-11 22:39:36 +01:00
. append ( this . _buildHTMLHeader ( this . resolutionTable [ 0 ] , minLevel , maxLevel ) ) ;
2020-11-25 23:17:13 +01:00
for ( var rowIndex = minCarac ; rowIndex <= maxCarac ; rowIndex ++ ) {
table . append ( this . _buildHTMLRow ( this . resolutionTable [ rowIndex ] , rowIndex , caracValue , levelValue , minLevel , maxLevel ) ) ;
2020-11-11 22:39:36 +01:00
}
2020-12-15 23:37:35 +01:00
table . append ( "</table>" ) ;
2020-11-11 22:39:36 +01:00
return table ;
}
2020-12-04 20:52:04 +01:00
/* -------------------------------------------- */
2020-11-11 22:39:36 +01:00
static _buildHTMLHeader ( dataRow , minLevel , maxLevel ) {
2020-11-12 23:37:12 +01:00
let tr = $ ( "<tr/>" ) ;
if ( minLevel > - 8 ) {
tr . append ( $ ( "<th class='table-resolution-level'/>" ) . text ( "-8" ) )
}
if ( minLevel > - 7 ) {
tr . append ( $ ( "<th class='table-resolution-level'/>" ) . text ( "..." ) ) ;
}
2020-11-14 15:57:42 +01:00
for ( let difficulte = minLevel ; difficulte <= maxLevel ; difficulte ++ ) {
2020-11-15 02:07:41 +01:00
tr . append ( $ ( "<th class='table-resolution-level'/>" ) . text ( Misc . toSignedString ( difficulte ) ) ) ;
2020-11-11 22:39:36 +01:00
}
return tr ;
}
2020-12-04 20:52:04 +01:00
/* -------------------------------------------- */
2020-11-11 22:39:36 +01:00
static _buildHTMLRow ( dataRow , rowIndex , caracValue , levelValue , minLevel , maxLevel ) {
2020-11-12 23:37:12 +01:00
let tr = $ ( "<tr/>" ) ;
let max = maxLevel ;
if ( minLevel > - 8 ) {
let score = dataRow [ - 8 + 10 ] . score ;
tr . append ( $ ( "<td class='table-resolution-carac'/>" ) . text ( score ) )
}
if ( minLevel > - 7 ) {
tr . append ( $ ( "<td/>" ) )
}
for ( let difficulte = minLevel ; difficulte <= max ; difficulte ++ ) {
let td = $ ( "<td/>" ) ;
2020-11-11 22:39:36 +01:00
let score = dataRow [ difficulte + 10 ] . score ;
if ( rowIndex == caracValue && levelValue == difficulte ) {
td . addClass ( 'table-resolution-target' ) ;
} else if ( difficulte == - 8 ) {
td . addClass ( 'table-resolution-carac' ) ;
}
tr . append ( td . text ( score ) ) ;
}
return tr ;
}
}