code cleanup

This commit is contained in:
Frederik Palmø 2022-04-07 01:57:32 +02:00
parent cfe9991ece
commit 70382ec02a

View File

@ -2,7 +2,7 @@ let WEAPONS;
let INFUSIONS; let INFUSIONS;
let CORRECTIONS; let CORRECTIONS;
let sort = "max"; let order = "max";
let ascending = true; let ascending = true;
async function init() { async function init() {
@ -13,26 +13,39 @@ async function init() {
} }
function update() { function update() {
// get all parameters // get parameters
let categories = [...document.getElementsByName("category")].filter(el => el.checked).map(el => el.id);
let upgraded = document.getElementById("max-upgrade").checked;
let requireStats = document.getElementById("requirements").checked; let requireStats = document.getElementById("requirements").checked;
let infIndex = Object.values(INFUSIONS).findIndex(inf => inf.id == sort); let twoHanding = document.getElementById("2handing").checked;
let allowedInfusions = [...document.getElementsByName("infusion")] let allowedInfusions = [...document.getElementsByName("infusion")]
.filter(elem => elem.checked) .filter(elem => elem.checked)
.map(elem => elem.value); .map(elem => elem.value);
let categories = [...document.getElementsByName("category")].filter(el => el.checked).map(el => el.id);
// get upgrade level
let upgraded = document.getElementById("max-upgrade").checked;
// get current stats
let stats = [...document.getElementsByName("stat")].map(el => parseInt(el.value)); let stats = [...document.getElementsByName("stat")].map(el => parseInt(el.value));
let twoHanding = document.getElementById("2handing").checked;
if (twoHanding) { if (twoHanding) {
stats[0] = Math.floor(stats[0] * 1.5); stats[0] = Math.floor(stats[0] * 1.5);
} }
// fill table let infIndex = Object.values(INFUSIONS).findIndex(inf => inf.id == order);
// update result table header to only include allowed infusions
[...document.getElementsByName("damage-result")].forEach(ty => {
ty.hidden = !allowedInfusions.includes(ty.id);
});
// clear table
let destination = document.getElementById("weapons"); let destination = document.getElementById("weapons");
destination.innerHTML = ""; // clear table destination.innerHTML = "";
// fill table
let template = document.getElementById("weapon"); let template = document.getElementById("weapon");
Object.values(WEAPONS) Object.values(WEAPONS)
.filter(weapon => { .filter(weapon => {
// filter out weapons that don't fit the current parameters
return ( return (
(weapon.requirements.every((stat, i) => stat <= stats[i]) || !requireStats) && (weapon.requirements.every((stat, i) => stat <= stats[i]) || !requireStats) &&
categories.includes(weapon.category) && categories.includes(weapon.category) &&
@ -40,16 +53,18 @@ function update() {
); );
}) })
.map(weapon => { .map(weapon => {
// calculate attack ratings for every allowed infusion as well as the maximum damage of any infusion
let attackRatings = Object.values(INFUSIONS) let attackRatings = Object.values(INFUSIONS)
.filter(inf => allowedInfusions.includes(inf.id)) .filter(inf => allowedInfusions.includes(inf.id))
.map(inf => { .map(inf => {
return weapon.infusions[inf.id] != null ? damage(weapon, inf.id, upgraded, stats) : 0; return weapon.infusions[inf.id] != null ? damage(weapon, INFUSIONS[inf.id], upgraded, stats) : 0;
}); });
let max = Math.max(0, ...attackRatings); let max = Math.max(0, ...attackRatings);
return [weapon, attackRatings, max]; return [weapon, attackRatings, max];
}) })
.sort(([_w1, ar1, m1], [_w2, ar2, m2]) => { .sort(([_w1, ar1, m1], [_w2, ar2, m2]) => {
// sort based on current sort order
if (infIndex == -1) { if (infIndex == -1) {
// sort by max // sort by max
return ascending ? m2 - m1 : m1 - m2; return ascending ? m2 - m1 : m1 - m2;
@ -58,10 +73,12 @@ function update() {
} }
}) })
.forEach(([weapon, attackRatings, max]) => { .forEach(([weapon, attackRatings, max]) => {
// clone and append table row to results with correct values
let clone = template.content.cloneNode(true); let clone = template.content.cloneNode(true);
let tr = clone.children[0]; let tr = clone.children[0];
tr.children[0].children[0].innerHTML = weapon.name; tr.children[0].children[0].innerHTML = weapon.name;
// add a link to the fextralife wiki
tr.children[0].children[0].href = tr.children[0].children[0].href =
weapon.id != "unarmed" weapon.id != "unarmed"
? "https://eldenring.wiki.fextralife.com/" + weapon.name.replaceAll(" ", "+") ? "https://eldenring.wiki.fextralife.com/" + weapon.name.replaceAll(" ", "+")
@ -69,36 +86,26 @@ function update() {
tr.children[1].innerHTML = max || "-"; tr.children[1].innerHTML = max || "-";
attackRatings.forEach((ar, i) => { attackRatings.forEach((ar, i) => {
tr.children[i + 2].innerHTML = attackRatings[i] || "-"; tr.children[i + 2].innerHTML = ar || "-";
}); });
destination.appendChild(clone); destination.appendChild(clone);
}); });
// update result table header to only include allowed infusions
[...document.getElementsByName("damage-result")].forEach(ty => {
ty.hidden = !allowedInfusions.includes(ty.id);
});
} }
function filterWeapons(stats, requireStats, allowedInfusions, damageTypes) { function setAll(name, state) {
let weapons = Object.values(WEAPONS).filter(weapon => { [...document.getElementsByName(name), ...document.getElementsByClassName(name)].forEach(el => (el.checked = state));
return allowedInfusions.some(inf => Object.values(weapon.infusions).includes(inf)); update();
});
if (requireStats) {
weapons = weapons.filter(weapon => weapon.requirements.every((stat, i) => stat <= stats[i]));
}
return weapons;
} }
function sortWeapons(weapons, infusionId, upgraded, stats) { function changeSort(newSort) {
return weapons.sort((a, b) => damage(b, infusionId, upgraded, stats) - damage(a, infusionId, upgraded, stats)); ascending = order == newSort ? !ascending : true;
order = newSort;
update();
} }
function damage(weapon, infusionId, upgraded, stats) { function damage(weapon, infusion, upgraded, stats) {
let weaponInfusion = weapon.infusions[infusionId]; let weaponInfusion = weapon.infusions[infusion.id];
let infusion = INFUSIONS[infusionId];
let upgradeLevel = upgraded ? (weapon.unique ? 10 : 25) : 0; let upgradeLevel = upgraded ? (weapon.unique ? 10 : 25) : 0;
let base = infusion.damage.map( let base = infusion.damage.map(
@ -107,22 +114,21 @@ function damage(weapon, infusionId, upgraded, stats) {
let scaling = stats.some((stat, i) => stat < weapon.requirements[i]) let scaling = stats.some((stat, i) => stat < weapon.requirements[i])
? base.map(dmg => dmg * -0.4) ? base.map(dmg => dmg * -0.4)
: base.map((amount, ty) => { : base.map((baseAmount, i) => {
let statCorrection = corrections( let statCorrection = corrections(
CORRECTIONS[weaponInfusion.corrections[ty]], CORRECTIONS[weaponInfusion.corrections[i]],
stats, stats,
weaponInfusion.masks[ty], weaponInfusion.masks[i],
); );
let statScaling = weaponInfusion.scaling.map(itemScaling => { let statScaling = weaponInfusion.scaling.map(itemScaling => {
return ( return (
itemScaling * infusion.scaling[ty] + itemScaling * infusion.scaling[i] +
itemScaling * infusion.scaling[ty] * infusion.growth[ty] * upgradeLevel itemScaling * infusion.scaling[i] * infusion.growth[i] * upgradeLevel
); );
}); });
let scaling = statScaling.map((statScaling, statIndex) => { return statScaling
return (amount * statScaling * statCorrection[statIndex]) / 100.0; .map((scaling, statIndex) => (baseAmount * scaling * statCorrection[statIndex]) / 100.0)
}); .reduce((sum, n) => sum + n);
return scaling.reduce((sum, n) => sum + n);
}); });
return Math.floor(base.reduce((sum, n) => sum + n) + scaling.reduce((sum, n) => sum + n)); return Math.floor(base.reduce((sum, n) => sum + n) + scaling.reduce((sum, n) => sum + n));
@ -130,7 +136,7 @@ function damage(weapon, infusionId, upgraded, stats) {
function corrections(calc, stats, masks) { function corrections(calc, stats, masks) {
return stats.map((stat, ty) => { return stats.map((stat, ty) => {
if (masks[ty] == 0) { if (!masks[ty]) {
return 0.0; return 0.0;
} }
@ -141,25 +147,8 @@ function corrections(calc, stats, masks) {
let growthDelta = (calc.growth[ty][capIndex + 1] || growth) - growth; let growthDelta = (calc.growth[ty][capIndex + 1] || growth) - growth;
let adjust = calc.adjustments[ty][capIndex]; let adjust = calc.adjustments[ty][capIndex];
if (Math.sign(adjust) != -1) { return Math.sign(adjust) != -1
return growth + growthDelta * ((stat - cap) / capDelta) ** adjust; ? growth + growthDelta * ((stat - cap) / capDelta) ** adjust
} else { : growth + growthDelta * (1 - (1 - (stat - cap) / capDelta) ** Math.abs(adjust));
return growth + growthDelta * (1 - (1 - (stat - cap) / capDelta) ** Math.abs(adjust));
}
}); });
} }
function setAll(name, state) {
[...document.getElementsByName(name), ...document.getElementsByClassName(name)].forEach(el => (el.checked = state));
update();
}
function changeSort(newSort) {
if (sort == newSort) {
ascending = !ascending;
} else {
ascending = true;
}
sort = newSort;
update();
}