예제 #1
0
        public async Task Handle(IBotNotification <PlayerJoined> notification)
        {
            var context = notification.Context;

            if (_started || context.Player == null)
            {
                return;
            }

            if (_currentCount == null)
            {
                var game = await context.Player.GetState();

                _currentCount = game.Players.Count;
            }
            else
            {
                _currentCount++;
            }
            if (_currentCount < _targetPlayersCount)
            {
                return;
            }
            _started = true;
            await Task.Delay(TimeSpan.FromMilliseconds(500));

            await context.Player.StartGame();
        }
예제 #2
0
 private async Task HandleInternal <T>(IBotNotification <T> notification) where T : IClientEvent
 {
     foreach (var behaviour in _behaviours.OfType <IBotBehaviour <T> >())
     {
         await behaviour.Handle(notification);
     }
 }
예제 #3
0
        public async Task Handle(IBotNotification <BotAuthenticated> notification)
        {
            var context = notification.Context;
            await Task.Delay(_delay);

            context.Player = await context.User.JoinRoom(_roomId, context.Connection.Id);

            context.Logger = context.Logger.ForContext("RoomId", _roomId);
        }
예제 #4
0
        public async Task Handle(IBotNotification <BotStarted> notification)
        {
            var context     = notification.Context;
            var loginResult = await context.Connection.Login();

            context.UserId = loginResult.UserId;
            context.User   = loginResult.User;
            context.Logger = context.Logger.ForContext("UserId", context.UserId.Value);
            context.Notify(new BotAuthenticated());
        }
예제 #5
0
        public Task Handle(IBotNotification <IClientEvent> notification)
        {
            var     eventType            = notification.Event.GetType();
            var     botNotificationType  = typeof(BotNotification <>).MakeGenericType(eventType);
            dynamic internalNotification = Activator.CreateInstance(
                botNotificationType,
                notification.Event,
                notification.Context);

            return(HandleInternal(internalNotification));
        }
예제 #6
0
        public Task Handle(IBotNotification <PlayerDead> notification)
        {
            var context = notification.Context;
            var @event  = notification.Event;

            if (@event.UserId != context.UserId)
            {
                return(Task.CompletedTask);
            }
            _ended = true;
            return(Task.CompletedTask);
        }
예제 #7
0
        public async Task Handle(IBotNotification <GameStarted> notification)
        {
            var context = notification.Context;

            if (context.Player == null)
            {
                return;
            }
            var game = (GameData)await context.Player.GetState();

            _map = game.Map;
            await Think(context);
        }
예제 #8
0
        public async Task Handle(IBotNotification <TurnCalculationEnded> notification)
        {
            var context = notification.Context;
            var @event  = notification.Event;

            if (_ended || context.Player == null)
            {
                return;
            }
            _map = @event.Map;
            using (context.Logger.BeginTimedOperation("Bot.Think"))
            {
                await Think(context);
            }
            ;
        }
예제 #9
0
        public async Task Handle(IBotNotification <IClientEvent> notification)
        {
            var context = notification.Context;

            try
            {
                await _next.Handle(notification);
            }
            catch (Exception ex)
            {
                context.Logger.Error(ex, "[Bot:{BotId}:{UserId}] Failed to handle {EventType}: {Message}",
                                     context.BotId,
                                     context.UserId,
                                     notification.Event.GetType().Name,
                                     ex.Message);
                context.Complete(ex);
            }
        }
예제 #10
0
        public Task Handle(IBotNotification <IClientEvent> notification)
        {
            var context = notification.Context;
            var i       = 0;
            var policy  = Policy.Handle <Exception>()
                          .WaitAndRetryAsync(3,
                                             retry => TimeSpan.FromMilliseconds(100 * retry),
                                             (ex, time) =>
            {
                context.Logger.Warning(
                    "[Bot:{BotId}:{UserId}]: Retrying handler for {EventType}. Retry number is {Retry}.",
                    context.BotId,
                    context.UserId,
                    notification.Event.GetType().Name,
                    i);
                i++;
            });

            return(policy.ExecuteAsync(() => _next.Handle(notification)));
        }
예제 #11
0
        public async Task Handle(IBotNotification <IClientEvent> notification)
        {
            var context = notification.Context;
            var @event  = notification.Event;

            _processed = _processed ??
                         context.Logger.CountOperation(ProcessedCounterName, resolution: ProcessedCounterResolution);
            var diagnosticProperties = new ILogEventEnricher[]
            {
                new PropertyEnricher("ConnectionId", context.Connection.Id),
                new PropertyEnricher("BotId", context.BotId),
                new PropertyEnricher("UserId", context.UserId),
                new PropertyEnricher("EventType", notification.Event.GetType().Name)
            };

            using (LogContext.Push(diagnosticProperties))
            {
                context.Logger.Debug("[Bot:{BotId}:{UserId}] Started to handle {EventType}.",
                                     context.BotId,
                                     context.UserId,
                                     @event.GetType().Name);
                try
                {
                    using (context.Logger.BeginTimedOperation(OperationDescription))
                    {
                        await _next.Handle(notification);
                    }
                }
                finally
                {
                    _processed.Increment();
                    context.Logger.Debug("[Bot:{BotId}:{UserId}] Finished to handle {EventType}.",
                                         context.BotId,
                                         context.UserId,
                                         @event.GetType().Name);
                }
            }
        }
예제 #12
0
 public Task Handle(IBotNotification <GameEnded> notification)
 {
     notification.Context.Complete();
     return(Task.CompletedTask);
 }
예제 #13
0
 public Task Handle(IBotNotification <GameEnded> notification)
 {
     _ended = true;
     return(Task.CompletedTask);
 }