From: Steinar H. Gunderson Date: Wed, 10 May 2023 16:17:50 +0000 (+0200) Subject: Add some rudimentary main menu support to the viewer. X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;h=3b1e8f15cc8d79dab4084bb039617480595843df;p=pkanalytics Add some rudimentary main menu support to the viewer. --- diff --git a/ultimate.js b/ultimate.js index b65a2b3..7de103e 100644 --- a/ultimate.js +++ b/ultimate.js @@ -2,11 +2,12 @@ // No frameworks, no compilers, no npm, just JavaScript. :-) -let json; +let global_json; +addEventListener('hashchange', () => { console.log('heei'); process_matches(global_json); }); fetch('ultimate.json') .then(response => response.json()) - .then(response => { process_matches(response); }); + .then(response => { global_json = response; process_matches(global_json); }); function attribute_player_time(player, to, from) { if (player.on_field_since > from) { @@ -175,7 +176,6 @@ function process_matches(json) { } else if (type === 'offensive_soft_plus' || type === 'offensive_soft_minus' || type === 'defensive_soft_plus' || type === 'defensive_soft_minus') { ++p[type]; } else if (type !== 'in' && type !== 'out' && type !== 'pull' && - type !== 'their_pull' && type !== 'restart' && type !== 'goal' && type !== 'their_goal' && type !== 'stoppage' && type !== 'unknown' && type !== 'set_defense' && type !== 'goal' && type !== 'throwaway' && type !== 'drop' && type !== 'set_offense' && type !== 'their_goal' && @@ -186,8 +186,65 @@ function process_matches(json) { } } + let chosen_category = get_chosen_category(); + write_main_menu(chosen_category); + let rows = []; + if (chosen_category === 'general') { + rows = make_table_general(players); + } else if (chosen_category === 'pulls') { + console.log("PULL STATS"); + for (const [q,p] of Object.entries(players)) { + if (p.pulls === 0) { + continue; + } + let sum_time = 0; + for (const t of p.pull_times) { + sum_time += t; + } + let avg_time = 1e-3 * sum_time / p.pulls; + let msg = p.name + ' did ' + p.pulls + ' pull(s), ' + p.oob_pulls + ' OOB'; + if (p.oob_pulls < p.pulls) { + msg += ', avg. hangtime ' + avg_time.toFixed(1) + ' sec for others'; + } + console.log(msg, p.pull_times); + } + } + document.getElementById('stats').replaceChildren(...rows); +} +function get_chosen_category() { + if (window.location.hash === '#pulls') { + return 'pulls'; + } else { + return 'general'; + } +} + +function write_main_menu(chosen_category) { + let elems = []; + if (chosen_category === 'general') { + elems.push(document.createTextNode('General')); + } else { + let a = document.createElement('a'); + a.appendChild(document.createTextNode('General')); + a.setAttribute('href', '#general'); + elems.push(a); + } + elems.push(document.createTextNode(' | ')); + if (chosen_category === 'pulls') { + elems.push(document.createTextNode('Pulls')); + } else { + let a = document.createElement('a'); + a.appendChild(document.createTextNode('Pulls')); + a.setAttribute('href', '#pulls'); + elems.push(a); + } + document.getElementById('mainmenu').replaceChildren(...elems); +} + +function make_table_general(players) { + let rows = []; { let header = document.createElement('tr'); add_cell(header, 'th', 'Player'); @@ -210,22 +267,5 @@ function process_matches(json) { rows.push(row); console.log(p.name + " played " + p.points_played + " points (" + Math.floor(p.playing_time_ms / 60000) + " min), " + p.goals + " goals, " + p.assists + " assists, plus/minus: " + pm); } - document.getElementById('stats').replaceChildren(...rows); - - console.log("PULL STATS"); - for (const [q,p] of Object.entries(players)) { - if (p.pulls === 0) { - continue; - } - let sum_time = 0; - for (const t of p.pull_times) { - sum_time += t; - } - let avg_time = 1e-3 * sum_time / p.pulls; - let msg = p.name + ' did ' + p.pulls + ' pull(s), ' + p.oob_pulls + ' OOB'; - if (p.oob_pulls < p.pulls) { - msg += ', avg. hangtime ' + avg_time.toFixed(1) + ' sec for others'; - } - console.log(msg, p.pull_times); - } + return rows; } diff --git a/viewer.html b/viewer.html index 569f696..0a20d88 100644 --- a/viewer.html +++ b/viewer.html @@ -7,6 +7,8 @@ +