Refactor loadOrCreatePlayerCode to support platform-specific implementations for Desktop and WebAssembly environments.
This commit is contained in:
45
cmd/client/storage_wasm.go
Normal file
45
cmd/client/storage_wasm.go
Normal file
@@ -0,0 +1,45 @@
|
||||
//go:build js && wasm
|
||||
// +build js,wasm
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"log"
|
||||
"syscall/js"
|
||||
)
|
||||
|
||||
// loadOrCreatePlayerCode lädt oder erstellt einen eindeutigen Spieler-Code (WebAssembly Version)
|
||||
func (g *Game) loadOrCreatePlayerCode() {
|
||||
const storageKey = "escape_from_teacher_player_code"
|
||||
|
||||
// Versuche aus LocalStorage zu laden
|
||||
if jsGlobal := js.Global(); !jsGlobal.IsUndefined() {
|
||||
localStorage := jsGlobal.Get("localStorage")
|
||||
if !localStorage.IsUndefined() {
|
||||
stored := localStorage.Call("getItem", storageKey)
|
||||
if !stored.IsNull() && stored.String() != "" {
|
||||
g.playerCode = stored.String()
|
||||
log.Printf("🔑 Player-Code aus LocalStorage geladen: %s", g.playerCode)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Erstelle neuen Code
|
||||
bytes := make([]byte, 16)
|
||||
if _, err := rand.Read(bytes); err != nil {
|
||||
log.Fatal("Fehler beim Generieren des Player-Codes:", err)
|
||||
}
|
||||
g.playerCode = hex.EncodeToString(bytes)
|
||||
|
||||
// In LocalStorage speichern
|
||||
if jsGlobal := js.Global(); !jsGlobal.IsUndefined() {
|
||||
localStorage := jsGlobal.Get("localStorage")
|
||||
if !localStorage.IsUndefined() {
|
||||
localStorage.Call("setItem", storageKey, g.playerCode)
|
||||
log.Printf("🆕 Neuer Player-Code in LocalStorage erstellt: %s", g.playerCode)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user