foundryvtt-reve-de-dragon/module/rdd-resolution-table.js
Vincent Vandemeulebrouck bfce6f59df Extrait table resolution
(cherry picked from commit a11ce394ce0d5e598da61c303c0665b1eadc8513)
2020-11-12 13:43:15 +01:00

99 lines
3.0 KiB
JavaScript

export class RdDResolutionTable {
static resolutionTable = this.build()
/* -------------------------------------------- */
static build() {
let table = []
for (var carac = 0; carac <= 30; carac++) {
table[carac] = this._computeRow(carac);
}
return table;
}
/* -------------------------------------------- */
static _computeRow(carac) {
let dataRow = [
this._computeScore(-10, Math.max(Math.floor(carac / 4), 1)),
this._computeScore(-9, Math.max(Math.floor(carac / 2), 1))
]
for (var diff = -8; diff <= 22; diff++) {
dataRow[diff + 10] = this._computeScore(diff, Math.max(Math.floor(carac * (diff + 10) / 2), 1));
}
return dataRow;
}
static _computeScore(diff, score) {
return {
niveau: diff,
score: score,
sign: this._reussiteSignificative(score),
part: this._reussitePart(score),
epart: this._echecParticulier(score),
etotal: this._echecTotal(score)
}
}
static _reussiteSignificative(score) {
return Math.floor(score / 2);
}
static _reussitePart(score) {
return Math.ceil(score / 5);
}
static _echecParticulier(score) {
return Math.ceil(score / 5) + 80;
}
static _echecTotal(score) {
return Math.ceil(score / 10) + 91;
}
/* -------------------------------------------- */
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) {
minCarac = Math.max(minCarac, 1);
maxCarac = Math.min(maxCarac, 30);
minLevel = Math.max(minLevel, -10);
maxLevel = Math.min(maxLevel, 22);
var table = $("<table class='table-resolution'/>")
.append(this._buildHTMLHeader(this.resolutionTable[0], minLevel, maxLevel));
for (var carac = minCarac; carac <= maxCarac; carac++) {
table.append(this._buildHTMLRow(this.resolutionTable[carac], carac, caracValue, levelValue, minLevel, maxLevel));
}
return table;
}
static _buildHTMLHeader(dataRow, minLevel, maxLevel) {
var tr = $("<tr/>");
for (var difficulte = minLevel; difficulte <= maxLevel; difficulte++) {
const niveau = dataRow[difficulte + 10].niveau;
const txt = (niveau > 0 ? "+" : "") + niveau;
tr.append($("<th class='table-resolution-level'/>").text(txt));
}
return tr;
}
static _buildHTMLRow(dataRow, rowIndex, caracValue, levelValue, minLevel, maxLevel) {
var tr = $("<tr/>");
for (var difficulte = minLevel; difficulte <= maxLevel; difficulte++) {
var 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;
}
}