Add durable recipe discovery and profession crafting
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"aeternalis/server/internal/game"
|
||||
p "aeternalis/server/internal/protocol"
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var ErrCraft = errors.New("craft rejected")
|
||||
|
||||
type craftReceipt struct{ ID, Account, CharacterID, Request, Result string }
|
||||
|
||||
func (d *DB) CraftReceipt(ctx context.Context, account string, req p.CraftRequest) (*p.CraftResult, error) {
|
||||
var request, result string
|
||||
e := d.sql.QueryRowContext(ctx, d.q(`SELECT request_json,result_json FROM craft_receipts WHERE account_id=? AND id=?`), account, req.ID).Scan(&request, &result)
|
||||
if errors.Is(e, sql.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
raw, _ := json.Marshal(req)
|
||||
if string(raw) != request {
|
||||
return nil, fmt.Errorf("%w: attempt ID already used with different ingredients", ErrCraft)
|
||||
}
|
||||
var out p.CraftResult
|
||||
e = json.Unmarshal([]byte(result), &out)
|
||||
return &out, e
|
||||
}
|
||||
|
||||
// Called under the world service mutex. Consumption, rolls, discovery, XP and
|
||||
// the replay receipt share the same fenced snapshot transaction.
|
||||
func (l *Lease) Craft(ctx context.Context, live *game.World, account string, req p.CraftRequest) (p.CraftResult, error) {
|
||||
fail := func(msg string) (p.CraftResult, error) { return p.CraftResult{}, fmt.Errorf("%w: %s", ErrCraft, msg) }
|
||||
if !game.ValidCraftRequest(req) {
|
||||
return fail("invalid crafting request")
|
||||
}
|
||||
if old, e := l.DB.CraftReceipt(ctx, account, req); e != nil {
|
||||
return p.CraftResult{}, e
|
||||
} else if old != nil {
|
||||
return *old, nil
|
||||
}
|
||||
active, e := l.DB.ActiveCharacter(ctx, account)
|
||||
if e != nil {
|
||||
return p.CraftResult{}, e
|
||||
}
|
||||
if active == nil || active.ID != req.CharacterID || active.WorldID != live.ID || active.State != "alive" {
|
||||
return fail("character is not active in this world")
|
||||
}
|
||||
original := live.Characters[req.CharacterID]
|
||||
if original == nil || original.Account != account {
|
||||
return fail("character unavailable")
|
||||
}
|
||||
staged := stageTitleWorld(live)
|
||||
// Deep-copy only the acting view; leave all other players' live inputs intact.
|
||||
raw, e := json.Marshal(original.View)
|
||||
if e != nil {
|
||||
return p.CraftResult{}, e
|
||||
}
|
||||
var view p.Character
|
||||
if e = json.Unmarshal(raw, &view); e != nil {
|
||||
return p.CraftResult{}, e
|
||||
}
|
||||
staged.Characters[req.CharacterID].View = view
|
||||
result, e := staged.AttemptCraft(staged.Characters[req.CharacterID], req)
|
||||
if e != nil {
|
||||
return fail(e.Error())
|
||||
}
|
||||
request, _ := json.Marshal(req)
|
||||
out, _ := json.Marshal(result)
|
||||
receipt := &craftReceipt{req.ID, account, req.CharacterID, string(request), string(out)}
|
||||
if e = l.save(ctx, staged, "", nil, "", receipt); e != nil {
|
||||
return p.CraftResult{}, e
|
||||
}
|
||||
// Retain character pointers captured by existing WebSocket handlers.
|
||||
for id, c := range staged.Characters {
|
||||
*live.Characters[id] = *c
|
||||
}
|
||||
live.RandomCounter = staged.RandomCounter
|
||||
live.PendingTitles = nil
|
||||
live.Saved()
|
||||
return result, nil
|
||||
}
|
||||
Reference in New Issue
Block a user