示例#1
0
// 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
}
示例#2
0
// 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
}