package store import ( "aeternalis/server/internal/game" "context" "database/sql" "errors" ) // World and character ownership rows are locked before changing player content. // Business rejections are audited without freezing the owner. Database failures // roll the entire command back and leave the live simulation untouched. func (d *DB) applyContentCommand(ctx context.Context, tx *sql.Tx, w *game.World, command *Command) (string, error) { c := w.Characters[command.Target] if c == nil { return "character not found", nil } if (c.View.Entity.State != "alive" && c.View.Entity.State != "eligible") || w.Phase == "lost" { return "only a living character or survivor can be changed", nil } var account, world, state string err := tx.QueryRowContext(ctx, d.q(`SELECT account_id,world_id,state FROM character_records WHERE character_id=?`), command.Target).Scan(&account, &world, &state) if err != nil { return "", err } if account != c.Account || world != w.ID || state != c.View.Entity.State { return "character has moved to another world", nil } switch command.Op { case "grant-item", "remove-item": if err := w.AdminItems(c, command.Op, command.Value); err != nil { return err.Error(), nil } case "grant-title": award, err := w.AdminTitle(command.Value) if err != nil { return err.Error(), nil } // Restore the original award if revoked; never repurpose a first claim. if _, err = tx.ExecContext(ctx, d.q(`DELETE FROM title_revocations WHERE account_id=? AND title_id=?`), c.Account, command.Value); err != nil { return "", err } w.PendingTitles = append(w.PendingTitles, game.TitleCandidate{CharacterID: c.View.Entity.ID, Award: award}) case "revoke-title": var found string err := tx.QueryRowContext(ctx, d.q(`SELECT title_id FROM title_awards WHERE account_id=? AND title_id=?`), c.Account, command.Value).Scan(&found) if errors.Is(err, sql.ErrNoRows) { return "player does not own that title", nil } if err != nil { return "", err } if _, err = tx.ExecContext(ctx, d.q(`INSERT INTO title_revocations(account_id,title_id,command_id) VALUES(?,?,?) ON CONFLICT(account_id,title_id) DO UPDATE SET command_id=excluded.command_id`), c.Account, command.Value, command.ID); err != nil { return "", err } default: return "unknown player content command", nil } return "", nil } func (d *DB) revokedTitles(ctx context.Context, tx *sql.Tx, account string) (map[string]bool, error) { rows, err := tx.QueryContext(ctx, d.q(`SELECT title_id FROM title_revocations WHERE account_id=?`), account) if err != nil { return nil, err } defer rows.Close() ids := map[string]bool{} for rows.Next() { var id string if err = rows.Scan(&id); err != nil { return nil, err } ids[id] = true } return ids, rows.Err() }