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
|
import {
Chart,
Colors,
LineController,
LineElement,
LinearScale,
PointElement,
TimeScale,
Tooltip,
} from 'chart.js'
import 'chartjs-adapter-date-fns';
Chart.register(
Colors,
LineController,
LineElement,
LinearScale,
PointElement,
TimeScale,
Tooltip,
);
document.addEventListener('DOMContentLoaded', async () => {
const chartCanvas = document.getElementById('chart');
const quizId = chartCanvas.dataset.quizId;
const apiUrl = `${process.env.ALBATROSS_BASE_PATH}/api/quizzes/${quizId}/chart`;
const apiResult = await fetch(apiUrl).then(res => res.json());
if (apiResult.error) {
return;
}
const stats = apiResult.stats;
// Filter best scores.
for (const s of stats) {
const bestScores = [];
for (const score of s.scores) {
if (bestScores.length === 0 || bestScores[bestScores.length - 1].code_size > score.code_size) {
bestScores.push(score);
}
}
s.scores = bestScores;
}
const scoresInChronologicalOrder = stats
.flatMap(s => s.scores.map(score => ({ ...score, user: s.user })))
.toSorted((a, b) => a.submitted_at - b.submitted_at);
const scoresAndRanksAtEachTime = (() => {
const result = [];
const currentScoresForUser = new Map();
for (const { user, submitted_at, code_size } of scoresInChronologicalOrder) {
currentScoresForUser.set(user.name, { user, submitted_at, code_size });
const ranking = currentScoresForUser
.values()
.toArray()
.toSorted(
(a, b) => a.code_size === b.code_size ? a.submitted_at - b.submitted_at : a.code_size - b.code_size,
);
const scores = new Map();
for (const [i, { user, code_size }] of ranking.entries()) {
scores.set(user.name, {
user,
code_size,
rank: i + 1,
});
}
result.push({ submitted_at: submitted_at, scores });
}
return result;
})();
const rankingHistory = (() => {
const result = new Map();
for (const { submitted_at, scores } of scoresAndRanksAtEachTime) {
for (const [username, { user, code_size, rank }] of scores.entries()) {
if (!result.has(username)) {
result.set(username, []);
}
const scores = result.get(username);
scores.push({ user, code_size, rank, submitted_at });
}
}
return result
.values()
.toArray()
.toSorted((a, b) => {
const finalRankA = a[a.length - 1].rank;
const finalRankB = b[b.length - 1].rank;
return finalRankA - finalRankB;
});
})();
new Chart(
chartCanvas,
{
type: 'line',
data: {
datasets: rankingHistory.map(s => ({
label: `${s[0].user.name}${s[0].user.is_admin ? ' (staff)' : ''}`,
data: s.map(row => ({ x: row.submitted_at * 1000, y: row.rank, code_size: row.code_size })),
}))
},
options: {
scales: {
x: {
type: 'time',
time: {
parsing: false,
display: false,
unit: 'day',
tooltipFormat: 'yyyy-MM-dd HH:mm:ss',
displayFormats: {
day: 'yyyy-MM-dd',
},
},
title: {
display: true,
text: '日時',
},
},
y: {
title: {
display: true,
text: '順位',
},
reverse: true,
min: 1,
offset: true,
},
},
plugins: {
tooltip: {
callbacks: {
label: (context) => {
const label = context.dataset.label;
const code_size = context.raw.code_size;
return `${label} (${code_size} byte)`;
},
},
},
},
},
},
);
});
|