142 lines
3.8 KiB
Go
142 lines
3.8 KiB
Go
package store
|
|
|
|
import (
|
|
"aeternalis/server/internal/game"
|
|
p "aeternalis/server/internal/protocol"
|
|
"context"
|
|
"database/sql"
|
|
"slices"
|
|
)
|
|
|
|
func (d *DB) destroyHouseKey(ctx context.Context, tx *sql.Tx, key, house string, visited map[string]bool) error {
|
|
h, e := d.readHouse(ctx, tx, house, true)
|
|
if e != nil {
|
|
return e
|
|
}
|
|
if _, e = tx.ExecContext(ctx, d.q(`UPDATE house_keys SET state='destroyed' WHERE id=? AND house_id=? AND state='active'`), key, house); e != nil {
|
|
return e
|
|
}
|
|
count, e := d.keyCount(ctx, tx, house)
|
|
if e != nil {
|
|
return e
|
|
}
|
|
if count != 0 {
|
|
return nil
|
|
}
|
|
return d.archiveHouse(ctx, tx, h, visited)
|
|
}
|
|
func (d *DB) archiveHouse(ctx context.Context, tx *sql.Tx, h *houseRecord, visited map[string]bool) error {
|
|
if visited[h.View.ID] {
|
|
return nil
|
|
}
|
|
visited[h.View.ID] = true
|
|
h.View.State = "archived"
|
|
ended, e := d.recoveryEnded(ctx, tx, h)
|
|
if e != nil {
|
|
return e
|
|
}
|
|
if ended {
|
|
// Purging stored keys also updates their houses; registry counts never retain
|
|
// keys whose containing inventory has been permanently removed.
|
|
keys := []p.ItemInstance{}
|
|
keys = append(keys, h.View.Floor.Items...)
|
|
for _, f := range h.View.Furniture {
|
|
keys = append(keys, f.Contents.Items...)
|
|
}
|
|
slices.SortFunc(keys, func(a, b p.ItemInstance) int {
|
|
if a.ID < b.ID {
|
|
return -1
|
|
}
|
|
if a.ID > b.ID {
|
|
return 1
|
|
}
|
|
return 0
|
|
})
|
|
purgeHouse(h)
|
|
if e = d.writeHouse(ctx, tx, h); e != nil {
|
|
return e
|
|
}
|
|
for _, key := range keys {
|
|
if key.HouseID != nil {
|
|
if e = d.destroyHouseKey(ctx, tx, key.ID, *key.HouseID, visited); e != nil {
|
|
return e
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
return d.writeHouse(ctx, tx, h)
|
|
}
|
|
func (d *DB) syncHousing(ctx context.Context, tx *sql.Tx, w *game.World) error {
|
|
ids := make([]string, 0, len(w.Characters))
|
|
for id := range w.Characters {
|
|
ids = append(ids, id)
|
|
}
|
|
slices.Sort(ids)
|
|
for _, id := range ids {
|
|
c := w.Characters[id]
|
|
var ownerWorld, state string
|
|
if e := tx.QueryRowContext(ctx, d.q(`SELECT world_id,state FROM character_records WHERE character_id=?`), id).Scan(&ownerWorld, &state); e != nil {
|
|
return e
|
|
}
|
|
// An old source copy may checkpoint, but cannot touch a transferred key.
|
|
if ownerWorld != w.ID || state == "transit" {
|
|
continue
|
|
}
|
|
if c.View.Entity.State == "dead" && c.DiedAt != nil && c.RevivalCorpse != "" {
|
|
// Compare death time so an old source-world copy cannot replace a later death.
|
|
if _, e := tx.ExecContext(ctx, d.q(`UPDATE houses SET death_world=?,death_id=?,death_at=? WHERE creator_character=? AND death_at<?`), w.ID, c.RevivalCorpse, c.DiedAt.UnixMilli(), id, c.DiedAt.UnixMilli()); e != nil {
|
|
return e
|
|
}
|
|
}
|
|
if a := c.View.DestroyKey; a != nil && w.NowSeconds() >= a.FinishesAt {
|
|
c.View.DestroyKey = nil
|
|
if c.Online && !c.Sheltered && c.View.RemainingSeconds > 0 && (c.View.Entity.State == "alive" || c.View.Entity.State == "eligible") {
|
|
for i, key := range c.View.Items {
|
|
if key.ID == a.KeyID && key.HouseID != nil {
|
|
if e := d.destroyHouseKey(ctx, tx, key.ID, *key.HouseID, map[string]bool{}); e != nil {
|
|
return e
|
|
}
|
|
c.View.Items = append(c.View.Items[:i:i], c.View.Items[i+1:]...)
|
|
w.RefreshStats(c)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
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)
|
|
if e != nil {
|
|
return e
|
|
}
|
|
houses := []string{}
|
|
for rows.Next() {
|
|
var id string
|
|
if e = rows.Scan(&id); e != nil {
|
|
rows.Close()
|
|
return e
|
|
}
|
|
houses = append(houses, id)
|
|
}
|
|
e = rows.Err()
|
|
rows.Close()
|
|
if e != nil {
|
|
return e
|
|
}
|
|
for _, id := range houses {
|
|
h, e := d.readHouse(ctx, tx, id, true)
|
|
if e != nil {
|
|
return e
|
|
}
|
|
if h.View.State == "archived" {
|
|
if e = d.archiveHouse(ctx, tx, h, map[string]bool{}); e != nil {
|
|
return e
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return d.syncHouseRooms(ctx, tx, w)
|
|
}
|