Scoresheet: Save results to localstorage

This commit is contained in:
LoveEevee 2020-03-06 03:02:07 +03:00
parent 41afc2a905
commit 21259abdda
6 changed files with 165 additions and 1 deletions

View File

@ -30,7 +30,8 @@ var assets = {
"session.js", "session.js",
"importsongs.js", "importsongs.js",
"logo.js", "logo.js",
"settings.js" "settings.js",
"scorestorage.js"
], ],
"css": [ "css": [
"main.css", "main.css",

View File

@ -204,6 +204,7 @@ class Loader{
} }
settings = new Settings() settings = new Settings()
scoreStorage = new ScoreStorage()
pageEvents.setKbd() pageEvents.setKbd()
Promise.all(this.promises).then(() => { Promise.all(this.promises).then(() => {

View File

@ -83,6 +83,7 @@ var perf = {
var strings var strings
var vectors var vectors
var settings var settings
var scoreStorage
pageEvents.add(root, ["touchstart", "touchmove", "touchend"], event => { pageEvents.add(root, ["touchstart", "touchmove", "touchend"], event => {
if(event.cancelable && cancelTouch && event.target.tagName !== "SELECT"){ if(event.cancelable && cancelTouch && event.target.tagName !== "SELECT"){

View File

@ -1,6 +1,7 @@
class Scoresheet{ class Scoresheet{
constructor(controller, results, multiplayer, touchEnabled){ constructor(controller, results, multiplayer, touchEnabled){
this.controller = controller this.controller = controller
this.resultsObj = results
this.results = {} this.results = {}
for(var i in results){ for(var i in results){
this.results[i] = results[i].toString() this.results[i] = results[i].toString()
@ -54,6 +55,7 @@ class Scoresheet{
"ura": 4 "ura": 4
} }
this.scoreSaved = false
this.redrawRunning = true this.redrawRunning = true
this.redrawBind = this.redraw.bind(this) this.redrawBind = this.redraw.bind(this)
this.redraw() this.redraw()
@ -248,6 +250,9 @@ class Scoresheet{
if(this.state.screen === "fadeIn" && elapsed < 1000){ if(this.state.screen === "fadeIn" && elapsed < 1000){
bgOffset = Math.min(1, this.draw.easeIn(1 - elapsed / 1000)) * (winH / 2) bgOffset = Math.min(1, this.draw.easeIn(1 - elapsed / 1000)) * (winH / 2)
} }
if((this.state.screen !== "fadeIn" || elapsed >= 1000) && !this.scoreSaved){
this.saveScore()
}
if(bgOffset){ if(bgOffset){
ctx.save() ctx.save()
@ -854,6 +859,25 @@ class Scoresheet{
return Date.now() return Date.now()
} }
saveScore(){
if(!this.controller.autoPlayEnabled && this.resultsObj.points > 0){
var title = this.controller.selectedSong.originalTitle
var difficulty = this.resultsObj.difficulty
var oldScore = scoreStorage.get(title, difficulty)
if(!oldScore || oldScore.points <= this.resultsObj.points){
this.resultsObj.crown = ""
if(this.controller.game.rules.clearReached(this.resultsObj.gauge)){
this.resultsObj.crown = this.resultsObj.bad === 0 ? "gold" : "silver"
}
delete this.resultsObj.title
delete this.resultsObj.difficulty
delete this.resultsObj.gauge
scoreStorage.add(title, difficulty, this.resultsObj)
}
}
this.scoreSaved = true
}
clean(){ clean(){
this.keyboard.clean() this.keyboard.clean()
this.gamepad.clean() this.gamepad.clean()

View File

@ -0,0 +1,135 @@
class ScoreStorage{
constructor(){
this.scores = {}
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){}
for(var song in this.scoreStrings){
var scoreString = this.scoreStrings[song]
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){
this.scores[song] = []
songAdded = true
}
this.scores[song][this.difficulty[i]] = score
}
}
}
}
}
save(){
for(var song in this.scores){
this.writeString(song)
}
this.write()
}
write(){
try{
localStorage.setItem("scoreStorage", JSON.stringify(this.scoreStrings))
}catch(e){}
}
writeString(song){
var score = this.scores[song]
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("")
}
}
this.scoreStrings[song] = diffArray.join(";")
}
get(song, difficulty){
if(!song){
return this.scores
}else if(song in this.scores){
if(difficulty){
return this.scores[song][difficulty]
}else{
return this.scores[song]
}
}
}
add(song, difficulty, scoreObject){
if(!(song in this.scores)){
this.scores[song] = {}
}
this.scores[song][difficulty] = scoreObject
this.writeString(song)
this.write()
}
template(){
var template = {crown: ""}
for(var i in this.scoreKeys){
var name = this.scoreKeys[i]
template[name] = 0
}
return template
}
remove(song, difficulty){
if(song in this.scores){
if(difficulty){
if(difficulty in this.scores[song]){
delete this.scores[song][difficulty]
var noDiff = true
for(var i in this.difficulty){
if(this.scores[song][this.difficulty[i]]){
noDiff = false
break
}
}
if(noDiff){
delete this.scores[song]
delete this.scoreStrings[song]
}else{
this.writeString(song)
}
}
}else{
delete this.scores[song]
delete this.scoreStrings[song]
}
this.write()
}
}
}

View File

@ -113,6 +113,7 @@ class SongSelect{
this.songs.push({ this.songs.push({
id: song.id, id: song.id,
title: title, title: title,
originalTitle: song.title,
subtitle: subtitle, subtitle: subtitle,
skin: song.category in this.songSkin ? this.songSkin[song.category] : this.songSkin.default, skin: song.category in this.songSkin ? this.songSkin[song.category] : this.songSkin.default,
stars: song.stars, stars: song.stars,
@ -738,6 +739,7 @@ class SongSelect{
new LoadSong({ new LoadSong({
"title": selectedSong.title, "title": selectedSong.title,
"originalTitle": selectedSong.originalTitle,
"folder": selectedSong.id, "folder": selectedSong.id,
"difficulty": this.difficultyId[difficulty], "difficulty": this.difficultyId[difficulty],
"category": selectedSong.category, "category": selectedSong.category,