Cleanup roll windows
- permettre plusieurs fenêtres de jets en même temps en éliminant les id dans le html et les jquery sur id pour éviter les interactions - génération de la table par handlebars
This commit is contained in:
@ -14,7 +14,7 @@ const levelDown = [
|
||||
{ level: -15, score: 1, norm: 1, sign: 0, part: 0, epart: 2, etotal: 10 },
|
||||
{ level: -16, score: 1, norm: 1, sign: 0, part: 0, epart: 0, etotal: 2 }
|
||||
];
|
||||
const levelImpossible = { score: 0, norm:0, sign: 0, part: 0, epart: 0, etotal: 1 };
|
||||
const levelImpossible = { score: 0, norm: 0, sign: 0, part: 0, epart: 0, etotal: 1 };
|
||||
|
||||
const reussites = [
|
||||
{ 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 },
|
||||
@ -42,6 +42,44 @@ export class RdDResolutionTable {
|
||||
return table;
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
static computeChances(carac, level) {
|
||||
if (level < -16) {
|
||||
return levelImpossible;
|
||||
}
|
||||
if (level < -10) {
|
||||
return levelDown.find(it => it.level == level);
|
||||
}
|
||||
const percentage = RdDResolutionTable.computePercentage(carac, level);
|
||||
return this._computeCell(level, percentage);
|
||||
}
|
||||
|
||||
|
||||
/* -------------------------------------------- */
|
||||
static _computeRow(caracValue) {
|
||||
let dataRow = [
|
||||
this._computeCell(-10, Math.max(Math.floor(caracValue / 4), 1)),
|
||||
this._computeCell(-9, Math.max(Math.floor(caracValue / 2), 1))
|
||||
]
|
||||
for (var diff = -8; diff <= 22; diff++) {
|
||||
dataRow[diff + 10] = this._computeCell(diff, RdDResolutionTable.computePercentage(caracValue, diff));
|
||||
}
|
||||
return dataRow;
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
static _computeCell(niveau, percentage) {
|
||||
return {
|
||||
niveau: niveau,
|
||||
score: percentage,
|
||||
norm: Math.min(99, percentage),
|
||||
sign: this._reussiteSignificative(percentage),
|
||||
part: this._reussitePart(percentage),
|
||||
epart: this._echecParticulier(percentage),
|
||||
etotal: this._echecTotal(percentage)
|
||||
};
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
static getResultat(code) {
|
||||
let resultat = reussites.find(r => code == r.code);
|
||||
@ -82,8 +120,8 @@ export class RdDResolutionTable {
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
static async roll(caracValue, finalLevel, rollData = {}){
|
||||
let chances = this.computeChances(caracValue, finalLevel);
|
||||
static async roll(caracValue, finalLevel, rollData = {}) {
|
||||
let chances = duplicate(this.computeChances(caracValue, finalLevel));
|
||||
this._updateChancesWithBonus(chances, rollData.bonus, finalLevel);
|
||||
this._updateChancesFactor(chances, rollData.diviseurSignificative);
|
||||
chances.showDice = rollData.showDice;
|
||||
@ -95,7 +133,7 @@ export class RdDResolutionTable {
|
||||
rolled.bonus = rollData.bonus;
|
||||
rolled.factorHtml = Misc.getFractionHtml(rollData.diviseurSignificative);
|
||||
|
||||
if (ReglesOptionelles.isUsing("afficher-colonnes-reussite")){
|
||||
if (ReglesOptionelles.isUsing("afficher-colonnes-reussite")) {
|
||||
rolled.niveauNecessaire = this.findNiveauNecessaire(caracValue, rolled.roll);
|
||||
rolled.ajustementNecessaire = rolled.niveauNecessaire - finalLevel;
|
||||
}
|
||||
@ -103,13 +141,24 @@ export class RdDResolutionTable {
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
static findNiveauNecessaire(caracValue, rollValue) {
|
||||
for (let cell of this.resolutionTable[caracValue]) {
|
||||
if ( rollValue <= cell.norm) {
|
||||
return cell.niveau;
|
||||
}
|
||||
static findNiveauNecessaire(carac, rolled) {
|
||||
if (carac == 0) {
|
||||
return NaN;
|
||||
}
|
||||
return 16; // Dummy default
|
||||
if (rolled >= carac){
|
||||
const upper = Math.ceil(rolled/carac);
|
||||
return 2*upper -10
|
||||
}
|
||||
if (rolled > Math.floor(carac/2)) {
|
||||
return -8
|
||||
}
|
||||
if (rolled > Math.floor(carac/4)) {
|
||||
return -9
|
||||
}
|
||||
if (rolled > 1) {
|
||||
return -10
|
||||
}
|
||||
return -11;
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
@ -122,7 +171,7 @@ export class RdDResolutionTable {
|
||||
|
||||
/* -------------------------------------------- */
|
||||
static _updateChancesWithBonus(chances, bonus, finalLevel) {
|
||||
if (bonus && finalLevel>-11) {
|
||||
if (bonus && finalLevel > -11) {
|
||||
let newScore = Number(chances.score) + bonus;
|
||||
mergeObject(chances, this._computeCell(undefined, newScore), { overwrite: true });
|
||||
}
|
||||
@ -142,21 +191,19 @@ export class RdDResolutionTable {
|
||||
|
||||
/* -------------------------------------------- */
|
||||
static async rollChances(chances, diviseur, forceDiceResult = -1) {
|
||||
chances.forceDiceResult = forceDiceResult <= 0 || forceDiceResult > 100 ? undefined : {total: forceDiceResult};
|
||||
chances.roll = await RdDDice.rollTotal( "1d100", chances);
|
||||
chances.forceDiceResult = forceDiceResult <= 0 || forceDiceResult > 100 ? undefined : { total: forceDiceResult };
|
||||
chances.roll = await RdDDice.rollTotal("1d100", chances);
|
||||
mergeObject(chances, this.computeReussite(chances, chances.roll, diviseur), { overwrite: true });
|
||||
return chances;
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
static computeChances(caracValue, difficulte) {
|
||||
if (difficulte < -16) {
|
||||
return duplicate(levelImpossible);
|
||||
}
|
||||
if (difficulte < -10) {
|
||||
return duplicate(levelDown.find(levelData => levelData.level == difficulte));
|
||||
}
|
||||
return duplicate(RdDResolutionTable.resolutionTable[caracValue][difficulte + 10]);
|
||||
static computePercentage(carac, diff) {
|
||||
if (diff < -16) return 0
|
||||
if (diff < -10) return 1
|
||||
if (diff == -10) return Math.max(Math.floor(carac / 4), 1)
|
||||
if (diff == -9) return Math.max(Math.floor(carac / 2), 1)
|
||||
return Math.max(Math.floor(carac * (diff + 10) / 2), 1);
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
@ -213,31 +260,6 @@ export class RdDResolutionTable {
|
||||
return reussite;
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
static _computeRow(caracValue) {
|
||||
let dataRow = [
|
||||
this._computeCell(-10, Math.max(Math.floor(caracValue / 4), 1)),
|
||||
this._computeCell(-9, Math.max(Math.floor(caracValue / 2), 1))
|
||||
]
|
||||
for (var diff = -8; diff <= 22; diff++) {
|
||||
dataRow[diff + 10] = this._computeCell(diff, Math.max(Math.floor(caracValue * (diff + 10) / 2), 1));
|
||||
}
|
||||
return dataRow;
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
static _computeCell(niveau, percentage) {
|
||||
return {
|
||||
niveau: niveau,
|
||||
score: percentage,
|
||||
norm: Math.min(99, percentage),
|
||||
sign: this._reussiteSignificative(percentage),
|
||||
part: this._reussitePart(percentage),
|
||||
epart: this._echecParticulier(percentage),
|
||||
etotal: this._echecTotal(percentage)
|
||||
};
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
static _reussiteSignificative(percentage) {
|
||||
return Math.floor(percentage / 2);
|
||||
@ -261,92 +283,34 @@ export class RdDResolutionTable {
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
static buildHTMLResults(caracValue, levelValue) {
|
||||
if (caracValue == undefined || isNaN(caracValue)) caracValue = 10;
|
||||
if (levelValue == undefined || isNaN(levelValue)) levelValue = 0;
|
||||
|
||||
let cell = this.computeChances(caracValue, levelValue);
|
||||
cell.epart = cell.epart > 99 ? 'N/A' : cell.epart;
|
||||
cell.etotal = cell.etotal > 100 ? 'N/A' : cell.etotal;
|
||||
cell.score = Math.min(cell.score, 99);
|
||||
|
||||
return `
|
||||
<span class="table-proba-reussite competence-label">
|
||||
Particulière: <span class="rdd-roll-part">${cell.part}</span>
|
||||
- Significative: <span class="rdd-roll-sign">${cell.sign}</span>
|
||||
- Réussite: <span class="rdd-roll-norm">${cell.score}</span>
|
||||
- Echec Particulier: <span class="rdd-roll-epart">${cell.epart}</span>
|
||||
- Echec Total: <span class="rdd-roll-etotal">${cell.etotal}</span>
|
||||
</span>
|
||||
`
|
||||
static subTable(carac, level, delta = { carac: 2, level: 5}) {
|
||||
return {
|
||||
carac,
|
||||
level,
|
||||
minCarac: carac - (delta?.carac ?? 2),
|
||||
maxCarac: carac + (delta?.carac ?? 2),
|
||||
minLevel: level - (delta?.level ?? 5),
|
||||
maxLevel: level + (delta?.level ?? 5)
|
||||
};
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
static buildHTMLTableExtract(caracValue, levelValue) {
|
||||
return this.buildHTMLTable(caracValue, levelValue, caracValue - 2, caracValue + 2, levelValue - 5, levelValue + 5)
|
||||
}
|
||||
|
||||
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) {
|
||||
let countColonnes = maxLevel - minLevel;
|
||||
static async buildHTMLTable({ carac: carac, level: level, minCarac = 1, maxCarac = 21, minLevel = -10, maxLevel = 11 }) {
|
||||
let colonnes = maxLevel - minLevel;
|
||||
minCarac = Math.max(minCarac, 1);
|
||||
maxCarac = Math.min(maxCarac, caracMaximumResolution);
|
||||
maxCarac = Math.min(maxCarac, minCarac + 20);
|
||||
minLevel = Math.max(minLevel, -10);
|
||||
maxLevel = Math.max(Math.min(maxLevel, 22), minLevel + countColonnes);
|
||||
|
||||
let table = $("<table class='table-resolution'/>")
|
||||
.append(this._buildHTMLHeader(RdDResolutionTable.resolutionTable[0], minLevel, maxLevel));
|
||||
|
||||
for (var rowIndex = minCarac; rowIndex <= maxCarac; rowIndex++) {
|
||||
table.append(this._buildHTMLRow(RdDResolutionTable.resolutionTable[rowIndex], rowIndex, caracValue, levelValue, minLevel, maxLevel));
|
||||
}
|
||||
table.append("</table>");
|
||||
return table;
|
||||
maxLevel = Math.max(Math.min(maxLevel, 30), minLevel + colonnes);
|
||||
return await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/resolution-table.html', {
|
||||
carac: carac,
|
||||
difficulte: level,
|
||||
min: minLevel,
|
||||
rows: RdDResolutionTable.incrementalArray(minCarac, maxCarac),
|
||||
cols: RdDResolutionTable.incrementalArray(minLevel, maxLevel)
|
||||
});
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
static _buildHTMLHeader(dataRow, minLevel, maxLevel) {
|
||||
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("..."));
|
||||
}
|
||||
for (let difficulte = minLevel; difficulte <= maxLevel; difficulte++) {
|
||||
tr.append($("<th class='table-resolution-level'/>").text(Misc.toSignedString(difficulte)));
|
||||
}
|
||||
return tr;
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
static _buildHTMLRow(dataRow, rowIndex, caracValue, levelValue, minLevel, maxLevel) {
|
||||
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/>");
|
||||
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;
|
||||
static incrementalArray(min, max) {
|
||||
return Array.from(Array(max-min+1).keys()).map(i=>i+min)
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user