Private
Public Access
1
0

Refactor loadOrCreatePlayerCode to support platform-specific implementations for Desktop and WebAssembly environments.

This commit is contained in:
Sebastian Unterschütz
2026-01-01 19:53:57 +01:00
parent de64329ce4
commit 85d697df19
3 changed files with 83 additions and 28 deletions

View 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)
}