// ReportLeaders returns a string of the leaderboard func ReportLeaders(ag *accessors.AccessorGroup) string { users := ag.GetAllUsers() pValues := []models.PortfolioValue{} // Compile portfolio data for _, user := range users { portfolio := ag.GetPortfolio(user.UserID) worth := portfolio.Turnips if worth != 1000000 && len(portfolio.Investments) > 0 { for _, value := range portfolio.Investments { if value.Quantity > 0 { price := models.CheckStock(value.Ticker).Price worth = worth + price*value.Quantity } } pValues = append(pValues, models.PortfolioValue{UserID: user.UserID, Username: user.Username, Value: worth}) } } // Sort the portfolios by value sort.Sort(models.SortedPortfolioValue(pValues)) message := []string{} message = append(message, fmt.Sprintf("*End of the Day Leaderboard*")) // Run through the sorted values and compile the message for _, pValue := range pValues { message = append(message, fmt.Sprintf("<@%s|%s> has a net worth of %s turnips.", pValue.UserID, pValue.Username, Comma(pValue.Value))) } response := strings.Join(message, "\\n") // Double escape the newline because Slack incoming webhooks are obsessive with JSON formatting while the /slash-command "endpoints" are now return response }
// Buy buys a given number of stocks for the user func Buy(userID string, quantity int, symbol string, ag *accessors.AccessorGroup) string { if !MarketOpen() { return fmt.Sprintf("The Stalk Market is currently closed.") } stock := models.CheckStock(symbol) user := ag.GetUser(userID) if stock.Name == "N/A" || stock.Price == 0 { return fmt.Sprintf("%s does not appear to be a valid stock...\n", strings.ToUpper(symbol)) // Return the price through the API endpoint } // Make sure they have enough turnips to buy if user.Turnips < stock.Price*quantity { return fmt.Sprintf("%s shares of %s costs %s turnips and you have %s turnips.\n", Comma(quantity), strings.ToUpper(symbol), Comma(stock.Price*quantity), Comma(user.Turnips)) // Return information about a user's portfolio } ag.SubtractTurnips(userID, stock.Price*quantity) if quantity == 0 { quantity = 1 } ag.AddShares(userID, symbol, quantity) Webhook(fmt.Sprintf("<@%s|%s> purchased %s share(s) of %s for %s turnips.", user.UserID, user.Username, Comma(quantity), strings.ToUpper(symbol), Comma(stock.Price*quantity))) return fmt.Sprintf("%s turnips were spent to add %s share(s) of %s to your portfolio.\n", Comma(stock.Price*quantity), Comma(quantity), strings.ToUpper(symbol)) // Return information about a user's portfolio }
// Check checks the given symbol for current price func Check(symbol string) string { stock := models.CheckStock(symbol) if stock.Name != "N/A" && stock.Price > 0 { // Make sure Yahoo gave us a valid stock return fmt.Sprintf("%s (%s) is currently worth %s turnips (%s).\n", stock.Name, strings.ToUpper(symbol), Comma(stock.Price), stock.Change) // Return the price through the API endpoint } else { return fmt.Sprintf("%s does not appear to be a valid stock...\n", strings.ToUpper(symbol)) // Return the price through the API endpoint } }
// Portfolio gets the given user's portfolio and returns it in string form func Portfolio(userID string, ag *accessors.AccessorGroup) string { portfolio := ag.GetPortfolio(userID) message := []string{} worth := portfolio.Turnips message = append(message, fmt.Sprintf("You have %s turnips in your wallet.", Comma(portfolio.Turnips))) for _, value := range portfolio.Investments { if value.Quantity > 0 { price := models.CheckStock(value.Ticker).Price worth = worth + price*value.Quantity message = append(message, fmt.Sprintf("You have %s share(s) of %s worth %s turnips total.", Comma(value.Quantity), value.Ticker, Comma(price*value.Quantity))) } } message = append(message, fmt.Sprintf("Your net worth is %s turnips.", Comma(worth))) response := strings.Join(message, "\n") return response }
// Sell sells a given number of the user's holdings in the symbol func Sell(userID string, quantity int, symbol string, ag *accessors.AccessorGroup) string { if !MarketOpen() { return fmt.Sprintf("The Stalk Market is currently closed.") } stock := models.CheckStock(symbol) user := ag.GetUser(userID) if stock.Name == "N/A" || stock.Price == 0 { return fmt.Sprintf("%s does not appear to be a valid stock...\n", strings.ToUpper(symbol)) // Return the price through the API endpoint } holdings := ag.GetShare(userID, symbol) if holdings >= quantity { // If we successfully sell ag.SubtractShares(userID, symbol, quantity) ag.AddTurnips(userID, stock.Price*quantity) // Add turnips to our wallet } else { // Else return a human-readable error return fmt.Sprintf("You do not have enough shares of %s to sell %s. You have %s shares.\n", strings.ToUpper(symbol), Comma(quantity), Comma(holdings)) // Return information about a user's portfolio } Webhook(fmt.Sprintf("<@%s|%s> sold %s share(s) of %s for %s turnips.", user.UserID, user.Username, Comma(quantity), strings.ToUpper(symbol), Comma(stock.Price*quantity))) return fmt.Sprintf("%s share(s) of %s have been sold for a total of %s turnips.\n", Comma(quantity), strings.ToUpper(symbol), Comma(quantity*stock.Price)) // Return information about a user's portfolio }