From c2c51fe8be2309e8dd1582519d2dd4ba30218c34 Mon Sep 17 00:00:00 2001 From: IM23a-bachmannj2 Date: Thu, 19 Sep 2024 09:35:45 +0200 Subject: [PATCH] changed the way how data.json is called --- app.py | 30 ------------------------------ static/populateTable.js | 28 ++++++++++++++++++++++++---- templates/calculator.html | 16 ++++++++++++++++ 3 files changed, 40 insertions(+), 34 deletions(-) diff --git a/app.py b/app.py index e638851..4e2db2e 100644 --- a/app.py +++ b/app.py @@ -3,42 +3,12 @@ import json app = Flask(__name__) -# Load the data from the JSON file -with open('static/json/data.json') as f: - guns = json.load(f) - @app.route('/') def index(): return render_template('index.html') -@app.route('/api/guns', methods=['GET']) -def get_guns(): - updated_guns = [] - for gun in guns: - original_damage = gun['damage'] - # Calculate RPS, DPS, DPM, avgDPS - rps = gun['rpm'] / 60 - dps = original_damage * rps - reload_time_min = gun['reload_time'] / 60 - dpm = (gun['mag_size'] * original_damage) / (reload_time_min + (gun['mag_size'] / gun['rpm'])) - avg_dps = dpm / 60 - total_damage = original_damage * gun['damage_multiplier'] - - # Add calculated stats to the gun data - gun.update({ - 'total_damage': total_damage, - 'rps': rps, - 'dps': dps, - 'dpm': dpm, - 'avg_dps': avg_dps - }) - updated_guns.append(gun) - - return jsonify(updated_guns) - - @app.route('/calculator') def calculator(): return render_template('calculator.html') diff --git a/static/populateTable.js b/static/populateTable.js index 5fb55e9..b8c35a6 100644 --- a/static/populateTable.js +++ b/static/populateTable.js @@ -1,8 +1,28 @@ document.addEventListener('DOMContentLoaded', () => { - fetch('/api/guns') + fetch('/static/json/data.json') // Fetch the JSON file directly .then(response => response.json()) .then(data => { - populateTable(data); + const updatedGuns = data.map(gun => { + const original_damage = gun['damage']; + const rps = gun['rpm'] / 60; + const dps = original_damage * rps; + const reload_time_min = gun['reload_time'] / 60; + const dpm = (gun['mag_size'] * original_damage) / (reload_time_min + (gun['mag_size'] / gun['rpm'])); + const avg_dps = dpm / 60; + const total_damage = original_damage * gun['damage_multiplier']; + + // Add the calculated stats to the gun object + return { + ...gun, + total_damage: total_damage, + rps: rps, + dps: dps, + dpm: dpm, + avg_dps: avg_dps + }; + }); + + populateTable(updatedGuns); }); }); @@ -13,7 +33,7 @@ function populateTable(data) { const row = table.insertRow(); row.insertCell(0).innerText = gun.id; row.insertCell(1).innerText = gun.name; - row.insertCell(2).innerText = gun.total_damage; + row.insertCell(2).innerText = gun.total_damage.toFixed(2); row.insertCell(3).innerText = gun.rpm; row.insertCell(4).innerText = gun.mag_size; row.insertCell(5).innerText = gun.reload_time; @@ -23,4 +43,4 @@ function populateTable(data) { row.insertCell(9).innerText = gun.dpm.toFixed(2); row.insertCell(10).innerText = gun.avg_dps.toFixed(2); }); -} \ No newline at end of file +} diff --git a/templates/calculator.html b/templates/calculator.html index 034a2c0..125850b 100644 --- a/templates/calculator.html +++ b/templates/calculator.html @@ -26,6 +26,22 @@ dropdownContent.style.display = "block"; } } + + // Collapsible functionality for perks, buffs, etc. + document.addEventListener('DOMContentLoaded', function () { + var coll = document.getElementsByClassName("collapsible-header"); + for (var i = 0; i < coll.length; i++) { + coll[i].addEventListener("click", function() { + this.classList.toggle("active"); + var content = this.nextElementSibling; + if (content.style.display === "block") { + content.style.display = "none"; + } else { + content.style.display = "block"; + } + }); + } + });