fix game
All checks were successful
Dynamic Branch Deploy / build-and-deploy (push) Successful in 6m49s
All checks were successful
Dynamic Branch Deploy / build-and-deploy (push) Successful in 6m49s
This commit is contained in:
@@ -6,6 +6,7 @@ package main
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"log"
|
||||
"syscall/js"
|
||||
)
|
||||
@@ -73,3 +74,30 @@ func (g *Game) savePlayerName(name string) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (g *Game) loadHighscore() int {
|
||||
const storageKey = "escape_from_teacher_highscore"
|
||||
if jsGlobal := js.Global(); !jsGlobal.IsUndefined() {
|
||||
localStorage := jsGlobal.Get("localStorage")
|
||||
if !localStorage.IsUndefined() {
|
||||
stored := localStorage.Call("getItem", storageKey)
|
||||
if !stored.IsNull() && stored.String() != "" {
|
||||
var score int
|
||||
if _, err := fmt.Sscanf(stored.String(), "%d", &score); err == nil {
|
||||
return score
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (g *Game) saveHighscore(score int) {
|
||||
const storageKey = "escape_from_teacher_highscore"
|
||||
if jsGlobal := js.Global(); !jsGlobal.IsUndefined() {
|
||||
localStorage := jsGlobal.Get("localStorage")
|
||||
if !localStorage.IsUndefined() {
|
||||
localStorage.Call("setItem", storageKey, fmt.Sprintf("%d", score))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user