Skip to content

Commit

Permalink
show totals for user-stats
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffrey-wu committed May 19, 2023
1 parent fd228f5 commit 5b9bc6f
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 71 deletions.
2 changes: 2 additions & 0 deletions client/user/stats/bonuses.html
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ <h3 class="text-center">Category Stats:</h3>
</tr>
</thead>
<tbody id="category-stats-body"></tbody>
<tfoot id="category-stats-foot"></tfoot>
</table>
</div>
</div>
Expand All @@ -134,6 +135,7 @@ <h3 class="text-center">Subcategory Stats:</h3>
</tr>
</thead>
<tbody id="subcategory-stats-body"></tbody>
<tfoot id="subcategory-stats-foot"></tfoot>
</table>
</div>
</div>
Expand Down
77 changes: 44 additions & 33 deletions client/user/stats/bonuses.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,55 @@ function fetchBonusStats({ difficulties = '', setName = '', includeMultiplayer =
})
.then(response => response.json())
.then(data => {
if (data.categoryStats) {
for (const type of ['category', 'subcategory']) {
if (!data[`${type}Stats`]) {
continue;
}

let innerHTML = '';
data.categoryStats.forEach(categoryStat => {
const category = categoryStat._id;
const totalStats = {};
data[`${type}Stats`].forEach(stat => {
innerHTML += `
<tr>
<th scope="row">${category}</th>
<td>${categoryStat.count}</td>
<td>${categoryStat['30s']}</td>
<td>${categoryStat['20s']}</td>
<td>${categoryStat['10s']}</td>
<td>${categoryStat['0s']}</td>
<td>${categoryStat.totalPoints}</td>
<td>${categoryStat.ppb.toFixed(2)}</td>
</tr>
`;
<tr>
<th scope="row">${stat._id}</th>
<td>${stat.count}</td>
<td>${stat['30s']}</td>
<td>${stat['20s']}</td>
<td>${stat['10s']}</td>
<td>${stat['0s']}</td>
<td>${stat.totalPoints}</td>
<td>${stat.ppb.toFixed(2)}</td>
</tr>
`;

Object.keys(stat).forEach(key => {
if (['_id', 'ppb'].includes(key)) {
return;
}

if (totalStats[key]) {
totalStats[key] += stat[key];
} else {
totalStats[key] = stat[key];
}
});

});
document.getElementById('category-stats-body').innerHTML = innerHTML;
}
document.getElementById(`${type}-stats-body`).innerHTML = innerHTML;

if (data.subcategoryStats) {
let innerHTML = '';
data.subcategoryStats.forEach(subcategoryStat => {
const subcategory = subcategoryStat._id;
innerHTML += `
<tr>
<th scope="row">${subcategory}</th>
<td>${subcategoryStat.count}</td>
<td>${subcategoryStat['30s']}</td>
<td>${subcategoryStat['20s']}</td>
<td>${subcategoryStat['10s']}</td>
<td>${subcategoryStat['0s']}</td>
<td>${subcategoryStat.totalPoints}</td>
<td>${subcategoryStat.ppb.toFixed(2)}</td>
</tr>
totalStats.ppb = totalStats.count > 0 ? totalStats.totalPoints / totalStats.count : 0;
document.getElementById(`${type}-stats-foot`).innerHTML = `
<tr>
<th scope="col">Total</th>
<th scope="col">${totalStats.count ?? 0}</th>
<th scope="col">${totalStats['30s'] ?? 0}</th>
<th scope="col">${totalStats['20s'] ?? 0}</th>
<th scope="col">${totalStats['10s'] ?? 0}</th>
<th scope="col">${totalStats['0s'] ?? 0}</th>
<th scope="col">${totalStats.totalPoints ?? 0}</th>
<th scope="col">${totalStats.ppb.toFixed(2)}</th>
</tr>
`;
});
document.getElementById('subcategory-stats-body').innerHTML = innerHTML;
}
})
.catch(error => {
Expand Down
7 changes: 4 additions & 3 deletions client/user/stats/tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ function sortTable(n, isFloat=false, tableId='table') {
// Start by saying: no switching is done:
switching = false;
rows = table.rows;
/* Loop through all table rows (except the
first, which contains table headers): */
for (i = 1; i < (rows.length - 1); i++) {
// Loop through all table rows (except the
// first, which contains table headers,
// and the last, which contains table footers):
for (i = 1; i < (rows.length - 2); i++) {
// Start by saying there should be no switching:
shouldSwitch = false;
/* Get the two elements you want to compare,
Expand Down
2 changes: 2 additions & 0 deletions client/user/stats/tossups.html
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ <h3 class="text-center">Category Stats:</h3>
</tr>
</thead>
<tbody id="category-stats-body"></tbody>
<tfoot id="category-stats-foot"></tfoot>
</table>
</div>
</div>
Expand All @@ -134,6 +135,7 @@ <h3 class="text-center">Subcategory Stats:</h3>
</tr>
</thead>
<tbody id="subcategory-stats-body"></tbody>
<tfoot id="subcategory-stats-foot"></tfoot>
</table>
</div>
</div>
Expand Down
80 changes: 45 additions & 35 deletions client/user/stats/tossups.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,46 +40,56 @@ function fetchTossupStats({ difficulties = '', setName = '', includeMultiplayer
document.getElementById('best-buzz').innerHTML = '';
}

if (data.categoryStats) {
for (const type of ['category', 'subcategory']) {
if (!data[`${type}Stats`]) {
continue;
}

let innerHTML = '';
data.categoryStats.forEach(categoryStat => {
const category = categoryStat._id;
const averageCelerity = categoryStat.numCorrect > 0 ? (categoryStat.totalCorrectCelerity / categoryStat.numCorrect).toFixed(3) : 0;
const totalStats = {};
data[`${type}Stats`].forEach(stat => {
const averageCelerity = stat.numCorrect > 0 ? (stat.totalCorrectCelerity / stat.numCorrect) : 0;
innerHTML += `
<tr>
<th scope="row">${category}</th>
<td>${categoryStat.count}</td>
<td>${categoryStat['15s']}</td>
<td>${categoryStat['10s']}</td>
<td>${categoryStat['-5s']}</td>
<td>${averageCelerity}</td>
<td>${categoryStat.totalPoints}</td>
<td>${categoryStat.pptu.toFixed(2)}</td>
</tr>
`;
<tr>
<th scope="row">${stat._id}</th>
<td>${stat.count}</td>
<td>${stat['15s']}</td>
<td>${stat['10s']}</td>
<td>${stat['-5s']}</td>
<td>${averageCelerity.toFixed(3)}</td>
<td>${stat.totalPoints}</td>
<td>${stat.pptu.toFixed(2)}</td>
</tr>
`;

Object.keys(stat).forEach(key => {
if (['_id', 'pptu'].includes(key)) {
return;
}

if (totalStats[key]) {
totalStats[key] += stat[key];
} else {
totalStats[key] = stat[key];
}
});
});
document.getElementById('category-stats-body').innerHTML = innerHTML;
}
document.getElementById(`${type}-stats-body`).innerHTML = innerHTML;

if (data.subcategoryStats) {
let innerHTML = '';
data.subcategoryStats.forEach(subcategoryStat => {
const subcategory = subcategoryStat._id;
const averageCelerity = subcategoryStat.numCorrect > 0 ? (subcategoryStat.totalCorrectCelerity / subcategoryStat.numCorrect).toFixed(3) : 0;
innerHTML += `
<tr>
<th scope="row">${subcategory}</th>
<td>${subcategoryStat.count}</td>
<td>${subcategoryStat['15s']}</td>
<td>${subcategoryStat['10s']}</td>
<td>${subcategoryStat['-5s']}</td>
<td>${averageCelerity}</td>
<td>${subcategoryStat.totalPoints}</td>
<td>${subcategoryStat.pptu.toFixed(2)}</td>
</tr>
totalStats.pptu = totalStats.count > 0 ? totalStats.totalPoints / totalStats.count : 0;
totalStats.averageCelerity = totalStats.numCorrect > 0 ? (totalStats.totalCorrectCelerity / totalStats.numCorrect) : 0;
document.getElementById(`${type}-stats-foot`).innerHTML = `
<tr>
<th scope="col">Total</th>
<th scope="col">${totalStats.count ?? 0}</th>
<th scope="col">${totalStats['15s'] ?? 0}</th>
<th scope="col">${totalStats['10s'] ?? 0}</th>
<th scope="col">${totalStats['-5s'] ?? 0}</th>
<th scope="col">${totalStats.averageCelerity.toFixed(3)}</th>
<th scope="col">${totalStats.totalPoints ?? 0}</th>
<th scope="col">${totalStats.pptu.toFixed(2)}</th>
</tr>
`;
});
document.getElementById('subcategory-stats-body').innerHTML = innerHTML;
}
})
.catch(error => {
Expand Down

0 comments on commit 5b9bc6f

Please sign in to comment.