Add durable recipe discovery and profession crafting

This commit is contained in:
dingo
2026-09-09 13:28:07 -04:00
parent 940a345613
commit b65f3e8562
42 changed files with 2183 additions and 136 deletions
+86
View File
@@ -0,0 +1,86 @@
package transport
import (
p "aeternalis/server/internal/protocol"
"aeternalis/server/internal/store"
"errors"
"net/http"
)
func (h *HTTP) craftingRoutes(mux *http.ServeMux) {
mux.HandleFunc("GET /api/crafting", func(w http.ResponseWriter, r *http.Request) {
account, e := h.auth.account(r)
if e != nil {
fail(w, 401, "sign in required")
return
}
h.s.mu.Lock()
defer h.s.mu.Unlock()
if !h.s.ready.Load() {
fail(w, 503, "world unavailable")
return
}
active, e := h.auth.db.ActiveCharacter(r.Context(), account)
if e != nil {
fail(w, 503, "character unavailable")
return
}
c := h.s.world.ForAccount(account)
if c == nil || active == nil || active.ID != c.View.Entity.ID || active.WorldID != h.s.world.ID {
fail(w, 409, "enter your character's world first")
return
}
// Publish profession levels and only recipes this character has discovered.
known := []p.KnownRecipe{}
stale := []string{}
for id, discovery := range c.View.DiscoveredRecipes {
known = append(known, discovery)
current := false
for _, recipe := range h.s.world.Balance.Crafting.Recipes {
if recipe.ID == id && recipe.Revision == discovery.Recipe.Revision {
current = true
}
}
if !current {
stale = append(stale, id)
}
}
respond(w, 200, map[string]any{"professions": h.s.world.CraftProfessions(c), "known": known, "stale": stale, "earnsXP": h.s.world.AllowsXP()})
})
mux.HandleFunc("POST /api/craft", func(w http.ResponseWriter, r *http.Request) {
account, e := h.auth.account(r)
if e != nil {
fail(w, 401, "sign in required")
return
}
var req p.CraftRequest
if decode(w, r, &req) != nil {
fail(w, 400, "invalid craft request")
return
}
h.s.mu.Lock()
defer h.s.mu.Unlock()
if !h.s.ready.Load() {
fail(w, 503, "world unavailable")
return
}
if e = h.s.refreshCatalog(r.Context()); e != nil {
fail(w, 503, "crafting catalog unavailable")
return
}
result, e := h.s.lease.Craft(r.Context(), h.s.world, account, req)
if errors.Is(e, store.ErrCraft) {
fail(w, 409, e.Error())
return
}
if e != nil {
h.s.ready.Store(false)
for _, peer := range h.s.peers {
peer.close()
}
fail(w, 503, "craft save unavailable; retry the same attempt after reconnecting")
return
}
respond(w, 200, result)
})
}
+2 -1
View File
@@ -98,6 +98,7 @@ func (h *HTTP) Handler() http.Handler {
mux := http.NewServeMux()
h.portalRoutes(mux)
h.travelRoutes(mux)
h.craftingRoutes(mux)
h.catalogRoutes(mux)
mux.HandleFunc("GET /healthz", func(w http.ResponseWriter, r *http.Request) {
if !h.s.ready.Load() {
@@ -149,7 +150,7 @@ func (h *HTTP) Handler() http.Handler {
h.s.mu.Lock()
defer h.s.mu.Unlock()
b := h.s.world.Balance
respond(w, 200, map[string]any{"worldId": h.s.world.ID, "kind": h.s.world.Kind, "development": h.auth.dev, "worlds": h.worlds, "chunkSize": b.ChunkSize, "playerSpeed": b.PlayerSpeed, "attackRange": b.AttackRange, "gatherRange": b.GatherRange, "recipes": b.Recipes, "tickHz": b.TickHz})
respond(w, 200, map[string]any{"worldId": h.s.world.ID, "kind": h.s.world.Kind, "development": h.auth.dev, "worlds": h.worlds, "chunkSize": b.ChunkSize, "playerSpeed": b.PlayerSpeed, "attackRange": b.AttackRange, "gatherRange": b.GatherRange, "tickHz": b.TickHz})
})
mux.HandleFunc("GET /api/preview", func(w http.ResponseWriter, r *http.Request) {
h.s.mu.Lock()
+53
View File
@@ -114,6 +114,59 @@ func TestHTTPAndSocketSecurity(t *testing.T) {
if bytes.Contains(data, []byte(`"account"`)) {
t.Fatal("account identity leaked")
}
// Public configuration and unauthenticated routes must never expose recipes.
for _, path := range []string{"/api/config", "/api/crafting", "/api/control/catalog"} {
response, e := server.Client().Get(server.URL + path)
if e != nil {
t.Fatal(e)
}
body, _ := io.ReadAll(response.Body)
response.Body.Close()
if bytes.Contains(body, []byte("recipe_spear")) || bytes.Contains(body, []byte(`"recipes"`)) {
t.Fatal("secret recipes exposed", path)
}
if path != "/api/config" && response.StatusCode == 200 {
t.Fatal("unauthenticated crafting/catalog access", path)
}
}
svc.mu.Lock()
c := svc.world.Characters[character.Entity.ID]
c.View.Inventory["herb"] = 10
svc.mu.Unlock()
craft := p.CraftRequest{ID: game.ID(), CharacterID: character.Entity.ID, Profession: "alchemist", Ingredients: map[string]int64{"herb": 2}}
body, _ := json.Marshal(craft)
for _, origin := range []string{"", "https://evil.example"} {
response := request("/api/craft", origin, string(body), cookie)
response.Body.Close()
if response.StatusCode != 403 {
t.Fatal("cross-origin crafting accepted")
}
}
response = request("/api/craft", server.URL, string(body), cookie)
var outcome p.CraftResult
if e := json.NewDecoder(response.Body).Decode(&outcome); e != nil {
t.Fatal(e)
}
response.Body.Close()
if response.StatusCode != 200 || !outcome.Valid || outcome.Recipe == nil {
t.Fatal("ingredient craft did not record valid recipe", outcome)
}
get, _ := http.NewRequest("GET", server.URL+"/api/crafting", nil)
get.AddCookie(cookie)
response, err = server.Client().Do(get)
if err != nil {
t.Fatal(err)
}
var known struct {
Known []p.KnownRecipe `json:"known"`
}
if err = json.NewDecoder(response.Body).Decode(&known); err != nil {
t.Fatal(err)
}
response.Body.Close()
if len(known.Known) != 1 || known.Known[0].Recipe.ID != "recipe_bandage" {
t.Fatal("crafting returned undiscovered recipes")
}
intent := p.Intent{Op: "move", Seq: 1, X: 1}
payload, _ := json.Marshal(intent)
if err = conn.Write(socketCtx, websocket.MessageText, payload); err != nil {