Table of Contents

Class CharacterAbilityService

Namespace
FishMMO.Database.Npgsql.Services
Assembly
FishMMO-DB.dll

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

dbContextFactory INpgsqlDbContextFactory

Factory 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

characterId long
cancellationToken CancellationToken

Token to cancel the operation.

Returns

Task<DatabaseResult<int>>

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

characterId long

The character that owns the ability. Checked, so one character cannot delete another's row.

abilityId long

The row identity, never the template id.

incomingVersion long

The authoritative, monotonic version for this delete. The row is removed when its own version is at or below this.

cancellationToken CancellationToken

Token to cancel the operation.

Returns

Task<DatabaseResult>

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

characterId long

The character that owns the ability.

abilityId long

The row identity, never the template id.

incomingVersion long

The version ceiling; the row is removed when its own version is at or below this.

claim CharacterSessionLeaseData

The claim the delete was captured under. Must name characterId.

admitReleased bool

Also admit an unclaimed character. Only for undoing the writer's own row.

cancellationToken CancellationToken

Token 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

characterId long

The character ID that owns the ability.

abilityId long

The ability identifier.

incomingVersion long

The authoritative, monotonic version for this delete operation.

cancellationToken CancellationToken

Token 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

characterId long
incomingVersion long

The authoritative, monotonic version for this delete operation.

cancellationToken CancellationToken

Token to cancel the operation.

Returns

Task<DatabaseResult>

FetchAsync(long, CancellationToken)

Fetches a collection of items for the given key.

public Task<DatabaseResult<IReadOnlyList<CharacterAbilityData>>> FetchAsync(long characterId, CancellationToken cancellationToken = default)

Parameters

characterId long
cancellationToken CancellationToken

Token to cancel the operation.

Returns

Task<DatabaseResult<IReadOnlyList<CharacterAbilityData>>>

PersistAsync(CharacterAbilityData, CancellationToken)

Persists the provided data.

public Task<DatabaseResult<long>> PersistAsync(CharacterAbilityData abilityData, CancellationToken cancellationToken = default)

Parameters

abilityData CharacterAbilityData
cancellationToken CancellationToken

Token to cancel the operation.

Returns

Task<DatabaseResult<long>>

PersistAsync(IEnumerable<CharacterAbilityData>, CancellationToken)

Persists the provided items.

public Task<DatabaseResult<BulkWriteResult>> PersistAsync(IEnumerable<CharacterAbilityData> abilities, CancellationToken cancellationToken = default)

Parameters

abilities IEnumerable<CharacterAbilityData>
cancellationToken CancellationToken

Token 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

abilityData CharacterAbilityData

The row.

claim CharacterSessionLeaseData

The claim the write was captured under. Must name the row's character.

cancellationToken CancellationToken

Token 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

abilities IEnumerable<CharacterAbilityData>
claims IReadOnlyCollection<CharacterSessionLeaseData>

The claim held for each character the rows name, as captured with them.

cancellationToken CancellationToken

Token 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.