138 lines
3.6 KiB
Go
138 lines
3.6 KiB
Go
package transport
|
|
|
|
import (
|
|
"aeternalis/server/content"
|
|
"aeternalis/server/internal/game"
|
|
"aeternalis/server/internal/store"
|
|
"errors"
|
|
"net/http"
|
|
)
|
|
|
|
func (h *HTTP) powerRoutes(mux *http.ServeMux) {
|
|
mux.HandleFunc("GET /api/powers", 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()
|
|
c := h.s.world.ForAccount(account)
|
|
if c == nil {
|
|
fail(w, 409, "character unavailable")
|
|
return
|
|
}
|
|
books := map[string]string{}
|
|
for id, name := range (content.Catalog{Powers: h.s.world.Balance.Powers}).PowerItems() {
|
|
if c.View.Inventory[id] > 0 {
|
|
books[id] = name
|
|
}
|
|
}
|
|
respond(w, 200, map[string]any{"items": books})
|
|
})
|
|
mux.HandleFunc("GET /api/revival", 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, "registry unavailable")
|
|
return
|
|
}
|
|
if active != nil {
|
|
respond(w, 200, map[string]any{"active": active.WorldID, "available": false})
|
|
return
|
|
}
|
|
var dead *game.Character
|
|
for _, c := range h.s.world.Characters {
|
|
if c.Account == account && c.View.Entity.State == "dead" && c.RevivalCorpse != "" {
|
|
current, e := h.auth.db.RevivalCurrent(r.Context(), account, c.View.Entity.ID)
|
|
if e != nil {
|
|
fail(w, 503, "registry unavailable")
|
|
return
|
|
}
|
|
if current {
|
|
dead = c
|
|
break
|
|
}
|
|
}
|
|
}
|
|
if dead == nil || h.s.world.Phase == "lost" || h.s.world.Corpses[dead.RevivalCorpse] == nil {
|
|
respond(w, 200, map[string]bool{"available": false})
|
|
return
|
|
}
|
|
respond(w, 200, map[string]any{"available": true, "characterId": dead.View.Entity.ID, "name": dead.View.Entity.Name, "corpse": dead.RevivalCorpse, "allowed": dead.RevivalAllowed})
|
|
})
|
|
mux.HandleFunc("POST /api/revival/consent", 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 struct {
|
|
CharacterID string `json:"characterId"`
|
|
Allowed bool `json:"allowed"`
|
|
}
|
|
if decode(w, r, &req) != nil {
|
|
fail(w, 400, "invalid consent")
|
|
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.lease.RevivalConsent(r.Context(), h.s.world, account, req.CharacterID, req.Allowed); e != nil {
|
|
h.powerFailure(w, e)
|
|
return
|
|
}
|
|
respond(w, 200, map[string]bool{"allowed": req.Allowed})
|
|
})
|
|
mux.HandleFunc("POST /api/revive", 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 struct {
|
|
Corpse string `json:"corpse"`
|
|
Power string `json:"power"`
|
|
}
|
|
if decode(w, r, &req) != nil {
|
|
fail(w, 400, "invalid revival")
|
|
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.lease.Revive(r.Context(), h.s.world, account, req.Corpse, req.Power); e != nil {
|
|
h.powerFailure(w, e)
|
|
return
|
|
}
|
|
respond(w, 200, map[string]bool{"revived": true})
|
|
})
|
|
}
|
|
func (h *HTTP) powerFailure(w http.ResponseWriter, e error) {
|
|
if errors.Is(e, store.ErrRevival) || errors.Is(e, store.ErrLivingCharacter) {
|
|
fail(w, 409, e.Error())
|
|
return
|
|
}
|
|
h.s.ready.Store(false)
|
|
for _, p := range h.s.peers {
|
|
p.close()
|
|
}
|
|
fail(w, 503, "revival save unavailable; reconnect to check the result")
|
|
}
|