private async Task <TResponse> HandleMessageAsync(TMessage messageObject, IMediationContext mediationContext, CancellationToken cancellationToken)
        {
            var type = typeof(TMessage);

            if (!_messageHandlers.Any())
            {
                throw new ArgumentException($"No handler of signature {typeof(IMessageHandler<,>).Name} was found for {typeof(TMessage).Name}", typeof(TMessage).FullName);
            }

            if (typeof(IEvent).IsAssignableFrom(type))
            {
                var tasks  = _messageHandlers.Select(r => r.HandleAsync(messageObject, mediationContext, cancellationToken));
                var result = default(TResponse);

                foreach (var task in tasks)
                {
                    result = await task;
                }

                return(result);
            }

            if (typeof(IQuery <TResponse>).IsAssignableFrom(type) || typeof(ICommand).IsAssignableFrom(type))
            {
                return(await _messageHandlers.Single().HandleAsync(messageObject, mediationContext, cancellationToken));
            }

            throw new ArgumentException($"{typeof(TMessage).Name} is not a known type of {typeof(IMessage<>).Name} - Query, Command or Event", typeof(TMessage).FullName);
        }
        protected override async Task HandleCommandAsync(JoinGameCommand command, IMediationContext mediationContext, CancellationToken cancellationToken)
        {
            var gameContext = mediationContext as GameContext;
            var game        = _gameManager.GetGame(command.GameCode);

            var player = new Player()
            {
                Initials = command.PlayerInitials
            };

            game.Players.TryAdd(player.Initials, player);

            _gameConnectionIdStore.StorePlayerConnectionIdForGame(command.GameCode, command.PlayerInitials, gameContext.ConnectionId);

            gameContext.GameCode      = command.GameCode;
            gameContext.PlayerIntials = command.PlayerInitials;

            // TODO: refactor into own handler
            var playerDetails = game.Players.Values
                                .Select(p => new PlayerDetails {
                Intials = p.Initials, Ready = p.Ready
            })
                                .ToList();

            await _clientMessageDispatcherFactory
            .CreateClientMessageDispatcher(x => x.UpdatePlayersList(playerDetails))
            .SendToAllGameClients(command.GameCode);
        }
Пример #3
0
        public async Task <Unit> HandleAsync(TEvent message, IMediationContext mediationContext,
                                             CancellationToken cancellationToken)
        {
            await HandleEventAsync(message, mediationContext, cancellationToken);

            return(Unit.Result);
        }
        protected override Task HandleEventAsync(ClientDisconnectedEvent @event, IMediationContext mediationContext, CancellationToken cancellationToken)
        {
            var gameContext = mediationContext as GameContext;

            // TODO: Add back in:
            // _gameConnectionIdStore.RemovePlayerConnectionIdForGame(gameContext.GameCode, gameContext.ConnectionId);
            return(Task.CompletedTask);
        }
Пример #5
0
        public async Task <TResponse> RunAsync(TMessage message, IMediationContext mediationContext,
                                               CancellationToken cancellationToken, HandleMessageDelegate <TMessage, TResponse> next)
        {
            Console.WriteLine("Constrained validator hit");
            var result = await next.Invoke(message, mediationContext, cancellationToken);

            return(result);
        }
        public async Task <TResponse> RunAsync(TMessage message, IMediationContext mediationContext,
                                               CancellationToken cancellationToken, HandleMessageDelegate <TMessage, TResponse> next)
        {
            Console.WriteLine("Message pre logged using middleware 1");
            var result = await next.Invoke(message, mediationContext, cancellationToken);

            Console.WriteLine("Message post logged using middleware 1");

            return(result);
        }
        private Task <TResponse> RunMiddleware(TMessage message, HandleMessageDelegate <TMessage, TResponse> handleMessageHandlerCall,
                                               IMediationContext mediationContext, CancellationToken cancellationToken)
        {
            HandleMessageDelegate <TMessage, TResponse> next = null;

            next = _middlewares.Reverse().Aggregate(handleMessageHandlerCall, (messageDelegate, middleware) =>
                                                    ((req, ctx, ct) => middleware.RunAsync(req, ctx, ct, messageDelegate)));

            return(next.Invoke(message, mediationContext, cancellationToken));
        }
