-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
386 lines (333 loc) · 12.2 KB
/
script.js
File metadata and controls
386 lines (333 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
document.addEventListener('DOMContentLoaded', () => {
const tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
tooltipTriggerList.forEach(el => new bootstrap.Tooltip(el));
});
// Timestamp (seconds) up to which PBs/metrics should be computed. null means no filter (use all solves).
let filterTimestamp = null;
function computeBestAverage(arr, n) {
if (arr.length < n) return null;
let best = Infinity;
for (let i = 0; i <= arr.length - n; i++) {
const slice = arr.slice(i, i + n);
let avg;
if (n >= 5) {
const sorted = [...slice].sort((a, b) => a - b).slice(1, -1);
avg = sorted.reduce((a, b) => a + b, 0) / sorted.length;
} else {
avg = slice.reduce((a, b) => a + b, 0) / slice.length;
}
if (avg < best) best = avg;
}
return best;
}
function formatTime(seconds) {
if (seconds === null) return '-';
if (seconds < 60) return seconds.toFixed(2);
const h = Math.floor(seconds / 3600);
const m = Math.floor((seconds % 3600) / 60);
const s = (seconds % 60).toFixed(2);
const mm = m.toString().padStart(2, '0');
const [intPart, decPart] = s.split('.');
const ss = intPart.padStart(2, '0') + '.' + decPart;
if (h > 0) {
return `${h}:${mm}:${ss}`;
} else {
return `${m}:${ss}`;
}
}
function formatDate(timestamp) {
if (!timestamp) return '-';
const d = new Date(timestamp * 1000);
return d.toLocaleDateString(undefined, { year: 'numeric', month: 'short', day: 'numeric' });
}
const outputDiv = document.getElementById('output');
let allEventData = [];
document.getElementById('fileInput').addEventListener('change', e => {
const file = e.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = evt => {
outputDiv.innerHTML = '';
allEventData = [];
try {
const data = JSON.parse(evt.target.result);
// Parse session names
let sessionNames = {};
if (data.properties && data.properties.sessionData) {
try {
const sd = JSON.parse(data.properties.sessionData);
for (const [key, val] of Object.entries(sd)) {
sessionNames['session' + key] = val.name ? val.name.toString() : ('session' + key);
}
} catch {}
}
for (const [sessionKey, solves] of Object.entries(data)) {
if (!Array.isArray(solves)) continue;
const eventName = sessionNames[sessionKey] || sessionKey;
const validSolves = solves
.filter(s => Array.isArray(s) && Array.isArray(s[0]) && s[0][0] === 0 && s[0][1] > 0)
.map(s => ({ time: s[0][1] / 1000, date: s[3] || null }));
if (validSolves.length === 0) continue;
const times = validSolves.map(s => s.time);
function bestAvg(n) {
if (times.length < n) return null;
let best = Infinity;
for (let i = 0; i <= times.length - n; i++) {
const slice = times.slice(i, i + n);
let avg;
if (n >= 5) {
const sorted = [...slice].sort((a, b) => a - b).slice(1, -1);
avg = sorted.reduce((a, b) => a + b, 0) / sorted.length;
} else {
avg = slice.reduce((a, b) => a + b, 0) / slice.length;
}
if (avg < best) best = avg;
}
return best;
}
function getSinglePBHistory(solves) {
const sortedByDate = solves.slice().sort((a, b) => a.date - b.date);
let currentPB = Infinity;
const pbHistory = [];
for (const s of sortedByDate) {
if (s.time < currentPB) {
currentPB = s.time;
pbHistory.push(s);
}
}
return pbHistory.reverse();
}
function getAveragePBHistory(solves, n) {
if (solves.length < n) return [];
const sortedByDate = solves.slice().sort((a, b) => a.date - b.date);
let currentPB = Infinity;
const pbHistory = [];
for (let i = 0; i <= sortedByDate.length - n; i++) {
const window = sortedByDate.slice(i, i + n);
const times = window.map(s => s.time);
let avg;
if (n >= 5) {
const trimmed = [...times].sort((a, b) => a - b).slice(1, -1);
avg = trimmed.reduce((sum, val) => sum + val, 0) / trimmed.length;
} else {
avg = times.reduce((sum, val) => sum + val, 0) / times.length;
}
if (avg < currentPB) {
currentPB = avg;
pbHistory.push({
avg,
solves: window,
date: window[n - 1].date
});
}
}
return pbHistory.reverse();
}
// Modify PB history functions to accept an optional cutoff timestamp (seconds)
function getSinglePBHistoryUpTo(solves, upTo) {
const filtered = solves.filter(s => (s.date === null ? 0 : s.date) <= (upTo || Infinity));
if (filtered.length === 0) return [];
const sortedByDate = filtered.slice().sort((a, b) => a.date - b.date);
let currentPB = Infinity;
const pbHistory = [];
for (const s of sortedByDate) {
if (s.time < currentPB) {
currentPB = s.time;
pbHistory.push(s);
}
}
return pbHistory.reverse();
}
function getAveragePBHistoryUpTo(solves, n, upTo) {
const filtered = solves.filter(s => (s.date === null ? 0 : s.date) <= (upTo || Infinity));
if (filtered.length < n) return [];
const sortedByDate = filtered.slice().sort((a, b) => a.date - b.date);
let currentPB = Infinity;
const pbHistory = [];
for (let i = 0; i <= sortedByDate.length - n; i++) {
const window = sortedByDate.slice(i, i + n);
const times = window.map(s => s.time);
let avg;
if (n >= 5) {
const trimmed = [...times].sort((a, b) => a - b).slice(1, -1);
avg = trimmed.reduce((sum, val) => sum + val, 0) / trimmed.length;
} else {
avg = times.reduce((sum, val) => sum + val, 0) / times.length;
}
if (avg < currentPB) {
currentPB = avg;
pbHistory.push({
avg,
solves: window,
date: window[n - 1].date
});
}
}
return pbHistory.reverse();
}
allEventData.push({
event: eventName,
solves: validSolves,
times,
single: Math.min(...times),
ao3: bestAvg(3),
ao5: bestAvg(5),
ao12: bestAvg(12),
ao50: bestAvg(50),
ao100: bestAvg(100),
// getPBHistory(metric, upToSeconds)
getPBHistory: function(metric, upTo) {
if (metric === 'single') {
return getSinglePBHistoryUpTo(this.solves, upTo);
}
const nMap = { ao3: 3, ao5: 5, ao12: 12, ao50: 50, ao100: 100 };
if (!nMap[metric]) return [];
return getAveragePBHistoryUpTo(this.solves, nMap[metric], upTo);
}
});
}
if (allEventData.length === 0) {
outputDiv.innerHTML = `<div class="alert alert-warning text-center" role="alert">
No valid solves found in the JSON file.
</div>`;
return;
}
renderTable();
} catch (err) {
outputDiv.innerHTML = `<div class="alert alert-danger text-center" role="alert">
Error parsing JSON file.
</div>`;
console.error(err);
}
};
reader.readAsText(file);
});
function renderTable() {
let html = `
<table class="table table-striped table-bordered table-hover table-fixed text-center align-middle">
<thead class="table-dark">
<tr>
<th style="width: 20%;">Event</th>
<th style="width: 12%;">Single</th>
<th style="width: 12%;">Ao3</th>
<th style="width: 12%;">Ao5</th>
<th style="width: 12%;">Ao12</th>
<th style="width: 12%;">Ao50</th>
<th style="width: 12%;">Ao100</th>
</tr>
</thead>
<tbody>
`;
allEventData.forEach((row, i) => {
// compute metrics up to the current filterTimestamp (if any)
const metrics = computeMetricsForEvent(row, filterTimestamp);
const cells = ['single', 'ao3', 'ao5', 'ao12', 'ao50', 'ao100'].map(metric => {
const val = metrics[metric];
const display = formatTime(val);
const isClickable = val !== null;
return `<td
class="${isClickable ? 'clickable-pb text-primary' : ''}"
style="cursor: ${isClickable ? 'pointer' : 'default'}"
data-event-index="${i}"
data-metric="${metric}"
>${display}</td>`;
}).join('');
html += `<tr>
<td class="text-start">${row.event}</td>
${cells}
</tr>`;
});
html += '</tbody></table>';
outputDiv.innerHTML = html;
const modal = new bootstrap.Modal(document.getElementById('pbModal'));
const modalBody = document.getElementById('pbModalBody');
const modalTitle = document.getElementById('pbModalLabel');
// PB cell click handler — ignore Shift+Click
document.querySelectorAll('.clickable-pb').forEach(cell => {
cell.addEventListener('click', (e) => {
if (e.shiftKey) return;
const eventIndex = parseInt(cell.dataset.eventIndex, 10);
const metric = cell.dataset.metric;
const eventData = allEventData[eventIndex];
if (!eventData) return;
const pbs = eventData.getPBHistory(metric, filterTimestamp);
if (!pbs.length) {
modalBody.innerHTML = `<p>No past PBs found for ${metric.toUpperCase()}.</p>`;
} else {
const listItems = pbs.map(s => {
if (metric === 'single') {
return `<li>${formatTime(s.time)} <small class="text-muted">(${formatDate(s.date)})</small></li>`;
} else {
return `<li>${formatTime(s.avg)} <small class="text-muted">(${formatDate(s.date)})</small></li>`;
}
}).join('');
modalBody.innerHTML = `<ul>${listItems}</ul>`;
}
modalTitle.textContent = `${eventData.event} — ${metric.toUpperCase()} PB Progression`;
modal.show();
});
});
// Shift-click row to delete without opening modal
document.querySelectorAll('tbody tr').forEach((row, i) => {
row.addEventListener('click', (e) => {
if (e.shiftKey) {
allEventData.splice(i, 1);
renderTable();
e.stopPropagation();
e.preventDefault();
}
});
});
}
// Compute best single/averages for an event up to optional cutoff timestamp (seconds)
function computeMetricsForEvent(eventData, upTo) {
const filteredSolves = eventData.solves.filter(s => (s.date === null ? 0 : s.date) <= (upTo || Infinity));
const times = filteredSolves.map(s => s.time);
function bestAvgTimes(arr, n) {
if (arr.length < n) return null;
let best = Infinity;
for (let i = 0; i <= arr.length - n; i++) {
const slice = arr.slice(i, i + n);
let avg;
if (n >= 5) {
const sorted = [...slice].sort((a, b) => a - b).slice(1, -1);
avg = sorted.reduce((a, b) => a + b, 0) / sorted.length;
} else {
avg = slice.reduce((a, b) => a + b, 0) / slice.length;
}
if (avg < best) best = avg;
}
return best;
}
return {
single: times.length ? Math.min(...times) : null,
ao3: bestAvgTimes(times, 3),
ao5: bestAvgTimes(times, 5),
ao12: bestAvgTimes(times, 12),
ao50: bestAvgTimes(times, 50),
ao100: bestAvgTimes(times, 100)
};
}
// Wire filter buttons
document.addEventListener('DOMContentLoaded', () => {
const applyBtn = document.getElementById('applyFilterBtn');
const clearBtn = document.getElementById('clearFilterBtn');
const dateInput = document.getElementById('filterDate');
if (applyBtn) {
applyBtn.addEventListener('click', () => {
const v = dateInput.value; // YYYY-MM-DD
if (!v) return;
// set to end of day in local time
const dt = new Date(v + 'T23:59:59');
filterTimestamp = Math.floor(dt.getTime() / 1000);
renderTable();
});
}
if (clearBtn) {
clearBtn.addEventListener('click', () => {
filterTimestamp = null;
if (dateInput) dateInput.value = '';
renderTable();
});
}
});