fvtt-rolemaster-frp/module/rmfrp-process-table.js

51 lines
1.7 KiB
JavaScript
Raw Normal View History

export class RMFRPProcessTable {
constructor() {
// Load a reference to the rmfrp-tables.json file
this.rmfrpTables = require('./rmfrp-tables.json');
// Loop thru the tables and create a definition object, with key, name and min/ax value of each table
this.rmfrpTablesDef = {}
for (let table in this.rmfrpTables) {
let tableData = this.rmfrpTables[table]
// Search min and max values in the tableData
let min = Number.MAX_SAFE_INTEGER;
let max = Number.MIN_SAFE_INTEGER;
tableData.forEach((element) => {
if (element.result) {
if (Number(element.result)) {
min = Math.min(min, Number(element.result));
max = Math.max(max, Number(element.result));
} else {
let range = element.result.split("-");
min = Math.min(min, Number(range[0]));
max = Math.max(max, Number(range[1]));
}
}
});
this.rmfrpTablesDef[table] = {
key: table,
name: RMFRPUtility.capitalizeFirstLetters(table.replace(/_/g, " ")),
min: min,
max: max
};
}
super();
}
// Get roll result from table
getAttackRollResult(table, roll, armorValue) {
let result = this.rmfrpTables[table].find((element) => {
if (Number(element.result) && Number(element.result) == Number(roll)) {
return element[String(armorValue)];
} else {
// SPlit the result into a range
let range = element.result.split("-");
if (Number(roll) >= Number(range[0]) && Number(roll) <= Number(range[1])) {
return element[String(armorValue)];
}
}
});
return undefined;
}
}