-
Notifications
You must be signed in to change notification settings - Fork 0
/
top.js
54 lines (47 loc) · 1.65 KB
/
top.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
class Top{
places = []
constructor() {
if(!localStorage.getItem("top")) {
localStorage.setItem("top", JSON.stringify([]))
}
for(let i = 0; i < top_length; i++){
this.places[i] = document.createElement('div')
document.body.appendChild(this.places[i])
this.places[i].style.position = "absolute"
this.places[i].style.width = `${scale*4}px`
this.places[i].style.height = `${scale}px`
this.places[i].style.backgroundColor = top_colors[i]
this.places[i].style.opacity = "1"
this.places[i].style.top = `${scale*(11+i)}px`
this.places[i].style.left = `${scale*12}px`
this.places[i].style.zIndex = "2"
this.places[i].style.fontSize = `${scale*0.75}px`
}
let top = JSON.parse(localStorage.getItem("top"))
console.log(top)
for(let i = 0; i < top.length; i++){
this.places[i].innerHTML = `${top[i][0]}: ${top[i][1]}`
}
}
add_to_top(name, score){
let top = JSON.parse(localStorage.getItem("top"))
top.push([name, score])
this.sort_top(top)
localStorage.setItem("top", JSON.stringify(top))
for(let i = 0; i < top.length; i++){
this.places[i].innerHTML = `${top[i][0]}: ${top[i][1]}`
}
}
sort_top(top){
for(let i = top.length - 1; i > 0; i--){
if(top[i][1] > top[i-1][1]){
let temp = top[i]
top[i] = top[i-1]
top[i-1] = temp
}
}
if(top.length > top_length){
top.pop()
}
}
}