Private
Public Access
1
0

fix game
All checks were successful
Dynamic Branch Deploy / build-and-deploy (push) Successful in 8m20s

This commit is contained in:
Sebastian Unterschütz
2026-03-21 13:31:34 +01:00
parent 78742fc1c4
commit f48ade50bb
6 changed files with 130 additions and 83 deletions

View File

@@ -66,11 +66,19 @@ type LoginPayload struct {
// Input vom Spieler während des Spiels
type ClientInput struct {
Type string `json:"type"` // "JUMP", "START", "LEFT_DOWN", "RIGHT_DOWN", "SET_TEAM_NAME", etc.
Type string `json:"type"` // "STATE", "START", "SET_TEAM_NAME", etc.
RoomID string `json:"room_id"`
PlayerID string `json:"player_id"`
Sequence uint32 `json:"sequence"` // Sequenznummer für Client Prediction
TeamName string `json:"team_name,omitempty"` // Für SET_TEAM_NAME Input
// Vollständiger Input-State (für TYPE="STATE")
// Sendet den kompletten Zustand in einer Nachricht, verhindert stuck-Inputs durch Paketverlust
InputLeft bool `json:"input_left,omitempty"`
InputRight bool `json:"input_right,omitempty"`
InputJump bool `json:"input_jump,omitempty"`
InputDown bool `json:"input_down,omitempty"`
InputJoyX float64 `json:"input_joy_x,omitempty"`
}
type JoinRequest struct {

View File

@@ -146,17 +146,16 @@ func (gw *Gateway) HandleWS(w http.ResponseWriter, r *http.Request) {
continue // Ignoriere böswilligen Input
}
// 🔒 SECURITY: Setze IMMER die korrekten IDs (überschreibe Client-Werte)
input := game.ClientInput{
Type: inputType,
RoomID: roomID, // Server setzt den Raum (nicht Client!)
PlayerID: playerID, // Server setzt die Player-ID (nicht Client!)
}
// 🔒 SECURITY: Alle Input-Felder übernehmen, aber IDs immer vom Server setzen
// Remarshal des raw-Objekts in ClientInput um alle Felder (inkl. STATE-Felder) zu übernehmen
inputBytes, _ := json.Marshal(raw)
var input game.ClientInput
json.Unmarshal(inputBytes, &input)
// Sequence-Nummer vom Client übernehmen (für Client Prediction)
if seq, ok := raw["sequence"].(float64); ok {
input.Sequence = uint32(seq)
}
// Security-kritische Felder vom Server überschreiben (nie Client-Werten vertrauen)
input.Type = inputType
input.RoomID = roomID // Server setzt den Raum
input.PlayerID = playerID // Server setzt die Player-ID
bytes, _ := json.Marshal(input)
gw.NC.Publish(fmt.Sprintf("game.room.%s.input", roomID), bytes)

View File

@@ -276,16 +276,49 @@ func (r *Room) HandleInput(input game.ClientInput) {
}
switch input.Type {
case "STATE":
// Vollständigen Input-State atomisch setzen verhindert stuck-Inputs durch
// Paketverlust oder Reihenfolge-Probleme bei Event-basierten Nachrichten.
// Out-of-Order-Schutz: nur neuere States übernehmen
if input.Sequence <= p.LastInputSeq {
return
}
// Richtung: JoyX hat Vorrang vor digitalen Tasten
if input.InputJoyX != 0 {
p.InputX = input.InputJoyX
} else if input.InputLeft && !input.InputRight {
p.InputX = -1
} else if input.InputRight && !input.InputLeft {
p.InputX = 1
} else {
p.InputX = 0
}
// Jump/Down: einmal setzen, nie löschen (Physics-Tick macht das)
if input.InputJump {
p.InputJump = true
// Double Jump spezial-Logik (außerhalb der Physik-Engine)
if !p.OnGround && p.HasDoubleJump && !p.DoubleJumpUsed {
p.VY = -config.JumpVelocity
p.DoubleJumpUsed = true
log.Printf("⚡ %s verwendet Double Jump!", p.Name)
}
}
if input.InputDown {
p.InputDown = true
}
// Legacy-Events (Rückwärtskompatibilität, werden vom neuen Client nicht mehr gesendet)
case "JUMP":
p.InputJump = true // Setze Jump-Flag für Physik-Engine
// Double Jump spezial-Logik (außerhalb der Physik-Engine)
p.InputJump = true
if !p.OnGround && p.HasDoubleJump && !p.DoubleJumpUsed {
p.VY = -config.JumpVelocity
p.DoubleJumpUsed = true
log.Printf("⚡ %s verwendet Double Jump!", p.Name)
}
case "DOWN":
p.InputDown = true // Setze Down-Flag für Fast Fall
p.InputDown = true
case "LEFT_DOWN":
p.InputX = -1
case "LEFT_UP":
@@ -298,6 +331,7 @@ func (r *Room) HandleInput(input game.ClientInput) {
if p.InputX == 1 {
p.InputX = 0
}
case "START":
if input.PlayerID == r.HostID && r.Status == "LOBBY" {
r.StartCountdown()