Add leaderboard functionality with Redis integration for scores. This includes a global leaderboard system, server-side score submission handling, and real-time player ranking updates. Refactor and improve collision logic and game state management for better player experience.
This commit is contained in:
@@ -28,6 +28,11 @@ func main() {
|
||||
globalWorld = game.NewWorld()
|
||||
loadServerAssets(globalWorld)
|
||||
|
||||
// 1b. Redis-Leaderboard initialisieren
|
||||
if err := server.InitLeaderboard("localhost:6379"); err != nil {
|
||||
log.Fatal("❌ Konnte nicht zu Redis verbinden: ", err)
|
||||
}
|
||||
|
||||
// 2. NATS VERBINDUNG
|
||||
natsURL := "nats://localhost:4222"
|
||||
nc, err := nats.Connect(natsURL)
|
||||
@@ -99,6 +104,22 @@ func main() {
|
||||
}
|
||||
})
|
||||
|
||||
// 5. HANDLER: SCORE SUBMISSION
|
||||
_, _ = ec.Subscribe("score.submit", func(submission *game.ScoreSubmission) {
|
||||
log.Printf("📊 Score-Submission: %s (%s) mit %d Punkten", submission.PlayerName, submission.PlayerCode, submission.Score)
|
||||
added := server.GlobalLeaderboard.AddScore(submission.PlayerName, submission.PlayerCode, submission.Score)
|
||||
if added {
|
||||
log.Printf("✅ Score akzeptiert für %s", submission.PlayerName)
|
||||
}
|
||||
})
|
||||
|
||||
// 6. HANDLER: LEADERBOARD REQUEST
|
||||
_, _ = ec.Subscribe("leaderboard.get", func(subject, reply string, _ *struct{}) {
|
||||
top10 := server.GlobalLeaderboard.GetTop10()
|
||||
log.Printf("📊 Leaderboard-Request beantwortet: %d Einträge", len(top10))
|
||||
ec.Publish(reply, top10)
|
||||
})
|
||||
|
||||
log.Println("✅ Server bereit. Warte auf Spieler...")
|
||||
|
||||
// Block forever
|
||||
|
||||
Reference in New Issue
Block a user