Interface IWorldClockControlService
- Namespace
- FishMMO.Database.Npgsql.Services.Interfaces
- Assembly
- FishMMO-DB.dll
The one world clock: the single world_clock_control row every scene server adopts on
its next pulse, and the admin writes that set, shift, pace, hold and resume it.
public interface IWorldClockControlService
Remarks
World time is world milliseconds since the calendar epoch, and the row stores an anchor
rather than a value: world ms now = base_world_ms + rate × max(0, db_now_ms −
base_reference_ms), floored, computed IN SQL against clock_timestamp(). No caller's
clock enters the arithmetic, so every scene server and the panel agree to the millisecond.
Every write is ONE statement. It locks the row, computes world-now from it at the
database instant, re-anchors there, bumps revision, stamps updated_by and
updated_at, and returns the new reading — exact to the millisecond and free of any
read-modify-write race between two admins. Each write also carries an idempotency key
(taken once, outside the retry loop), so a retried write whose first attempt committed but
lost its reply is not applied twice; that matters for ShiftAsync(long, string, CancellationToken).
The row is seeded by scene servers (EnsureSeededAsync(long, CancellationToken)), never by an admin write. Every write fails with NotFound and a sentence saying so when no scene server has run against this database yet.
A write is adopted by the scene servers on their next pulse. An acknowledgement says what was WRITTEN, never that it has taken effect.
Methods
EnsureSeededAsync(long, CancellationToken)
Creates the row if there is none (INSERT … ON CONFLICT (id) DO NOTHING): world time
= the database's now − the epoch (floored at zero), real-time pace, revision 1. An existing
row is left exactly as it is, epoch included.
Task<DatabaseResult<WorldClockReading>> EnsureSeededAsync(long epochUnixSeconds, CancellationToken cancellationToken = default)
Parameters
epochUnixSecondslongThe calendar epoch the scene servers use, Unix seconds.
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<DatabaseResult<WorldClockReading>>
The row as it now stands (Exists is true on success). Compare its EpochUnixSeconds with your own: a row seeded earlier keeps the epoch it was seeded with.
HoldAsync(string, CancellationToken)
Holds world time where it is now (pace 0). The running pace becomes the resume pace; holding an already held clock keeps the resume pace it had.
Task<DatabaseResult<WorldClockReading>> HoldAsync(string actor, CancellationToken cancellationToken = default)
Parameters
actorstringcancellationTokenCancellationToken
Returns
ReadAsync(CancellationToken)
Reads the database clock and the row in one round trip. Succeeds with Exists false when there is no row yet.
Task<DatabaseResult<WorldClockReading>> ReadAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationToken
Returns
ResumeAsync(string, CancellationToken)
Resumes at the resume pace from where world time stands (while held, that is the held time). On a clock that is already running it re-applies the resume pace.
Task<DatabaseResult<WorldClockReading>> ResumeAsync(string actor, CancellationToken cancellationToken = default)
Parameters
actorstringcancellationTokenCancellationToken
Returns
SetAsync(long, string, CancellationToken)
Sets world time to worldMs at this database instant, keeping the pace.
Task<DatabaseResult<WorldClockReading>> SetAsync(long worldMs, string actor, CancellationToken cancellationToken = default)
Parameters
worldMslongWorld milliseconds since the epoch; at least zero, and a calendar date (year ≤ 9999).
actorstringThe ACCOUNT making the change (recorded as updated_by).
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<DatabaseResult<WorldClockReading>>
The new reading, with ReplacedWorldMs and ReplacedRate set.
SetRateAsync(double, string, CancellationToken)
Changes the pace from this instant without moving world time. Above zero it also becomes the resume pace; zero is a hold (the running pace is kept as the resume pace).
Task<DatabaseResult<WorldClockReading>> SetRateAsync(double rate, string actor, CancellationToken cancellationToken = default)
Parameters
ratedoubleWorld seconds per real second, 0 to MaxRate. Negative, NaN and infinity are refused.
actorstringThe ACCOUNT making the change.
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<DatabaseResult<WorldClockReading>>
The new reading.
ShiftAsync(long, string, CancellationToken)
Moves world time by deltaMs from world-now, keeping the pace.
Task<DatabaseResult<WorldClockReading>> ShiftAsync(long deltaMs, string actor, CancellationToken cancellationToken = default)
Parameters
deltaMslongSigned milliseconds. Refused when the result would fall before the epoch or past year 9999.
actorstringThe ACCOUNT making the change.
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<DatabaseResult<WorldClockReading>>
The new reading.