Refactor loadOrCreatePlayerCode to support platform-specific implementations for Desktop and WebAssembly environments.
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"image/color"
|
||||
@@ -796,32 +794,7 @@ func (g *Game) getMyPlayerID() string {
|
||||
return g.playerName
|
||||
}
|
||||
|
||||
// loadOrCreatePlayerCode lädt oder erstellt einen eindeutigen Spieler-Code
|
||||
func (g *Game) loadOrCreatePlayerCode() {
|
||||
const codeFile = "player_code.txt"
|
||||
|
||||
// Versuche zu laden
|
||||
data, err := ioutil.ReadFile(codeFile)
|
||||
if err == nil {
|
||||
g.playerCode = strings.TrimSpace(string(data))
|
||||
log.Printf("🔑 Player-Code geladen: %s", g.playerCode)
|
||||
return
|
||||
}
|
||||
|
||||
// Erstelle neuen Code (16 Byte = 32 Hex-Zeichen)
|
||||
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)
|
||||
|
||||
// Speichern
|
||||
if err := ioutil.WriteFile(codeFile, []byte(g.playerCode), 0644); err != nil {
|
||||
log.Printf("⚠️ Konnte Player-Code nicht speichern: %v", err)
|
||||
} else {
|
||||
log.Printf("🆕 Neuer Player-Code erstellt: %s", g.playerCode)
|
||||
}
|
||||
}
|
||||
// loadOrCreatePlayerCode wird in storage_*.go implementiert (platform-specific)
|
||||
|
||||
// submitScore sendet den individuellen Score an den Server (für Solo-Mode)
|
||||
func (g *Game) submitScore() {
|
||||
|
||||
37
cmd/client/storage_native.go
Normal file
37
cmd/client/storage_native.go
Normal file
@@ -0,0 +1,37 @@
|
||||
//go:build !js || !wasm
|
||||
// +build !js !wasm
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
)
|
||||
|
||||
// loadOrCreatePlayerCode lädt oder erstellt einen eindeutigen Spieler-Code (Desktop Version)
|
||||
func (g *Game) loadOrCreatePlayerCode() {
|
||||
const codeFile = "player_code.txt"
|
||||
|
||||
// Versuche bestehenden Code zu laden
|
||||
if data, err := ioutil.ReadFile(codeFile); err == nil {
|
||||
g.playerCode = string(data)
|
||||
log.Printf("🔑 Player-Code geladen: %s", g.playerCode)
|
||||
return
|
||||
}
|
||||
|
||||
// Erstelle neuen Code (32 Hex-Zeichen = 16 Bytes)
|
||||
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)
|
||||
|
||||
// Speichere Code in Datei
|
||||
if err := ioutil.WriteFile(codeFile, []byte(g.playerCode), 0644); err != nil {
|
||||
log.Fatal("Fehler beim Speichern des Player-Codes:", err)
|
||||
}
|
||||
|
||||
log.Printf("🆕 Neuer Player-Code erstellt: %s", g.playerCode)
|
||||
}
|
||||
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