Add anatomical character customization at initial creation

This commit is contained in:
dingo
2026-09-12 16:00:15 -04:00
parent 90e33cbcd9
commit 4aca703b6f
25 changed files with 733 additions and 133 deletions
+5 -44
View File
@@ -1,54 +1,15 @@
package transport
import (
"aeternalis/server/content"
p "aeternalis/server/internal/protocol"
"context"
"net/http"
"time"
)
import "net/http"
// Looks are committed together with the name on initial join. Keep the old route
// explicit so a stale client cannot silently offer a free appearance change.
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 {
if _, err := h.auth.account(r); 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})
fail(w, 403, "appearance can only be chosen when creating a character")
})
}
+20 -8
View File
@@ -9,6 +9,7 @@ import (
"net/http"
"net/http/httptest"
"path/filepath"
"reflect"
"strings"
"testing"
"time"
@@ -28,12 +29,14 @@ func TestCharacterAppearanceOwnershipAndDurability(t *testing.T) {
t.Fatal(err)
}
svc := NewService(world, lease)
a := content.Appearance{Version: 1, Family: "human", Variant: 3, Seed: 42, Height: 1, Width: 1, Detail: .5, Primary: "#d8a582", Secondary: "#493947", Accent: "#578f8b"}
a := *content.DefaultCharacterAppearance()
a.Body.HeadWidth = 1.1
a.Body.Chest = "female"
view, err := svc.join(ctx, "artist", "Artist", "", &a)
if err != nil {
t.Fatal(err)
}
if view.Entity.Appearance == nil || *view.Entity.Appearance != a {
if !reflect.DeepEqual(view.Entity.Appearance, &a) {
t.Fatal("new character appearance missing")
}
other := a
@@ -41,7 +44,7 @@ func TestCharacterAppearanceOwnershipAndDurability(t *testing.T) {
if _, err = svc.join(ctx, "artist", "Artist", "", &other); err != nil {
t.Fatal(err)
}
if *world.ForAccount("artist").View.Entity.Appearance != a {
if !reflect.DeepEqual(world.ForAccount("artist").View.Entity.Appearance, &a) {
t.Fatal("join retry overwrote appearance")
}
auth, _ := NewAuth(ctx, db, true, "http://127.0.0.1", "", "", "")
@@ -72,15 +75,24 @@ func TestCharacterAppearanceOwnershipAndDurability(t *testing.T) {
}
bad := other
bad.Family = "creature"
if request(bad, token, server.URL) != 400 {
if request(bad, token, server.URL) != 403 {
t.Fatal("player could choose creature")
}
before := world.ForAccount("artist").View.Stats["movementSpeed"]
if request(other, token, server.URL) != 200 {
t.Fatal("appearance save rejected")
before := world.ForAccount("artist").View.Stats["moveSpeed"]
if request(other, token, server.URL) != 403 {
t.Fatal("existing character appearance could be changed")
}
record, err := db.ActiveCharacter(ctx, "artist")
if err != nil || *record.Character.View.Entity.Appearance != other || record.Character.View.Stats["movementSpeed"] != before {
if err != nil || !reflect.DeepEqual(record.Character.View.Entity.Appearance, &a) || record.Character.View.Stats["moveSpeed"] != before {
t.Fatal("appearance not durable or changed stats", err)
}
invalid := *content.DefaultCharacterAppearance()
invalid.Body.NoseScale = 200
if _, err := svc.join(ctx, "invalid", "Invalid", "", &invalid); err == nil || world.ForAccount("invalid") != nil {
t.Fatal("invalid initial appearance created a character")
}
plain, err := svc.join(ctx, "plain", "Plain", "")
if err != nil || !reflect.DeepEqual(plain.Entity.Appearance, content.DefaultCharacterAppearance()) {
t.Fatal("creation without customization did not get the default anatomy", err)
}
}
+7
View File
@@ -159,8 +159,15 @@ func (s *Service) join(ctx context.Context, account, name, claim string, appeara
return p.Character{}, err
}
copy := *appearance[0]
if copy.Body != nil {
body := *copy.Body
copy.Body = &body
}
chosen = &copy
}
if existing == nil && chosen == nil {
chosen = content.DefaultCharacterAppearance()
}
if claim != "" {
c, err = s.lease.DB.ReadPassage(ctx, claim, account)
if err != nil {