let text;
if (ci.format === 'percentage') {
text = (100 * ci.val).toFixed(0) + '%';
+ } else if (ci.decimals !== undefined) {
+ text = ci.val.toFixed(ci.decimals);
} else {
text = ci.val.toFixed(2);
}
+ if (ci.unit !== undefined) {
+ text += ' '+ ci.unit;
+ }
let element = add_3cell(tr, text);
let to_x = (val) => { return ci_width * (val - ci.min) / (ci.max - ci.min); };
'touches_for_turnover': 0,
'touches_for_goal': 0,
+ 'time_for_turnover': [],
+ 'time_for_goal': [],
+ 'their_time_for_turnover': [],
+ 'their_time_for_goal': [],
};
let globals = players['globals'];
let we_lost_disc = false;
let they_lost_disc = false;
let touches_this_possession = 0;
+ let possession_started = null;
+ let extra_ms_this_possession = 0; // Used at stoppages.
// The last used formations of the given kind, if any; they may be reused
// when the point starts, if nothing else is set.
if (keep && type !== 'goal' && !(type === 'set_defense' && last_pull_was_ours === null)) {
++globals.turnovers_lost;
globals.touches_for_turnover += touches_this_possession;
+ if (possession_started != null) globals.time_for_turnover.push((t - possession_started) + extra_ms_this_possession);
} else if (keep && type === 'goal') {
globals.touches_for_goal += touches_this_possession;
+ if (possession_started != null) globals.time_for_goal.push((t - possession_started) + extra_ms_this_possession);
}
touches_this_possession = 0;
+ possession_started = null;
+ extra_ms_this_possession = 0;
} 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 (possession_started != null) globals.their_time_for_turnover.push((t - possession_started) + extra_ms_this_possession);
+ } else if (keep && type === 'their_goal') {
+ if (possession_started != null) globals.their_time_for_goal.push((t - possession_started) + extra_ms_this_possession);
}
touches_this_possession = 0;
+ possession_started = null;
+ extra_ms_this_possession = 0;
}
if (type === 'goal' || type === 'their_goal') {
between_points = true;
}
live_since = t;
}
+ // The rules for possessions are slightly different; we want to start at pulls,
+ // not previous goals.
+ if ((last_offense !== offense && type !== 'goal' && type !== 'their_goal' && live_since !== null) ||
+ type === 'pull' || type === 'their_pull') {
+ possession_started = t;
+ } else if (type === 'stoppage') {
+ if (possession_started !== null) {
+ extra_ms_this_possession = t - possession_started;
+ possession_started = null;
+ }
+ } else if (type === 'restart' && live_since !== null && !between_points) {
+ possession_started = t;
+ }
if (type === 'pull') {
last_pull_was_ours = true;
return players;
}
-function process_matches(json, filtersets) {
+function recursively_expand_filterset(filterset, base, result)
+{
+ if (result.length >= 100) {
+ return;
+ }
+ if (base.length == filterset.length) {
+ result.push(base);
+ return;
+ }
+ let filter = filterset[base.length];
+ if (filter.split) {
+ let elements = filter.elements;
+ if (elements.size === 0) {
+ elements = filter.choices;
+ }
+ for (const element of elements) {
+ let split_filter = {
+ 'type': filter.type,
+ 'elements': new Set([ element ]),
+ 'split': false, // Not used.
+ };
+ recursively_expand_filterset(filterset, [...base, split_filter], result);
+ }
+ } else {
+ recursively_expand_filterset(filterset, [...base, filter], result);
+ }
+}
+
+function expand_filtersets(filtersets) {
+ if (filtersets.length === 0) {
+ return [[]];
+ }
+
+ let expanded = [];
+ for (const filterset of filtersets) {
+ recursively_expand_filterset(filterset, [], expanded);
+ }
+ return expanded;
+}
+
+function process_matches(json, unexpanded_filtersets) {
let chosen_category = get_chosen_category();
write_main_menu(chosen_category);
- let filterset_values = Object.values(filtersets);
- if (filterset_values.length === 0) {
- filterset_values = [[]];
- }
+ let filtersets = expand_filtersets(Object.values(unexpanded_filtersets));
let rowsets = [];
- for (const filter of filterset_values) {
+ for (const filter of filtersets) {
let players = calc_stats(json, filter);
let rows = [];
if (chosen_category === 'general') {
}
let merged_rows = [];
- if (filterset_values.length > 1) {
+ if (filtersets.length > 1) {
for (let i = 0; i < rowsets.length; ++i) {
- let marker = make_filter_marker(filterset_values[i]);
+ let marker = make_filter_marker(filtersets[i]);
// Make filter header.
let tr = rowsets[i][0];
};
}
+// We don't really know what to expect for possessions; it appears roughly
+// lognormal, but my head doesn't work well enough to extract the CI for
+// the estimated sample mean, and now my head spins around whether we do
+// this correctly even for Poisson :-) But we do a simple (non-BCa) bootstrap
+// here, which should give us what we want.
+//
+// FIXME: z is ignored.
+function make_ci_duration(vals, z)
+{
+ // Bootstrap.
+ let sample_means = [];
+ for (let i = 0; i < 300; ++i) {
+ let sum = 0.0;
+ for (let j = 0; j < vals.length; ++j) {
+ sum += vals[Math.floor(Math.random() * vals.length)];
+ }
+ sample_means.push(sum / vals.length);
+ }
+ sample_means.sort();
+
+ // Interpolating here would probably be better, but OK.
+ let low = sample_means[Math.floor(0.025 * sample_means.length)];
+ let high = sample_means[Math.floor(0.975 * sample_means.length)];
+
+ // Find the point estimate of the mean.
+ let sum = 0.0;
+ for (const v of vals) {
+ sum += v;
+ }
+ let avg = sum / vals.length;
+
+ return {
+ 'val': avg / 1000.0,
+ 'lower_ci': low / 1000.0,
+ 'upper_ci': high / 1000.0,
+ 'min': 0.0,
+ 'max': 120.0,
+ 'desired': 0.0,
+ 'inverted': false,
+ 'unit': 'sec',
+ 'decimals': 1,
+ };
+}
+
function make_table_general(players) {
let rows = [];
{
if (q === 'globals') continue;
goals += p.goals;
}
-
let row = document.createElement('tr');
let name = add_3cell(row, 'Touches per possession (all)', 'name');
let touches = globals.touches_for_turnover + globals.touches_for_goal;
rows.push(row);
}
+ // Time. Here we have (roughly) for both.
+ {
+ let row = document.createElement('tr');
+ let name = add_3cell(row, 'Time per possession (all)', 'name');
+ let time = globals.time_for_turnover.concat(globals.time_for_goal);
+ let their_time = globals.their_time_for_turnover.concat(globals.their_time_for_goal);
+ add_3cell(row, '');
+ add_3cell_ci(row, make_ci_duration(time, z));
+ add_3cell(row, '');
+ add_3cell_ci(row, make_ci_duration(their_time, z));
+ rows.push(row);
+
+ row = document.createElement('tr');
+ add_3cell(row, 'Time per possession (goals)', 'name');
+ add_3cell(row, '');
+ add_3cell_ci(row, make_ci_duration(globals.time_for_goal, z));
+ add_3cell(row, '');
+ add_3cell_ci(row, make_ci_duration(globals.their_time_for_goal, z));
+ rows.push(row);
+
+ row = document.createElement('tr');
+ add_3cell(row, 'Time per possession (turnovers)', 'name');
+ add_3cell(row, '');
+ add_3cell_ci(row, make_ci_duration(globals.time_for_turnover, z));
+ add_3cell(row, '');
+ add_3cell_ci(row, make_ci_duration(globals.their_time_for_turnover, z));
+ rows.push(row);
+ }
+
return rows;
}
}
}
+ let choice_set = new Set();
+ for (const choice of choices) {
+ choice_set.add(choice.id);
+ }
+
for (const choice of choices) {
let label = document.createElement('label');
if (filter !== null && filter.elements.has(choice.id)) {
check.setAttribute('checked', 'checked');
}
- check.addEventListener('change', (e) => { checkbox_changed(filter_div, filterset, e, filter_type, choice.id); });
+ check.addEventListener('change', (e) => { checkbox_changed(filter_div, filterset, e, filter_type, choice.id, choice_set); });
subitem.appendChild(check);
subitem.appendChild(document.createTextNode(choice.title));
label.appendChild(subitem);
subitems.push(label);
}
+
+ // If meaningful (it's not for player_all, since all data for that would be
+ // the same anyway), show the split checkbox.
+ if (choices.length > 1 && filter_type !== 'player_all') {
+ let label = document.createElement('label');
+
+ let subitem = document.createElement('div');
+ subitem.classList.add('option');
+
+ let check = document.createElement('input');
+ check.setAttribute('type', 'checkbox');
+ check.setAttribute('id', 'choice_split');
+ if (filter !== null && filter.split) {
+ check.setAttribute('checked', 'checked');
+ }
+ check.addEventListener('change', (e) => { checkbox_changed(filter_div, filterset, e, filter_type, 'split', choice_set); });
+
+ subitem.appendChild(check);
+ let text_span = document.createElement('span'); // So that we can style its disabling later.
+ text_span.appendChild(document.createTextNode('Split'));
+ subitem.appendChild(text_span);
+ subitem.style.borderTop = '1px solid #ddd';
+
+ label.appendChild(subitem);
+ subitems.push(label);
+ }
+
submenu.replaceChildren(...subitems);
submenu.style.display = 'block';
}
}
-function checkbox_changed(filter_div, filterset, e, filter_type, id) {
+function checkbox_changed(filter_div, filterset, e, filter_type, id, choices) {
let filter = find_filter(filterset, filter_type);
let pills_div = filter_div.querySelector('.filters');
if (e.target.checked) {
if (filter === null) {
filter = {
'type': filter_type,
- 'elements': new Set([ id ]),
+ 'elements': new Set(),
+ 'choices': choices,
+ 'split': false,
};
+ if (id === 'split') {
+ filter.split = true;
+ } else {
+ filter.elements.add(id);
+ }
filter.pill = make_filter_pill(filter_div, filterset, filter);
filterset.push(filter);
pills_div.appendChild(filter.pill);
} else {
- filter.elements.add(id);
+ if (id === 'split') {
+ filter.split = true;
+ } else {
+ filter.elements.add(id);
+ }
let new_pill = make_filter_pill(filter_div, filterset, filter);
pills_div.replaceChild(new_pill, filter.pill);
filter.pill = new_pill;
}
} else {
- filter.elements.delete(id);
+ if (id === 'split') {
+ filter.split = false;
+ } else {
+ filter.elements.delete(id);
+ }
if (filter.elements.size === 0) {
pills_div.removeChild(filter.pill);
inplace_filter(filterset, f => f !== filter);
filter.pill = new_pill;
}
}
+ let choice_split = document.getElementById('choice_split');
+ if (filter.elements.size === 1) {
+ choice_split.nextSibling.style.opacity = 0.2;
+ choice_split.setAttribute('disabled', 'disabled');
+ } else {
+ choice_split.nextSibling.style.opacity = 1.0;
+ choice_split.removeAttribute('disabled');
+ }
process_matches(global_json, global_filters);
}
first = false;
}
}
+ if (filter.split && filter.elements.size !== 1) {
+ text += ' (split)';
+ }
let text_node = document.createElement('span');
text_node.innerText = text;