2020-03-06 08:02:07 +08:00
|
|
|
class ScoreStorage{
|
|
|
|
constructor(){
|
|
|
|
this.scores = {}
|
2020-03-07 09:48:30 +08:00
|
|
|
this.songTitles = {}
|
2020-03-06 08:02:07 +08:00
|
|
|
this.difficulty = ["oni", "ura", "hard", "normal", "easy"]
|
|
|
|
this.scoreKeys = ["points", "good", "ok", "bad", "maxCombo", "drumroll"]
|
|
|
|
this.crownValue = ["", "silver", "gold"]
|
|
|
|
this.load()
|
|
|
|
}
|
|
|
|
load(){
|
|
|
|
this.scores = {}
|
|
|
|
this.scoreStrings = {}
|
|
|
|
try{
|
|
|
|
var localScores = localStorage.getItem("scoreStorage")
|
|
|
|
if(localScores){
|
|
|
|
this.scoreStrings = JSON.parse(localScores)
|
|
|
|
}
|
|
|
|
}catch(e){}
|
2020-03-07 09:48:30 +08:00
|
|
|
for(var hash in this.scoreStrings){
|
|
|
|
var scoreString = this.scoreStrings[hash]
|
2020-03-06 08:02:07 +08:00
|
|
|
var songAdded = false
|
|
|
|
if(typeof scoreString === "string" && scoreString){
|
|
|
|
var diffArray = scoreString.split(";")
|
|
|
|
for(var i in this.difficulty){
|
|
|
|
if(diffArray[i]){
|
|
|
|
var crown = parseInt(diffArray[i].slice(0, 1)) || 0
|
|
|
|
var score = {
|
|
|
|
crown: this.crownValue[crown] || ""
|
|
|
|
}
|
|
|
|
var scoreArray = diffArray[i].slice(1).split(",")
|
|
|
|
for(var j in this.scoreKeys){
|
|
|
|
var name = this.scoreKeys[j]
|
|
|
|
var value = parseInt(scoreArray[j], 36) || 0
|
|
|
|
if(value < 0){
|
|
|
|
value = 0
|
|
|
|
}
|
|
|
|
score[name] = value
|
|
|
|
}
|
|
|
|
if(!songAdded){
|
2020-03-07 09:48:30 +08:00
|
|
|
this.scores[hash] = {title: null}
|
2020-03-06 08:02:07 +08:00
|
|
|
songAdded = true
|
|
|
|
}
|
2020-03-07 09:48:30 +08:00
|
|
|
this.scores[hash][this.difficulty[i]] = score
|
2020-03-06 08:02:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
save(){
|
2020-03-07 09:48:30 +08:00
|
|
|
for(var hash in this.scores){
|
|
|
|
this.writeString(hash)
|
2020-03-06 08:02:07 +08:00
|
|
|
}
|
|
|
|
this.write()
|
|
|
|
}
|
|
|
|
write(){
|
|
|
|
try{
|
|
|
|
localStorage.setItem("scoreStorage", JSON.stringify(this.scoreStrings))
|
|
|
|
}catch(e){}
|
|
|
|
}
|
2020-03-07 09:48:30 +08:00
|
|
|
writeString(hash){
|
|
|
|
var score = this.scores[hash]
|
2020-03-06 08:02:07 +08:00
|
|
|
var diffArray = []
|
|
|
|
var notEmpty = false
|
|
|
|
for(var i = this.difficulty.length; i--;){
|
|
|
|
var diff = this.difficulty[i]
|
|
|
|
if(score[diff]){
|
|
|
|
var scoreArray = []
|
|
|
|
var crown = this.crownValue.indexOf(score[diff].crown).toString()
|
|
|
|
for(var j in this.scoreKeys){
|
|
|
|
var name = this.scoreKeys[j]
|
|
|
|
var value = score[diff][name]
|
|
|
|
value = Math.floor(value).toString(36)
|
|
|
|
scoreArray.push(value)
|
|
|
|
}
|
|
|
|
diffArray.unshift(crown + scoreArray.join(","))
|
|
|
|
notEmpty = true
|
|
|
|
}else if(notEmpty){
|
|
|
|
diffArray.unshift("")
|
|
|
|
}
|
|
|
|
}
|
2020-03-07 09:48:30 +08:00
|
|
|
this.scoreStrings[hash] = diffArray.join(";")
|
2020-03-06 08:02:07 +08:00
|
|
|
}
|
2020-03-07 09:48:30 +08:00
|
|
|
titleHash(song){
|
|
|
|
if(song in this.songTitles){
|
|
|
|
return this.songTitles[song]
|
|
|
|
}else{
|
|
|
|
return song
|
|
|
|
}
|
|
|
|
}
|
|
|
|
get(song, difficulty, isHash){
|
2020-03-06 08:02:07 +08:00
|
|
|
if(!song){
|
|
|
|
return this.scores
|
2020-03-07 09:48:30 +08:00
|
|
|
}else{
|
|
|
|
var hash = isHash ? song : this.titleHash(song)
|
2020-03-06 08:02:07 +08:00
|
|
|
if(difficulty){
|
2020-03-07 09:48:30 +08:00
|
|
|
if(hash in this.scores){
|
|
|
|
return this.scores[hash][difficulty]
|
|
|
|
}
|
2020-03-06 08:02:07 +08:00
|
|
|
}else{
|
2020-03-07 09:48:30 +08:00
|
|
|
return this.scores[hash]
|
2020-03-06 08:02:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-07 09:48:30 +08:00
|
|
|
add(song, difficulty, scoreObject, isHash, setTitle){
|
|
|
|
var hash = isHash ? song : this.titleHash(song)
|
|
|
|
if(!(hash in this.scores)){
|
|
|
|
this.scores[hash] = {}
|
|
|
|
}
|
|
|
|
if(setTitle){
|
|
|
|
this.scores[hash].title = setTitle
|
2020-03-06 08:02:07 +08:00
|
|
|
}
|
2020-03-07 09:48:30 +08:00
|
|
|
this.scores[hash][difficulty] = scoreObject
|
|
|
|
this.writeString(hash)
|
2020-03-06 08:02:07 +08:00
|
|
|
this.write()
|
|
|
|
}
|
|
|
|
template(){
|
|
|
|
var template = {crown: ""}
|
|
|
|
for(var i in this.scoreKeys){
|
|
|
|
var name = this.scoreKeys[i]
|
|
|
|
template[name] = 0
|
|
|
|
}
|
|
|
|
return template
|
|
|
|
}
|
2020-03-07 09:48:30 +08:00
|
|
|
remove(song, difficulty, isHash){
|
|
|
|
var hash = isHash ? song : this.titleHash(song)
|
|
|
|
if(hash in this.scores){
|
2020-03-06 08:02:07 +08:00
|
|
|
if(difficulty){
|
2020-03-07 09:48:30 +08:00
|
|
|
if(difficulty in this.scores[hash]){
|
|
|
|
delete this.scores[hash][difficulty]
|
2020-03-06 08:02:07 +08:00
|
|
|
var noDiff = true
|
|
|
|
for(var i in this.difficulty){
|
2020-03-07 09:48:30 +08:00
|
|
|
if(this.scores[hash][this.difficulty[i]]){
|
2020-03-06 08:02:07 +08:00
|
|
|
noDiff = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(noDiff){
|
2020-03-07 09:48:30 +08:00
|
|
|
delete this.scores[hash]
|
|
|
|
delete this.scoreStrings[hash]
|
2020-03-06 08:02:07 +08:00
|
|
|
}else{
|
2020-03-07 09:48:30 +08:00
|
|
|
this.writeString(hash)
|
2020-03-06 08:02:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}else{
|
2020-03-07 09:48:30 +08:00
|
|
|
delete this.scores[hash]
|
|
|
|
delete this.scoreStrings[hash]
|
2020-03-06 08:02:07 +08:00
|
|
|
}
|
|
|
|
this.write()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|