This repository has been archived by the owner on Jun 6, 2021. It is now read-only.
/
leaderboard.ts
105 lines (98 loc) · 2.67 KB
/
leaderboard.ts
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
import Chart from 'chart.js';
import 'moment';
export default class Leaderboard {
constructor() {
const canvas = <HTMLCanvasElement> document.getElementById("graph");
let chart = new Chart(canvas.getContext("2d"), {
type: "line",
options: {
spanGaps: true,
scales: {
xAxes: [
{
type: "time",
time: {
parser: "ddd MMM DD HH:mm:ss ZZ Y",
// @ts-ignore
unit: "minute",
tooltipFormat: "ddd HH:mm:ss"
},
ticks: {
source: "auto"
}
}
],
yAxes: [
{
ticks: {
beginAtZero: true
}
}
]
},
elements: {
line: {
tension: 0,
fill: false
}
},
legend: {
display: false
},
tooltips: {
callbacks: {
label: function(tooltipItem, data) {
let dataset = data.datasets[tooltipItem.datasetIndex];
let datapoint = dataset.data[tooltipItem.index];
// @ts-ignore
let challName = datapoint.tooltipLabel;
let userName = dataset.label;
// @ts-ignore
let points = datapoint.y;
return userName + ': ' + challName + ' ('+points+')';
}
}
},
maintainAspectRatio: false,
responsive: true,
// showScale: false
}
});
let protocol = "wss://";
if(document.URL.includes("http://"))
protocol = "ws://";
let ws = new WebSocket(protocol + window.location.host + "/ws");
ws.onopen = function() {
// Web Socket is connected, send data uting send()
console.log("ws connected");
};
ws.onclose = function() {
alert("WS Disconnected, reload the page");
};
ws.onmessage = evt => {
var rec = evt.data;
console.log(evt.data);
let score = JSON.parse(rec);
let table = <HTMLTableElement> document.getElementById("leaderboardtable");
table.innerHTML = "<tr><th>Name</th><th>Points</th></tr>";
for (let i = 0; i < score.table.name.length; i++) {
let row = table.insertRow();
let namecell = row.insertCell();
namecell.innerHTML = score.table.name[i];
let pointcell = row.insertCell();
pointcell.innerHTML = score.table.points[i];
}
chart.data = {
datasets: score.chart,
labels: ["a"]
};
// @ts-ignore
chart.options.scales.xAxes[0].ticks.min = chart.data.datasets[0].data[0].t;
chart.options.scales.xAxes[0].ticks.max = Date.now();
chart.update();
};
window.addEventListener("resize", function() {
chart.update();
});
}
}