Пример #8
0
        protected override async Task <SimpleResponse> HandleQueryAsync(SimpleQuery query,
                                                                        IMediationContext mediationContext, CancellationToken cancellationToken)
        {
            Console.WriteLine("Test query");

            return(new SimpleResponse()
            {
                Message = "Test query messsage"
            });
        }
Пример #9
0
        public async Task <TResponse> RunAsync(TMessage message, IMediationContext mediationContext, CancellationToken cancellationToken, HandleMessageDelegate <TMessage, TResponse> next)
        {
            var gameContext = mediationContext as GameContext;

            if (gameContext != null)
            {
                gameContext.Game = gameContext.GameCode != null?_gameManager.GetGame(gameContext.GameCode) : null;
            }

            return(await next.Invoke(message, mediationContext, cancellationToken));
        }
        public async Task <TResponse> RunAsync(TMessage message, IMediationContext mediationContext,
                                               CancellationToken cancellationToken, HandleMessageDelegate <TMessage, TResponse> next)
        {
            if (mediationContext.GetType().IsAssignableFrom(typeof(MassTransitSendMediationContext <TMessage, TResponse>)))
            {
                var context = mediationContext as MassTransitSendMediationContext <TMessage, TResponse>;
                return(await context.Client.Request(message, cancellationToken));
            }

            // Pass through
            return(await next.Invoke(message, mediationContext, cancellationToken));
        }
Пример #11
0
        private Task <TResponse> InvokeInstanceAsync <TResponse>(object instance, IMessage <TResponse> message, Type targetHandler,
                                                                 IMediationContext mediationContext, CancellationToken cancellationToken)
        {
            var method = instance.GetType()
                         .GetTypeInfo()
                         .GetMethod(nameof(IMessageProcessor <IMessage <TResponse>, TResponse> .HandleAsync));

            if (method == null)
            {
                throw new ArgumentException($"{instance.GetType().Name} is not a known {targetHandler.Name}",
                                            instance.GetType().FullName);
            }

            return((Task <TResponse>)method.Invoke(instance, new object[] { message, mediationContext, cancellationToken }));
        }
Пример #12
0
        public Task <TResponse> HandleAsync <TResponse>(IMessage <TResponse> message,
                                                        IMediationContext mediationContext = default(MediationContext), CancellationToken cancellationToken = default(CancellationToken))
        {
            if (mediationContext == null)
            {
                mediationContext = MediationContext.Default;
            }

            var targetType    = message.GetType();
            var targetHandler = typeof(IMessageProcessor <,>).MakeGenericType(targetType, typeof(TResponse));
            var instance      = _serviceFactory.GetInstance(targetHandler);

            var result = InvokeInstanceAsync(instance, message, targetHandler, mediationContext, cancellationToken);

            return(result);
        }
