From 420a0dc497a40c3ade4bff1e37cf233bdbf2fd1e Mon Sep 17 00:00:00 2001 From: IM23a-spirgif Date: Sat, 14 Sep 2024 23:34:27 +0200 Subject: [PATCH] made DPS/DPM calculator use the data from the json file, removed the search bar from the calculator, and made it so the calculator displays the stats of the picked gun. Moved JSON from / to /static, this doesn't seem to have changed anything. filterWeapons.js has been updated with the required functions so the calculator can spit out the stats --- app.py | 2 +- data.json => static/data.json | 0 static/filterWeapons.js | 159 ++++++++++++---------------------- templates/calculator.html | 9 +- 4 files changed, 64 insertions(+), 106 deletions(-) rename data.json => static/data.json (100%) diff --git a/app.py b/app.py index 829a428..32b7028 100644 --- a/app.py +++ b/app.py @@ -4,7 +4,7 @@ import json app = Flask(__name__) # Load the data from the JSON file -with open('data.json') as f: +with open('static/data.json') as f: guns = json.load(f) diff --git a/data.json b/static/data.json similarity index 100% rename from data.json rename to static/data.json diff --git a/static/filterWeapons.js b/static/filterWeapons.js index 2079e36..100e7f2 100644 --- a/static/filterWeapons.js +++ b/static/filterWeapons.js @@ -1,91 +1,16 @@ -// Weapon data from JSON - -// Full weapons data from JSON -const weapons = [ - {"id": 1, "name": "G18"}, - {"id": 2, "name": "M1911"}, - {"id": 3, "name": "M9A"}, - {"id": 4, "name": "M1911MK2"}, - {"id": 5, "name": "Luger"}, - {"id": 6, "name": "AK47"}, - {"id": 7, "name": "G36"}, - {"id": 8, "name": "CRL10"}, - {"id": 9, "name": "BRH17"}, - {"id": 10, "name": "M4"}, - {"id": 11, "name": "M1"}, - {"id": 12, "name": "R870"}, - {"id": 14, "name": "Scattergun"}, - {"id": 16, "name": "Shorty"}, - {"id": 18, "name": "AA12"}, - {"id": 20, "name": "P90"}, - {"id": 21, "name": "Thompson"}, - {"id": 22, "name": "Uzi"}, - {"id": 23, "name": "Vector"}, - {"id": 24, "name": "MP5"}, - {"id": 25, "name": "U10"}, - {"id": 26, "name": "MPX"}, - {"id": 27, "name": "UMP45"}, - {"id": 28, "name": "Prototype"}, - {"id": 29, "name": "Prototype (Overcharged)"}, - {"id": 30, "name": "MP40"}, - {"id": 31, "name": "PKM"}, - {"id": 32, "name": "M249"}, - {"id": 33, "name": "T49"}, - {"id": 34, "name": "T32"}, - {"id": 35, "name": "Improved T49"}, - {"id": 36, "name": "Improved T32"}, - {"id": 37, "name": "AMR 12.7"}, - {"id": 38, "name": "Golden Thompson"}, - {"id": 39, "name": "MilTech SMGs"}, - {"id": 40, "name": "Eradicator"}, - {"id": 41, "name": "A10"}, - {"id": 42, "name": "Turret (Manual)"}, - {"id": 43, "name": "Turret (Automatic)"}, - {"id": 44, "name": "Improved G18"}, - {"id": 45, "name": "Silenced G18"}, - {"id": 46, "name": "RC12"}, - {"id": 48, "name": "MRL4"}, - {"id": 49, "name": "Improved MRL4"}, - {"id": 50, "name": "K4LAS"}, - {"id": 51, "name": "Improved K4LAS"}, - {"id": 52, "name": "MK49"}, - {"id": 53, "name": "Improved MK49"}, - {"id": 55, "name": "STEN Gun"}, - {"id": 56, "name": "Akimbo G18"}, - {"id": 57, "name": "ASR"}, - {"id": 58, "name": "Improved ASR"}, - {"id": 59, "name": "Improved G36"}, - {"id": 60, "name": "Akimbo Voids"}, - {"id": 61, "name": "Improved Akimbo Voids"}, - {"id": 62, "name": "Improved MP5"}, - {"id": 63, "name": "LTL Laser"}, - {"id": 64, "name": "Improved Vector"}, - {"id": 65, "name": "T45"}, - {"id": 66, "name": "Improved T45"}, - {"id": 67, "name": "Freeze Ray"}, - {"id": 68, "name": "Freeze Rifle"}, - {"id": 69, "name": "R416"}, - {"id": 70, "name": "Improved R416"}, - {"id": 71, "name": "Flare Gun"}, - {"id": 72, "name": "Improved BRH17"}, - {"id": 73, "name": "Deagle"}, - {"id": 74, "name": "Akimbo Deagles"}, - {"id": 75, "name": "CRL11"}, - {"id": 76, "name": "Scan Rifle"}, - {"id": 77, "name": "PPSH41"}, - {"id": 78, "name": "AK74M"}, - {"id": 79, "name": "Akimbo Uzi"}, - {"id": 80, "name": "Akimbo MP5"}, - {"id": 81, "name": "Improved Thompson"}, - {"id": 82, "name": "RV9"}, - {"id": 83, "name": "SPAS-12"}, - {"id": 84, "name": "Akimbo Shotguns"}, - {"id": 85, "name": "SK17"}, - {"id": 86, "name": "Type49"}, - {"id": 87, "name": "M32"}, - {"id": 88, "name": "M32NL"} -]; +let selectedWeapon = null; // Store the selected weapon's data +let weapons = []; // This will be populated by the fetched JSON data +// Function to fetch weapons data from JSON +function fetchWeaponsData() { + fetch('static/data.json') // Adjust the path to the actual location of your JSON file + .then(response => response.json()) + .then(data => { + weapons = data; // Store the fetched data in the global 'weapons' variable + populateWeapons(); // Populate the weapon dropdown once data is loaded + }) + .catch(error => console.error('Error fetching weapons data:', error)); +} // Function to populate the select element with weapons function populateWeapons() { @@ -98,23 +23,53 @@ function populateWeapons() { option.text = weapon.name; select.appendChild(option); }); -} -// Filter function for the search box -function filterWeapons() { - const input = document.getElementById('search').value.toLowerCase(); - const select = document.getElementById('weapon-select'); - select.innerHTML = ""; // Clear previous options - - weapons.forEach(weapon => { - if (weapon.name.toLowerCase().indexOf(input) > -1) { - const option = document.createElement('option'); - option.value = weapon.id; - option.text = weapon.name; - select.appendChild(option); + // Attach event listener to detect when a weapon is selected + select.addEventListener('change', (event) => { + const selectedWeaponId = parseInt(event.target.value); + selectedWeapon = weapons.find(weapon => weapon.id === selectedWeaponId); + if (selectedWeapon) { + updateWeaponStats(selectedWeapon); // Update stats on selection } }); } -// Populate the weapon list on page load -populateWeapons(); \ No newline at end of file +// Function to display the selected weapon's stats in the UI +function updateWeaponStats(weapon) { + // Example: Display the stats in variables or in the HTML + const statsElement = document.getElementById('weapon-stats'); + statsElement.innerHTML = ` +

