]> git.sesse.net Git - pkanalytics/blobdiff - ultimate.js
Start working on some team-wide stats.
[pkanalytics] / ultimate.js
index d50429e3d9509a96adaabf2fc72fc9f9d73d85d2..b5bc2147034c4765ad41c1eda4aabc6062d7bdbd 100644 (file)
@@ -229,8 +229,16 @@ function calc_stats(json, filters) {
 
                'offensive_points_completed': 0,
                'offensive_points_won': 0,
+               'clean_holds': 0,
+
                'defensive_points_completed': 0,
                'defensive_points_won': 0,
+               'clean_breaks': 0,
+
+               'turnovers_won': 0,
+               'turnovers_lost': 0,
+               'their_clean_holds': 0,
+               'their_clean_breaks': 0,
        };
        let globals = players['globals'];
 
@@ -241,6 +249,7 @@ function calc_stats(json, filters) {
 
                let our_score = 0;
                let their_score = 0;
+               let between_points = true;
                let handler = null;
                let handler_got_by_interception = false;  // Only relevant if handler !== null.
                let prev_handler = null;
@@ -252,6 +261,11 @@ function calc_stats(json, filters) {
                let point_num = 0;
                let game_started = null;
                let last_goal = null;
+               let current_predominant_gender = null;
+               let current_num_players_on_field = null;
+
+               let we_lost_disc = false;
+               let they_lost_disc = false;
 
                // The last used formations of the given kind, if any; they may be reused
                // when the point starts, if nothing else is set.
@@ -309,14 +323,56 @@ function calc_stats(json, filters) {
 
                        keep = keep_event(players, formations_used_this_point, last_pull_was_ours, filters);  // Recompute after in/out.
 
+                       if (match['gender_rule_a']) {
+                               if (type === 'restart' && !between_points) {
+                                       let predominant_gender = find_predominant_gender(players);
+                                       let num_players_on_field = find_num_players_on_field(players);
+                                       if (predominant_gender !== current_predominant_gender && current_predominant_gender !== null) {
+                                               console.log(match['description'] + ' ' + format_time(t) + ': Stoppage changed predominant gender from ' + current_predominant_gender + ' to ' + predominant_gender);
+                                       }
+                                       if (num_players_on_field !== current_num_players_on_field && current_num_players_on_field !== null) {
+                                               console.log(match['description'] + ' ' + format_time(t) + ': Stoppage changed number of players on field from ' + current_num_players_on_field + ' to ' + num_players_on_field);
+                                       }
+                                       current_predominant_gender = predominant_gender;
+                                       current_num_players_on_field = num_players_on_field;
+                               } else if (type === 'pull' || type === 'their_pull') {
+                                       let predominant_gender = find_predominant_gender(players);
+                                       let num_players_on_field = find_num_players_on_field(players);
+                                       let changed = (predominant_gender !== current_predominant_gender);
+                                       if (point_num !== 0) {
+                                               let should_change = (point_num % 4 == 1 || point_num % 4 == 3);  // ABBA changes on 1 and 3.
+                                               if (changed && !should_change) {
+                                                       console.log(match['description'] + ' ' + format_time(t) + ': Gender ratio should have stayed the same, changed to predominance of ' + predominant_gender);
+                                               } else if (!changed && should_change) {
+                                                       console.log(match['description'] + ' ' + format_time(t) + ': Gender ratio should have changed, remained predominantly ' + predominant_gender);
+                                               }
+                                               if (num_players_on_field !== current_num_players_on_field && current_num_players_on_field !== null) {
+                                                       console.log(match['description'] + ' ' + format_time(t) + ': Number of players on field changed from ' + current_num_players_on_field + ' to ' + num_players_on_field);
+                                               }
+                                       }
+                                       current_predominant_gender = predominant_gender;
+                                       current_num_players_on_field = num_players_on_field;
+                               }
+                       }
+                       if (match['gender_pull_rule']) {
+                               if (type === 'pull') {
+                                       if (current_predominant_gender !== null &&
+                                           p.gender !== current_predominant_gender) {
+                                               console.log(match['description'] + ' ' + format_time(t) + ': ' + p.name + ' pulled, should have been ' + current_predominant_gender);
+                                       }
+                               }
+                       }
+
                        // Liveness management
                        if (type === 'pull' || type === 'their_pull' || type === 'restart') {
                                live_since = t;
+                               between_points = false;
                        } else if (type === 'catch' && last_pull_was_ours === null) {
                                // Someone forgot to add the pull, so we'll need to wing it.
                                console.log(match['description'] + ' ' + format_time(t) + ': Missing pull on ' + our_score + '\u2013' + their_score + '; pretending to have one.');
                                live_since = t;
                                last_pull_was_ours = !offense;
+                               between_points = false;
                        } else if (type === 'goal' || type === 'their_goal' || type === 'stoppage') {
                                for (const [q,p] of Object.entries(players)) {
                                        if (p.on_field_since === null) {
@@ -416,12 +472,42 @@ function calc_stats(json, filters) {
                                puller = pull_started = null;
                        }
 
+                       // Stats for clean holds or not (must be done before resetting we_lost_disc etc. below).
+                       if (keep) {
+                               if (type === 'goal' && !we_lost_disc) {
+                                       if (last_pull_was_ours === false) {  // O point.
+                                               ++globals.clean_holds;
+                                       } else if (last_pull_was_ours === true) {
+                                               ++globals.clean_breaks;
+                                       }
+                               } else if (type === 'their_goal' && !they_lost_disc) {
+                                       if (last_pull_was_ours === true) {  // O point for them.
+                                               ++globals.their_clean_holds;
+                                       } else if (last_pull_was_ours === false) {
+                                               ++globals.their_clean_breaks;
+                                       }
+                               }
+                       }
+
                        // Offense/defense management
                        let last_offense = offense;
                        if (type === 'set_defense' || type === 'goal' || type === 'throwaway' || type === 'drop' || type === 'was_d' || type === 'stallout') {
                                offense = false;
+                               we_lost_disc = true;
+                               if (keep && type !== 'goal' && !(type === 'set_defense' && last_pull_was_ours === null)) {
+                                       ++globals.turnovers_lost;
+                               }
                        } else if (type === 'set_offense' || type === 'their_goal' || type === 'their_throwaway' || type === 'defense' || type === 'interception') {
                                offense = true;
+                               they_lost_disc = true;
+                               if (keep && type !== 'their_goal' && !(type === 'set_offense' && last_pull_was_ours === null)) {
+                                       ++globals.turnovers_won;
+                               }
+                       }
+                       if (type === 'goal' || type === 'their_goal') {
+                               between_points = true;
+                               we_lost_disc = false;
+                               they_lost_disc = false;
                        }
                        if (last_offense !== offense && live_since !== null) {
                                // Switched offense/defense status, so attribute this drive as needed,
@@ -516,8 +602,10 @@ function calc_stats(json, filters) {
                                // _not_ after an interception, or a self-pass that's not a goal.
                                // (It must mean we tipped off someone.) We'll count it as a regular one
                                // for the time being, although it will make hockey assists weird.
-                               ++p.goals;
-                               ++p.callahans;
+                               if (keep) {
+                                       ++p.goals;
+                                       ++p.callahans;
+                               }
                                handler = prev_handler = null;
                        } else if (type === 'catch' || type === 'goal') {
                                if (handler !== null) {
@@ -635,6 +723,8 @@ function process_matches(json, filtersets) {
                        rows = make_table_playing_time(players);
                } else if (chosen_category === 'per_point') {
                        rows = make_table_per_point(players);
+               } else if (chosen_category === 'team_wide') {
+                       rows = make_table_team_wide(players);
                }
                rowsets.push(rows);
        }
@@ -694,6 +784,8 @@ function get_chosen_category() {
                return 'playing_time';
        } else if (window.location.hash === '#per_point') {
                return 'per_point';
+       } else if (window.location.hash === '#team_wide') {
+               return 'team_wide';
        } else {
                return 'general';
        }
@@ -701,59 +793,25 @@ function get_chosen_category() {
 
 function write_main_menu(chosen_category) {
        let elems = [];
-       if (chosen_category === 'general') {
-               let span = document.createElement('span');
-               span.innerText = 'General';
-               elems.push(span);
-       } else {
-               let a = document.createElement('a');
-               a.appendChild(document.createTextNode('General'));
-               a.setAttribute('href', '#general');
-               elems.push(a);
-       }
-
-       if (chosen_category === 'offense') {
-               let span = document.createElement('span');
-               span.innerText = 'Offense';
-               elems.push(span);
-       } else {
-               let a = document.createElement('a');
-               a.appendChild(document.createTextNode('Offense'));
-               a.setAttribute('href', '#offense');
-               elems.push(a);
-       }
-
-       if (chosen_category === 'defense') {
-               let span = document.createElement('span');
-               span.innerText = 'Defense';
-               elems.push(span);
-       } else {
-               let a = document.createElement('a');
-               a.appendChild(document.createTextNode('Defense'));
-               a.setAttribute('href', '#defense');
-               elems.push(a);
-       }
-
-       if (chosen_category === 'playing_time') {
-               let span = document.createElement('span');
-               span.innerText = 'Playing time';
-               elems.push(span);
-       } else {
-               let a = document.createElement('a');
-               a.appendChild(document.createTextNode('Playing time'));
-               a.setAttribute('href', '#playing_time');
-               elems.push(a);
-       }
-
-       if (chosen_category === 'per_point') {
-               let span = document.createElement('span');
-               span.innerText = 'Per point';
-               elems.push(span);
-       } else {
-               let a = document.createElement('a');
-               a.appendChild(document.createTextNode('Per point'));
-               a.setAttribute('href', '#per_point');
-               elems.push(a);
+       const categories = [
+               ['general', 'General'],
+               ['offense', 'Offense'],
+               ['defense', 'Defense'],
+               ['playing_time', 'Playing time'],
+               ['per_point', 'Per point'],
+               ['team_wide', 'Team-wide'],
+       ];
+       for (const [id, title] of categories) {
+               if (chosen_category === id) {
+                       let span = document.createElement('span');
+                       span.innerText = title;
+                       elems.push(span);
+               } else {
+                       let a = document.createElement('a');
+                       a.appendChild(document.createTextNode(title));
+                       a.setAttribute('href', '#' + id);
+                       elems.push(a);
+               }
        }
 
        document.getElementById('mainmenu').replaceChildren(...elems);
@@ -870,6 +928,56 @@ function make_table_general(players) {
        return rows;
 }
 
+function make_table_team_wide(players) {
+       let globals = players['globals'];
+
+       let rows = [];
+       {
+               let header = document.createElement('tr');
+               add_th(header, '');
+               add_th(header, 'Our team');
+               add_th(header, 'Their team');
+               rows.push(header);
+       }
+
+       // Turnovers.
+       {
+               let row = document.createElement('tr');
+               let name = add_3cell(row, 'Turnovers generated', 'name');
+               add_3cell(row, globals.turnovers_won);
+               add_3cell(row, globals.turnovers_lost);
+               rows.push(row);
+       }
+
+       // Clean holds.
+       {
+               let row = document.createElement('tr');
+               let name = add_3cell(row, 'Clean holds', 'name');
+               let our_clean_holds = make_binomial_ci(globals.clean_holds, globals.offensive_points_completed, z);
+               let their_clean_holds = make_binomial_ci(globals.their_clean_holds, globals.defensive_points_completed, z);
+               our_clean_holds.desired = 0.3;  // Arbitrary.
+               their_clean_holds.desired = 0.3;
+               add_3cell_ci(row, our_clean_holds);
+               add_3cell_ci(row, their_clean_holds);
+               rows.push(row);
+       }
+
+       // Clean breaks.
+       {
+               let row = document.createElement('tr');
+               let name = add_3cell(row, 'Clean breaks', 'name');
+               let our_clean_breaks = make_binomial_ci(globals.clean_breaks, globals.defensive_points_completed, z);
+               let their_clean_breaks = make_binomial_ci(globals.their_clean_breaks, globals.offensive_points_completed, z);
+               our_clean_breaks.desired = 0.3;  // Arbitrary.
+               their_clean_breaks.desired = 0.3;
+               add_3cell_ci(row, our_clean_breaks);
+               add_3cell_ci(row, their_clean_breaks);
+               rows.push(row);
+       }
+
+       return rows;
+}
+
 function make_table_offense(players) {
        let rows = [];
        {
@@ -1778,7 +1886,8 @@ function keep_match(match_id, filters) {
        return true;
 }
 
-function find_gender_ratio_code(players) {
+// Returns a map of e.g. F => 4, M => 3.
+function find_gender_ratio(players) {
        let map = {};
        for (const [q,p] of Object.entries(players)) {
                if (p.on_field_since === null) {
@@ -1794,6 +1903,11 @@ q                } else {
                        ++map[gender];
                }
        }
+       return map;
+}
+
+function find_gender_ratio_code(players) {
+       let map = find_gender_ratio(players);
        let all_genders = Array.from(Object.keys(map)).sort(
                (a,b) => {
                        if (map[a] !== map[b]) {
@@ -1818,6 +1932,31 @@ q                } else {
        return code;
 }
 
+// null if none (e.g., if playing 3–3).
+function find_predominant_gender(players) {
+       let max = 0;
+       let predominant_gender = null;
+       for (const [gender, num] of Object.entries(find_gender_ratio(players))) {
+               if (num > max) {
+                       max = num;
+                       predominant_gender = gender;
+               } else if (num == max) {
+                       predominant_gender = null;  // At least two have the same.
+               }
+       }
+       return predominant_gender;
+}
+
+function find_num_players_on_field(players) {
+       let num = 0;
+       for (const [q,p] of Object.entries(players)) {
+               if (p.on_field_since !== null) {
+                       ++num;
+               }
+       }
+       return num;
+}
+
 function filter_passes(players, formations_used_this_point, last_pull_was_ours, filter) {
        if (filter.type === 'player_any') {
                for (const p of Array.from(filter.elements)) {