This repository has been archived by the owner on Feb 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
78 lines (71 loc) · 2.7 KB
/
main.js
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
var socket = new ReconnectingWebSocket(`ws://${settings.ip}:${settings.port}/room/${settings.room}`);
var nameElement = document.getElementById("name");
var metaOneElement = document.getElementById("metaOne");
socket.onopen = function (event) {
console.log('Connected');
};
const sleep = ms => new Promise(resolve => {
setTimeout(() => { resolve() }, ms)
})
var bar=new RadialProgress(document.getElementById("bar"), {thick:10,colorBg:"rgba(255,255,255,0.2)",colorFg:"#FFFFFF",noPercentage:true, noAnimations: true})
bar.setText('0:00')
async function GetTrackInfo(id) {
let response = await fetch(` https://beatsaver.com/api/songs/search/hash/${id}`);
try {
if (response.ok) {
let data = await response.json();
let song = data.songs[0];
return {name: song.songName, subname: song.songSubName, uploader: song.uploader, author: song.authorName, bpm: song.bpm};
}
return null;
} catch (e) {
console.log(e)
return null;
}
}
const time_loop = async (time) => {
time_remaining = time
time_passed = 0
while (time_remaining > 0) {
// Do stuff
time_remaining -= 1
var minutes = Math.floor(time_passed / 60)
var seconds = time_passed - minutes * 60
var progress = time_passed / time
secondsFormatted = ("0" + seconds).slice(-2);
bar.setText(`${minutes}:${secondsFormatted}`)
bar.setValue(progress)
await sleep(1000)
time_passed += 1
}
bar.setValue(1)
}
socket.onmessage = async function (event) {
var data = JSON.parse(event.data)
if (data.commandType === "StartLevel") {
nameElement.innerHTML = data.data.song.songName;
let songInfo = await GetTrackInfo(data.data.song.levelId);
if (songInfo) {
var author = `${songInfo.author} | ` || ''
nameElement.innerHTML = songInfo.name;
metaOneElement.innerHTML = `${author}Beatmap by ${songInfo.uploader} | ${songInfo.bpm} BPM`
} else {
metaOneElement.innerHTML = ''
}
await time_loop(data.data.song.songDuration)
} else if (data.commandType === "SetSelectedSong") {
bar.setText("0:00")
bar.setValue(0)
if (data.data) {
nameElement.innerHTML = data.data.songName;
let songInfo = await GetTrackInfo(data.data.levelId);
if (songInfo) {
var author = `${songInfo.author} | ` || ''
nameElement.innerHTML = songInfo.name;
metaOneElement.innerHTML = `${author}Beatmap by ${songInfo.uploader} | ${songInfo.bpm} BPM`
} else {
metaOneElement.innerHTML = ''
}
}
}
}