diff --git a/Aetheria.State.Daemon/Program.cs b/Aetheria.State.Daemon/Program.cs index e2eaa5570..19a9a62a4 100644 --- a/Aetheria.State.Daemon/Program.cs +++ b/Aetheria.State.Daemon/Program.cs @@ -117,6 +117,7 @@ static async Task TickAsync( AetheriaRuntimeDaemonFrameDocument? currentFrame, bool buildPublications) { + await AcceptCoreEveInvocationsAsync(node, options, currentFrame).ConfigureAwait(false); await AcceptEveCommandsAsync(node, options).ConfigureAwait(false); var fixedDeltaSeconds = currentFrame?.FixedDeltaSeconds > 0 @@ -239,7 +240,8 @@ static async Task PublishCommittedCommandFactsAsync( if (string.IsNullOrWhiteSpace(commandId) || !byCommandId.TryGetValue(commandId, out var command)) continue; - await node.PutCommittedCommandFactAsync( + await PublishCommittedFactAsync( + node, AetheriaRuntimeCommittedCommandFactDocument.FromAppliedCommand( frame, command, @@ -252,7 +254,8 @@ static async Task PublishCommittedCommandFactsAsync( !byCommandId.TryGetValue(commandId, out var command)) continue; - await node.PutCommittedCommandFactAsync( + await PublishCommittedFactAsync( + node, AetheriaRuntimeCommittedCommandFactDocument.FromRejectedCommand( frame, command, @@ -260,6 +263,31 @@ static async Task PublishCommittedCommandFactsAsync( } } +static async Task PublishCommittedFactAsync( + AetheriaStateNode node, + AetheriaRuntimeCommittedCommandFactDocument fact) +{ + await node.PutCommittedCommandFactAsync(fact).ConfigureAwait(false); + var applied = string.Equals( + fact.Outcome, + AetheriaRuntimeCommandFactOutcomes.Applied, + StringComparison.Ordinal); + var receipt = new EveCommandReceiptDocument( + fact.FactId, + fact.CommandId, + fact.CommandKind.ToString(), + applied ? "reconciled" : "denied", + "Aetheria", + fact.SourceDaemonId, + "aetheria.daemon", + AetheriaRuntimeDaemonGameSurfaceBuilder.SurfaceId, + applied ? "Command applied by authoritative daemon." : "Command rejected by authoritative daemon.", + fact.CommittedAtUtc, + Math.Max(fact.SourceFrameId, 0)); + await node.Database.PutAsync(AetheriaRuntimeVerseRecordKeys.EveReceipt(receipt.ReceiptId), receipt) + .ConfigureAwait(false); +} + static RudpCultNetSchemaServer StartClientCultMeshHost( AetheriaStateNode node, AetheriaDaemonHostOptions options, @@ -890,31 +918,40 @@ static async Task InjectEveSurfaceSnapshotAsync( SourceRole = "aetheria-daemon" }); response.Documents = response.Documents - .Where(document => !string.Equals(document.SchemaId, surfacePut.Document.SchemaId, StringComparison.Ordinal) || - !string.Equals(document.RecordKey, surfacePut.Document.RecordKey, StringComparison.Ordinal)) + .Where(document => !string.Equals(document.RecordKey, surfacePut.Document.RecordKey, StringComparison.Ordinal)) .Concat(new[] { surfacePut.Document }) .ToArray(); } static Task ReadEveSurfacePublicationAsync(AetheriaStateNode node, string surfaceKind) { - return surfaceKind switch + var key = surfaceKind switch { - "game" => node.MutableDocument(AetheriaRuntimeVerseRecordKeys.DaemonGameSurface).ReadAsync(), - "game-tui" => node.MutableDocument(AetheriaRuntimeVerseRecordKeys.DaemonGameTuiSurface).ReadAsync(), - "editor" => node.MutableDocument(AetheriaRuntimeVerseRecordKeys.DaemonEditorSurface).ReadAsync(), - "editor-tui" => node.MutableDocument(AetheriaRuntimeVerseRecordKeys.DaemonEditorTuiSurface).ReadAsync(), - "main-menu" => node.MutableDocument(AetheriaRuntimeVerseRecordKeys.MainMenuSurface).ReadAsync(), - "main-menu-settings" => node.MutableDocument(AetheriaRuntimeVerseRecordKeys.MainMenuSettingsSurface).ReadAsync(), - "main-menu-input-settings" => node.MutableDocument(AetheriaRuntimeVerseRecordKeys.MainMenuInputSettingsSurface).ReadAsync(), - "main-menu-player-settings" => node.MutableDocument(AetheriaRuntimeVerseRecordKeys.MainMenuPlayerSettingsSurface).ReadAsync(), - "main-menu-verse-settings" => node.MutableDocument(AetheriaRuntimeVerseRecordKeys.MainMenuVerseSettingsSurface).ReadAsync(), - "inventory-panel" => node.MutableDocument(AetheriaRuntimeVerseRecordKeys.InventoryPanelSurface).ReadAsync(), - "inventory-dropdown" => node.MutableDocument(AetheriaRuntimeVerseRecordKeys.InventoryDropdownSurface).ReadAsync(), - "map-menu" => node.MutableDocument(AetheriaRuntimeVerseRecordKeys.MapMenuSurface).ReadAsync(), - "trade-menu" => node.MutableDocument(AetheriaRuntimeVerseRecordKeys.TradeMenuSurface).ReadAsync(), + "game" => AetheriaRuntimeVerseRecordKeys.DaemonGameSurface, + "game-tui" => AetheriaRuntimeVerseRecordKeys.DaemonGameTuiSurface, + "editor" => AetheriaRuntimeVerseRecordKeys.DaemonEditorSurface, + "editor-tui" => AetheriaRuntimeVerseRecordKeys.DaemonEditorTuiSurface, + "main-menu" => AetheriaRuntimeVerseRecordKeys.MainMenuSurface, + "main-menu-settings" => AetheriaRuntimeVerseRecordKeys.MainMenuSettingsSurface, + "main-menu-input-settings" => AetheriaRuntimeVerseRecordKeys.MainMenuInputSettingsSurface, + "main-menu-player-settings" => AetheriaRuntimeVerseRecordKeys.MainMenuPlayerSettingsSurface, + "main-menu-verse-settings" => AetheriaRuntimeVerseRecordKeys.MainMenuVerseSettingsSurface, + "inventory-panel" => AetheriaRuntimeVerseRecordKeys.InventoryPanelSurface, + "inventory-dropdown" => AetheriaRuntimeVerseRecordKeys.InventoryDropdownSurface, + "map-menu" => AetheriaRuntimeVerseRecordKeys.MapMenuSurface, + "trade-menu" => AetheriaRuntimeVerseRecordKeys.TradeMenuSurface, _ => throw new ArgumentOutOfRangeException(nameof(surfaceKind), surfaceKind, "Unknown Eve surface publication.") }; + return ReadPortableSurfaceAsync(node, key); +} + +static async Task ReadPortableSurfaceAsync(AetheriaStateNode node, CultRecordKey key) +{ + var portableSurface = await node.MutableDocument(key).ReadAsync().ConfigureAwait(false); + if (portableSurface != null) + return portableSurface; + var providerSurface = await node.MutableDocument(key).ReadAsync().ConfigureAwait(false); + return providerSurface == null ? null : AetheriaRuntimeSurfaceDocuments.ToEveSurfaceDocument(providerSurface); } static async Task RunClientCultMeshPumpAsync( @@ -1181,6 +1218,9 @@ static async Task PublishDaemonApiDocumentsAsync( await node.MutableDocument(AetheriaRuntimeVerseRecordKeys.DaemonProviderAdvertisement) .ReplaceAsync(result.ProviderAdvertisement) .ConfigureAwait(false); + await node.MutableDocument(AetheriaRuntimeVerseRecordKeys.EveProviderAdvertisement) + .ReplaceAsync(BuildCoreProviderAdvertisement(options, result.Frame.PublishedAtUtc)) + .ConfigureAwait(false); if (result.Health != null) await node.MutableDocument(AetheriaRuntimeVerseRecordKeys.DaemonHealth) .ReplaceAsync(result.Health) @@ -1203,18 +1243,15 @@ static async Task PublishDaemonApiDocumentsAsync( var activeMainMenuSurfaceId = string.IsNullOrWhiteSpace(mainMenuState?.ActiveSurfaceId) ? AetheriaRuntimeMainMenuCommands.RootSurfaceId : mainMenuState.ActiveSurfaceId; - if (result.GameSurface != null) - { - var gameSurface = AetheriaRuntimeDaemonGameSurfaceBuilder.Build( - result.Frame, - result.Health ?? new AetheriaRuntimeDaemonHealthDocument(), - result.CommandBoundary ?? AetheriaRuntimeDaemonCommandBoundaryDocument.Create(options.DaemonId), - result.StarbridgeSessionSummary, - activeMainMenuSurfaceId); - await node.MutableDocument(AetheriaRuntimeVerseRecordKeys.DaemonGameSurface) - .ReplaceAsync(AetheriaRuntimeSurfaceDocuments.ToPortableSurface(gameSurface)) - .ConfigureAwait(false); - } + var gameSurface = AetheriaRuntimeDaemonGameSurfaceBuilder.Build( + result.Frame, + result.Health ?? new AetheriaRuntimeDaemonHealthDocument(), + result.CommandBoundary ?? AetheriaRuntimeDaemonCommandBoundaryDocument.Create(options.DaemonId), + result.StarbridgeSessionSummary, + activeMainMenuSurfaceId); + await node.MutableDocument(AetheriaRuntimeVerseRecordKeys.DaemonGameSurface) + .ReplaceAsync(AetheriaRuntimeSurfaceDocuments.ToPortableSurface(gameSurface)) + .ConfigureAwait(false); if (result.GameTuiSurface != null) await node.MutableDocument(AetheriaRuntimeVerseRecordKeys.DaemonGameTuiSurface) .ReplaceAsync(AetheriaRuntimeSurfaceDocuments.ToPortableSurface(result.GameTuiSurface)) @@ -1231,6 +1268,50 @@ static async Task PublishDaemonApiDocumentsAsync( await PublishDaemonMenuSurfacesAsync(node, options, result.Frame).ConfigureAwait(false); } +static EveProviderAdvertisementDocument BuildCoreProviderAdvertisement( + AetheriaDaemonHostOptions options, + string updatedAtUtc) +{ + var interaction = new EveWorldInteractionAdvertisement( + "provider-authored-world-surface", + new[] { AetheriaRuntimeDaemonSchemas.Frame }, + "aetheria.daemon.commands", + AetheriaRuntimeVerseRecordKeys.EveCommandRecordPrefix, + EveCommandReceiptDocument.SchemaId, + AetheriaRuntimeVerseRecordKeys.EveReceiptRecordPrefix, + AetheriaRuntimeVerseRecordKeys.DaemonAssetManifest.ToString(), + new[] { "unity-scene", "web-reference", "electron-shell", "tui" }, + "provider-owns-world-state-command-acceptance-and-receipts"); + return new EveProviderAdvertisementDocument( + "aetheria.daemon", + options.DaemonId, + options.VerseId, + "Aetheria Daemon", + "game.daemon", + options.CultMeshAddress, + updatedAtUtc, + new GameCult.Eve.Surface.EveProviderFreshness("fresh", updatedAtUtc, 5000), + new[] + { + EveSurfaceDocument.SchemaId, + EveSurfaceCommandRequest.SchemaId, + EveCommandReceiptDocument.SchemaId + }, + Array.Empty(), + new[] + { + new EveAdvertisedSurface( + AetheriaRuntimeDaemonGameSurfaceBuilder.SurfaceId, + EveSurfaceDocument.SchemaId, + AetheriaRuntimeVerseRecordKeys.DaemonGameSurface.ToString(), + "cultmesh-record", + "active", + "interactive-world", + interaction) + }, + Array.Empty()); +} + static async Task PublishDaemonMenuSurfacesAsync( AetheriaStateNode node, AetheriaDaemonHostOptions options, @@ -1470,6 +1551,57 @@ static AetheriaRuntimeSurfaceComponent SurfaceNode( children); } +static async Task AcceptCoreEveInvocationsAsync( + AetheriaStateNode node, + AetheriaDaemonHostOptions options, + AetheriaRuntimeDaemonFrameDocument? currentFrame) +{ + var accounted = new HashSet( + currentFrame?.AccountedCommandIds ?? Array.Empty(), + StringComparer.Ordinal); + var receipted = node.Documents() + .Where(receipt => !string.IsNullOrWhiteSpace(receipt.CommandId)) + .Select(receipt => receipt.CommandId) + .ToHashSet(StringComparer.Ordinal); + foreach (var request in node.Documents() + .Where(request => request != null && !string.IsNullOrWhiteSpace(request.CommandId)) + .OrderBy(request => request.IssuedAt)) + { + if (accounted.Contains(request.CommandId) || receipted.Contains(request.CommandId)) + continue; + + if (AetheriaRuntimeDaemonOperationsClient.TryCreateSurfaceCommandDocument( + request, + currentFrame, + node.StatePath, + request.ClientId, + currentFrame?.SessionId ?? options.SessionId, + out var command) && command != null) + { + command.CommandId = request.CommandId; + command.ClientId = request.ClientId; + command.AuthorRuntimeId = request.ClientId; + await node.SubmitDaemonCommandAsync(command).ConfigureAwait(false); + continue; + } + + var denied = new EveCommandReceiptDocument( + $"receipt:{request.CommandId}:denied", + request.CommandId, + request.Command, + "denied", + "Aetheria", + options.DaemonId, + request.ProviderId, + request.SurfaceId, + "Command is not advertised by the Aetheria daemon surface.", + DateTimeOffset.UtcNow.ToString("O"), + Math.Max(currentFrame?.FrameId ?? 0, 0)); + await node.Database.PutAsync(AetheriaRuntimeVerseRecordKeys.EveReceipt(denied.ReceiptId), denied) + .ConfigureAwait(false); + } +} + static async Task AcceptEveCommandsAsync(AetheriaStateNode node, AetheriaDaemonHostOptions options) { var commandCountBefore = node.Documents().Count; diff --git a/Aetheria.State/AetheriaDocumentRegistry.cs b/Aetheria.State/AetheriaDocumentRegistry.cs index 396e2b9fe..34f65d177 100644 --- a/Aetheria.State/AetheriaDocumentRegistry.cs +++ b/Aetheria.State/AetheriaDocumentRegistry.cs @@ -66,7 +66,10 @@ public static class AetheriaDocumentRegistry typeof(AetheriaRuntimeCommittedCommandFactDocument), typeof(AetheriaRuntimeEveCommandDocument), typeof(AetheriaRuntimeSurfaceDocument), + typeof(EveProviderAdvertisementDocument), typeof(EveSurfaceDocument), + typeof(EveSurfaceCommandRequest), + typeof(EveCommandReceiptDocument), typeof(EveProviderAdvertisementState) ]; } diff --git a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonOperationsClient.cs b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonOperationsClient.cs index a84d47b6e..c8bf39d0c 100644 --- a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonOperationsClient.cs +++ b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeDaemonOperationsClient.cs @@ -16,6 +16,37 @@ public sealed class AetheriaRuntimeDaemonOperationsClient _submit = submit ?? throw new ArgumentNullException(nameof(submit)); } + public static bool TryCreateSurfaceCommandDocument( + EveSurfaceCommandRequest request, + AetheriaRuntimeDaemonFrameDocument? frame, + string stateFilePath, + string clientId, + string sessionId, + out AetheriaRuntimeDaemonCommandDocument? command) + { + command = null; + if (request == null) + return false; + + AetheriaRuntimeDaemonCommandDocument? translated = null; + var operations = new AetheriaRuntimeDaemonOperationsClient(submit => + { + var client = new AetheriaRuntimeDaemonOperationClient( + string.IsNullOrWhiteSpace(stateFilePath) ? "." : stateFilePath, + clientId, + sessionId, + document => + { + translated = document; + return AetheriaRuntimeDaemonOperationClient.ToEnvelope(document); + }); + return submit(client, frame); + }); + var accepted = operations.TrySubmitSurfaceCommand(request, out _); + command = translated; + return accepted && command != null; + } + public AetheriaRuntimeDaemonCommandEnvelope SetTarget(string targetEntityKey) { return Submit((client, frame) => client.SetTarget(frame, targetEntityKey)); diff --git a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeVerseClient.cs b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeVerseClient.cs index 2573932eb..43cc79649 100644 --- a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeVerseClient.cs +++ b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeVerseClient.cs @@ -6,6 +6,7 @@ using System.Linq; using System.Threading.Tasks; using GameCult.Caching; using GameCult.Caching.MessagePack; +using GameCult.Eve.Surface; using GameCult.Mesh; using GameCult.Networking; using R3; @@ -21,6 +22,13 @@ namespace GameCult.Aetheria.State.Verse public static CultRecordKey DaemonProviderAdvertisement { get; } = new CultRecordKey("daemon:aetheria.provider_advertisement.v1"); + public static CultRecordKey EveProviderAdvertisement { get; } = + new CultRecordKey("eve:provider:aetheria.daemon"); + + public const string EveCommandRecordPrefix = "eve:commands:aetheria.daemon"; + + public const string EveReceiptRecordPrefix = "eve:receipts:aetheria.daemon"; + public static CultRecordKey DaemonHealth { get; } = new CultRecordKey("daemon:aetheria.health.v1"); @@ -102,6 +110,11 @@ namespace GameCult.Aetheria.State.Verse return new CultRecordKey($"eve:commands:{StableToken(commandId)}:gamecult.eve.command.v1"); } + public static CultRecordKey EveReceipt(string receiptId) + { + return new CultRecordKey($"{EveReceiptRecordPrefix}:{StableToken(receiptId)}"); + } + public static string StableToken(string value) { if (string.IsNullOrWhiteSpace(value)) @@ -149,7 +162,10 @@ namespace GameCult.Aetheria.State.Verse typeof(AetheriaRuntimePlayerSettingsDocument), typeof(AetheriaRuntimeVerseHostSettingsDocument), typeof(AetheriaRuntimeSurfaceDocument), + typeof(EveProviderAdvertisementDocument), typeof(EveSurfaceDocument), + typeof(EveSurfaceCommandRequest), + typeof(EveCommandReceiptDocument), typeof(AetheriaRuntimeDaemonCommandDocument), typeof(AetheriaRuntimeEveCommandDocument), typeof(AetheriaRuntimeCommittedCommandFactDocument)