124 lines
3.4 KiB
Go
124 lines
3.4 KiB
Go
package store
|
|
|
|
import (
|
|
"aeternalis/server/internal/game"
|
|
p "aeternalis/server/internal/protocol"
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"slices"
|
|
)
|
|
|
|
func (d *DB) HouseState(ctx context.Context, w *game.World, account string) (p.HouseState, error) {
|
|
out := p.HouseState{Houses: []p.HouseSummary{}, People: []p.Entity{}, Effective: map[string]bool{}}
|
|
c := w.ForAccount(account)
|
|
if c == nil {
|
|
return out, houseError("living character required")
|
|
}
|
|
tx, e := d.sql.BeginTx(ctx, nil)
|
|
if e != nil {
|
|
return out, e
|
|
}
|
|
defer tx.Rollback()
|
|
var world string
|
|
e = tx.QueryRowContext(ctx, d.q(`SELECT world_id FROM character_records WHERE character_id=? AND state IN ('alive','eligible')`), c.View.Entity.ID).Scan(&world)
|
|
if errors.Is(e, sql.ErrNoRows) || e == nil && world != w.ID {
|
|
return out, houseError("character is in another world")
|
|
}
|
|
if e != nil {
|
|
return out, e
|
|
}
|
|
ids := map[string]bool{}
|
|
rows, e := tx.QueryContext(ctx, d.q(`SELECT id FROM houses WHERE creator_account=? UNION SELECT house_id FROM house_invites WHERE character_id=?`), account, c.View.Entity.ID)
|
|
if e != nil {
|
|
return out, e
|
|
}
|
|
for rows.Next() {
|
|
var id string
|
|
if e = rows.Scan(&id); e != nil {
|
|
rows.Close()
|
|
return out, e
|
|
}
|
|
ids[id] = true
|
|
}
|
|
e = rows.Err()
|
|
rows.Close()
|
|
if e != nil {
|
|
return out, e
|
|
}
|
|
for _, i := range c.View.Items {
|
|
if i.HouseID != nil {
|
|
ids[*i.HouseID] = true
|
|
}
|
|
}
|
|
sorted := make([]string, 0, len(ids))
|
|
for id := range ids {
|
|
sorted = append(sorted, id)
|
|
}
|
|
slices.Sort(sorted)
|
|
for _, id := range sorted {
|
|
h, e := d.readHouse(ctx, tx, id, false)
|
|
if e != nil {
|
|
return out, e
|
|
}
|
|
role, e := d.houseRole(ctx, tx, h, c)
|
|
if e != nil {
|
|
return out, e
|
|
}
|
|
n, e := d.keyCount(ctx, tx, id)
|
|
if e != nil {
|
|
return out, e
|
|
}
|
|
out.Houses = append(out.Houses, p.HouseSummary{ID: id, Name: h.View.Name, State: h.View.State, Role: role, Creator: h.CreatorAccount == account, RecoveryWorld: h.DeathWorld, Keys: n})
|
|
if game.HouseID(c) == id && h.View.State == "active" && role != "" {
|
|
out.Role = role
|
|
out.Effective = housePermissions(h, c, role)
|
|
if !out.Effective["openFurniture"] {
|
|
for key, f := range h.View.Furniture {
|
|
f.Contents = game.EmptyHouseInventory()
|
|
h.View.Furniture[key] = f
|
|
}
|
|
}
|
|
if role != "owner" && role != "co-owner" {
|
|
h.View.Roles = map[string]string{}
|
|
h.View.Overrides = map[string]map[string]bool{}
|
|
h.View.Permissions = map[string]bool{}
|
|
}
|
|
out.Room = &h.View
|
|
}
|
|
}
|
|
for _, other := range w.Characters {
|
|
if other.View.Entity.State == "alive" || other.View.Entity.State == "eligible" {
|
|
v := p.Entity{ID: other.View.Entity.ID, Name: other.View.Entity.Name, Kind: "player"}
|
|
out.People = append(out.People, v)
|
|
}
|
|
}
|
|
// Keep invitations understandable after guests switch hubs or disconnect.
|
|
if out.Room != nil && (out.Role == "owner" || out.Role == "co-owner") {
|
|
for id := range out.Room.Roles {
|
|
if slices.ContainsFunc(out.People, func(v p.Entity) bool { return v.ID == id }) {
|
|
continue
|
|
}
|
|
var name string
|
|
e := tx.QueryRowContext(ctx, d.q(`SELECT name FROM character_records WHERE character_id=?`), id).Scan(&name)
|
|
if errors.Is(e, sql.ErrNoRows) {
|
|
continue
|
|
}
|
|
if e != nil {
|
|
return out, e
|
|
}
|
|
out.People = append(out.People, p.Entity{ID: id, Name: name, Kind: "player"})
|
|
}
|
|
}
|
|
slices.SortFunc(out.People, func(a, b p.Entity) int {
|
|
if a.Name < b.Name {
|
|
return -1
|
|
}
|
|
if a.Name > b.Name {
|
|
return 1
|
|
}
|
|
return 0
|
|
})
|
|
return out, tx.Commit()
|
|
}
|