Class CharacterGuildService
Service for managing character guild membership in the database. Provides async operations for CRUD operations on character guild membership data. Implements execution strategies for automatic retry on transient database failures. Returns DatabaseResult for consistent, safe error handling.
public sealed class CharacterGuildService : BaseService<CharacterGuildEntity>, ICharacterGuildService, ICountByKeyAction<long>, IDeleteByKeyVersionedAction<long>, IFetchByKeyAction<long, CharacterGuildData?>, IFetchManyByKeyAction<long, CharacterGuildData>
- Inheritance
-
CharacterGuildService
- Implements
- Inherited Members
Remarks
This service manages character guild membership including:
- Guild membership save/update with atomic UPSERT operations
- Rank updates
- Membership deletion
- Membership retrieval (individual and guild-wide)
- Member count queries
All exceptions are classified by BaseService and mapped to DatabaseResult error codes
(e.g., UNIQUE_VIOLATION, FOREIGN_KEY_VIOLATION, STALE_STATE, DATABASE_ERROR). Transient failures are retried automatically.
Methods return DatabaseResult to provide structured error handling
without throwing exceptions to calling code.
Constructors
CharacterGuildService(INpgsqlDbContextFactory)
Initializes a new instance of the CharacterGuildService class.
public CharacterGuildService(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 guildId, CancellationToken cancellationToken = default)
Parameters
guildIdlongcancellationTokenCancellationTokenToken to cancel the operation.
Returns
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 an entity for the given key.
public Task<DatabaseResult<CharacterGuildData?>> FetchAsync(long characterId, CancellationToken cancellationToken = default)
Parameters
characterIdlongcancellationTokenCancellationTokenToken to cancel the operation.
Returns
FetchManyAsync(long, CancellationToken)
Fetches many items for the given key.
public Task<DatabaseResult<IReadOnlyList<CharacterGuildData>>> FetchManyAsync(long guildId, CancellationToken cancellationToken = default)
Parameters
guildIdlongcancellationTokenCancellationTokenToken to cancel the operation.
Returns
FetchManyAsync(long[], CancellationToken)
Fetches the rosters of several guilds in one query.
public Task<DatabaseResult<IReadOnlyDictionary<long, IReadOnlyList<CharacterGuildData>>>> FetchManyAsync(long[] guildIds, CancellationToken cancellationToken = default)
Parameters
guildIdslong[]The guilds to read. Non-positive and repeated IDs are ignored.
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
- Task<DatabaseResult<IReadOnlyDictionary<long, IReadOnlyList<CharacterGuildData>>>>
One entry per distinct requested guild, ALWAYS present: a guild with no membership rows maps to an empty list, as
FetchManyAsync(long)answers for it.
Remarks
For the scene server's guild update pump. It used to read each changed guild's roster in
its own round trip — two, with the ladder — so a login wave touching many guilds cost two
serial queries per guild before any of them could be delivered. Each row carries the same
columns as FetchManyAsync(long), race and last-seen included.
PersistAsync(CharacterGuildData, int, CancellationToken)
Persists the provided guild membership data, enforcing capacity limits.
public Task<DatabaseResult> PersistAsync(CharacterGuildData guildData, int maxCapacity, CancellationToken cancellationToken = default)
Parameters
guildDataCharacterGuildDataThe guild membership data to persist.
maxCapacityintThe maximum number of members allowed in the guild.
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
- Task<DatabaseResult>
A DatabaseResult indicating success or failure.
UpdateLocationAsync(long, long, string, CancellationToken)
Writes where a member is — the scene name, or "Offline" — without touching anything else.
public Task<DatabaseResult<bool>> UpdateLocationAsync(long characterId, long guildId, string location, CancellationToken cancellationToken = default)
Parameters
characterIdlongThe member.
guildIdlongThe guild the member must still belong to.
locationstringThe location label.
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
- Task<DatabaseResult<bool>>
True when the member's row was updated; false when the character no longer holds a membership in
guildId— kicked, left, or moved — which is not an error.
Remarks
An UPDATE, never an insert, and the guild is in the WHERE clause. The location used to be written through PersistAsync(CharacterGuildData, int, CancellationToken), a version-gated UPSERT, after reading the row for its version and rank. Between that read and the write a kick could delete the row, and the UPSERT then INSERTED it again: the kicked member was back in the guild, permanently. A write that can only change an existing row cannot do that.
No version bump, like UpdateNoteAsync(long, long, string, bool, CancellationToken). The version arbitrates membership (rank, removal); a location label is not membership, and bumping it would make every rank change or kick that raced a login or logout fail as stale.
UpdateNoteAsync(long, long, string, bool, CancellationToken)
Writes one of a member's two guild notes.
public Task<DatabaseResult> UpdateNoteAsync(long characterId, long guildId, string note, bool isOfficerNote, CancellationToken cancellationToken = default)
Parameters
characterIdlongThe member the note is about.
guildIdlongThe guild the note belongs to.
notestringThe note text. Capped at 128 characters.
isOfficerNoteboolTrue for the officer-only note, false for the public one.
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
- Task<DatabaseResult>
A DatabaseResult indicating success or failure.
Remarks
The guild is in the WHERE clause. A note request naming a character who has since left — or who was never in the editor's guild — must not write, and this is where that is guaranteed rather than merely checked earlier.
UpdateRankAsync(long, long, byte, long, CancellationToken)
Updates a character's guild rank if incomingVersion is newer.
public Task<DatabaseResult> UpdateRankAsync(long characterId, long guildId, byte rank, long incomingVersion, CancellationToken cancellationToken = default)
Parameters
characterIdlongThe character ID.
guildIdlongThe guild ID.
rankbyteThe new rank.
incomingVersionlongThe authoritative, monotonic version for this update operation.
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
- Task<DatabaseResult>
A DatabaseResult indicating success or failure.