using System;
using System.Collections.Generic;
using System.Linq;
namespace GameCult.Mesh
{
///
/// Grants one peer authority to perform roles for a Verse over a bounded interval.
///
public sealed class CultMeshAuthorityLease
{
///
/// Creates an authority lease.
///
public CultMeshAuthorityLease(
string leaseId,
string verseId,
string peerId,
IEnumerable roles,
IEnumerable? shardIds,
string issuerRuntimeId,
DateTimeOffset validFrom,
DateTimeOffset expiresAt,
string? signature = null)
{
LeaseId = RequireNonEmpty(leaseId, nameof(leaseId));
VerseId = RequireNonEmpty(verseId, nameof(verseId));
PeerId = RequireNonEmpty(peerId, nameof(peerId));
Roles = Clean(roles);
ShardIds = Clean(shardIds);
IssuerRuntimeId = RequireNonEmpty(issuerRuntimeId, nameof(issuerRuntimeId));
ValidFrom = validFrom;
ExpiresAt = expiresAt;
Signature = signature;
if (expiresAt <= validFrom)
{
throw new ArgumentException("Lease expiry must be after its valid-from timestamp.", nameof(expiresAt));
}
}
/// Gets the stable lease id.
public string LeaseId { get; }
/// Gets the Verse id governed by the lease.
public string VerseId { get; }
/// Gets the peer id receiving authority.
public string PeerId { get; }
/// Gets the roles granted by this lease.
public IReadOnlyList Roles { get; }
/// Gets shard ids covered by this lease. Empty means every shard in the Verse.
public IReadOnlyList ShardIds { get; }
/// Gets the runtime that issued the lease.
public string IssuerRuntimeId { get; }
/// Gets the first timestamp when the lease is valid.
public DateTimeOffset ValidFrom { get; }
/// Gets the timestamp when the lease expires.
public DateTimeOffset ExpiresAt { get; }
/// Gets an optional signature over the lease.
public string? Signature { get; }
///
/// Returns whether this lease is valid at the supplied time.
///
public bool IsValidAt(DateTimeOffset at)
{
return at >= ValidFrom && at < ExpiresAt;
}
///
/// Returns whether this lease covers a peer card, role, and shard.
///
public bool Covers(CultMeshPeerCard peer, string role, string? shardId = null, DateTimeOffset? at = null)
{
if (peer == null) throw new ArgumentNullException(nameof(peer));
if (string.IsNullOrWhiteSpace(role)) throw new ArgumentException("Value must be non-empty.", nameof(role));
var time = at ?? DateTimeOffset.UtcNow;
return IsValidAt(time) &&
string.Equals(VerseId, peer.VerseId, StringComparison.Ordinal) &&
string.Equals(PeerId, peer.PeerId, StringComparison.Ordinal) &&
string.Equals(LeaseId, peer.AuthorityLeaseId, StringComparison.Ordinal) &&
Roles.Contains(role, StringComparer.Ordinal) &&
peer.Roles.Contains(role, StringComparer.Ordinal) &&
(string.IsNullOrWhiteSpace(shardId) ||
ShardIds.Count == 0 ||
ShardIds.Contains(shardId!, StringComparer.Ordinal));
}
private static string[] Clean(IEnumerable? values)
{
return values?.Where(value => !string.IsNullOrWhiteSpace(value)).Distinct(StringComparer.Ordinal).ToArray()
?? Array.Empty();
}
private static string RequireNonEmpty(string value, string paramName)
{
return string.IsNullOrWhiteSpace(value)
? throw new ArgumentException("Value must be non-empty.", paramName)
: value;
}
}
///
/// Local authority lease catalog.
///
public sealed class CultMeshAuthorityLeaseCatalog
{
private readonly Dictionary _leases = new(StringComparer.Ordinal);
/// Gets all known leases.
public IReadOnlyList Leases => _leases.Values.OrderBy(lease => lease.LeaseId, StringComparer.Ordinal).ToArray();
/// Adds or replaces an authority lease.
public void Upsert(CultMeshAuthorityLease lease)
{
if (lease == null) throw new ArgumentNullException(nameof(lease));
_leases[lease.LeaseId] = lease;
}
/// Gets a lease by id, if known.
public CultMeshAuthorityLease? Get(string leaseId)
{
if (string.IsNullOrWhiteSpace(leaseId)) throw new ArgumentException("Value must be non-empty.", nameof(leaseId));
return _leases.TryGetValue(leaseId, out var lease) ? lease : null;
}
///
/// Returns whether a peer card is authorized for a role and optional shard.
///
public bool IsAuthorized(CultMeshPeerCard peer, string role, string? shardId = null, DateTimeOffset? at = null)
{
if (peer == null) throw new ArgumentNullException(nameof(peer));
if (string.IsNullOrWhiteSpace(peer.AuthorityLeaseId))
{
return false;
}
return _leases.TryGetValue(peer.AuthorityLeaseId!, out var lease) &&
lease.Covers(peer, role, shardId, at);
}
}
}