From: Steinar H. Gunderson Date: Wed, 10 May 2023 20:00:45 +0000 (+0200) Subject: Add a playing time menu. X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;h=32a897b24d9d1c5f5433bc5c910d28b089690bbc;p=pkanalytics Add a playing time menu. --- diff --git a/ultimate.js b/ultimate.js index bb2f449..250f4f7 100644 --- a/ultimate.js +++ b/ultimate.js @@ -24,6 +24,9 @@ function take_off_field(player, t, live_since) { } else { attribute_player_time(player, t, live_since); } + if (player.on_field_since !== null) { // Just a safeguard; out without in should never happen. + player.field_time_ms += t - player.on_field_since; + } player.on_field_since = null; } @@ -53,6 +56,7 @@ function process_matches(json) { 'interceptions': 0, 'points_played': 0, 'playing_time_ms': 0, + 'field_time_ms': 0, // For efficiency. 'offensive_points_completed': 0, @@ -84,6 +88,7 @@ function process_matches(json) { let pull_started = null; let last_pull_was_ours = null; // Effectively whether we're playing an O or D point (not affected by turnovers). let point_num = 0; + let last_goal = null; for (const [q,p] of Object.entries(players)) { p.on_field_since = null; p.last_point_seen = null; @@ -138,6 +143,7 @@ function process_matches(json) { } if (type === 'goal' || type === 'their_goal') { ++point_num; + last_goal = t; } // Pull management @@ -230,6 +236,13 @@ function process_matches(json) { console.log("Unknown event:", e); } } + + // Add field time for all players still left at match end. + for (const [q,p] of Object.entries(players)) { + if (p.on_field_since !== null && last_goal !== null) { + p.field_time_ms += last_goal - p.on_field_since; + } + } } let chosen_category = get_chosen_category(); @@ -242,6 +255,8 @@ function process_matches(json) { rows = make_table_offense(players); } else if (chosen_category === 'defense') { rows = make_table_defense(players); + } else if (chosen_category === 'playing_time') { + rows = make_table_playing_time(players); } document.getElementById('stats').replaceChildren(...rows); } @@ -251,6 +266,8 @@ function get_chosen_category() { return 'offense'; } else if (window.location.hash === '#defense') { return 'defense'; + } else if (window.location.hash === '#playing_time') { + return 'playing_time'; } else { return 'general'; } @@ -287,6 +304,16 @@ function write_main_menu(chosen_category) { elems.push(a); } + elems.push(document.createTextNode(' | ')); + if (chosen_category === 'playing_time') { + elems.push(document.createTextNode('Playing time')); + } else { + let a = document.createElement('a'); + a.appendChild(document.createTextNode('Playing time')); + a.setAttribute('href', '#playing_time'); + elems.push(a); + } + document.getElementById('mainmenu').replaceChildren(...elems); } @@ -300,7 +327,6 @@ function make_table_general(players) { add_cell(header, 'th', 'O efficiency'); add_cell(header, 'th', 'D efficiency'); add_cell(header, 'th', 'Points played'); - add_cell(header, 'th', 'Time played'); rows.push(header); } @@ -316,7 +342,6 @@ function make_table_general(players) { add_cell(row, 'td', p.offensive_points_completed > 0 ? o_efficiency.toFixed(2) : 'N/A'); add_cell(row, 'td', p.defensive_points_completed > 0 ? d_efficiency.toFixed(2) : 'N/A'); add_cell(row, 'td', p.points_played); - add_cell(row, 'td', Math.floor(p.playing_time_ms / 60000) + ' min'); rows.push(row); } return rows; @@ -402,3 +427,29 @@ function make_table_defense(players) { } return rows; } + +function make_table_playing_time(players) { + let rows = []; + { + let header = document.createElement('tr'); + add_cell(header, 'th', 'Player'); + add_cell(header, 'th', 'Points played'); + add_cell(header, 'th', 'Time played'); + add_cell(header, 'th', 'Time on field'); + add_cell(header, 'th', 'O points'); + add_cell(header, 'th', 'D points'); + rows.push(header); + } + + for (const [q,p] of Object.entries(players)) { + let row = document.createElement('tr'); + add_cell(row, 'td', p.name); // TODO: number? + add_cell(row, 'td', p.points_played); + add_cell(row, 'td', Math.floor(p.playing_time_ms / 60000) + ' min'); + add_cell(row, 'td', Math.floor(p.field_time_ms / 60000) + ' min'); + add_cell(row, 'td', p.offensive_points_completed); + add_cell(row, 'td', p.defensive_points_completed); + rows.push(row); + } + return rows; +}