Add walkable housing interiors and cross-hub visitors
This commit is contained in:
@@ -0,0 +1,152 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
p "aeternalis/server/internal/protocol"
|
||||
"errors"
|
||||
"math"
|
||||
"slices"
|
||||
)
|
||||
|
||||
// House geometry contains no storage contents, permissions, or account identities.
|
||||
func (w *World) HouseScene(h p.House) *p.HouseScene {
|
||||
out := &p.HouseScene{ID: h.ID, Name: h.Name, Size: h.Size, Furniture: []p.RoomFurniture{}, People: []p.Entity{}}
|
||||
for id, f := range h.Furniture {
|
||||
if f.Item.Furniture == nil {
|
||||
continue
|
||||
}
|
||||
profile := f.Item.Furniture
|
||||
height := w.Balance.Housing.TableHeight
|
||||
if profile.Container {
|
||||
height = w.Balance.Housing.StorageHeight
|
||||
}
|
||||
out.Furniture = append(out.Furniture, p.RoomFurniture{ID: id, Name: f.Item.Name, X: f.X, Z: f.Z, Width: profile.Width, Depth: profile.Depth, Height: height, Container: profile.Container})
|
||||
}
|
||||
slices.SortFunc(out.Furniture, func(a, b p.RoomFurniture) int {
|
||||
if a.ID < b.ID {
|
||||
return -1
|
||||
}
|
||||
if a.ID > b.ID {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
})
|
||||
return out
|
||||
}
|
||||
func roomGround(room *p.HouseScene, pos p.Vec3) float64 {
|
||||
ground := 0.
|
||||
for _, f := range room.Furniture {
|
||||
if math.Abs(pos.X-f.X) < f.Width/2 && math.Abs(pos.Z-f.Z) < f.Depth/2 {
|
||||
ground = math.Max(ground, f.Height)
|
||||
}
|
||||
}
|
||||
return ground
|
||||
}
|
||||
func (w *World) HousePositionValid(room *p.HouseScene, pos p.Vec3) bool {
|
||||
radius := w.Balance.Housing.PlayerRadius
|
||||
if math.IsNaN(pos.X) || math.IsNaN(pos.Y) || math.IsNaN(pos.Z) || math.IsInf(pos.X, 0) || math.IsInf(pos.Y, 0) || math.IsInf(pos.Z, 0) || math.Abs(pos.X)+radius > room.Size/2 || math.Abs(pos.Z)+radius > room.Size/2 || pos.Y < 0 || pos.Y > 3 {
|
||||
return false
|
||||
}
|
||||
for _, f := range room.Furniture {
|
||||
if math.Abs(pos.X-f.X) < f.Width/2+radius && math.Abs(pos.Z-f.Z) < f.Depth/2+radius && pos.Y < f.Height-.001 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
func (w *World) HouseSpawn(room *p.HouseScene) (p.Vec3, bool) {
|
||||
for z := room.Size/2 - 1.; z >= -room.Size/2+1; z -= .5 {
|
||||
for x := -room.Size/2 + 1; x <= room.Size/2-1; x += .5 {
|
||||
pos := p.Vec3{X: x, Z: z}
|
||||
if w.HousePositionValid(room, pos) {
|
||||
return pos, true
|
||||
}
|
||||
}
|
||||
}
|
||||
return p.Vec3{}, false
|
||||
}
|
||||
func (w *World) houseIntent(c *Character, i p.Intent) error {
|
||||
if !w.IsHub() || c.HouseRoom == nil || c.View.HousePosition == nil {
|
||||
return errors.New("house is synchronizing")
|
||||
}
|
||||
switch i.Op {
|
||||
case "move":
|
||||
n := math.Max(1, math.Hypot(i.X, i.Z))
|
||||
c.MoveX = i.X / n
|
||||
c.MoveZ = i.Z / n
|
||||
c.LastMove = w.now
|
||||
return nil
|
||||
case "jump":
|
||||
if math.Abs(c.View.HousePosition.Y-roomGround(c.HouseRoom, *c.View.HousePosition)) > .02 || c.JumpVelocity != 0 {
|
||||
return errors.New("already jumping")
|
||||
}
|
||||
c.JumpVelocity = w.Balance.Housing.JumpSpeed
|
||||
w.dirty = true
|
||||
return nil
|
||||
default:
|
||||
return errors.New("use house controls or leave to use world actions")
|
||||
}
|
||||
}
|
||||
func (w *World) moveHouse(c *Character, dt float64) {
|
||||
room := c.HouseRoom
|
||||
if room == nil || c.View.HousePosition == nil || room.ID != HouseID(c) {
|
||||
return
|
||||
}
|
||||
old := *c.View.HousePosition
|
||||
pos := old
|
||||
r := w.Balance.Housing
|
||||
// Small substeps prevent tunneling through narrow furniture on a delayed tick.
|
||||
steps := max(1, int(math.Ceil(dt*r.WalkSpeed/.1)))
|
||||
step := dt / float64(steps)
|
||||
for range steps {
|
||||
if c.JumpVelocity != 0 || pos.Y > roomGround(room, pos) {
|
||||
c.JumpVelocity -= r.Gravity * step
|
||||
pos.Y += c.JumpVelocity * step
|
||||
}
|
||||
pos.Y = math.Max(pos.Y, roomGround(room, pos))
|
||||
if pos.Y >= 3 {
|
||||
pos.Y = 3
|
||||
c.JumpVelocity = math.Min(0, c.JumpVelocity)
|
||||
}
|
||||
next := pos
|
||||
next.X += c.MoveX * r.WalkSpeed * step
|
||||
if w.HousePositionValid(room, next) {
|
||||
pos.X = next.X
|
||||
}
|
||||
next = pos
|
||||
next.Z += c.MoveZ * r.WalkSpeed * step
|
||||
if w.HousePositionValid(room, next) {
|
||||
pos.Z = next.Z
|
||||
}
|
||||
ground := roomGround(room, pos)
|
||||
if pos.Y <= ground {
|
||||
pos.Y = ground
|
||||
c.JumpVelocity = 0
|
||||
}
|
||||
}
|
||||
if pos != old {
|
||||
c.View.HousePosition = &pos
|
||||
w.dirty = true
|
||||
}
|
||||
}
|
||||
func (w *World) HouseSnapshot(c *Character) *p.HouseScene {
|
||||
if HouseID(c) == "" || c.HouseRoom == nil {
|
||||
return nil
|
||||
}
|
||||
out := *c.HouseRoom
|
||||
out.People = []p.Entity{}
|
||||
// Local movement is current to this simulation tick. Foreign visitors are read
|
||||
// from their owning world's leased presence heartbeat, never simulated here.
|
||||
for _, v := range c.HouseRoom.People {
|
||||
if _, local := w.Characters[v.ID]; !local {
|
||||
out.People = append(out.People, v)
|
||||
}
|
||||
}
|
||||
for _, other := range w.orderedCharacters() {
|
||||
if HouseID(other) == out.ID && other.Online && other.View.HousePosition != nil {
|
||||
v := other.View.Entity
|
||||
v.Position = *other.View.HousePosition
|
||||
out.People = append(out.People, v)
|
||||
}
|
||||
}
|
||||
return &out
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
p "aeternalis/server/internal/protocol"
|
||||
"math"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestIndoorMovementCollisionJumpAndReturn(t *testing.T) {
|
||||
w, c := fixture(t)
|
||||
w.Kind = "hub"
|
||||
w.Phase = "hub"
|
||||
house := ID()
|
||||
c.View.HouseID = &house
|
||||
c.HouseRoom = &p.HouseScene{ID: house, Size: 12, Furniture: []p.RoomFurniture{{ID: ID(), Width: 2, Depth: 2, Height: .8}}}
|
||||
pos := p.Vec3{X: -3}
|
||||
c.View.HousePosition = &pos
|
||||
hub := c.View.Entity.Position
|
||||
xp := c.View.Experience
|
||||
if e := w.Intent(c.View.Entity.ID, p.Intent{Seq: 1, Op: "move", X: 1}); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
for range 40 {
|
||||
w.moveHouse(c, .05)
|
||||
}
|
||||
if c.View.HousePosition.X > -1.34 || c.View.HousePosition.X < -1.5 {
|
||||
t.Fatal("walked through table", *c.View.HousePosition)
|
||||
}
|
||||
if e := w.Intent(c.View.Entity.ID, p.Intent{Seq: 2, Op: "jump"}); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if e := w.Intent(c.View.Entity.ID, p.Intent{Seq: 3, Op: "jump"}); e == nil {
|
||||
t.Fatal("air jump accepted")
|
||||
}
|
||||
top := 0.
|
||||
for range 12 {
|
||||
w.moveHouse(c, .05)
|
||||
top = math.Max(top, c.View.HousePosition.Y)
|
||||
}
|
||||
if top < .8 || c.View.HousePosition.X < -.8 {
|
||||
t.Fatal("could not jump onto furniture", top, *c.View.HousePosition)
|
||||
}
|
||||
for range 100 {
|
||||
w.moveHouse(c, .05)
|
||||
}
|
||||
if c.View.HousePosition.X > 6-w.Balance.Housing.PlayerRadius || c.View.HousePosition.Y != 0 {
|
||||
t.Fatal("room boundary or landing failed", *c.View.HousePosition)
|
||||
}
|
||||
if e := w.Intent(c.View.Entity.ID, p.Intent{Seq: 4, Op: "attack"}); e == nil {
|
||||
t.Fatal("house combat accepted")
|
||||
}
|
||||
if c.View.Entity.Position != hub || c.View.Experience != xp {
|
||||
t.Fatal("indoor movement changed hub position or XP")
|
||||
}
|
||||
c.LastMove = w.now.Add(-time.Second)
|
||||
w.Advance(w.now, .05)
|
||||
if c.MoveX != 0 {
|
||||
t.Fatal("stale indoor move did not stop")
|
||||
}
|
||||
LeaveHouse(c)
|
||||
if c.View.HousePosition != nil || c.HouseRoom != nil || c.View.Entity.Position != hub || c.JumpVelocity != 0 {
|
||||
t.Fatal("exit did not clear room state")
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,14 @@ func HouseID(c *Character) string {
|
||||
}
|
||||
return *c.View.HouseID
|
||||
}
|
||||
func LeaveHouse(c *Character) { c.View.HouseID = nil; c.MoveX = 0; c.MoveZ = 0 }
|
||||
func LeaveHouse(c *Character) {
|
||||
c.View.HouseID = nil
|
||||
c.View.HousePosition = nil
|
||||
c.HouseRoom = nil
|
||||
c.JumpVelocity = 0
|
||||
c.MoveX = 0
|
||||
c.MoveZ = 0
|
||||
}
|
||||
func (w *World) restoreHousing() error {
|
||||
if w.Balance.Housing == nil {
|
||||
b, e := content.Load()
|
||||
@@ -28,6 +35,20 @@ func (w *World) restoreHousing() error {
|
||||
cat.RestoreFurniture(b)
|
||||
w.Balance.Items = cat.Items
|
||||
}
|
||||
if w.Balance.Housing.WalkSpeed == 0 {
|
||||
b, e := content.Load()
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
r := *w.Balance.Housing
|
||||
r.WalkSpeed = b.Housing.WalkSpeed
|
||||
r.PlayerRadius = b.Housing.PlayerRadius
|
||||
r.JumpSpeed = b.Housing.JumpSpeed
|
||||
r.Gravity = b.Housing.Gravity
|
||||
r.TableHeight = b.Housing.TableHeight
|
||||
r.StorageHeight = b.Housing.StorageHeight
|
||||
w.Balance.Housing = &r
|
||||
}
|
||||
for _, c := range w.Characters {
|
||||
LeaveHouse(c)
|
||||
c.View.DestroyKey = nil
|
||||
|
||||
@@ -14,15 +14,16 @@ import (
|
||||
)
|
||||
|
||||
type Character struct {
|
||||
XPCurveVersion int `json:"xpCurveVersion"`
|
||||
PendingCast *PendingCast `json:"pendingCast,omitempty"`
|
||||
RevivalCorpse string `json:"revivalCorpse,omitempty"`
|
||||
RevivalAllowed bool `json:"revivalAllowed"`
|
||||
DeathCount int64 `json:"deathCount"`
|
||||
CraftReadyAt float64 `json:"craftReadyAt"`
|
||||
AttackReadyAt float64 `json:"attackReadyAt"`
|
||||
JumpVelocity float64 `json:"jumpVelocity"`
|
||||
GatherReadyAt float64 `json:"gatherReadyAt"`
|
||||
HouseRoom *p.HouseScene `json:"-"`
|
||||
XPCurveVersion int `json:"xpCurveVersion"`
|
||||
PendingCast *PendingCast `json:"pendingCast,omitempty"`
|
||||
RevivalCorpse string `json:"revivalCorpse,omitempty"`
|
||||
RevivalAllowed bool `json:"revivalAllowed"`
|
||||
DeathCount int64 `json:"deathCount"`
|
||||
CraftReadyAt float64 `json:"craftReadyAt"`
|
||||
AttackReadyAt float64 `json:"attackReadyAt"`
|
||||
JumpVelocity float64 `json:"jumpVelocity"`
|
||||
GatherReadyAt float64 `json:"gatherReadyAt"`
|
||||
|
||||
View p.Character `json:"view"`
|
||||
Account string `json:"account"`
|
||||
@@ -526,7 +527,7 @@ func (w *World) Intent(id string, i p.Intent) error {
|
||||
}
|
||||
c.View.ACK = i.Seq
|
||||
if HouseID(c) != "" {
|
||||
return errors.New("leave the house to use world actions")
|
||||
return w.houseIntent(c, i)
|
||||
}
|
||||
if c.Sheltered && ((i.Op == "move" && (i.X != 0 || i.Z != 0)) || (i.Op != "move" && i.Op != "heal" && i.Op != "allocate-stat" && i.Op != "choose-vital" && i.Op != "equip" && i.Op != "unequip" && i.Op != "drop")) {
|
||||
c.Sheltered = false
|
||||
@@ -791,6 +792,8 @@ func (w *World) Advance(now time.Time, dt float64) {
|
||||
}
|
||||
if HouseID(c) == "" {
|
||||
w.moveCharacter(c, dt)
|
||||
} else {
|
||||
w.moveHouse(c, dt)
|
||||
}
|
||||
for _, ch := range w.neighbors(c.View.Entity.Position) {
|
||||
active[ch.Key] = true
|
||||
@@ -859,6 +862,7 @@ func (w *World) Advance(now time.Time, dt float64) {
|
||||
func (w *World) Snapshot(id string, known map[string]bool) p.Snapshot {
|
||||
c := w.Characters[id]
|
||||
s := p.Snapshot{Type: "snapshot", Version: 1, Tick: w.Tick, World: w.Info(), Self: c.View, Message: c.Message, Entities: []p.Entity{}, Chunks: []p.Chunk{}, ChunkKeys: []string{}, UnavailableResources: []string{}}
|
||||
s.House = w.HouseSnapshot(c)
|
||||
s.GatherRisks = map[string]p.GatheringRisk{}
|
||||
allowed := map[string]bool{}
|
||||
for _, ch := range w.neighbors(c.View.Entity.Position) {
|
||||
|
||||
@@ -725,6 +725,7 @@ type Snapshot struct {
|
||||
Chunks []Chunk `json:"chunks"`
|
||||
Entities []Entity `json:"entities"`
|
||||
GatherRisks map[string]GatheringRisk `json:"gatherRisks,omitempty"`
|
||||
House *HouseScene `json:"house,omitempty"`
|
||||
Message string `json:"message"`
|
||||
ProjectileVisuals []ProjectileVisual `json:"projectileVisuals,omitempty"`
|
||||
Self Character `json:"self"`
|
||||
@@ -756,6 +757,25 @@ type GatheringRisk struct {
|
||||
Seconds float64 `json:"seconds"`
|
||||
}
|
||||
|
||||
type HouseScene struct {
|
||||
Furniture []RoomFurniture `json:"furniture"`
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
People []Entity `json:"people"`
|
||||
Size float64 `json:"size"`
|
||||
}
|
||||
|
||||
type RoomFurniture struct {
|
||||
Container bool `json:"container"`
|
||||
Depth float64 `json:"depth"`
|
||||
Height float64 `json:"height"`
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Width float64 `json:"width"`
|
||||
X float64 `json:"x"`
|
||||
Z float64 `json:"z"`
|
||||
}
|
||||
|
||||
type ProjectileVisual struct {
|
||||
At float64 `json:"at"`
|
||||
Destination Vec3 `json:"destination"`
|
||||
@@ -788,6 +808,7 @@ type Character struct {
|
||||
HasKeystone bool `json:"hasKeystone"`
|
||||
HealthChoices int64 `json:"healthChoices"`
|
||||
HouseID *string `json:"houseId,omitempty"`
|
||||
HousePosition *Vec3 `json:"housePosition,omitempty"`
|
||||
Inventory map[string]int64 `json:"inventory"`
|
||||
Items []ItemInstance `json:"items"`
|
||||
Kills int64 `json:"kills"`
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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))`,
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user