From 21259abdda488ce0b41bd153c28e95bfd4fd6835 Mon Sep 17 00:00:00 2001 From: LoveEevee Date: Fri, 6 Mar 2020 03:02:07 +0300 Subject: [PATCH] Scoresheet: Save results to localstorage --- public/src/js/assets.js | 3 +- public/src/js/loader.js | 1 + public/src/js/main.js | 1 + public/src/js/scoresheet.js | 24 ++++++ public/src/js/scorestorage.js | 135 ++++++++++++++++++++++++++++++++++ public/src/js/songselect.js | 2 + 6 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 public/src/js/scorestorage.js diff --git a/public/src/js/assets.js b/public/src/js/assets.js index 39a32b8..45c5e93 100644 --- a/public/src/js/assets.js +++ b/public/src/js/assets.js @@ -30,7 +30,8 @@ var assets = { "session.js", "importsongs.js", "logo.js", - "settings.js" + "settings.js", + "scorestorage.js" ], "css": [ "main.css", diff --git a/public/src/js/loader.js b/public/src/js/loader.js index a3ba450..ec19352 100644 --- a/public/src/js/loader.js +++ b/public/src/js/loader.js @@ -204,6 +204,7 @@ class Loader{ } settings = new Settings() + scoreStorage = new ScoreStorage() pageEvents.setKbd() Promise.all(this.promises).then(() => { diff --git a/public/src/js/main.js b/public/src/js/main.js index e9067f1..25dea20 100644 --- a/public/src/js/main.js +++ b/public/src/js/main.js @@ -83,6 +83,7 @@ var perf = { var strings var vectors var settings +var scoreStorage pageEvents.add(root, ["touchstart", "touchmove", "touchend"], event => { if(event.cancelable && cancelTouch && event.target.tagName !== "SELECT"){ diff --git a/public/src/js/scoresheet.js b/public/src/js/scoresheet.js index 54b9710..c2cd2cd 100644 --- a/public/src/js/scoresheet.js +++ b/public/src/js/scoresheet.js @@ -1,6 +1,7 @@ class Scoresheet{ constructor(controller, results, multiplayer, touchEnabled){ this.controller = controller + this.resultsObj = results this.results = {} for(var i in results){ this.results[i] = results[i].toString() @@ -54,6 +55,7 @@ class Scoresheet{ "ura": 4 } + this.scoreSaved = false this.redrawRunning = true this.redrawBind = this.redraw.bind(this) this.redraw() @@ -248,6 +250,9 @@ class Scoresheet{ if(this.state.screen === "fadeIn" && elapsed < 1000){ 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){ ctx.save() @@ -854,6 +859,25 @@ class Scoresheet{ 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(){ this.keyboard.clean() this.gamepad.clean() diff --git a/public/src/js/scorestorage.js b/public/src/js/scorestorage.js new file mode 100644 index 0000000..f06e729 --- /dev/null +++ b/public/src/js/scorestorage.js @@ -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() + } + } +} diff --git a/public/src/js/songselect.js b/public/src/js/songselect.js index 926d1e1..97a7f13 100644 --- a/public/src/js/songselect.js +++ b/public/src/js/songselect.js @@ -113,6 +113,7 @@ class SongSelect{ this.songs.push({ id: song.id, title: title, + originalTitle: song.title, subtitle: subtitle, skin: song.category in this.songSkin ? this.songSkin[song.category] : this.songSkin.default, stars: song.stars, @@ -738,6 +739,7 @@ class SongSelect{ new LoadSong({ "title": selectedSong.title, + "originalTitle": selectedSong.originalTitle, "folder": selectedSong.id, "difficulty": this.difficultyId[difficulty], "category": selectedSong.category,