Пример #13
0
        protected override async Task <SimpleMassTransitResponse> HandleQueryAsync(SimpleMassTransitMessage query,
                                                                                   IMediationContext mediationContext, CancellationToken cancellationToken)
        {
            Console.WriteLine($"Proccessed with mediation context of {mediationContext.GetType().Name}");

            var response = new SimpleMassTransitResponse()
            {
                Message = query.Message + " received."
            };

            // Or you can respond using the context directly
            //if (mediationContext is MassTransitReceiveMediationContext<SimpleMassTransitMessage, SimpleMassTransitResponse> context)
            //{
            //    await context.ConsumeContext.RespondAsync(response);
            //    context.IsHandled = true;
            //}

            return(response);
        }
        protected override async Task HandleCommandAsync(StartGameCommand command, IMediationContext mediationContext, CancellationToken cancellationToken)
        {
            var gameContext = mediationContext as GameContext;
            var gameReady   = gameContext.Game.Players.All(o => o.Value.Ready);

            // TODO: Add validation

            gameContext.Game.CurrentState = GameState.Started;
            foreach (var playerReset in gameContext.Game.Players)
            {
                playerReset.Value.Ready = false;
            }

            var players = gameContext.Game.Players.Values.ToList();

            _characterAssignment.AssignRoles(players);

            foreach (var player in players)
            {
                var playerCharacterNotification = new ShowCharacterNotification()
                {
                    Role = player.Character.Role,
                    Team = player.Character.Team
                };

                await _clientMessageDispatcherFactory
                .CreateClientMessageDispatcher(x => x.ShowCharacter(playerCharacterNotification))
                .SendToPlayerInGame(gameContext.GameCode, player.Initials);
            }

            gameContext.Game.SortedPlayers = _playerOrderInitialisation.GetSortedPlayers(players);
            gameContext.Game.Missions      = _missionInitialisation.InitiliseMissions(players.Count);

            var firstMission = gameContext.Game.Missions.Where(o => o.Number == 1).SingleOrDefault();

            firstMission.Team.Leader = gameContext.Game.SortedPlayers.First();

            //TODO broadcast mission update
        }
        protected override async Task HandleCommandAsync(PlayerReadyCommand command, IMediationContext mediationContext, CancellationToken cancellationToken)
        {
            var gameContext = mediationContext as GameContext;
            var player      = gameContext.Game.Players
                              .Where(o => o.Key == gameContext.PlayerIntials)
                              .SingleOrDefault()
                              .Value;

            player.Ready = command.Ready;

            //var playerDetails = _mapper.ProjectTo<Dispatchers.Models.PlayerDetails>(request.GameState.Players.Values.AsQueryable()).ToList();
            var playerDetails = gameContext.Game.Players.Values.Select(p => new PlayerDetails {
                Intials = p.Initials, Ready = p.Ready
            }).ToList();
            await _clientMessageDispatcherFactory
            .CreateClientMessageDispatcher(x => x.UpdatePlayersList(playerDetails))
            .SendToAllGameClients(gameContext.GameCode);

            var allPlayersReady = gameContext.Game.Players.All(o => o.Value.Ready);

            if (allPlayersReady && gameContext.Game.Players.Count > 4 || gameContext.Game.Players.Count == 1)
            {
                if (gameContext.Game.CurrentState == GameState.GamePending)
                {
                    await _clientMessageDispatcherFactory
                    .CreateClientMessageDispatcher(x => x.Countdown(true))
                    .SendToAllGameClients(gameContext.GameCode);
                }
                else
                {
                    await _clientMessageDispatcherFactory
                    .CreateClientMessageDispatcher(x => x.ShowLeaderScript(true))
                    .SendToAllGameClients(gameContext.GameCode);
                }
            }
        }
Пример #16
0
 protected override async Task HandleEventAsync(SimpleEvent @event, IMediationContext mediationContext,
                                                CancellationToken cancellationToken)
 {
     Console.WriteLine("Event handler 2");
 }
 protected override async Task HandleCommandAsync(CreateGameCommand command, IMediationContext mediationContext, CancellationToken cancellationToken)
 {
     var code = _gameManager.CreateGame();
     await Task.CompletedTask;
 }
Пример #18
0
 protected abstract Task HandleEventAsync(TEvent @event, IMediationContext mediationContext,
                                          CancellationToken cancellationToken);
 protected override async Task HandleCommandAsync(SimpleCommand message, IMediationContext mediationContext,
                                                  CancellationToken cancellationToken)
 {
     Console.WriteLine("Test Command");
 }
Пример #20
0
 protected abstract Task HandleCommandAsync(TCommand command, IMediationContext mediationContext,
                                            CancellationToken cancellationToken);
Пример #21
0
 public Task <TResponse> HandleAsync(TQuery message, IMediationContext mediationContext,
                                     CancellationToken cancellationToken)
 {
     return(HandleQueryAsync(message, mediationContext, cancellationToken));
 }
Пример #22
0
 protected abstract Task <TResponse> HandleQueryAsync(TQuery query, IMediationContext mediationContext,
                                                      CancellationToken cancellationToken);
 public Task <TResponse> HandleAsync(TMessage message, IMediationContext mediationContext,
                                     CancellationToken cancellationToken)
 {
     return(RunMiddleware(message, HandleMessageAsync, mediationContext, cancellationToken));
 }
 public async Task <SimpleResponse> RunAsync(SimpleQuery message, IMediationContext mediationContext,
                                             CancellationToken cancellationToken, HandleMessageDelegate <SimpleQuery, SimpleResponse> next)
 {
     Console.WriteLine("Validation hit");
     return(await next.Invoke(message, mediationContext, cancellationToken));
 }