2024-08-23 11:45:23 +02:00
|
|
|
import { RMFRPUtility } from "./rmfrp-utility.js";
|
|
|
|
import { RMFRP_ATTACK_TABLES } from "./rmfrp-attack-tables.js";
|
|
|
|
|
2024-08-15 22:52:36 +02:00
|
|
|
export class RMFRPProcessTable {
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
// Load a reference to the rmfrp-tables.json file
|
2024-08-23 11:45:23 +02:00
|
|
|
this.rmfrpTables = RMFRP_ATTACK_TABLES
|
2024-08-15 22:52:36 +02:00
|
|
|
// Loop thru the tables and create a definition object, with key, name and min/ax value of each table
|
|
|
|
this.rmfrpTablesDef = {}
|
2024-08-23 11:45:23 +02:00
|
|
|
|
2024-08-15 22:52:36 +02:00
|
|
|
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;
|
2024-08-23 11:45:23 +02:00
|
|
|
let minKey = "";
|
2024-08-15 22:52:36 +02:00
|
|
|
let max = Number.MIN_SAFE_INTEGER;
|
2024-08-23 11:45:23 +02:00
|
|
|
let maxKey = "";
|
|
|
|
for (let resultData of tableData) {
|
|
|
|
let element = resultData["result"];
|
|
|
|
if (Number(element.result)) {
|
|
|
|
let value = Number(element.result)
|
|
|
|
if (value > max) {
|
|
|
|
max = value
|
|
|
|
maxKey = element.result
|
|
|
|
}
|
|
|
|
if (value < min) {
|
|
|
|
min = value
|
|
|
|
minKey = element.result
|
2024-08-15 22:52:36 +02:00
|
|
|
} else {
|
|
|
|
let range = element.result.split("-");
|
2024-08-23 11:45:23 +02:00
|
|
|
if (Number(range[0]) > max) {
|
|
|
|
max = Number(range[0])
|
|
|
|
maxKey = element.result
|
|
|
|
}
|
|
|
|
if (Number(range[1]) < min) {
|
|
|
|
min = Number(range[1])
|
|
|
|
minKey = element.result
|
|
|
|
}
|
2024-08-15 22:52:36 +02:00
|
|
|
}
|
|
|
|
}
|
2024-08-23 11:45:23 +02:00
|
|
|
}
|
2024-08-15 22:52:36 +02:00
|
|
|
this.rmfrpTablesDef[table] = {
|
|
|
|
key: table,
|
|
|
|
name: RMFRPUtility.capitalizeFirstLetters(table.replace(/_/g, " ")),
|
|
|
|
min: min,
|
2024-08-23 11:45:23 +02:00
|
|
|
minKey: minKey,
|
|
|
|
max: max,
|
|
|
|
maxKey: maxKey
|
2024-08-15 22:52:36 +02:00
|
|
|
};
|
|
|
|
}
|
2024-08-23 11:45:23 +02:00
|
|
|
// Sort rmfrpTablesDef by name
|
|
|
|
this.rmfrpTablesDef = Object.values(this.rmfrpTablesDef).sort((a, b) => a.name.localeCompare(b.name))
|
|
|
|
}
|
|
|
|
|
|
|
|
getTableDef() {
|
|
|
|
return this.rmfrpTablesDef;
|
2024-08-15 22:52:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get roll result from table
|
2024-08-23 11:45:23 +02:00
|
|
|
getAttackRollResult(tableKey, roll, armorValue) {
|
|
|
|
roll = Number(roll);
|
|
|
|
if (isNaN(roll)) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
let table = this.rmfrpTables[tableKey]
|
|
|
|
let tableDef = this.rmfrpTablesDef[tableKey]
|
|
|
|
if (!table || !tableDef) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check min and max values
|
|
|
|
if (roll < tableDef.min) {
|
|
|
|
// return the min value
|
|
|
|
return table.find((element) => {
|
|
|
|
let elem = element.result == tableDef.minKey;
|
|
|
|
return elem[String(armorValue)];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (roll > tableDef.max) {
|
|
|
|
// return the min value
|
|
|
|
return table.find((element) => {
|
|
|
|
let elem = element.result == tableDef.maxKey;
|
|
|
|
return elem[String(armorValue)];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return table.find((element) => {
|
2024-08-15 22:52:36 +02:00
|
|
|
if (Number(element.result) && Number(element.result) == Number(roll)) {
|
|
|
|
return element[String(armorValue)];
|
|
|
|
} else {
|
2024-08-23 11:45:23 +02:00
|
|
|
// Split the result into a range
|
2024-08-15 22:52:36 +02:00
|
|
|
let range = element.result.split("-");
|
|
|
|
if (Number(roll) >= Number(range[0]) && Number(roll) <= Number(range[1])) {
|
|
|
|
return element[String(armorValue)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|