From 5aff1615840d40361f11b6ea773b034a99711cbd Mon Sep 17 00:00:00 2001 From: Rutherther Date: Fri, 13 Jan 2023 19:26:03 +0100 Subject: [PATCH] feat(game): add dialog handling, dialog opened event --- Core/NosSmooth.Game/Apis/DialogHandler.cs | 56 +++++++ Core/NosSmooth.Game/Data/Dialogs/Dialog.cs | 7 +- .../Events/Ui/DialogOpenedEvent.cs | 11 ++ .../Extensions/ServiceCollectionExtensions.cs | 1 + .../PacketHandlers/Ui/DialogOpenResponder.cs | 147 ++++++++++++++++++ 5 files changed, 220 insertions(+), 2 deletions(-) create mode 100644 Core/NosSmooth.Game/Apis/DialogHandler.cs create mode 100644 Core/NosSmooth.Game/Events/Ui/DialogOpenedEvent.cs create mode 100644 Core/NosSmooth.Game/PacketHandlers/Ui/DialogOpenResponder.cs diff --git a/Core/NosSmooth.Game/Apis/DialogHandler.cs b/Core/NosSmooth.Game/Apis/DialogHandler.cs new file mode 100644 index 0000000..b62a671 --- /dev/null +++ b/Core/NosSmooth.Game/Apis/DialogHandler.cs @@ -0,0 +1,56 @@ +// +// DialogHandler.cs +// +// Copyright (c) František Boháček. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using NosSmooth.Core.Client; +using NosSmooth.Game.Data.Dialogs; +using Remora.Results; + +namespace NosSmooth.Game.Apis; + +/// +/// Handles accepting and denying of dialogs. +/// +public class DialogHandler +{ + private readonly INostaleClient _client; + + /// + /// Initializes a new instance of the class. + /// + /// The client. + public DialogHandler(INostaleClient client) + { + _client = client; + } + + /// + /// Accept the operation the dialog does. + /// + /// The opened dialog. + /// The cancellation token used for cancelling the operation. + /// A result that may or may not have succeeded. + public Task AcceptAsync(Dialog dialog, CancellationToken ct = default) + => _client.SendPacketAsync(dialog.AcceptCommand, ct); + + /// + /// Try to deny the operation the dialog does. + /// + /// + /// Some dialogs do not allow denying, they are just ignored instead. + /// + /// The opened dialog. + /// The cancellation token used for cancelling the operation. + /// A result that may or may not have succeeded. + public Task DenyAsync(Dialog dialog, CancellationToken ct = default) + { + if (dialog.DenyCommand is null) + { + return Task.FromResult(Result.FromSuccess()); + } + + return _client.SendPacketAsync(dialog.DenyCommand, ct); + } +} \ No newline at end of file diff --git a/Core/NosSmooth.Game/Data/Dialogs/Dialog.cs b/Core/NosSmooth.Game/Data/Dialogs/Dialog.cs index 9b3d6e2..f8e3a66 100644 --- a/Core/NosSmooth.Game/Data/Dialogs/Dialog.cs +++ b/Core/NosSmooth.Game/Data/Dialogs/Dialog.cs @@ -4,6 +4,9 @@ // Copyright (c) František Boháček. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using NosSmooth.Packets.Enums; +using OneOf; + namespace NosSmooth.Game.Data.Dialogs; /// @@ -14,7 +17,7 @@ namespace NosSmooth.Game.Data.Dialogs; public record Dialog ( string AcceptCommand, - - // OneOf Message, + string? DenyCommand, + OneOf Message, IReadOnlyList Parameters ); \ No newline at end of file diff --git a/Core/NosSmooth.Game/Events/Ui/DialogOpenedEvent.cs b/Core/NosSmooth.Game/Events/Ui/DialogOpenedEvent.cs new file mode 100644 index 0000000..e96c57a --- /dev/null +++ b/Core/NosSmooth.Game/Events/Ui/DialogOpenedEvent.cs @@ -0,0 +1,11 @@ +// +// DialogOpenedEvent.cs +// +// Copyright (c) František Boháček. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using NosSmooth.Game.Data.Dialogs; + +namespace NosSmooth.Game.Events.Ui; + +public record DialogOpenedEvent(Dialog Dialog) : IGameEvent; \ No newline at end of file diff --git a/Core/NosSmooth.Game/Extensions/ServiceCollectionExtensions.cs b/Core/NosSmooth.Game/Extensions/ServiceCollectionExtensions.cs index 2ca67d5..6c2f5d9 100644 --- a/Core/NosSmooth.Game/Extensions/ServiceCollectionExtensions.cs +++ b/Core/NosSmooth.Game/Extensions/ServiceCollectionExtensions.cs @@ -68,6 +68,7 @@ public static class ServiceCollectionExtensions .AddPacketResponder(); serviceCollection + .AddTransient() .AddTransient() .AddTransient() .AddTransient() diff --git a/Core/NosSmooth.Game/PacketHandlers/Ui/DialogOpenResponder.cs b/Core/NosSmooth.Game/PacketHandlers/Ui/DialogOpenResponder.cs new file mode 100644 index 0000000..77db444 --- /dev/null +++ b/Core/NosSmooth.Game/PacketHandlers/Ui/DialogOpenResponder.cs @@ -0,0 +1,147 @@ +// +// DialogOpenResponder.cs +// +// Copyright (c) František Boháček. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using NosSmooth.Core.Packets; +using NosSmooth.Game.Data.Dialogs; +using NosSmooth.Game.Events.Core; +using NosSmooth.Game.Events.Ui; +using NosSmooth.Packets.Enums; +using NosSmooth.Packets.Server.UI; +using Remora.Results; + +namespace NosSmooth.Game.PacketHandlers.Ui; + +/// +/// A responder to dialog events that calls DialogOpen. +/// +public class DialogOpenResponder : IPacketResponder, IPacketResponder, + IPacketResponder, IPacketResponder, IPacketResponder +{ + private readonly EventDispatcher _eventDispatcher; + + /// + /// Initializes a new instance of the class. + /// + /// The event dispatcher. + public DialogOpenResponder(EventDispatcher eventDispatcher) + { + _eventDispatcher = eventDispatcher; + + } + + /// + public Task Respond(PacketEventArgs packetArgs, CancellationToken ct = default) + { + var packet = packetArgs.Packet; + if (packet.Type != QnamlType.Dialog) + { + return Task.FromResult(Result.FromSuccess()); + } + + return _eventDispatcher.DispatchEvent + ( + new DialogOpenedEvent + ( + new Dialog + ( + packet.AcceptCommand, + null, + packet.Message, + Array.Empty() + ) + ), + ct + ); + } + + /// + public Task Respond(PacketEventArgs packetArgs, CancellationToken ct = default) + { + var packet = packetArgs.Packet; + if (packet.Type != QnamlType.Dialog) + { + return Task.FromResult(Result.FromSuccess()); + } + + return _eventDispatcher.DispatchEvent + ( + new DialogOpenedEvent + ( + new Dialog + ( + packet.AcceptCommand, + null, + packet.MessageConst, + packet.Parameters + ) + ), + ct + ); + } + + /// + public Task Respond(PacketEventArgs packetArgs, CancellationToken ct = default) + { + var packet = packetArgs.Packet; + + return _eventDispatcher.DispatchEvent + ( + new DialogOpenedEvent + ( + new Dialog + ( + packet.AcceptCommand, + null, + packet.Message, + Array.Empty() + ) + ), + ct + ); + } + + /// + public Task Respond(PacketEventArgs packetArgs, CancellationToken ct = default) + { + var packet = packetArgs.Packet; + + return _eventDispatcher.DispatchEvent + ( + new DialogOpenedEvent + ( + new Dialog + ( + packet.AcceptCommand, + packet.DenyCommand, + packet.Message, + Array.Empty() + ) + ), + ct + ); + } + + /// + public Task Respond(PacketEventArgs packetArgs, CancellationToken ct = default) + { + var packet = packetArgs.Packet; + + return _eventDispatcher.DispatchEvent + ( + new DialogOpenedEvent + ( + new Dialog + ( + packet.AcceptCommand, + packet.DenyCommand, + packet.MessageConst, + packet.Parameters + ) + ), + ct + ); + } +} \ No newline at end of file -- 2.49.0