PSFPortal/app/views/Avatar.svelte

217 lines
6.6 KiB
Svelte
Raw Normal View History

2024-03-16 23:10:16 +00:00
<script>
import { onMount } from 'svelte';
import axios from 'axios'
import Alert from '../components/Alert'
2024-03-22 11:22:30 +00:00
import { bepRanges, cepRanges, calculateBr, calculateCr, getFactionIcon, getFactionName,
weaponNames, getWeaponName } from '../statFunctions';
2024-03-16 23:10:16 +00:00
onMount(() => {
2024-03-19 01:15:42 +00:00
get_iWeaponStats();
2024-03-22 11:22:30 +00:00
get_avatar();
get_avatarKdByDate();
2024-03-16 23:10:16 +00:00
});
2024-03-22 11:22:30 +00:00
export let params;
2024-03-19 01:15:42 +00:00
let iWeapons = [];
2024-03-22 16:37:25 +00:00
let vehicles = [];
2024-03-16 23:10:16 +00:00
let alert;
2024-03-22 11:22:30 +00:00
let avatar = {};
2024-03-19 01:15:42 +00:00
let iWeaponsKillsSum
2024-03-22 16:37:25 +00:00
let vehicleKillsSum
2024-03-22 11:22:30 +00:00
let kdByDate = [];
let url = params.id || avatar.id
let face;
let totalKills
let totalDeaths
2024-03-16 23:10:16 +00:00
2024-03-22 11:22:30 +00:00
const weaponstatsUrl = "/api/weaponstats/"+url
const avatarUrl = "/api/avatar/"+url
const avatarKdUrl = "/api/avatar/"+url+"/kd_byDate"
2024-03-16 23:10:16 +00:00
2024-03-22 11:22:30 +00:00
async function get_avatar() {
try {
const resp = await axios.get(avatarUrl);
avatar = resp.data
const head = avatar.gender.toString() + avatar.head.toString();
face = "/img/faces/" + head + ".png";
// Reset alert message if needed
alert.message("");
} catch (e) {
console.log(e);
alert.message("Failed to fetch stats from server");
}
}
async function get_avatarKdByDate() {
try {
const resp = await axios.get(avatarKdUrl);
const stats = resp.data;
kdByDate = stats.kd;
totalKills = kdByDate.reduce((total, kd) => total + kd.kills, 0);
totalDeaths = kdByDate.reduce((total, kd) => total + kd.deaths, 0);
// Reset alert message if needed
alert.message("");
} catch (e) {
console.log(e);
alert.message("Failed to fetch stats from server");
}
}
2024-03-16 23:10:16 +00:00
2024-03-19 01:15:42 +00:00
async function get_iWeaponStats() {
2024-03-16 23:10:16 +00:00
try {
2024-03-22 16:37:25 +00:00
const ids = [55, 56, 57, 140, 146, 148, 175, 233, 299, 304, 324, 334, 345, 396, 406,
2024-03-19 01:15:42 +00:00
407, 411, 425, 429, 462, 468, 556, 587, 588, 589, 599, 673, 680, 701, 706, 714,
716, 730, 737, 817, 838, 845, 864, 888, 889, 890, 968, 969, 970];
2024-03-22 11:22:30 +00:00
const resp = await axios.get(weaponstatsUrl);
2024-03-16 23:10:16 +00:00
const stats = resp.data;
2024-03-19 01:15:42 +00:00
const filteredWeapons = stats.weapons.filter(weapon => {
return ids.includes(weapon.weapon_id);
});
filteredWeapons.forEach(weapon => {
switch (weapon.weapon_id) {
case 304:
weapon.shots_fired = Math.ceil(weapon.shots_fired / 36);
weapon.shots_landed = Math.ceil(weapon.shots_landed / 8);
break;
case 411:
weapon.shots_fired = Math.ceil(weapon.shots_fired / 21);
weapon.shots_landed = Math.ceil(weapon.shots_landed / 6);
break;
case 588:
weapon.shots_fired = Math.ceil(weapon.shots_fired / 55);
weapon.shots_landed = Math.ceil(weapon.shots_landed / 10);
break;
case 714:
weapon.shots_fired = Math.ceil(weapon.shots_fired / 36);
weapon.shots_landed = Math.ceil(weapon.shots_landed / 8);
break;
default:
break;
}
});
iWeapons = filteredWeapons;
iWeaponsKillsSum = iWeapons.reduce((total, weapon) => total + weapon.kills, 0);
2024-03-22 16:37:25 +00:00
const vIds = [46, 60, 62, 66, 67, 68, 79, 83, 84, 118, 135, 179, 199, 200, 239, 259, 294, 335, 338,
353, 432, 441, 446, 459, 470, 480, 532, 572, 632, 642, 643, 671, 685, 686, 687, 688, 697,
707, 710, 741, 759, 784, 819, 825, 826, 847, 849, 860, 862, 865, 896, 898, 900, 923, 943, 986, 997];
const filteredVehicles = stats.weapons.filter(weapon => {
return vIds.includes(weapon.weapon_id);
});
vehicles = filteredVehicles;
vehicleKillsSum = vehicles.reduce((total, weapon) => total + weapon.kills, 0);
2024-03-16 23:10:16 +00:00
// Reset alert message if needed
alert.message("");
} catch (e) {
console.log(e);
alert.message("Failed to fetch stats from server");
}
}
</script>
<svelte:head>
<title>Player Stats</title>
</svelte:head>
2024-03-22 11:22:30 +00:00
<table width="70%">
2024-03-16 23:10:16 +00:00
<tbody>
<tr>
<td width="%50" valign="top">
<table>
<tbody>
<tr>
<td>
2024-03-22 11:22:30 +00:00
<span style="color:lightgrey;">Character Name:</span> {avatar.name}<br>
<span style="color:lightgrey;">Empire:</span> {getFactionName(avatar.faction)}
2024-03-16 23:10:16 +00:00
</td>
2024-03-22 11:22:30 +00:00
<td><img height="60" src={getFactionIcon(avatar.faction)} alt={avatar.faction}/></td>
</tr>
<tr>
<td><img height="100" src={face} alt="{face} not found"/></td>
2024-03-16 23:10:16 +00:00
</tr>
</tbody>
</table>
2024-03-22 11:22:30 +00:00
<span style="color:lightgrey;">Battle Rank:</span> {calculateBr(avatar.bep)}<br>
<span style="color:lightgrey;">Command Rank:</span> {calculateCr(avatar.cep)}<br>
<span style="color:lightgrey;">Kills:</span> {totalKills}<br>
<span style="color:lightgrey;">Deaths:</span> {totalDeaths}<br>
<span style="color:lightgrey;">KDR:</span>
{#if totalDeaths !== 0}
{(totalKills / totalDeaths).toFixed(2)}
{:else}
{totalKills}
{/if}
2024-03-16 23:10:16 +00:00
</td>
</tr>
</tbody>
</table>
<br>
2024-03-22 11:22:30 +00:00
<br>
<span style="color:lightgrey;">Kills by Weapon - Total: </span>{iWeaponsKillsSum}
2024-03-16 23:10:16 +00:00
<table class="table table-sm table-dark table-responsive-md table-striped table-hover">
<thead class="thead-light">
<th>Weapon</th>
<th>Kills</th>
<th>Assists</th>
<th>Shots Fired</th>
<th>Shots Landed</th>
<th>Accuracy</th>
</thead>
<tbody>
2024-03-19 01:15:42 +00:00
{#each iWeapons as weapon}
2024-03-16 23:10:16 +00:00
<tr>
2024-03-19 01:15:42 +00:00
<td>{getWeaponName(weapon.weapon_id)}</td>
2024-03-16 23:10:16 +00:00
<td>{weapon.kills}</td>
<td>{weapon.assists}</td>
<td>{weapon.shots_fired}</td>
<td>{weapon.shots_landed}</td>
<td>{((weapon.shots_landed / weapon.shots_fired) * 100).toFixed(2)}%</td>
</tr>
{/each}
</tbody>
</table>
2024-03-22 16:37:25 +00:00
<span style="color:lightgrey;">Kills by Vehicle - Total: </span>{vehicleKillsSum}
<table class="table table-sm table-dark table-responsive-md table-striped table-hover">
<thead class="thead-light">
<th>Vehicle</th>
<th>Kills</th>
</thead>
<tbody>
{#each vehicles as veh}
<tr>
<td>{getWeaponName(veh.weapon_id)}</td>
<td>{veh.kills}</td>
</tr>
{/each}
</tbody>
</table>
2024-03-22 11:22:30 +00:00
<span style="color:lightgrey;">Daily Stats</span>
<table class="table table-sm table-dark table-responsive-md table-striped table-hover">
<thead class="thead-light">
<th>Date</th>
<th>Kills</th>
<th>Deaths</th>
<th>KDR</th>
</thead>
<tbody>
{#each kdByDate as date}
<tr>
<td>{date.date}</td>
<td>{date.kills}</td>
<td>{date.deaths}</td>
<td>{#if date.deaths !== 0}
{(date.kills / date.deaths).toFixed(2)}
{:else}
{date.kills}
{/if}
</td>
</tr>
{/each}
</tbody>
</table>