Add durable recipe discovery and profession crafting
This commit is contained in:
@@ -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)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user