Files
aeternalis/server/internal/transport/appearance.go
T

55 lines
1.5 KiB
Go

package transport
import (
"aeternalis/server/content"
p "aeternalis/server/internal/protocol"
"context"
"net/http"
"time"
)
func (h *HTTP) appearanceRoutes(mux *http.ServeMux) {
mux.HandleFunc("POST /api/appearance", func(w http.ResponseWriter, r *http.Request) {
account, err := h.auth.account(r)
if err != nil {
fail(w, 401, "sign in required")
return
}
var appearance p.Appearance
if decode(w, r, &appearance) != nil || content.ValidateAppearance(&appearance, "human") != nil {
fail(w, 400, "invalid character appearance")
return
}
h.s.mu.Lock()
defer h.s.mu.Unlock()
if !h.s.ready.Load() {
fail(w, 503, "world unavailable")
return
}
c := h.s.world.ForAccount(account)
if c == nil || (c.View.Entity.State != "alive" && c.View.Entity.State != "eligible") {
fail(w, 409, "no living character in this world")
return
}
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
defer cancel()
active, err := h.auth.db.ActiveCharacter(ctx, account)
if err != nil {
fail(w, 503, "character registry unavailable")
return
}
if active == nil || active.WorldID != h.s.world.ID || active.ID != c.View.Entity.ID || active.State == "transit" {
fail(w, 409, "character belongs to another world or is traveling")
return
}
previous := c.View.Entity.Appearance
c.View.Entity.Appearance = &appearance
if err := h.s.persist(ctx, ""); err != nil {
c.View.Entity.Appearance = previous
fail(w, 503, "appearance could not be saved")
return
}
respond(w, 200, map[string]bool{"saved": true})
})
}