From 768f36ecdc2d876f332e28b8e80d77aa5f8c3f6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Wed, 11 Jan 2023 22:12:55 +0100 Subject: [PATCH] feat(game): add every game event responder --- .../Events/Core/EventDispatcher.cs | 26 ++++++++++++++++--- .../Events/Core/IGameResponder.cs | 20 ++++++++++++-- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/Core/NosSmooth.Game/Events/Core/EventDispatcher.cs b/Core/NosSmooth.Game/Events/Core/EventDispatcher.cs index ff09c1e..06584a9 100644 --- a/Core/NosSmooth.Game/Events/Core/EventDispatcher.cs +++ b/Core/NosSmooth.Game/Events/Core/EventDispatcher.cs @@ -36,10 +36,30 @@ public class EventDispatcher where TEvent : IGameEvent { using var scope = _provider.CreateScope(); - var results = await Task.WhenAll( + + async Task SafeCall(Func> result) + { + try + { + return await result(); + } + catch (Exception e) + { + return e; + } + } + + var results = await Task.WhenAll + ( scope.ServiceProvider .GetServices>() - .Select(responder => responder.Respond(@event, ct)) + .Select(responder => SafeCall(() => responder.Respond(@event, ct))) + .Concat + ( + scope.ServiceProvider + .GetServices() + .Select(responder => SafeCall(() => responder.Respond(@event, ct))) + ) ); return results.Length switch @@ -49,4 +69,4 @@ public class EventDispatcher _ => new AggregateError(results.Cast().ToArray()), }; } -} +} \ No newline at end of file diff --git a/Core/NosSmooth.Game/Events/Core/IGameResponder.cs b/Core/NosSmooth.Game/Events/Core/IGameResponder.cs index e578f4d..a1f14b5 100644 --- a/Core/NosSmooth.Game/Events/Core/IGameResponder.cs +++ b/Core/NosSmooth.Game/Events/Core/IGameResponder.cs @@ -24,10 +24,26 @@ public interface IGameResponder : IGameResponder where TEvent : IGameEvent { /// - /// Respond to the given packet. + /// Respond to the given event. /// - /// The packet to respond to. + /// The event to respond to. /// The cancellation token for cancelling the operation. /// A result that may or may not have succeeded. public Task Respond(TEvent gameEvent, CancellationToken ct = default); } + +/// +/// Represents interface for classes that respond to every game event. +/// Responds to any game event. +/// +public interface IEveryGameResponder +{ + /// + /// Respond to any event. + /// + /// The event to respond to. + /// The cancellation token for cancelling the operation. + /// The current event type. + /// A result that may or may not have succeeded. + public Task Respond(TEvent gameEvent, CancellationToken ct = default); +} \ No newline at end of file -- 2.49.0