diff --git a/src/script/weapons.js b/src/script/weapons.js index 4f0a276..4c239a3 100644 --- a/src/script/weapons.js +++ b/src/script/weapons.js @@ -1,77 +1,100 @@ -const WEAPONS = fetch("/data/weapons.json") - .then(response => response.json()) - .catch(error => console.log(error)); -const INFUSIONS = fetch("/data/infusions.json") - .then(response => response.json()) - .catch(error => console.log(error)); -const CORRECTIONS = fetch("/data/damage.json") - .then(response => response.json()) - .catch(error => console.log(error)); - -let weapons; -let infusions; -let corrections; - -const sum = (a, b) => a + b; +let WEAPONS; +let INFUSIONS; +let CORRECTIONS; async function init() { - weapons = await WEAPONS; - infusions = await INFUSIONS; - corrections = await CORRECTIONS; - + WEAPONS = await fetch("/data/weapons.json").then((response) => response.json()); + INFUSIONS = await fetch("/data/infusions.json").then((response) => response.json()); + CORRECTIONS = await fetch("/data/damage.json").then((response) => response.json()); update(); } -async function update() { - let infusionId = [...document.getElementsByName("infusion")].find(radio => radio.checked).id; - let upgradeLevel = 25; +function update() { + // get information from document + let allowedInfusions = [ + "standard", + ...[...document.getElementsByName("infusion")].filter((elem) => elem.checked).map((elem) => elem.id), + ]; - let stats = [...document.getElementsByName("stat")].map(el => parseInt(el.value)); + let upgraded = document.getElementById("max-upgrade").checked; + let requireStats = document.getElementById("requirements").checked; + let damageTypes = [...document.getElementsByName("damage-type")].map((el) => el.checked); - let sorted = sortWeapons(infusionId, upgradeLevel, stats); + let stats = [...document.getElementsByName("stat")].map((el) => parseInt(el.value)); + let twoHanding = document.getElementById("2handing").checked; + if (twoHanding) { + stats[0] = Math.floor(stats[0] * 1.5); + } + + let filtered = filterWeapons(stats, requireStats, allowedInfusions, damageTypes); + let sorted = sortWeapons(filtered, "standard", upgraded, stats); + + // show sorted list + let destination = document.getElementById("weapons"); + let template = document.getElementById("weaponTemplate"); + sorted.forEach((weapon) => { + let clone = template.content.cloneNode(true); + + let tr = clone.children[0]; + tr.children[0].innerHTML = weapon.name; + tr.children[1].innerHTML = weapon.damage; + + destination.appendChild(clone); + }); console.log(sorted); - - console.log(sorted.find(item => item.id == "royal-greatsword")) } -function sortWeapons(infusionId, upgradeLevel, stats) { - return Object.values(weapons).sort((a, b) => damage(b, infusionId, upgradeLevel, stats) - damage(a, infusionId, upgradeLevel, stats)); +function filterWeapons(stats, requireStats, allowedInfusions, damageTypes) { + let weapons = Object.values(WEAPONS).filter((weapon) => { + return allowedInfusions.some((inf) => Object.values(weapon.infusions).includes(inf)); + }); + if (requireStats) { + weapons = weapons.filter((weapon) => weapon.requirements.every((stat, i) => stat <= stats[i])); + } + + return weapons; } -function damage(weapon, infusionId, upgradeLevel, stats) { - if (weapon.total != undefined && weapon.total != null) { - return weapon.total; +function sortWeapons(weapons, infusionId, upgraded, stats) { + return weapons.sort((a, b) => damage(b, infusionId, upgraded, stats) - damage(a, infusionId, upgraded, stats)); +} + +function damage(weapon, infusionId, upgraded, stats) { + if (weapon.damage != undefined) { + return weapon.damage; } let weaponInfusion = weapon.infusions[infusionId]; - let infusion = infusions[infusionId]; - - let bases = infusion.damage.map((amount, ty) => weaponInfusion.damage[ty] * (amount + infusion.upgrade[ty] * upgradeLevel)); + let infusion = INFUSIONS[infusionId]; + let upgradeLevel = upgraded ? (weapon.unique ? 10 : 25) : 0; + let bases = infusion.damage.map( + (amount, ty) => weaponInfusion.damage[ty] * (amount + infusion.upgrade[ty] * upgradeLevel) + ); let extras; if (stats.some((stat, i) => stat <= weapon.requirements[i])) { - extras = bases.map(dmg => dmg * -0.4); + extras = bases.map((dmg) => dmg * -0.4); } else { extras = bases.map((amount, ty) => { - let calc = corrections[weaponInfusion.corrections[ty]]; + let calc = CORRECTIONS[weaponInfusion.corrections[ty]]; let correction = typeCorrections(calc, stats, weaponInfusion.masks[ty]); - let scalings = weaponInfusion.scaling.map(itemScaling => { - return (itemScaling * infusion.scaling[ty] + itemScaling * infusion.scaling[ty] * infusion.growth[ty] * upgradeLevel); - }) + let scalings = weaponInfusion.scaling.map((itemScaling) => { + return ( + itemScaling * infusion.scaling[ty] + + itemScaling * infusion.scaling[ty] * infusion.growth[ty] * upgradeLevel + ); + }); let extras = scalings.map((statScaling, statIndex) => { - return amount * statScaling * correction[statIndex] / 100.0; - }) - return extras.reduce(sum); + return (amount * statScaling * correction[statIndex]) / 100.0; + }); + return extras.reduce((sum, n) => sum + n); }); } - let total = Math.floor(bases.reduce(sum) + extras.reduce(sum)); + let damage = Math.floor(bases.reduce((sum, n) => sum + n) + extras.reduce((sum, n) => sum + n)); + weapon.damage = damage; - weapon.bases = bases; - weapon.extras = extras; - weapon.total = total; - - return total; + return damage; } function typeCorrections(calc, stats, masks) { @@ -81,7 +104,7 @@ function typeCorrections(calc, stats, masks) { return 0.0; } - let capIndex = calc.softcaps[ty].findIndex(cap => cap >= stat) - 1; + let capIndex = calc.softcaps[ty].findIndex((cap) => cap >= stat) - 1; let cap = calc.softcaps[ty][capIndex]; let capDelta = (calc.softcaps[ty][capIndex + 1] || cap) - cap; let growth = calc.growth[ty][capIndex]; @@ -91,7 +114,7 @@ function typeCorrections(calc, stats, masks) { if (Math.sign(adjust) != -1) { return growth + growthDelta * ((stat - cap) / capDelta) ** adjust; } else { - return growth + growthDelta * (1 - (1 - ((stat - cap) / capDelta)) ** Math.abs(adjust)); + return growth + growthDelta * (1 - (1 - (stat - cap) / capDelta) ** Math.abs(adjust)); } - }) -} \ No newline at end of file + }); +}