bug fixes
This commit is contained in:
86
handlers.go
86
handlers.go
@@ -3,9 +3,11 @@ package main
|
||||
import (
|
||||
"encoding/json"
|
||||
"html"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -63,7 +65,7 @@ func handleValidate(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// ---> HIER RUFEN WIR JETZT DIE SIMULATION AUF <---
|
||||
isDead, score, obstacles := simulateChunk(req.SessionID, req.Inputs, req.TotalTicks, vals)
|
||||
isDead, score, obstacles, powerUpState, serverTick := simulateChunk(req.SessionID, req.Inputs, req.TotalTicks, vals)
|
||||
|
||||
status := "alive"
|
||||
if isDead {
|
||||
@@ -76,6 +78,8 @@ func handleValidate(w http.ResponseWriter, r *http.Request) {
|
||||
Status: status,
|
||||
VerifiedScore: score,
|
||||
ServerObs: obstacles,
|
||||
PowerUps: powerUpState,
|
||||
ServerTick: serverTick,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -86,8 +90,19 @@ func handleSubmitName(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// Validierung
|
||||
if len(req.Name) > 4 {
|
||||
http.Error(w, "Zu lang", 400)
|
||||
return
|
||||
}
|
||||
if containsBadWord(req.Name) {
|
||||
http.Error(w, "Name verboten", 400)
|
||||
return
|
||||
}
|
||||
|
||||
safeName := html.EscapeString(req.Name)
|
||||
sessionKey := "session:" + req.SessionID
|
||||
|
||||
scoreVal, err := rdb.HGet(ctx, sessionKey, "score").Result()
|
||||
if err != nil {
|
||||
http.Error(w, "Session expired", 404)
|
||||
@@ -104,10 +119,13 @@ func handleSubmitName(w http.ResponseWriter, r *http.Request) {
|
||||
"created_at": timestamp,
|
||||
})
|
||||
|
||||
rdb.ZAdd(ctx, "leaderboard:unverified", redis.Z{
|
||||
Score: float64(scoreInt),
|
||||
Member: req.SessionID,
|
||||
})
|
||||
// Leaderboard Eintrag
|
||||
rdb.ZAdd(ctx, "leaderboard:unverified", redis.Z{Score: float64(scoreInt), Member: req.SessionID})
|
||||
rdb.ZAdd(ctx, "leaderboard:public", redis.Z{Score: float64(scoreInt), Member: req.SessionID})
|
||||
|
||||
rdb.Persist(ctx, sessionKey)
|
||||
|
||||
rdb.HDel(ctx, sessionKey, "obstacles", "rng_state", "pos_y", "vel_y", "p_god_lives", "p_has_bat", "p_boot_ticks")
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(SubmitResponse{ClaimCode: claimCode})
|
||||
@@ -191,6 +209,9 @@ func handleAdminAction(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "Bad Request", 400)
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("👮 ADMIN ACTION: %s on %s", req.Action, req.SessionID)
|
||||
|
||||
if req.Action == "approve" {
|
||||
score, err := rdb.ZScore(ctx, "leaderboard:unverified", req.SessionID).Result()
|
||||
if err == nil {
|
||||
@@ -200,7 +221,10 @@ func handleAdminAction(w http.ResponseWriter, r *http.Request) {
|
||||
} else if req.Action == "delete" {
|
||||
rdb.ZRem(ctx, "leaderboard:unverified", req.SessionID)
|
||||
rdb.ZRem(ctx, "leaderboard:public", req.SessionID)
|
||||
|
||||
rdb.Del(ctx, "session:"+req.SessionID)
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
@@ -210,16 +234,28 @@ func handleClaimDelete(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "Bad Request", 400)
|
||||
return
|
||||
}
|
||||
|
||||
sessionKey := "session:" + req.SessionID
|
||||
realCode, err := rdb.HGet(ctx, sessionKey, "claim_code").Result()
|
||||
|
||||
if err != nil || realCode != req.ClaimCode {
|
||||
http.Error(w, "Error", 403)
|
||||
if err != nil || realCode == "" {
|
||||
http.Error(w, "Not found", 404)
|
||||
return
|
||||
}
|
||||
|
||||
if realCode != req.ClaimCode {
|
||||
http.Error(w, "Wrong Code", 403)
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("🗑️ USER DELETE: Session %s deleted via code", req.SessionID)
|
||||
|
||||
// Aus Listen entfernen
|
||||
rdb.ZRem(ctx, "leaderboard:unverified", req.SessionID)
|
||||
rdb.ZRem(ctx, "leaderboard:public", req.SessionID)
|
||||
rdb.HDel(ctx, sessionKey, "name")
|
||||
|
||||
rdb.Del(ctx, sessionKey)
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
@@ -231,3 +267,37 @@ func generateClaimCode() string {
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func handleAdminBadwords(w http.ResponseWriter, r *http.Request) {
|
||||
key := "config:badwords"
|
||||
|
||||
// GET: Liste abrufen
|
||||
if r.Method == http.MethodGet {
|
||||
words, _ := rdb.SMembers(ctx, key).Result()
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(words)
|
||||
return
|
||||
}
|
||||
|
||||
// POST: Hinzufügen oder Löschen
|
||||
if r.Method == http.MethodPost {
|
||||
// Wir nutzen ein einfaches Struct für den Request
|
||||
type WordReq struct {
|
||||
Word string `json:"word"`
|
||||
Action string `json:"action"` // "add" oder "remove"
|
||||
}
|
||||
var req WordReq
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
http.Error(w, "Bad Request", 400)
|
||||
return
|
||||
}
|
||||
|
||||
if req.Action == "add" && req.Word != "" {
|
||||
rdb.SAdd(ctx, key, strings.ToLower(req.Word))
|
||||
} else if req.Action == "remove" && req.Word != "" {
|
||||
rdb.SRem(ctx, key, strings.ToLower(req.Word))
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user