public async Task InfoAsync([Remainder] string ticker = null) { var upperTicker = ticker.ToUpperInvariant(); RonStock stock = RonStockMarketService.GetStock(upperTicker); await ReplyAsync("", false, BuildEmbed(stock)); }
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."); }
public async Task SellAsync(string ticker, int quantity) { var upperTicker = ticker.ToUpperInvariant(); if (quantity < 1) { await ReplyAsync($"Invalid quantity. Must be greater than 0"); return; } RonStock stock = RonStockMarketService.GetStock(upperTicker); if (stock == null) { await ReplyAsync($"Stock {upperTicker} doesn't exist."); return; } UserRonStock usr = await GameService.GetUserRonStockAsync(Context.User, upperTicker); if (usr == null || usr.Quantity < quantity) { await ReplyAsync($"You don't own enough {upperTicker} to sell {quantity} shares."); return; } int cost = quantity * stock.Price; usr.Quantity -= quantity; if (usr.Quantity == 0) { await GameService.DeleteUserRonStock(usr); } else { await GameService.UpdateUserRonStock(usr); } await GameService.AddRonPoints(Context.User, cost); await ReplyAsync($"Sold {quantity} shares of {upperTicker} for {cost}."); }
private Embed BuildEmbed(RonStock stock) { EmbedBuilder builder = new EmbedBuilder(); builder.WithTitle(stock.CompanyName); builder.AddField("Ticker", stock.Symbol); builder.AddField("Price", string.Format("{0}rp", stock.Price), true); builder.AddField("Change", string.Format("{0}", stock.Change), true); builder.WithFooter("Stock Data provided by Ronners!"); if (stock.Change > 0) { builder.WithColor(Color.Green); } else if (stock.Change < 0) { builder.WithColor(Color.Red); } else { builder.WithColor(Color.LightGrey); } return(builder.Build()); }