Class PlotService
Reads and writes ownership of authored plots of land.
public sealed class PlotService : BaseService<PlotEntity>, IPlotService
- Inheritance
-
PlotService
- Implements
- Inherited Members
Constructors
PlotService(INpgsqlDbContextFactory)
public PlotService(INpgsqlDbContextFactory dbContextFactory)
Parameters
dbContextFactoryINpgsqlDbContextFactory
Methods
BackfillTaxDueAsync(long, DateTime, CancellationToken)
Gives a first due date to character-owned plots that have none.
public Task<DatabaseResult<int>> BackfillTaxDueAsync(long worldServerID, DateTime firstDueUtc, CancellationToken cancellationToken = default)
Parameters
worldServerIDlongThe world whose land to backfill.
firstDueUtcDateTimeWhen those plots should first fall due.
cancellationTokenCancellationToken
Returns
- Task<DatabaseResult<int>>
How many plots were given a due date.
Remarks
Land claimed while tax was switched off is stored with no due date, and the sweep only looks at plots that have one. Turning tax on afterwards would therefore never touch any of it: those plots would be free forever, and the difference between a founding player and a later one would be permanent.
Only rows with no date are touched, so this cannot reset a plot that is already being billed — running it repeatedly is safe, which matters because it runs on every scene resolve.
Character-owned only. Guild land is deferred rather than charged until a treasury exists, so giving it a date would only make every sweep pick it up and push it along again.
ChargeTaxOfflineAsync(IReadOnlyList<PlotTaxCharge>, int, DateTime, int, int, CancellationToken)
Bills a batch of due plots to owners nobody is hosting, in one transaction.
public Task<DatabaseResult<List<PlotTaxChargeResult>>> ChargeTaxOfflineAsync(IReadOnlyList<PlotTaxCharge> charges, int currencyTemplateID, DateTime ownedElsewhereRetryUtc, int ledgerReason, int ledgerState, CancellationToken cancellationToken = default)
Parameters
chargesIReadOnlyList<PlotTaxCharge>One entry per plot, as the sweep read and priced them: each carries its own amount and next due date, because a bill covers every period that plot has fallen behind by. An owner may appear only once.
currencyTemplateIDintThe attribute template tax is paid in.
ownedElsewhereRetryUtcDateTimeWhen a plot whose owner a server is holding may next be tried (stored as
tax_next_attempt_utc).ledgerReasonintThe ledger reason recorded for each payment (
CurrencyMovementReason.LandTax).ledgerStateintThe ledger state recorded for each payment (
CurrencyMovementState.Absorbed).cancellationTokenCancellationToken
Returns
- Task<DatabaseResult<List<PlotTaxChargeResult>>>
One result per distinct plot in
charges.
Remarks
The offline charge that used to be one transaction per plot — ownership assertion, attribute read, advance, debit, lifted mark, each a round trip — as a handful of set-based statements for the whole batch. The orderings it keeps are the ones the housing README names: the bill is won before any money moves (a locked plot still holding the date the sweep read), a missed-payment mark comes off in the same commit as the payment, and an unpaid bill is marked in the commit that advanced it. A bill is paid in full or not at all.
An owner is charged here only while no server holds their session, asserted under the row lock for the whole transaction, because a held owner's balance is in that server's memory and its next save would overwrite a debit made underneath it. A held owner's plot is deferred instead (OwnedElsewhere); a deleted or missing owner cannot pay and is marked like any other miss (OwnerGone), so their land reaches the end of its grace rather than staying owned forever.
Never waits on a lock: a character or plot another transaction holds is skipped for this sweep. Safe to run from every scene server at once — a period produces one charge however many servers sweep it — and a retry after a lost commit reply changes nothing.
Paid bills are recorded in the currency ledger inside the same commit. Nothing here
marks plot_updates: a charge changes nothing another channel draws.
ClearTaxDelinquencyAsync(long, CancellationToken)
Clears a plot's delinquency after a successful payment.
public Task<DatabaseResult<int>> ClearTaxDelinquencyAsync(long plotID, CancellationToken cancellationToken = default)
Parameters
plotIDlongcancellationTokenCancellationToken
Returns
FetchByIdsAsync(IReadOnlyCollection<long>, CancellationToken)
Fetches plots by ID. IDs with no row are simply absent from the result.
public Task<DatabaseResult<List<PlotData>>> FetchByIdsAsync(IReadOnlyCollection<long> plotIDs, CancellationToken cancellationToken = default)
Parameters
plotIDsIReadOnlyCollection<long>cancellationTokenCancellationToken
Returns
Remarks
For the cross-channel sync, which knows exactly which plots changed and used to re-read every plot of every scene to find them.
FetchByOwnerCharacterAsync(long, CancellationToken)
Fetches every plot a character owns, across all scenes.
public Task<DatabaseResult<List<PlotData>>> FetchByOwnerCharacterAsync(long characterID, CancellationToken cancellationToken = default)
Parameters
characterIDlongcancellationTokenCancellationToken
Returns
FetchByOwnerGuildAsync(long, CancellationToken)
Fetches every plot a guild owns, across all scenes.
public Task<DatabaseResult<List<PlotData>>> FetchByOwnerGuildAsync(long guildID, CancellationToken cancellationToken = default)
Parameters
guildIDlongcancellationTokenCancellationToken
Returns
FetchBySceneAsync(long, string, CancellationToken)
Fetches every plot in one world server's copy of a scene, owned or not.
public Task<DatabaseResult<List<PlotData>>> FetchBySceneAsync(long worldServerID, string sceneName, CancellationToken cancellationToken = default)
Parameters
worldServerIDlongsceneNamestringcancellationTokenCancellationToken
Returns
FetchTaxDueAsync(long, DateTime, DateTime, long, int, CancellationToken)
Fetches one page of a world's owned plots whose tax has fallen due, in due order.
public Task<DatabaseResult<List<PlotData>>> FetchTaxDueAsync(long worldServerID, DateTime asOfUtc, DateTime afterDueUtc, long afterPlotID, int limit, CancellationToken cancellationToken = default)
Parameters
worldServerIDlongThe world whose land to sweep.
asOfUtcDateTimeThe moment to judge against.
afterDueUtcDateTimeThe keyset cursor: the due date of the last plot of the previous page, or MinValue for the first page.
afterPlotIDlongThe ID of the last plot of the previous page, or zero for the first page.
limitintMost rows to return.
cancellationTokenCancellationToken
Returns
Remarks
Bounded, because a server that has been down over a billing period comes back to every plot at once and an unbounded read would load all of them.
Paged by a keyset rather than from the head, because the plots a page could not
settle stay exactly where they were: a sweep that re-read from the head after each page
would read them again and never reach the plots behind them. Plots whose
tax_next_attempt_utc is still ahead of asOfUtc are left out —
their owner is being billed by the server holding them.
FetchTaxDueForOwnersAsync(IReadOnlyCollection<long>, DateTime, CancellationToken)
Fetches the plots, in any world, whose tax has fallen due and whose owner is one of
characterIDs.
public Task<DatabaseResult<List<PlotData>>> FetchTaxDueForOwnersAsync(IReadOnlyCollection<long> characterIDs, DateTime asOfUtc, CancellationToken cancellationToken = default)
Parameters
characterIDsIReadOnlyCollection<long>The characters the calling server holds.
asOfUtcDateTimeThe moment to judge against.
cancellationTokenCancellationToken
Returns
Remarks
The holding server's half of the sweep. A logged-in owner's balance lives in the memory of the server holding them, so only that server may charge them — and that server may be hosting nothing of the world their land is in. This asks by owner instead of by world, so where the owner happens to be playing no longer decides whether they are billed.
Ignores the deferral: it exists to keep other servers from re-reading a plot that only this one can bill. At most one row per character, by the one-house index.
MarkTaxDelinquentAsync(long, DateTime, CancellationToken)
Records that a tax payment was missed, without disturbing an earlier miss.
public Task<DatabaseResult<int>> MarkTaxDelinquentAsync(long plotID, DateTime delinquentSinceUtc, CancellationToken cancellationToken = default)
Parameters
plotIDlongThe plot that went unpaid.
delinquentSinceUtcDateTimeWhen the first missed payment fell due.
cancellationTokenCancellationToken
Returns
Remarks
Only sets the mark when there is not one already. The grace period is measured from the first miss, so overwriting it on every later failure would restart the clock each period and the plot could never be reclaimed.
RegisterAsync(long, string, IReadOnlyList<string>, CancellationToken)
Ensures a row exists for every foundation authored in a scene.
public Task<DatabaseResult<int>> RegisterAsync(long worldServerID, string sceneName, IReadOnlyList<string> plotKeys, CancellationToken cancellationToken = default)
Parameters
worldServerIDlongThe world server whose land this is.
sceneNamestringThe scene the foundations belong to.
plotKeysIReadOnlyList<string>Canonicalised keys of the foundations found in the scene.
cancellationTokenCancellationToken
Returns
- Task<DatabaseResult<int>>
The number of plots that did not previously exist.
Remarks
Idempotent, because every scene server hosting a channel of the scene runs it on load and they all describe the same land. Plots that already exist keep the ownership they have — registration must never disturb it, or a restart would evict whoever lives there.
Scoped to one world server. The same scene runs on every world, and registering it unscoped would give them all a single shared row per plot — one player's house appearing as already-owned land to everybody on every other world.
ReleaseAllForGuildAsync(long, CancellationToken)
Releases every plot a guild owns.
public Task<DatabaseResult<int>> ReleaseAllForGuildAsync(long guildID, CancellationToken cancellationToken = default)
Parameters
guildIDlongcancellationTokenCancellationToken
Returns
- Task<DatabaseResult<int>>
The number of plots released.
Remarks
Guilds are hard-deleted, and the owner column carries no foreign key to clean up behind them, so land held by a disbanded guild would otherwise stay claimed by an identifier that no longer resolves to anything — unclaimable, and unbuildable, permanently.
ReleaseAsync(long, long, long, int, CancellationToken)
Releases a plot back to the unowned pool, if the expected owner still holds it.
public Task<DatabaseResult<int>> ReleaseAsync(long plotID, long expectedOwnerCharacterID, long expectedOwnerGuildID, int releasedState = 0, CancellationToken cancellationToken = default)
Parameters
plotIDlongThe plot to release.
expectedOwnerCharacterIDlongThe character believed to own it, or zero.
expectedOwnerGuildIDlongThe guild believed to own it, or zero.
releasedStateintThe
FishMMO.Shared.PlotStatethe plot is left in, as its underlying integer. Defaults toEmpty; reclamation passesAbandoned.cancellationTokenCancellationToken
Returns
- Task<DatabaseResult<int>>
1 when released, 0 when somebody else owns it or it was already unowned.
Remarks
The state the plot lands in is the caller's to choose, because releasing is two different events wearing one name. An owner handing the deed back leaves a bare lot; a sweep taking land off somebody who stopped paying leaves a house standing empty, which renders differently and tells a passer-by a different story.
TryAdvanceTaxAsync(long, DateTime, DateTime, CancellationToken)
Moves a plot's tax date forward, if it still holds the date the caller charged against.
public Task<DatabaseResult<int>> TryAdvanceTaxAsync(long plotID, DateTime expectedDueUtc, DateTime nextDueUtc, CancellationToken cancellationToken = default)
Parameters
plotIDlongThe plot being charged.
expectedDueUtcDateTimeThe due date the caller read.
nextDueUtcDateTimeThe date the next payment falls due.
cancellationTokenCancellationToken
Returns
- Task<DatabaseResult<int>>
1 when this call advanced it, 0 when somebody else already did.
Remarks
The pin is what makes the tax safe to sweep from every scene server at once. Several may host the same world's scenes, they all see the same plot come due, and only the one whose expected date still matches may charge it — so a period produces one payment rather than one per server.
Called before the money is taken, for the same reason a plot is claimed before it is paid for: winning the right to charge is the contended step, and a caller that loses it must not have taken anything.
TryClaimAsync(long, long, long, DateTime?, int, CancellationToken)
Claims an unowned plot.
public Task<DatabaseResult<int>> TryClaimAsync(long plotID, long ownerCharacterID, long ownerGuildID, DateTime? taxDueUtc, int claimedState = 1, CancellationToken cancellationToken = default)
Parameters
plotIDlongThe plot to claim.
ownerCharacterIDlongThe claiming character, or zero for a guild claim.
ownerGuildIDlongThe claiming guild, or zero for a character claim.
taxDueUtcDateTime?When the first tax payment falls due, or null for untaxed land.
claimedStateintThe
FishMMO.Shared.PlotStatethe plot enters, as its underlying integer. Defaults toBuilding.cancellationTokenCancellationToken
Returns
- Task<DatabaseResult<int>>
1 when this call took the plot; 0 when it was already owned, does not exist, or the claiming character already owns one.
Remarks
Exactly one of the two identifiers may be non-zero.
The first tax date and the new state are set by the same statement that takes the plot, so there is no moment where land is owned but untaxed, or owned but still showing as an empty lot. Set separately, a crash in the gap would leave a plot nobody ever has to pay for, or one that every channel draws as available.
A character may hold only one plot. That is asked here and enforced by a
partial unique index, because this clause alone cannot stop two simultaneous claims on
two scene servers. A caller that gets UNIQUE_VIOLATION back rather than a row
count lost that race and should report it the same way it reports zero.
TryRestoreTaxDueAsync(long, DateTime, DateTime, CancellationToken)
Hands a won tax period back: moves the due date back to what it was, if it still holds the date this caller advanced it to.
public Task<DatabaseResult<int>> TryRestoreTaxDueAsync(long plotID, DateTime advancedToUtc, DateTime restoreToUtc, CancellationToken cancellationToken = default)
Parameters
plotIDlongThe plot.
advancedToUtcDateTimeThe date TryAdvanceTaxAsync(long, DateTime, DateTime, CancellationToken) set it to.
restoreToUtcDateTimeThe date it held before; must be earlier.
cancellationTokenCancellationToken
Returns
- Task<DatabaseResult<int>>
1 when the period was handed back, 0 when the date has moved on since.
Remarks
For a caller that won a period and then could not charge the owner at all (the character unreachable, the charge faulting on every attempt). Without it the period simply went unbilled: the date only ever moves forward. Pinned the same way as the advance, so it can never undo a LATER period another server has since won — that one is left alone and this returns 0.
TrySetStateAsync(long, int, int, long, long, CancellationToken)
Moves a plot from one lifecycle state to another, if it is still where and whose the caller thinks it is.
public Task<DatabaseResult<int>> TrySetStateAsync(long plotID, int expectedState, int newState, long expectedOwnerCharacterID, long expectedOwnerGuildID, CancellationToken cancellationToken = default)
Parameters
plotIDlongThe plot to move.
expectedStateintThe state it must currently be in.
newStateintThe state to move it to. Must differ from the expected one.
expectedOwnerCharacterIDlongThe character believed to own it, or zero.
expectedOwnerGuildIDlongThe guild believed to own it, or zero.
cancellationTokenCancellationToken
Returns
- Task<DatabaseResult<int>>
1 when the plot moved, 0 when it had already moved or changed hands.