Add walkable housing interiors and cross-hub visitors

This commit is contained in:
dingo
2026-09-10 10:48:26 -04:00
parent b2dd2cb3ba
commit 1bd5d487e2
28 changed files with 721 additions and 52 deletions
+2 -14
View File
@@ -104,19 +104,7 @@ func (d *DB) syncHousing(ctx context.Context, tx *sql.Tx, w *game.World) error {
}
}
}
if house := game.HouseID(c); house != "" {
h, e := d.readHouse(ctx, tx, house, false)
if e != nil {
return e
}
role, e := d.houseRole(ctx, tx, h, c)
if e != nil {
return e
}
if h.View.State != "active" || role == "" || !c.Online || c.View.Entity.State == "dead" || c.View.RemainingSeconds <= 0 {
game.LeaveHouse(c)
}
}
}
if w.Phase == "lost" {
rows, e := tx.QueryContext(ctx, d.q(`SELECT id FROM houses WHERE death_world=? AND state='archived' ORDER BY id`), w.ID)
@@ -149,5 +137,5 @@ func (d *DB) syncHousing(ctx context.Context, tx *sql.Tx, w *game.World) error {
}
}
}
return nil
return d.syncHouseRooms(ctx, tx, w)
}
+94
View File
@@ -0,0 +1,94 @@
package store
import (
"aeternalis/server/internal/game"
p "aeternalis/server/internal/protocol"
"context"
"database/sql"
"encoding/json"
)
func (d *DB) syncHouseRooms(ctx context.Context, tx *sql.Tx, w *game.World) error {
for id, c := range w.Characters {
var world, state string
if e := tx.QueryRowContext(ctx, d.q(`SELECT world_id,state FROM character_records WHERE character_id=?`), id).Scan(&world, &state); e != nil {
return e
}
if world != w.ID || state == "transit" {
continue
}
if house := game.HouseID(c); house != "" {
h, e := d.readHouse(ctx, tx, house, false)
if e != nil {
return e
}
role, e := d.houseRole(ctx, tx, h, c)
if e != nil {
return e
}
if h.View.State != "active" || role == "" || !c.Online || c.Sheltered || c.View.Entity.State == "dead" || c.View.RemainingSeconds <= 0 || !w.IsHub() {
game.LeaveHouse(c)
} else {
c.HouseRoom = w.HouseScene(h.View)
if c.View.HousePosition == nil || !w.HousePositionValid(c.HouseRoom, *c.View.HousePosition) {
pos, ok := w.HouseSpawn(c.HouseRoom)
if !ok {
game.LeaveHouse(c)
} else {
c.View.HousePosition = &pos
c.JumpVelocity = 0
}
}
}
}
if game.HouseID(c) == "" {
if _, e := tx.ExecContext(ctx, d.q(`DELETE FROM house_visitors WHERE character_id=? AND world_id=?`), id, w.ID); e != nil {
return e
}
continue
}
v := c.View.Entity
v.Position = *c.View.HousePosition
raw, e := json.Marshal(v)
if e != nil {
return e
}
if _, e = tx.ExecContext(ctx, d.q(`INSERT INTO house_visitors(character_id,house_id,world_id,updated_at,entity) VALUES(?,?,?,`+d.now()+`,?) ON CONFLICT(character_id) DO UPDATE SET house_id=excluded.house_id,world_id=excluded.world_id,updated_at=excluded.updated_at,entity=excluded.entity`), id, game.HouseID(c), w.ID, string(raw)); e != nil {
return e
}
}
rooms := map[string][]p.Entity{}
for _, c := range w.Characters {
house := game.HouseID(c)
if house == "" || c.HouseRoom == nil {
continue
}
if _, ok := rooms[house]; !ok {
rooms[house] = []p.Entity{}
rows, e := tx.QueryContext(ctx, d.q(`SELECT v.entity FROM house_visitors v JOIN worlds w ON w.id=v.world_id WHERE v.house_id=? AND v.updated_at>=`+d.now()+`-5 AND w.owner<>'' AND w.lease_until>=`+d.now()+` ORDER BY v.character_id LIMIT 256`), house)
if e != nil {
return e
}
for rows.Next() {
var raw string
var v p.Entity
if e = rows.Scan(&raw); e != nil {
rows.Close()
return e
}
if e = json.Unmarshal([]byte(raw), &v); e != nil {
rows.Close()
return e
}
rooms[house] = append(rooms[house], v)
}
e = rows.Err()
rows.Close()
if e != nil {
return e
}
}
c.HouseRoom.People = rooms[house]
}
return nil
}
@@ -0,0 +1,114 @@
package store
import (
"aeternalis/server/content"
"aeternalis/server/internal/game"
p "aeternalis/server/internal/protocol"
"context"
"errors"
"path/filepath"
"testing"
"time"
)
func TestHouseVisitorsAcrossHubOwners(t *testing.T) {
ctx := context.Background()
db, e := Open(ctx, "file:"+filepath.ToSlash(filepath.Join(t.TempDir(), "visitors.db")))
if e != nil {
t.Fatal(e)
}
defer db.Close()
b, _ := content.Load()
now := time.Now()
makeHub := func(id string) (*game.World, *Lease, *game.Character) {
t.Helper()
w := game.New(id, 1, now, b)
c, e := w.Join(id, id)
if e != nil {
t.Fatal(e)
}
c.WorldsSurvived = 1
_ = w.Connect(c.View.Entity.ID)
w.Kind = "hub"
w.Phase = "hub"
l, _, e := db.Acquire(ctx, w)
if e != nil {
t.Fatal(e)
}
if e = l.Save(ctx, w, ""); e != nil {
t.Fatal(e)
}
return w, l, c
}
a, al, owner := makeHub("hub-a")
bworld, bl, guest := makeHub("hub-b")
act := func(w *game.World, l *Lease, c *game.Character, r HouseRequest) {
t.Helper()
r.RequestID = game.ID()
if e := l.HouseAction(ctx, w, c.Account, r); e != nil {
t.Fatal(r.Op, e)
}
}
save := func(w *game.World, l *Lease) {
t.Helper()
if e := l.Save(ctx, w, ""); e != nil {
t.Fatal(e)
}
}
act(a, al, owner, HouseRequest{Op: "create", Name: "Shared room"})
directory, e := db.HouseState(ctx, a, owner.Account)
if e != nil {
t.Fatal(e)
}
house := directory.Houses[0].ID
act(a, al, owner, HouseRequest{Op: "enter", HouseID: house})
act(a, al, owner, HouseRequest{Op: "invite", HouseID: house, Target: guest.View.Entity.ID, Role: "guest"})
act(bworld, bl, guest, HouseRequest{Op: "enter", HouseID: house})
if e := bworld.Intent(guest.View.Entity.ID, p.Intent{Seq: 1, Op: "move", X: 1}); e != nil {
t.Fatal(e)
}
bworld.Advance(now.Add(100*time.Millisecond), .1)
save(bworld, bl)
save(a, al)
scene := a.HouseSnapshot(owner)
if scene == nil || len(scene.People) != 2 {
t.Fatal("cross-hub visitor missing", scene)
}
found := false
for _, v := range scene.People {
if v.ID == guest.View.Entity.ID {
found = true
if v.Position != *guest.View.HousePosition {
t.Fatal("visitor used hub coordinates")
}
}
}
if !found {
t.Fatal("guest missing")
}
if _, exists := a.Characters[guest.View.Entity.ID]; exists {
t.Fatal("replicated visitor into another simulation owner")
}
stale := *bl
stale.Revision--
if e := stale.HouseAction(ctx, bworld, guest.Account, HouseRequest{RequestID: game.ID(), Op: "leave"}); !errors.Is(e, ErrOwnership) {
t.Fatal("unfenced visitor change", e)
}
if game.HouseID(guest) != house {
t.Fatal("rollback published eviction")
}
act(a, al, owner, HouseRequest{Op: "invite", HouseID: house, Target: guest.View.Entity.ID, Role: "remove"})
save(bworld, bl)
save(a, al)
if guest.View.HousePosition != nil || game.HouseID(guest) != "" || len(a.HouseSnapshot(owner).People) != 1 {
t.Fatal("revoked guest retained room presence")
}
act(a, al, owner, HouseRequest{Op: "leave"})
if owner.HouseRoom != nil || owner.View.HousePosition != nil {
t.Fatal("exit retained cached room")
}
var n int
if e = db.sql.QueryRow(`SELECT COUNT(*) FROM house_visitors`).Scan(&n); e != nil || n != 0 {
t.Fatal("stale presence retained", n, e)
}
}
+6
View File
@@ -308,7 +308,13 @@ func (d *DB) mutateHouse(ctx context.Context, tx *sql.Tx, w *game.World, c *game
return houseError("leave the current house first")
}
id := h.View.ID
pos, ok := w.HouseSpawn(w.HouseScene(h.View))
if !ok {
return houseError("no clear space to enter this house")
}
c.View.HouseID = &id
c.View.HousePosition = &pos
c.JumpVelocity = 0
c.View.DestroyKey = nil
c.View.Casting = nil
return nil
+1 -1
View File
@@ -110,7 +110,7 @@ func testHousingLifecycle(t *testing.T, dsn string) {
reject(owner, HouseRequest{Op: "create", Name: "Duplicate"})
reject(guest, HouseRequest{Op: "enter", HouseID: house})
act(owner, HouseRequest{Op: "enter", HouseID: house})
if e := w.Intent(owner.View.Entity.ID, p.Intent{Seq: 1, Op: "jump"}); e == nil {
if e := w.Intent(owner.View.Entity.ID, p.Intent{Seq: 1, Op: "attack"}); e == nil {
t.Fatal("world action inside house")
}
if _, e := w.AttemptCraft(owner, p.CraftRequest{Profession: "carver", Ingredients: map[string]int64{"wood": 1}}); e == nil {
+2
View File
@@ -67,6 +67,8 @@ func (d *DB) migrate(ctx context.Context) error {
for _, stmt := range []string{
`CREATE TABLE IF NOT EXISTS houses(id TEXT PRIMARY KEY,creator_account TEXT NOT NULL,creator_character TEXT NOT NULL,death_world TEXT NOT NULL,death_id TEXT NOT NULL,death_at BIGINT NOT NULL DEFAULT 0,state TEXT NOT NULL,document TEXT NOT NULL)`,
`CREATE UNIQUE INDEX IF NOT EXISTS one_created_house ON houses(creator_account) WHERE state<>'purged'`,
`CREATE TABLE IF NOT EXISTS house_visitors(character_id TEXT PRIMARY KEY,house_id TEXT NOT NULL,world_id TEXT NOT NULL,updated_at BIGINT NOT NULL,entity TEXT NOT NULL)`,
`CREATE INDEX IF NOT EXISTS visitors_house ON house_visitors(house_id)`,
`CREATE TABLE IF NOT EXISTS house_keys(id TEXT PRIMARY KEY,house_id TEXT NOT NULL,state TEXT NOT NULL)`,
`CREATE INDEX IF NOT EXISTS keys_house ON house_keys(house_id,state)`,
`CREATE TABLE IF NOT EXISTS house_invites(house_id TEXT NOT NULL,character_id TEXT NOT NULL,PRIMARY KEY(house_id,character_id))`,
+9
View File
@@ -205,6 +205,9 @@ func (l *Lease) save(ctx context.Context, w *game.World, claimID string, command
if err = d.applyHouse(ctx, tx, w, housing); err != nil {
return err
}
if err = d.syncHouseRooms(ctx, tx, w); err != nil {
return err
}
}
if err = d.syncTitles(ctx, tx, w); err != nil {
return err
@@ -259,7 +262,13 @@ func (l *Lease) save(ctx context.Context, w *game.World, claimID string, command
}
l.Revision++
for id, c := range w.Characters {
if game.HouseID(live.Characters[id]) != game.HouseID(c) {
live.Characters[id].MoveX = 0
live.Characters[id].MoveZ = 0
live.Characters[id].JumpVelocity = 0
}
live.Characters[id].View = c.View
live.Characters[id].HouseRoom = c.HouseRoom
}
live.PendingTitles = nil
live.Saved()