Add anatomical character customization at initial creation
This commit is contained in:
@@ -1,36 +1,42 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
"aeternalis/server/content"
|
||||
p "aeternalis/server/internal/protocol"
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAppearanceRestartAndReplacement(t *testing.T) {
|
||||
w, c := fixture(t)
|
||||
a := p.Appearance{Version: 1, Family: "human", Variant: 1, Seed: 42, Height: 1, Width: 1, Detail: .5, Primary: "#d8a582", Secondary: "#493947", Accent: "#578f8b"}
|
||||
c.View.Entity.Appearance = &a
|
||||
raw, err := json.Marshal(w)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var restored World
|
||||
if err = json.Unmarshal(raw, &restored); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = restored.Restore(w.now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
life := restored.Characters[c.View.Entity.ID]
|
||||
if life.View.Entity.Appearance == nil || *life.View.Entity.Appearance != a {
|
||||
t.Fatal("appearance lost on restart")
|
||||
}
|
||||
restored.kill(life, "test replacement")
|
||||
replacement, err := restored.Join(c.Account, "Replacement")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if replacement.View.Entity.ID == life.View.Entity.ID || replacement.View.Entity.Appearance != nil {
|
||||
t.Fatal("replacement inherited prior life's appearance")
|
||||
for _, a := range []*p.Appearance{
|
||||
{Version: 1, Family: "human", Variant: 1, Seed: 42, Height: 1, Width: 1, Detail: .5, Primary: "#d8a582", Secondary: "#493947", Accent: "#578f8b"},
|
||||
content.DefaultCharacterAppearance(),
|
||||
} {
|
||||
w, c := fixture(t)
|
||||
c.View.Entity.Appearance = a
|
||||
raw, err := json.Marshal(w)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var restored World
|
||||
if err = json.Unmarshal(raw, &restored); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = restored.Restore(w.now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
life := restored.Characters[c.View.Entity.ID]
|
||||
if !reflect.DeepEqual(life.View.Entity.Appearance, a) {
|
||||
t.Fatal("appearance lost on restart")
|
||||
}
|
||||
restored.kill(life, "test replacement")
|
||||
replacement, err := restored.Join(c.Account, "Replacement")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if replacement.View.Entity.ID == life.View.Entity.ID || replacement.View.Entity.Appearance != nil {
|
||||
t.Fatal("replacement inherited prior life's appearance")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
p "aeternalis/server/internal/protocol"
|
||||
"aeternalis/server/content"
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"testing"
|
||||
@@ -153,7 +153,7 @@ func TestCastingInterruptMissAndWeaponUnlock(t *testing.T) {
|
||||
|
||||
func TestCorpseRevivalKeepsLootAndDailyBudget(t *testing.T) {
|
||||
w, c := fixture(t)
|
||||
appearance := p.Appearance{Version: 1, Family: "human", Variant: 2, Seed: 42, Height: 1, Width: 1, Detail: .5, Primary: "#d8a582", Secondary: "#493947", Accent: "#578f8b"}
|
||||
appearance := *content.DefaultCharacterAppearance()
|
||||
c.View.Entity.Appearance = &appearance
|
||||
rescuer, e := w.Join("rescuer", "Rescuer")
|
||||
if e != nil {
|
||||
|
||||
@@ -115,16 +115,39 @@ type MobDefinition struct {
|
||||
}
|
||||
|
||||
type Appearance struct {
|
||||
Accent string `json:"accent"`
|
||||
Detail float64 `json:"detail"`
|
||||
Family string `json:"family"`
|
||||
Height float64 `json:"height"`
|
||||
Primary string `json:"primary"`
|
||||
Secondary string `json:"secondary"`
|
||||
Seed int64 `json:"seed"`
|
||||
Variant int64 `json:"variant"`
|
||||
Version int64 `json:"version"`
|
||||
Width float64 `json:"width"`
|
||||
Accent string `json:"accent"`
|
||||
Body *HumanoidBody `json:"body,omitempty"`
|
||||
Detail float64 `json:"detail"`
|
||||
Family string `json:"family"`
|
||||
Height float64 `json:"height"`
|
||||
Primary string `json:"primary"`
|
||||
Secondary string `json:"secondary"`
|
||||
Seed int64 `json:"seed"`
|
||||
Variant int64 `json:"variant"`
|
||||
Version int64 `json:"version"`
|
||||
Width float64 `json:"width"`
|
||||
}
|
||||
|
||||
type HumanoidBody struct {
|
||||
ArmLength float64 `json:"armLength"`
|
||||
ArmWidth float64 `json:"armWidth"`
|
||||
Chest string `json:"chest"`
|
||||
ChestWidth float64 `json:"chestWidth"`
|
||||
EyeColor string `json:"eyeColor"`
|
||||
EyeHeight float64 `json:"eyeHeight"`
|
||||
EyeSpacing float64 `json:"eyeSpacing"`
|
||||
Eyebrows string `json:"eyebrows"`
|
||||
FootSize float64 `json:"footSize"`
|
||||
HeadDepth float64 `json:"headDepth"`
|
||||
HeadLength float64 `json:"headLength"`
|
||||
HeadWidth float64 `json:"headWidth"`
|
||||
HipsWidth float64 `json:"hipsWidth"`
|
||||
LegLength float64 `json:"legLength"`
|
||||
LegWidth float64 `json:"legWidth"`
|
||||
MouthHeight float64 `json:"mouthHeight"`
|
||||
MouthStyle string `json:"mouthStyle"`
|
||||
NoseHeight float64 `json:"noseHeight"`
|
||||
NoseScale float64 `json:"noseScale"`
|
||||
}
|
||||
|
||||
type MobSpawn struct {
|
||||
|
||||
@@ -64,7 +64,7 @@ func runTravelTests(t *testing.T, dsn string) {
|
||||
c.View.Experience = 7
|
||||
c.View.StatPoints = 20
|
||||
c.DailySeconds = 123
|
||||
c.View.Entity.Appearance = &content.Appearance{Version: 1, Family: "human", Variant: 2, Seed: 42, Height: 1, Width: 1, Detail: .5, Primary: "#d8a582", Secondary: "#493947", Accent: "#578f8b"}
|
||||
c.View.Entity.Appearance = content.DefaultCharacterAppearance()
|
||||
c.WorldsSurvived = 2
|
||||
if e = source.AdminItems(c, "grant-item", "spear:1"); e != nil {
|
||||
t.Fatal(e)
|
||||
|
||||
@@ -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")
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 = ©
|
||||
}
|
||||
if existing == nil && chosen == nil {
|
||||
chosen = content.DefaultCharacterAppearance()
|
||||
}
|
||||
if claim != "" {
|
||||
c, err = s.lease.DB.ReadPassage(ctx, claim, account)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user