Name: ${weapon.name}

+

Damage: ${weapon.damage}

+

RPM: ${weapon.rpm}

+

Mag Size: ${weapon.mag_size}

+

Reload Time: ${weapon.reload_time}

+ `; + + // You can store the values for further calculations: + let damage = weapon.damage; + let rpm = weapon.rpm; + let magSize = weapon.mag_size; + let reloadTime = weapon.reload_time; + + // Calculate DPS and DPM here or trigger a calculation function + calculateDPSAndDPM(damage, rpm, magSize, reloadTime); +} + +// Example DPS and DPM calculation function +function calculateDPSAndDPM(damage, rpm, magSize, reloadTime) { + const rps = rpm / 60; // Rounds per second + const dps = damage * rps; + const reloadTimeMin = reloadTime / 60; + const dpm = (magSize * damage) / (reloadTimeMin + (magSize / rpm)); + + // Display the calculated stats + const resultElement = document.getElementById('calculated-stats'); + resultElement.innerHTML = ` +

DPS: ${dps.toFixed(2)}

+

DPM: ${dpm.toFixed(2)}

+ `; +} + +// Call the fetch function to load the data and populate the dropdown on page load +fetchWeaponsData(); \ No newline at end of file diff --git a/templates/calculator.html b/templates/calculator.html index 82ac154..4faffad 100644 --- a/templates/calculator.html +++ b/templates/calculator.html @@ -53,14 +53,17 @@
- - - + +
+ + +
+