Exemplo n.º 1
0
        protected override void HandleCommand(IChatClient chatClient,
                                              CommandReceivedEventArgs eventArgs)
        {
            var    survivor = _survivorRepo.GetOrCreate(eventArgs.ChatUser);
            string operand  = eventArgs.Arguments.FirstOrDefault();

            var operation = _operations.SingleOrDefault(op => op.ShouldExecute(operand));

            if (operation != null)
            {
                string message = operation.TryToExecute(eventArgs, survivor);
                chatClient.SendMessage(message);
            }
            else
            {
                var    items       = _gameRepository.List(ShopItemPolicy.All());
                string itemDisplay = string.Join(", ", items.Select(x => $"{x.Id}:{x.Name}-{x.Price}"));
                chatClient.SendMessage($"Buy somethin' will ya! For sale: {itemDisplay}");
            }
        }
        public override string TryToExecute(CommandReceivedEventArgs eventArgs,
                                            Survivor survivor)
        {
            string itemRequested = eventArgs.Arguments.ElementAtOrDefault(1);

            if (int.TryParse(itemRequested, out int itemId) && itemId > 0)
            {
                ShopItem shopItem =
                    _gameRepository.Single(ShopItemPolicy.ById(itemId));
                if (shopItem != null)
                {
                    if (survivor.BuyItem(shopItem))
                    {
                        _gameRepository.Update(survivor);
                        return($"{survivor.DisplayName} bought the {shopItem.Name} item.");
                    }
                    return($"{survivor.Money} is not enough money to buy the {shopItem.Name} item.");
                }
            }

            return("Please buy one of the listed items by Id.");
        }