Files
dsabot/functions/CompareResults.js
TobenderZephyr c6cacdae5e switched nedb to nedb-promises (#37)
* breaking change: switched to nedb-promises
* Linting
* some dev dependencies
* more tests
* updated package version
* bug fixing
* changed isNaN to isString
* (fix) args[0]
2021-05-03 18:39:33 +02:00

48 lines
1.4 KiB
JavaScript

/**
* Compares each item inside an array Throws
* with corresponding AttributeLevels (With added bonus)
*
* @param {Array} Throws=[]
* @param {Array} AttributeLevels=[8,8,8]
* @param {BigInt} Bonus=0
* @param {BigInt} PointsRemaining=0
*/
const CompareResults = (Throws = [], AttributeLevels = [8, 8, 8], Bonus = 0, Points = 0) => {
let Passed = 0;
let Fumbles = 0;
let CriticalHit = 0;
let PointsRemaining = Points;
const AllPointsUsed = [];
Throws.forEach((Throw, key) => {
let PointsUsed = 0;
const AttributeLevel = AttributeLevels.find((v, k) => key === k);
if (Math.floor(AttributeLevel + Bonus) >= Throw) {
Passed += 1;
} else if (Math.floor(AttributeLevel + PointsRemaining + Bonus) >= Throw) {
Passed += 1;
PointsUsed = Throw - Bonus - AttributeLevel;
PointsRemaining -= PointsUsed;
} else {
PointsUsed = PointsRemaining;
PointsRemaining -= PointsUsed;
}
if (Throw === 1) {
CriticalHit += 1;
}
if (Throw === 20) {
Fumbles += 1;
}
AllPointsUsed.push(PointsUsed);
});
return {
Passed: Passed,
CriticalHit: CriticalHit,
Fumbles: Fumbles,
PointsUsed: AllPointsUsed,
PointsRemaining: PointsRemaining,
};
};
module.exports = { CompareResults };