Ejemplo n.º 1
0
        public async Task BuyAsync(string ticker, int quantity)
        {
            if (quantity < 1)
            {
                await ReplyAsync("Invalid quantity. Must be greater than 0");

                return;
            }
            var upperTicker = ticker.ToUpperInvariant();

            RonStock stock = RonStockMarketService.GetStock(upperTicker);

            if (stock == null)
            {
                await ReplyAsync($"Stock {upperTicker} doesn't exist.");

                return;
            }

            int cost = stock.Price * quantity * -1;


            UserRonStock usr = await GameService.GetUserRonStockAsync(Context.User, upperTicker);


            if (!await GameService.AddRonPoints(Context.User, cost))
            {
                await ReplyAsync($"Can't Afford to buy {quantity} shares of {upperTicker} for {cost} rp.");

                return;
            }

            if (usr == null)
            {
                usr = new UserRonStock()
                {
                    UserID = Context.User.Id, Symbol = upperTicker, Quantity = quantity
                };
                await GameService.AddUserRonStock(usr);
            }
            else
            {
                usr.Quantity += quantity;
                await GameService.UpdateUserRonStock(usr);
            }

            await ReplyAsync($"Bought {quantity} shares of {upperTicker} for {cost} rp.");
        }