Class CharacterAbilityService
Service for managing character abilities in the database. Provides async operations for CRUD operations on character ability data. Uses the BaseService execution strategy for automatic retry on transient database failures. Returns DatabaseResult for consistent, safe error handling.
public sealed class CharacterAbilityService : BaseService<CharacterAbilityEntity>, ICharacterAbilityService, ICountByKeyAction<long>, IPersistAction<CharacterAbilityData, long>, IPersistManyAction<CharacterAbilityData>, IPersistManyOwnedAction<CharacterAbilityData>, IDeleteByKeyVersionedAction<long>, IFetchCollectionByKeyAction<long, CharacterAbilityData>
- Inheritance
-
CharacterAbilityService
- Implements
- Inherited Members
Constructors
CharacterAbilityService(INpgsqlDbContextFactory)
Initializes a new instance of the CharacterAbilityService class.
public CharacterAbilityService(INpgsqlDbContextFactory dbContextFactory)
Parameters
dbContextFactoryINpgsqlDbContextFactoryFactory for creating database contexts.
Exceptions
- ArgumentNullException
Thrown when dbContextFactory is null.
Methods
CountAsync(long, CancellationToken)
Counts items for the given key.
public Task<DatabaseResult<int>> CountAsync(long characterId, CancellationToken cancellationToken = default)
Parameters
characterIdlongcancellationTokenCancellationTokenToken to cancel the operation.
Returns
DeleteAbilityAsync(long, long, long, CancellationToken)
Removes one ability outright, so the character may craft the same template again.
public Task<DatabaseResult> DeleteAbilityAsync(long characterId, long abilityId, long incomingVersion, CancellationToken cancellationToken = default)
Parameters
characterIdlongThe character that owns the ability. Checked, so one character cannot delete another's row.
abilityIdlongThe row identity, never the template id.
incomingVersionlongThe authoritative, monotonic version for this delete. The row is removed when its own version is at or below this.
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
Remarks
Distinct from DeleteAsync(long, long, long, CancellationToken), which leaves
a tombstone. A forgotten ability is not a record of anything — it is the character no
longer having it — and the tombstone is what would stand in the way of re-crafting it.
The upsert keys on (character_id, template_id) and refuses to drop below the
surviving row's version, so a soft delete that stamped the caller's version into the row
would leave it holding a version no fresh craft could outrank, and the upsert's own
id <= 0 guard would then reject the re-craft as a stale write. Forget-then-craft
is a flow the craft panel explicitly advertises.
This mirrors ICharacterItemService.DeleteItemAsync, which removed a vacated row
outright for the same reason.
DeleteAbilityOwnedAsync(long, long, long, CharacterSessionLeaseData, bool, CancellationToken)
DeleteAbilityAsync(long, long, long, CancellationToken), admitted only while the writer still holds the character's session claim.
public Task<DatabaseResult> DeleteAbilityOwnedAsync(long characterId, long abilityId, long incomingVersion, CharacterSessionLeaseData claim, bool admitReleased = false, CancellationToken cancellationToken = default)
Parameters
characterIdlongThe character that owns the ability.
abilityIdlongThe row identity, never the template id.
incomingVersionlongThe version ceiling; the row is removed when its own version is at or below this.
claimCharacterSessionLeaseDataThe claim the delete was captured under. Must name
characterId.admitReleasedboolAlso admit an unclaimed character. Only for undoing the writer's own row.
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
- Task<DatabaseResult>
Success when the row is gone. Fails with Forbidden when the claim is not held (see
admitReleased).
Remarks
The forget a resident character asks for. The claim is checked, under the character's
share lock, in the delete's own transaction — see CharacterWriteGate.
admitReleased is for a delete that UNDOES a row the same writer wrote
under claim (the grant's revoke): it is then also admitted while the
character holds no claim at all, released and not yet claimed again, because the
character may have left before the revoke could run and nobody has the row loaded. It is
still refused once another session holds the character.
DeleteAsync(long, long, long, CancellationToken)
Deletes a single ability record for a character if incomingVersion is newer.
public Task<DatabaseResult> DeleteAsync(long characterId, long abilityId, long incomingVersion, CancellationToken cancellationToken = default)
Parameters
characterIdlongThe character ID that owns the ability.
abilityIdlongThe ability identifier.
incomingVersionlongThe authoritative, monotonic version for this delete operation.
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
- Task<DatabaseResult>
A DatabaseResult indicating success or failure.
DeleteAsync(long, long, CancellationToken)
Deletes the entity identified by the given key if incomingVersion is newer.
public Task<DatabaseResult> DeleteAsync(long characterId, long incomingVersion, CancellationToken cancellationToken = default)
Parameters
characterIdlongincomingVersionlongThe authoritative, monotonic version for this delete operation.
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
FetchAsync(long, CancellationToken)
Fetches a collection of items for the given key.
public Task<DatabaseResult<IReadOnlyList<CharacterAbilityData>>> FetchAsync(long characterId, CancellationToken cancellationToken = default)
Parameters
characterIdlongcancellationTokenCancellationTokenToken to cancel the operation.
Returns
PersistAsync(CharacterAbilityData, CancellationToken)
Persists the provided data.
public Task<DatabaseResult<long>> PersistAsync(CharacterAbilityData abilityData, CancellationToken cancellationToken = default)
Parameters
abilityDataCharacterAbilityDatacancellationTokenCancellationTokenToken to cancel the operation.
Returns
PersistAsync(IEnumerable<CharacterAbilityData>, CancellationToken)
Persists the provided items.
public Task<DatabaseResult<BulkWriteResult>> PersistAsync(IEnumerable<CharacterAbilityData> abilities, CancellationToken cancellationToken = default)
Parameters
abilitiesIEnumerable<CharacterAbilityData>cancellationTokenCancellationTokenToken to cancel the operation.
Returns
- Task<DatabaseResult<BulkWriteResult>>
What the write actually did, or a failure.
Remarks
A successful result does not mean every supplied row was written. Batched writes are version-gated and the service filters what it cannot act on, so the outcome carries counts rather than a bare boolean — see BulkWriteResult, which explains which discrepancies a caller should care about and which are routine.
PersistOwnedAsync(CharacterAbilityData, CharacterSessionLeaseData, CancellationToken)
Persists one ability row, and returns its identity, only while the writer still holds the character's session claim.
public Task<DatabaseResult<long>> PersistOwnedAsync(CharacterAbilityData abilityData, CharacterSessionLeaseData claim, CancellationToken cancellationToken = default)
Parameters
abilityDataCharacterAbilityDataThe row.
claimCharacterSessionLeaseDataThe claim the write was captured under. Must name the row's character.
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
- Task<DatabaseResult<long>>
The row's identity. Fails with Forbidden when the claim is no longer held, and with NotFound when the character is gone.
Remarks
The single-row sibling of PersistOwnedAsync(IEnumerable<TItem>, IReadOnlyCollection<CharacterSessionLeaseData>, CancellationToken),
for a grant made while the character is resident: the claim is captured with the request
and checked, under the character's share lock, in the write's own transaction — see
CharacterWriteGate. The ungated PersistAsync(TRequest, CancellationToken)
remains for writers that hold no claim by design.
PersistOwnedAsync(IEnumerable<CharacterAbilityData>, IReadOnlyCollection<CharacterSessionLeaseData>, CancellationToken)
Persists the rows whose characters the writer still owns.
public Task<DatabaseResult<BulkWriteResult>> PersistOwnedAsync(IEnumerable<CharacterAbilityData> abilities, IReadOnlyCollection<CharacterSessionLeaseData> claims, CancellationToken cancellationToken = default)
Parameters
abilitiesIEnumerable<CharacterAbilityData>claimsIReadOnlyCollection<CharacterSessionLeaseData>The claim held for each character the rows name, as captured with them.
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
- Task<DatabaseResult<BulkWriteResult>>
What the write did. Rows of a live character whose claim is not held are counted as Unowned (and so as filtered). The write fails with Forbidden when it could touch none of its characters for that reason, and with NotFound when none exists.