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

This commit is contained in:
IM23a-spirgif
2024-09-14 23:34:27 +02:00
parent b44ff431b2
commit 420a0dc497
4 changed files with 64 additions and 106 deletions
+57 -102
View File
@@ -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();
// 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 = `
<p>Name: ${weapon.name}</p>
<p>Damage: ${weapon.damage}</p>
<p>RPM: ${weapon.rpm}</p>
<p>Mag Size: ${weapon.mag_size}</p>
<p>Reload Time: ${weapon.reload_time}</p>
`;
// 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 = `
<p>DPS: ${dps.toFixed(2)}</p>
<p>DPM: ${dpm.toFixed(2)}</p>
`;
}
// Call the fetch function to load the data and populate the dropdown on page load
fetchWeaponsData();