Add WebAssembly support for assets and chunks, implement gameover screen rendering, and enhance server gameplay logic with dynamic speeds, team naming, and score components.
This commit is contained in:
@@ -2,7 +2,10 @@ package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
@@ -39,17 +42,31 @@ func InitLeaderboard(redisAddr string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (lb *Leaderboard) AddScore(name, code string, score int) bool {
|
||||
// GenerateProofCode erstellt einen kryptografisch sicheren Proof-Code
|
||||
func GenerateProofCode(playerCode string, score int, timestamp int64) string {
|
||||
// Secret Salt (sollte eigentlich aus Config kommen, aber für Demo hier hardcoded)
|
||||
secret := "EscapeFromTeacher_Secret_2026"
|
||||
data := fmt.Sprintf("%s:%d:%d:%s", playerCode, score, timestamp, secret)
|
||||
hash := sha256.Sum256([]byte(data))
|
||||
// Nehme erste 12 Zeichen des Hex-Hash
|
||||
return hex.EncodeToString(hash[:])[:12]
|
||||
}
|
||||
|
||||
func (lb *Leaderboard) AddScore(name, code string, score int) (bool, string) {
|
||||
// Erstelle eindeutigen Key für diesen Score: PlayerCode + Timestamp
|
||||
timestamp := time.Now().Unix()
|
||||
uniqueKey := code + "_" + time.Now().Format("20060102_150405")
|
||||
|
||||
// Generiere Proof-Code
|
||||
proofCode := GenerateProofCode(code, score, timestamp)
|
||||
|
||||
// Score speichern
|
||||
entry := game.LeaderboardEntry{
|
||||
PlayerName: name,
|
||||
PlayerCode: code,
|
||||
Score: score,
|
||||
Timestamp: timestamp,
|
||||
ProofCode: proofCode,
|
||||
}
|
||||
|
||||
data, _ := json.Marshal(entry)
|
||||
@@ -61,8 +78,8 @@ func (lb *Leaderboard) AddScore(name, code string, score int) bool {
|
||||
Member: uniqueKey,
|
||||
})
|
||||
|
||||
log.Printf("🏆 Leaderboard: %s mit %d Punkten (Entry: %s)", name, score, uniqueKey)
|
||||
return true
|
||||
log.Printf("🏆 Leaderboard: %s mit %d Punkten (Entry: %s, Proof: %s)", name, score, uniqueKey, proofCode)
|
||||
return true, proofCode
|
||||
}
|
||||
|
||||
func (lb *Leaderboard) GetTop10() []game.LeaderboardEntry {
|
||||
|
||||
@@ -23,6 +23,8 @@ type ServerPlayer struct {
|
||||
InputX float64 // -1 (Links), 0, 1 (Rechts)
|
||||
LastInputSeq uint32 // Letzte verarbeitete Input-Sequenz
|
||||
Score int
|
||||
DistanceScore int // Score basierend auf zurückgelegter Distanz
|
||||
BonusScore int // Score aus Coins und anderen Boni
|
||||
IsAlive bool
|
||||
IsSpectator bool
|
||||
|
||||
@@ -68,9 +70,12 @@ type Room struct {
|
||||
Countdown int
|
||||
NextStart time.Time
|
||||
HostID string
|
||||
TeamName string // Name des Teams (vom Host gesetzt)
|
||||
CollectedCoins map[string]bool // Key: "chunkID_objectIndex"
|
||||
CollectedPowerups map[string]bool // Key: "chunkID_objectIndex"
|
||||
ScoreAccum float64 // Akkumulator für Distanz-Score
|
||||
CurrentSpeed float64 // Aktuelle Geschwindigkeit (steigt mit der Zeit)
|
||||
GameStartTime time.Time // Wann das Spiel gestartet wurde
|
||||
|
||||
// Chunk-Pool für fairen Random-Spawn
|
||||
ChunkPool []string // Verfügbare Chunks für nächsten Spawn
|
||||
@@ -100,7 +105,8 @@ func NewRoom(id string, nc *nats.Conn, w *game.World) *Room {
|
||||
CollectedCoins: make(map[string]bool),
|
||||
CollectedPowerups: make(map[string]bool),
|
||||
ChunkSpawnedCount: make(map[string]int),
|
||||
pW: 40, pH: 60, // Fallback
|
||||
CurrentSpeed: config.RunSpeed, // Startet mit normaler Geschwindigkeit
|
||||
pW: 40, pH: 60, // Fallback
|
||||
}
|
||||
|
||||
// Initialisiere Chunk-Pool mit allen verfügbaren Chunks
|
||||
@@ -281,6 +287,12 @@ func (r *Room) HandleInput(input game.ClientInput) {
|
||||
if input.PlayerID == r.HostID && r.Status == "LOBBY" {
|
||||
r.StartCountdown()
|
||||
}
|
||||
case "SET_TEAM_NAME":
|
||||
// Nur Host darf Team-Name setzen und nur in der Lobby
|
||||
if input.PlayerID == r.HostID && r.Status == "LOBBY" {
|
||||
r.TeamName = input.TeamName
|
||||
log.Printf("🏷️ Team-Name gesetzt: '%s' (von Host %s)", r.TeamName, p.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -301,9 +313,19 @@ func (r *Room) Update() {
|
||||
r.Countdown = int(rem.Seconds()) + 1
|
||||
if rem <= 0 {
|
||||
r.Status = "RUNNING"
|
||||
r.GameStartTime = time.Now()
|
||||
r.CurrentSpeed = config.RunSpeed
|
||||
}
|
||||
} else if r.Status == "RUNNING" {
|
||||
r.GlobalScrollX += config.RunSpeed
|
||||
// Geschwindigkeit erhöhen: +0.5 pro 10 Sekunden (max +5.0 nach 100 Sekunden)
|
||||
elapsed := time.Since(r.GameStartTime).Seconds()
|
||||
speedIncrease := (elapsed / 10.0) * 0.5
|
||||
if speedIncrease > 5.0 {
|
||||
speedIncrease = 5.0
|
||||
}
|
||||
r.CurrentSpeed = config.RunSpeed + speedIncrease
|
||||
|
||||
r.GlobalScrollX += r.CurrentSpeed
|
||||
// Bewegende Plattformen updaten
|
||||
r.UpdateMovingPlatforms()
|
||||
}
|
||||
@@ -333,9 +355,9 @@ func (r *Room) Update() {
|
||||
|
||||
// X Bewegung
|
||||
// Symmetrische Geschwindigkeit: Links = Rechts
|
||||
// Nach rechts: RunSpeed + 11, Nach links: RunSpeed - 11
|
||||
// Ergebnis: Rechts = 18, Links = -4 (beide gleich weit vom Scroll)
|
||||
currentSpeed := config.RunSpeed + (p.InputX * 11.0)
|
||||
// Nach rechts: CurrentSpeed + 11, Nach links: CurrentSpeed - 11
|
||||
// Verwendet r.CurrentSpeed statt config.RunSpeed für dynamische Geschwindigkeit
|
||||
currentSpeed := r.CurrentSpeed + (p.InputX * 11.0)
|
||||
nextX := p.X + currentSpeed
|
||||
|
||||
hitX, typeX := r.CheckCollision(nextX+r.pDrawOffX+r.pHitboxOffX, p.Y+r.pDrawOffY+r.pHitboxOffY, r.pW, r.pH)
|
||||
@@ -343,7 +365,7 @@ func (r *Room) Update() {
|
||||
if typeX == "wall" {
|
||||
// Wand getroffen - kann klettern!
|
||||
p.OnWall = true
|
||||
// X-Position nicht ändern (bleibt an der Wand)
|
||||
// X-Position NICHT ändern (bleibt vor der Wand stehen)
|
||||
} else if typeX == "obstacle" {
|
||||
// Godmode prüfen
|
||||
if p.HasGodMode && time.Now().Before(p.GodModeEndTime) {
|
||||
@@ -405,15 +427,9 @@ func (r *Room) Update() {
|
||||
hitY, typeY := r.CheckCollision(p.X+r.pDrawOffX+r.pHitboxOffX, nextY+r.pDrawOffY+r.pHitboxOffY, r.pW, r.pH)
|
||||
if hitY {
|
||||
if typeY == "wall" {
|
||||
// An der Wand: Nicht töten, sondern Position halten
|
||||
if p.OnWall {
|
||||
p.VY = 0
|
||||
} else {
|
||||
// Von oben/unten gegen Wand - töten (kein Klettern in Y-Richtung)
|
||||
p.Y = nextY
|
||||
r.KillPlayer(p)
|
||||
continue
|
||||
}
|
||||
// An der Wand: Nicht töten, Position halten und klettern ermöglichen
|
||||
p.VY = 0
|
||||
p.OnWall = true
|
||||
} else if typeY == "obstacle" {
|
||||
// Obstacle - immer töten
|
||||
p.Y = nextY
|
||||
@@ -774,6 +790,7 @@ func (r *Room) Broadcast() {
|
||||
TimeLeft: r.Countdown,
|
||||
WorldChunks: r.ActiveChunks,
|
||||
HostID: r.HostID,
|
||||
TeamName: r.TeamName,
|
||||
ScrollX: r.GlobalScrollX,
|
||||
CollectedCoins: r.CollectedCoins,
|
||||
CollectedPowerups: r.CollectedPowerups,
|
||||
|
||||
@@ -60,7 +60,8 @@ func (r *Room) CheckCoinCollision(p *ServerPlayer) {
|
||||
if game.CheckRectCollision(playerHitbox, coinHitbox) {
|
||||
// Coin einsammeln!
|
||||
r.CollectedCoins[coinKey] = true
|
||||
p.Score += 200
|
||||
p.BonusScore += 200
|
||||
p.Score = p.DistanceScore + p.BonusScore
|
||||
log.Printf("💰 %s hat Coin eingesammelt! Score: %d", p.Name, p.Score)
|
||||
}
|
||||
}
|
||||
@@ -143,18 +144,28 @@ func (r *Room) UpdateDistanceScore() {
|
||||
return
|
||||
}
|
||||
|
||||
// Jeder Spieler bekommt Punkte basierend auf seiner eigenen Distanz
|
||||
// Punkte = (X-Position / TileSize) = Distanz in Tiles
|
||||
// Zähle lebende Spieler
|
||||
aliveCount := 0
|
||||
for _, p := range r.Players {
|
||||
if p.IsAlive && !p.IsSpectator {
|
||||
// Berechne Score basierend auf X-Position
|
||||
// 1 Punkt pro Tile (64px)
|
||||
newScore := int(p.X / 64.0)
|
||||
aliveCount++
|
||||
}
|
||||
}
|
||||
|
||||
// Nur updaten wenn höher als aktueller Score
|
||||
if newScore > p.Score {
|
||||
p.Score = newScore
|
||||
}
|
||||
if aliveCount == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
// Pro lebendem Spieler werden Punkte hinzugefügt
|
||||
// Dies akkumuliert die Punkte: mehr Spieler = schnellere Punktesammlung
|
||||
// Jeder Tick (bei 60 FPS) fügt aliveCount Punkte hinzu
|
||||
pointsToAdd := aliveCount
|
||||
|
||||
// Jeder lebende Spieler bekommt die gleichen Punkte
|
||||
for _, p := range r.Players {
|
||||
if p.IsAlive && !p.IsSpectator {
|
||||
p.DistanceScore += pointsToAdd
|
||||
p.Score = p.DistanceScore + p.BonusScore
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user