Table of Contents

Class BetaCodeService

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

Beta codes and account links. Raw SQL for the writes whose WHERE clause is the concurrency control, EF for reads.

public sealed class BetaCodeService : BaseService<BetaCodeEntity>, IBetaCodeService
Inheritance
BetaCodeService
Implements
Inherited Members

Remarks

Every raw statement names its table through TableName or GetTableName<T>(), both schema-qualified: an unqualified name resolves against the connection's search_path and silently misses the model's schema where they differ.

Constructors

BetaCodeService(INpgsqlDbContextFactory)

Creates the service.

public BetaCodeService(INpgsqlDbContextFactory dbContextFactory)

Parameters

dbContextFactory INpgsqlDbContextFactory

Methods

CheckRedeemableAsync(string, IReadOnlyCollection<string>, CancellationToken)

Whether a code could be redeemed right now for one of programs, without redeeming it.

public Task<DatabaseResult<bool>> CheckRedeemableAsync(string code, IReadOnlyCollection<string> programs, CancellationToken cancellationToken = default)

Parameters

code string
programs IReadOnlyCollection<string>
cancellationToken CancellationToken

Returns

Task<DatabaseResult<bool>>

Remarks

For registration, which must refuse a bad code BEFORE it creates the account: redeeming first would attach a use to an account name the insert may then find already taken — handing beta access to whoever owns that name. A check can race the redeem that follows it; the redeem is still the authority, and an account whose redeem loses that race exists without access and can redeem another code. An empty or null programs means any program. A malformed code is simply not redeemable.

FetchForAccountAsync(string, CancellationToken)

Every code the account has redeemed, oldest first, with whether each has been revoked.

public Task<DatabaseResult<IReadOnlyList<AccountBetaCodeData>>> FetchForAccountAsync(string accountName, CancellationToken cancellationToken = default)

Parameters

accountName string
cancellationToken CancellationToken

Returns

Task<DatabaseResult<IReadOnlyList<AccountBetaCodeData>>>

HasAccessAsync(string, IReadOnlyCollection<string>, CancellationToken)

Whether the account holds a non-revoked code of any of the given programs.

public Task<DatabaseResult<bool>> HasAccessAsync(string accountName, IReadOnlyCollection<string> programs, CancellationToken cancellationToken = default)

Parameters

accountName string

The account.

programs IReadOnlyCollection<string>

The programs currently active.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<DatabaseResult<bool>>

Remarks

Expiry does not end access; revocation does. A code's expiry is a deadline for redeeming it, so an account that redeemed in time stays admitted after the date passes. Ending a tester's access is done by revoking the code.

Program names are normalised; a null or empty list, or one with no valid names, is false without querying — a gate configured with no programs admits nobody by code.

ListProgramsAsync(CancellationToken)

Totals for every program that has codes, ordered by name.

public Task<DatabaseResult<IReadOnlyList<BetaProgramSummary>>> ListProgramsAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Returns

Task<DatabaseResult<IReadOnlyList<BetaProgramSummary>>>

MintAsync(string, int, int, DateTime?, string, string?, CancellationToken)

Mints a batch of codes under one program, in one transaction.

public Task<DatabaseResult<IReadOnlyList<BetaCodeData>>> MintAsync(string program, int count, int maxUses, DateTime? expiresUtc, string createdBy, string? note, CancellationToken cancellationToken = default)

Parameters

program string

Program name; normalised to lowercase, 1–32 of [a-z0-9_-].

count int

How many codes, 1–500.

maxUses int

How many accounts each code admits, 1–100000.

expiresUtc DateTime?

When the codes stop being redeemable, or null. Must be in the future.

createdBy string

The staff account minting them.

note string

Optional staff note, at most 256 characters.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<DatabaseResult<IReadOnlyList<BetaCodeData>>>

The minted codes.

Remarks

Either every code in the batch is created or none is. A generated code that collides with an existing one is regenerated (a bounded number of times) instead of failing the batch; at 2^60 possible codes that path exists for correctness, not because it runs.

RedeemAsync(string, string, CancellationToken)

Redeems a code for an account: takes one use and records the permanent link, atomically.

public Task<DatabaseResult<AccountBetaCodeData>> RedeemAsync(string accountName, string code, CancellationToken cancellationToken = default)

Parameters

accountName string

The redeeming account; stored lowercase.

code string

The code as the player typed it.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<DatabaseResult<AccountBetaCodeData>>

Remarks

An unknown, revoked, expired or used-up code all fail with the same NOT_FOUND and the same message, "That beta code is not valid.", and a malformed code fails with that message too. Registration calls this anonymously, so any difference between those answers would let a script tell a real code from a guess. The caller must pass the message through unaltered and must not add its own distinctions.

Redeeming a code the account already holds fails VALIDATION_ERROR and consumes nothing. The use is taken by one conditional UPDATE (use_count < max_uses, not revoked, not expired), and that WHERE clause is the concurrency control: two accounts racing for a code's last use cannot both succeed.

RevokeAsync(long, string, CancellationToken)

Revokes a code, ending redemption of it and the access of every account that redeemed it.

public Task<DatabaseResult> RevokeAsync(long id, string revokedBy, CancellationToken cancellationToken = default)

Parameters

id long
revokedBy string
cancellationToken CancellationToken

Returns

Task<DatabaseResult>

Remarks

One conditional UPDATE on revoked_utc IS NULL, so the first revocation's who and when are never overwritten by a second. An unknown id fails NOT_FOUND; an already revoked code fails VALIDATION_ERROR.

SearchAsync(BetaCodeQuery, CancellationToken)

Lists codes, newest first. Page size is clamped to 200.

public Task<DatabaseResult<BetaCodePage>> SearchAsync(BetaCodeQuery query, CancellationToken cancellationToken = default)

Parameters

query BetaCodeQuery
cancellationToken CancellationToken

Returns

Task<DatabaseResult<BetaCodePage>>