package store import ( "aeternalis/server/internal/game" "context" "database/sql" "encoding/json" "errors" "fmt" "maps" ) var ErrRevival = errors.New("revival rejected") type reviveClaim struct{ Account, Character, Corpse, World string } // A permanent account pointer records the most recently created life, including // after its death. Repeated deaths never make an older life current again. func (d *DB) reserveCharacterLife(ctx context.Context, tx *sql.Tx, world string, c *game.Character) error { if _, e := tx.ExecContext(ctx, d.q(`INSERT INTO account_lives(account_id,character_id) VALUES(?,?) ON CONFLICT(account_id) DO NOTHING`), c.Account, c.View.Entity.ID); e != nil { return e } if _, e := tx.ExecContext(ctx, d.q(`UPDATE account_lives SET character_id=character_id WHERE account_id=?`), c.Account); e != nil { return e } var current string if e := tx.QueryRowContext(ctx, d.q(`SELECT character_id FROM account_lives WHERE account_id=?`), c.Account).Scan(¤t); e != nil { return e } var state, oldWorld string e := tx.QueryRowContext(ctx, d.q(`SELECT state,world_id FROM character_records WHERE character_id=?`), c.View.Entity.ID).Scan(&state, &oldWorld) if e != nil && !errors.Is(e, sql.ErrNoRows) { return e } if errors.Is(e, sql.ErrNoRows) && c.View.Entity.State != "dead" { if _, e = tx.ExecContext(ctx, d.q(`UPDATE account_lives SET character_id=? WHERE account_id=?`), c.View.Entity.ID, c.Account); e != nil { return e } } else if e == nil && oldWorld == world && c.View.Entity.State != "dead" { if current != c.View.Entity.ID { return ErrLivingCharacter } if state == "dead" { var revived int if e = tx.QueryRowContext(ctx, d.q(`SELECT revived FROM character_deaths WHERE corpse_id=? AND character_id=?`), c.RevivalCorpse, c.View.Entity.ID).Scan(&revived); e != nil || revived != 1 { return ErrRevival } } } if c.View.Entity.State == "dead" && c.RevivalCorpse != "" { raw, e := json.Marshal(c) if e != nil { return e } _, e = tx.ExecContext(ctx, d.q(`INSERT INTO character_deaths(corpse_id,character_id,world_id,character,revived) VALUES(?,?,?,?,0) ON CONFLICT(corpse_id) DO NOTHING`), c.RevivalCorpse, c.View.Entity.ID, world, string(raw)) if e != nil { return e } } return nil } func (d *DB) checkRevival(ctx context.Context, tx *sql.Tx, r *reviveClaim) error { if _, e := tx.ExecContext(ctx, d.q(`UPDATE account_lives SET character_id=character_id WHERE account_id=?`), r.Account); e != nil { return e } var current string if e := tx.QueryRowContext(ctx, d.q(`SELECT character_id FROM account_lives WHERE account_id=?`), r.Account).Scan(¤t); e != nil { return e } if current != r.Character { return fmt.Errorf("%w: a replacement character was created", ErrRevival) } var state, world, raw string if e := tx.QueryRowContext(ctx, d.q(`SELECT state,world_id,character FROM character_records WHERE character_id=? AND account_id=?`), r.Character, r.Account).Scan(&state, &world, &raw); e != nil { return e } var dead game.Character if e := json.Unmarshal([]byte(raw), &dead); e != nil { return e } if state != "dead" || world != r.World || dead.RevivalCorpse != r.Corpse || !dead.RevivalAllowed { return ErrRevival } result, e := tx.ExecContext(ctx, d.q(`UPDATE character_deaths SET revived=1 WHERE corpse_id=? AND character_id=? AND revived=0`), r.Corpse, r.Character) if e != nil { return e } n, _ := result.RowsAffected() if n != 1 { return ErrRevival } return nil } func (d *DB) RevivalCurrent(ctx context.Context, account, id string) (bool, error) { var current string e := d.sql.QueryRowContext(ctx, d.q(`SELECT character_id FROM account_lives WHERE account_id=?`), account).Scan(¤t) if errors.Is(e, sql.ErrNoRows) { return false, nil } return current == id, e } func (l *Lease) RevivalConsent(ctx context.Context, w *game.World, account, id string, allowed bool) error { c := w.Characters[id] if c == nil || c.Account != account || c.View.Entity.State != "dead" || c.RevivalCorpse == "" || w.Corpses[c.RevivalCorpse] == nil || w.Phase == "lost" { return ErrRevival } current, e := l.DB.RevivalCurrent(ctx, account, id) if e != nil { return e } if !current { return ErrRevival } staged := stageTitleWorld(w) staged.Characters[id].RevivalAllowed = allowed if e = l.Save(ctx, staged, ""); e != nil { return e } for id, c := range staged.Characters { *w.Characters[id] = *c } w.Saved() return nil } func (l *Lease) Revive(ctx context.Context, w *game.World, account, corpse, power string) error { caster := w.ForAccount(account) if caster == nil { return ErrRevival } active, e := l.DB.ActiveCharacter(ctx, account) if e != nil { return e } if active == nil || active.ID != caster.View.Entity.ID || active.WorldID != w.ID { return ErrRevival } staged := stageTitleWorld(w) staged.Corpses = maps.Clone(w.Corpses) target, e := staged.Revive(staged.Characters[caster.View.Entity.ID], corpse, power) if e != nil { return fmt.Errorf("%w: %v", ErrRevival, e) } claim := &reviveClaim{target.Account, target.View.Entity.ID, corpse, w.ID} if e = l.save(ctx, staged, "", nil, "", nil, claim); e != nil { return e } for id, c := range staged.Characters { *w.Characters[id] = *c } w.Corpses = staged.Corpses w.PendingTitles = nil w.Saved() return nil }